1 /* Copyright (c) 2008, 2009, 2010, 2011 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.
18 #include "byte-order.h"
21 #include <arpa/inet.h>
24 #include <sys/socket.h>
26 #include <openflow/openflow.h>
31 #include <sys/socket.h>
32 #include <sys/types.h>
36 #include "classifier.h"
41 #include "dynamic-string.h"
48 #include "mac-learning.h"
52 #include "ofp-print.h"
54 #include "ofproto/netflow.h"
55 #include "ofproto/ofproto.h"
56 #include "ovsdb-data.h"
58 #include "poll-loop.h"
62 #include "socket-util.h"
63 #include "stream-ssl.h"
65 #include "system-stats.h"
70 #include "vswitchd/vswitch-idl.h"
71 #include "xenserver.h"
73 #include "sflow_api.h"
75 VLOG_DEFINE_THIS_MODULE(bridge);
77 COVERAGE_DEFINE(bridge_flush);
78 COVERAGE_DEFINE(bridge_process_flow);
79 COVERAGE_DEFINE(bridge_process_cfm);
80 COVERAGE_DEFINE(bridge_process_lacp);
81 COVERAGE_DEFINE(bridge_reconfigure);
82 COVERAGE_DEFINE(bridge_lacp_update);
90 struct dst builtin[32];
95 static void dst_set_init(struct dst_set *);
96 static void dst_set_add(struct dst_set *, const struct dst *);
97 static void dst_set_free(struct dst_set *);
100 /* These members are always valid. */
101 struct port *port; /* Containing port. */
102 size_t port_ifidx; /* Index within containing port. */
103 char *name; /* Host network device name. */
104 tag_type tag; /* Tag associated with this interface. */
105 long long delay_expires; /* Time after which 'enabled' may change. */
107 /* These members are valid only after bridge_reconfigure() causes them to
109 struct hmap_node dp_ifidx_node; /* In struct bridge's "ifaces" hmap. */
110 int dp_ifidx; /* Index within kernel datapath. */
111 struct netdev *netdev; /* Network device. */
112 bool enabled; /* May be chosen for flows? */
113 bool up; /* Is the interface up? */
114 const char *type; /* Usually same as cfg->type. */
115 struct cfm *cfm; /* Connectivity Fault Management */
116 const struct ovsrec_interface *cfg;
118 /* LACP information. */
119 uint16_t lacp_priority; /* LACP port priority. */
122 #define BOND_MASK 0xff
124 int iface_idx; /* Index of assigned iface, or -1 if none. */
125 uint64_t tx_bytes; /* Count of bytes recently transmitted. */
126 tag_type iface_tag; /* Tag associated with iface_idx. */
130 BM_TCP, /* Transport Layer Load Balance. */
131 BM_SLB, /* Source Load Balance. */
132 BM_AB /* Active Backup. */
135 #define MAX_MIRRORS 32
136 typedef uint32_t mirror_mask_t;
137 #define MIRROR_MASK_C(X) UINT32_C(X)
138 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
140 struct bridge *bridge;
143 struct uuid uuid; /* UUID of this "mirror" record in database. */
145 /* Selection criteria. */
146 struct shash src_ports; /* Name is port name; data is always NULL. */
147 struct shash dst_ports; /* Name is port name; data is always NULL. */
152 struct port *out_port;
156 #define FLOOD_PORT ((struct port *) 1) /* The 'flood' output port. */
158 struct bridge *bridge;
160 int vlan; /* -1=trunk port, else a 12-bit VLAN ID. */
161 unsigned long *trunks; /* Bitmap of trunked VLANs, if 'vlan' == -1.
162 * NULL if all VLANs are trunked. */
163 const struct ovsrec_port *cfg;
167 struct netdev_monitor *monitor; /* Tracks carrier. NULL if miimon. */
168 long long int miimon_interval; /* Miimon status refresh interval. */
169 long long int miimon_next_update; /* Time of next miimon update. */
171 /* An ordinary bridge port has 1 interface.
172 * A bridge port for bonding has at least 2 interfaces. */
173 struct iface **ifaces;
174 size_t n_ifaces, allocated_ifaces;
177 enum bond_mode bond_mode; /* Type of the bond. BM_SLB is the default. */
178 int active_iface; /* Ifidx on which bcasts accepted, or -1. */
179 tag_type active_iface_tag; /* Tag for bcast flows. */
180 tag_type no_ifaces_tag; /* Tag for flows when all ifaces disabled. */
181 int updelay, downdelay; /* Delay before iface goes up/down, in ms. */
182 bool bond_fake_iface; /* Fake a bond interface for legacy compat? */
183 long long int bond_next_fake_iface_update; /* Time of next update. */
185 /* LACP information. */
186 struct lacp *lacp; /* LACP object. NULL if LACP is disabled. */
187 bool lacp_active; /* True if LACP is active */
188 bool lacp_fast; /* True if LACP is in fast mode. */
189 uint16_t lacp_priority; /* LACP system priority. */
191 /* SLB specific bonding info. */
192 struct bond_entry *bond_hash; /* An array of (BOND_MASK + 1) elements. */
193 int bond_rebalance_interval; /* Interval between rebalances, in ms. */
194 long long int bond_next_rebalance; /* Next rebalancing time. */
196 /* Port mirroring info. */
197 mirror_mask_t src_mirrors; /* Mirrors triggered when packet received. */
198 mirror_mask_t dst_mirrors; /* Mirrors triggered when packet sent. */
199 bool is_mirror_output_port; /* Does port mirroring send frames here? */
203 struct list node; /* Node in global list of bridges. */
204 char *name; /* User-specified arbitrary name. */
205 struct mac_learning *ml; /* MAC learning table. */
206 uint8_t ea[ETH_ADDR_LEN]; /* Bridge Ethernet Address. */
207 uint8_t default_ea[ETH_ADDR_LEN]; /* Default MAC. */
208 const struct ovsrec_bridge *cfg;
210 /* OpenFlow switch processing. */
211 struct ofproto *ofproto; /* OpenFlow switch. */
213 /* Kernel datapath information. */
214 struct dpif *dpif; /* Datapath. */
215 struct hmap ifaces; /* Contains "struct iface"s. */
219 size_t n_ports, allocated_ports;
220 struct shash iface_by_name; /* "struct iface"s indexed by name. */
221 struct shash port_by_name; /* "struct port"s indexed by name. */
224 bool has_bonded_ports;
229 /* Port mirroring. */
230 struct mirror *mirrors[MAX_MIRRORS];
233 /* List of all bridges. */
234 static struct list all_bridges = LIST_INITIALIZER(&all_bridges);
236 /* OVSDB IDL used to obtain configuration. */
237 static struct ovsdb_idl *idl;
239 /* Each time this timer expires, the bridge fetches systems and interface
240 * statistics and pushes them into the database. */
241 #define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
242 static long long int stats_timer = LLONG_MIN;
244 static struct bridge *bridge_create(const struct ovsrec_bridge *br_cfg);
245 static void bridge_destroy(struct bridge *);
246 static struct bridge *bridge_lookup(const char *name);
247 static unixctl_cb_func bridge_unixctl_dump_flows;
248 static unixctl_cb_func bridge_unixctl_reconnect;
249 static int bridge_run_one(struct bridge *);
250 static size_t bridge_get_controllers(const struct bridge *br,
251 struct ovsrec_controller ***controllersp);
252 static void bridge_reconfigure_one(struct bridge *);
253 static void bridge_reconfigure_remotes(struct bridge *,
254 const struct sockaddr_in *managers,
256 static void bridge_get_all_ifaces(const struct bridge *, struct shash *ifaces);
257 static void bridge_fetch_dp_ifaces(struct bridge *);
258 static void bridge_flush(struct bridge *);
259 static void bridge_pick_local_hw_addr(struct bridge *,
260 uint8_t ea[ETH_ADDR_LEN],
261 struct iface **hw_addr_iface);
262 static uint64_t bridge_pick_datapath_id(struct bridge *,
263 const uint8_t bridge_ea[ETH_ADDR_LEN],
264 struct iface *hw_addr_iface);
265 static struct iface *bridge_get_local_iface(struct bridge *);
266 static uint64_t dpid_from_hash(const void *, size_t nbytes);
268 static unixctl_cb_func bridge_unixctl_fdb_show;
269 static unixctl_cb_func qos_unixctl_show;
271 static void bond_init(void);
272 static void bond_run(struct port *);
273 static void bond_wait(struct port *);
274 static void bond_rebalance_port(struct port *);
275 static void bond_send_learning_packets(struct port *);
276 static void bond_enable_slave(struct iface *iface, bool enable);
278 static void port_run(struct port *);
279 static void port_wait(struct port *);
280 static struct port *port_create(struct bridge *, const char *name);
281 static void port_reconfigure(struct port *, const struct ovsrec_port *);
282 static void port_del_ifaces(struct port *, const struct ovsrec_port *);
283 static void port_destroy(struct port *);
284 static struct port *port_lookup(const struct bridge *, const char *name);
285 static struct iface *port_lookup_iface(const struct port *, const char *name);
286 static struct port *port_from_dp_ifidx(const struct bridge *,
288 static void port_update_bonding(struct port *);
289 static void port_update_lacp(struct port *);
291 static void mirror_create(struct bridge *, struct ovsrec_mirror *);
292 static void mirror_destroy(struct mirror *);
293 static void mirror_reconfigure(struct bridge *);
294 static void mirror_reconfigure_one(struct mirror *, struct ovsrec_mirror *);
295 static bool vlan_is_mirrored(const struct mirror *, int vlan);
297 static struct iface *iface_create(struct port *port,
298 const struct ovsrec_interface *if_cfg);
299 static void iface_destroy(struct iface *);
300 static struct iface *iface_lookup(const struct bridge *, const char *name);
301 static struct iface *iface_find(const char *name);
302 static struct iface *iface_from_dp_ifidx(const struct bridge *,
304 static void iface_set_mac(struct iface *);
305 static void iface_set_ofport(const struct ovsrec_interface *, int64_t ofport);
306 static void iface_update_qos(struct iface *, const struct ovsrec_qos *);
307 static void iface_update_cfm(struct iface *);
308 static void iface_refresh_cfm_stats(struct iface *iface);
309 static void iface_send_packet(struct iface *, struct ofpbuf *packet);
310 static void iface_update_carrier(struct iface *, bool carrier);
312 static void shash_from_ovs_idl_map(char **keys, char **values, size_t n,
314 static void shash_to_ovs_idl_map(struct shash *,
315 char ***keys, char ***values, size_t *n);
318 /* Hooks into ofproto processing. */
319 static struct ofhooks bridge_ofhooks;
321 /* Public functions. */
323 /* Initializes the bridge module, configuring it to obtain its configuration
324 * from an OVSDB server accessed over 'remote', which should be a string in a
325 * form acceptable to ovsdb_idl_create(). */
327 bridge_init(const char *remote)
329 /* Create connection to database. */
330 idl = ovsdb_idl_create(remote, &ovsrec_idl_class, true);
332 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_cur_cfg);
333 ovsdb_idl_omit_alert(idl, &ovsrec_open_vswitch_col_statistics);
334 ovsdb_idl_omit(idl, &ovsrec_open_vswitch_col_external_ids);
336 ovsdb_idl_omit(idl, &ovsrec_bridge_col_external_ids);
338 ovsdb_idl_omit(idl, &ovsrec_port_col_external_ids);
339 ovsdb_idl_omit(idl, &ovsrec_port_col_fake_bridge);
341 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_ofport);
342 ovsdb_idl_omit_alert(idl, &ovsrec_interface_col_statistics);
343 ovsdb_idl_omit(idl, &ovsrec_interface_col_external_ids);
345 /* Register unixctl commands. */
346 unixctl_command_register("fdb/show", bridge_unixctl_fdb_show, NULL);
347 unixctl_command_register("qos/show", qos_unixctl_show, NULL);
348 unixctl_command_register("bridge/dump-flows", bridge_unixctl_dump_flows,
350 unixctl_command_register("bridge/reconnect", bridge_unixctl_reconnect,
359 struct bridge *br, *next_br;
361 LIST_FOR_EACH_SAFE (br, next_br, node, &all_bridges) {
364 ovsdb_idl_destroy(idl);
367 /* Performs configuration that is only necessary once at ovs-vswitchd startup,
368 * but for which the ovs-vswitchd configuration 'cfg' is required. */
370 bridge_configure_once(const struct ovsrec_open_vswitch *cfg)
372 static bool already_configured_once;
373 struct svec bridge_names;
374 struct svec dpif_names, dpif_types;
377 /* Only do this once per ovs-vswitchd run. */
378 if (already_configured_once) {
381 already_configured_once = true;
383 stats_timer = time_msec() + STATS_INTERVAL;
385 /* Get all the configured bridges' names from 'cfg' into 'bridge_names'. */
386 svec_init(&bridge_names);
387 for (i = 0; i < cfg->n_bridges; i++) {
388 svec_add(&bridge_names, cfg->bridges[i]->name);
390 svec_sort(&bridge_names);
392 /* Iterate over all system dpifs and delete any of them that do not appear
394 svec_init(&dpif_names);
395 svec_init(&dpif_types);
396 dp_enumerate_types(&dpif_types);
397 for (i = 0; i < dpif_types.n; i++) {
400 dp_enumerate_names(dpif_types.names[i], &dpif_names);
402 /* Delete each dpif whose name is not in 'bridge_names'. */
403 for (j = 0; j < dpif_names.n; j++) {
404 if (!svec_contains(&bridge_names, dpif_names.names[j])) {
408 retval = dpif_open(dpif_names.names[j], dpif_types.names[i],
417 svec_destroy(&bridge_names);
418 svec_destroy(&dpif_names);
419 svec_destroy(&dpif_types);
422 /* Callback for iterate_and_prune_ifaces(). */
424 check_iface(struct bridge *br, struct iface *iface, void *aux OVS_UNUSED)
426 if (!iface->netdev) {
427 /* We already reported a related error, don't bother duplicating it. */
431 if (iface->dp_ifidx < 0) {
432 VLOG_ERR("%s interface not in %s, dropping",
433 iface->name, dpif_name(br->dpif));
437 VLOG_DBG("%s has interface %s on port %d", dpif_name(br->dpif),
438 iface->name, iface->dp_ifidx);
442 /* Callback for iterate_and_prune_ifaces(). */
444 set_iface_properties(struct bridge *br OVS_UNUSED, struct iface *iface,
445 void *aux OVS_UNUSED)
447 /* Set policing attributes. */
448 netdev_set_policing(iface->netdev,
449 iface->cfg->ingress_policing_rate,
450 iface->cfg->ingress_policing_burst);
452 /* Set MAC address of internal interfaces other than the local
454 if (iface->dp_ifidx != ODPP_LOCAL && !strcmp(iface->type, "internal")) {
455 iface_set_mac(iface);
461 /* Calls 'cb' for each interfaces in 'br', passing along the 'aux' argument.
462 * Deletes from 'br' all the interfaces for which 'cb' returns false, and then
463 * deletes from 'br' any ports that no longer have any interfaces. */
465 iterate_and_prune_ifaces(struct bridge *br,
466 bool (*cb)(struct bridge *, struct iface *,
472 for (i = 0; i < br->n_ports; ) {
473 struct port *port = br->ports[i];
474 for (j = 0; j < port->n_ifaces; ) {
475 struct iface *iface = port->ifaces[j];
476 if (cb(br, iface, aux)) {
479 iface_set_ofport(iface->cfg, -1);
480 iface_destroy(iface);
484 if (port->n_ifaces) {
487 VLOG_WARN("%s port has no interfaces, dropping", port->name);
493 /* Looks at the list of managers in 'ovs_cfg' and extracts their remote IP
494 * addresses and ports into '*managersp' and '*n_managersp'. The caller is
495 * responsible for freeing '*managersp' (with free()).
497 * You may be asking yourself "why does ovs-vswitchd care?", because
498 * ovsdb-server is responsible for connecting to the managers, and ovs-vswitchd
499 * should not be and in fact is not directly involved in that. But
500 * ovs-vswitchd needs to make sure that ovsdb-server can reach the managers, so
501 * it has to tell in-band control where the managers are to enable that.
502 * (Thus, only managers connected in-band are collected.)
505 collect_in_band_managers(const struct ovsrec_open_vswitch *ovs_cfg,
506 struct sockaddr_in **managersp, size_t *n_managersp)
508 struct sockaddr_in *managers = NULL;
509 size_t n_managers = 0;
510 struct shash targets;
513 /* Collect all of the potential targets from the "targets" columns of the
514 * rows pointed to by "manager_options", excluding any that are
516 shash_init(&targets);
517 for (i = 0; i < ovs_cfg->n_manager_options; i++) {
518 struct ovsrec_manager *m = ovs_cfg->manager_options[i];
520 if (m->connection_mode && !strcmp(m->connection_mode, "out-of-band")) {
521 shash_find_and_delete(&targets, m->target);
523 shash_add_once(&targets, m->target, NULL);
527 /* Now extract the targets' IP addresses. */
528 if (!shash_is_empty(&targets)) {
529 struct shash_node *node;
531 managers = xmalloc(shash_count(&targets) * sizeof *managers);
532 SHASH_FOR_EACH (node, &targets) {
533 const char *target = node->name;
534 struct sockaddr_in *sin = &managers[n_managers];
536 if ((!strncmp(target, "tcp:", 4)
537 && inet_parse_active(target + 4, JSONRPC_TCP_PORT, sin)) ||
538 (!strncmp(target, "ssl:", 4)
539 && inet_parse_active(target + 4, JSONRPC_SSL_PORT, sin))) {
544 shash_destroy(&targets);
546 *managersp = managers;
547 *n_managersp = n_managers;
551 bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
553 struct shash old_br, new_br;
554 struct shash_node *node;
555 struct bridge *br, *next;
556 struct sockaddr_in *managers;
559 int sflow_bridge_number;
561 COVERAGE_INC(bridge_reconfigure);
563 collect_in_band_managers(ovs_cfg, &managers, &n_managers);
565 /* Collect old and new bridges. */
568 LIST_FOR_EACH (br, node, &all_bridges) {
569 shash_add(&old_br, br->name, br);
571 for (i = 0; i < ovs_cfg->n_bridges; i++) {
572 const struct ovsrec_bridge *br_cfg = ovs_cfg->bridges[i];
573 if (!shash_add_once(&new_br, br_cfg->name, br_cfg)) {
574 VLOG_WARN("more than one bridge named %s", br_cfg->name);
578 /* Get rid of deleted bridges and add new bridges. */
579 LIST_FOR_EACH_SAFE (br, next, node, &all_bridges) {
580 struct ovsrec_bridge *br_cfg = shash_find_data(&new_br, br->name);
587 SHASH_FOR_EACH (node, &new_br) {
588 const char *br_name = node->name;
589 const struct ovsrec_bridge *br_cfg = node->data;
590 br = shash_find_data(&old_br, br_name);
592 /* If the bridge datapath type has changed, we need to tear it
593 * down and recreate. */
594 if (strcmp(br->cfg->datapath_type, br_cfg->datapath_type)) {
596 bridge_create(br_cfg);
599 bridge_create(br_cfg);
602 shash_destroy(&old_br);
603 shash_destroy(&new_br);
605 /* Reconfigure all bridges. */
606 LIST_FOR_EACH (br, node, &all_bridges) {
607 bridge_reconfigure_one(br);
610 /* Add and delete ports on all datapaths.
612 * The kernel will reject any attempt to add a given port to a datapath if
613 * that port already belongs to a different datapath, so we must do all
614 * port deletions before any port additions. */
615 LIST_FOR_EACH (br, node, &all_bridges) {
616 struct dpif_port_dump dump;
617 struct shash want_ifaces;
618 struct dpif_port dpif_port;
620 bridge_get_all_ifaces(br, &want_ifaces);
621 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
622 if (!shash_find(&want_ifaces, dpif_port.name)
623 && strcmp(dpif_port.name, br->name)) {
624 int retval = dpif_port_del(br->dpif, dpif_port.port_no);
626 VLOG_WARN("failed to remove %s interface from %s: %s",
627 dpif_port.name, dpif_name(br->dpif),
632 shash_destroy(&want_ifaces);
634 LIST_FOR_EACH (br, node, &all_bridges) {
635 struct shash cur_ifaces, want_ifaces;
636 struct dpif_port_dump dump;
637 struct dpif_port dpif_port;
639 /* Get the set of interfaces currently in this datapath. */
640 shash_init(&cur_ifaces);
641 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
642 struct dpif_port *port_info = xmalloc(sizeof *port_info);
643 dpif_port_clone(port_info, &dpif_port);
644 shash_add(&cur_ifaces, dpif_port.name, port_info);
647 /* Get the set of interfaces we want on this datapath. */
648 bridge_get_all_ifaces(br, &want_ifaces);
650 hmap_clear(&br->ifaces);
651 SHASH_FOR_EACH (node, &want_ifaces) {
652 const char *if_name = node->name;
653 struct iface *iface = node->data;
654 struct dpif_port *dpif_port;
658 type = iface ? iface->type : "internal";
659 dpif_port = shash_find_data(&cur_ifaces, if_name);
661 /* If we have a port or a netdev already, and it's not the type we
662 * want, then delete the port (if any) and close the netdev (if
664 if ((dpif_port && strcmp(dpif_port->type, type))
665 || (iface && iface->netdev
666 && strcmp(type, netdev_get_type(iface->netdev)))) {
668 error = ofproto_port_del(br->ofproto, dpif_port->port_no);
675 netdev_close(iface->netdev);
676 iface->netdev = NULL;
680 /* If the port doesn't exist or we don't have the netdev open,
681 * we need to do more work. */
682 if (!dpif_port || (iface && !iface->netdev)) {
683 struct netdev_options options;
684 struct netdev *netdev;
687 /* First open the network device. */
688 options.name = if_name;
690 options.args = &args;
691 options.ethertype = NETDEV_ETH_TYPE_NONE;
695 shash_from_ovs_idl_map(iface->cfg->key_options,
696 iface->cfg->value_options,
697 iface->cfg->n_options, &args);
699 error = netdev_open(&options, &netdev);
700 shash_destroy(&args);
703 VLOG_WARN("could not open network device %s (%s)",
704 if_name, strerror(error));
708 /* Then add the port if we haven't already. */
710 error = dpif_port_add(br->dpif, netdev, NULL);
712 netdev_close(netdev);
713 if (error == EFBIG) {
714 VLOG_ERR("ran out of valid port numbers on %s",
715 dpif_name(br->dpif));
718 VLOG_WARN("failed to add %s interface to %s: %s",
719 if_name, dpif_name(br->dpif),
726 /* Update 'iface'. */
728 iface->netdev = netdev;
729 iface->enabled = netdev_get_carrier(iface->netdev);
730 iface->up = iface->enabled;
732 } else if (iface && iface->netdev) {
736 shash_from_ovs_idl_map(iface->cfg->key_options,
737 iface->cfg->value_options,
738 iface->cfg->n_options, &args);
739 netdev_set_config(iface->netdev, &args);
740 shash_destroy(&args);
743 shash_destroy(&want_ifaces);
745 SHASH_FOR_EACH (node, &cur_ifaces) {
746 struct dpif_port *port_info = node->data;
747 dpif_port_destroy(port_info);
750 shash_destroy(&cur_ifaces);
752 sflow_bridge_number = 0;
753 LIST_FOR_EACH (br, node, &all_bridges) {
756 struct iface *local_iface;
757 struct iface *hw_addr_iface;
760 bridge_fetch_dp_ifaces(br);
762 iterate_and_prune_ifaces(br, check_iface, NULL);
764 /* Pick local port hardware address, datapath ID. */
765 bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
766 local_iface = bridge_get_local_iface(br);
768 int error = netdev_set_etheraddr(local_iface->netdev, ea);
770 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
771 VLOG_ERR_RL(&rl, "bridge %s: failed to set bridge "
772 "Ethernet address: %s",
773 br->name, strerror(error));
776 memcpy(br->ea, ea, ETH_ADDR_LEN);
778 dpid = bridge_pick_datapath_id(br, ea, hw_addr_iface);
779 ofproto_set_datapath_id(br->ofproto, dpid);
781 dpid_string = xasprintf("%016"PRIx64, dpid);
782 ovsrec_bridge_set_datapath_id(br->cfg, dpid_string);
785 /* Set NetFlow configuration on this bridge. */
786 if (br->cfg->netflow) {
787 struct ovsrec_netflow *nf_cfg = br->cfg->netflow;
788 struct netflow_options opts;
790 memset(&opts, 0, sizeof opts);
792 dpif_get_netflow_ids(br->dpif, &opts.engine_type, &opts.engine_id);
793 if (nf_cfg->engine_type) {
794 opts.engine_type = *nf_cfg->engine_type;
796 if (nf_cfg->engine_id) {
797 opts.engine_id = *nf_cfg->engine_id;
800 opts.active_timeout = nf_cfg->active_timeout;
801 if (!opts.active_timeout) {
802 opts.active_timeout = -1;
803 } else if (opts.active_timeout < 0) {
804 VLOG_WARN("bridge %s: active timeout interval set to negative "
805 "value, using default instead (%d seconds)", br->name,
806 NF_ACTIVE_TIMEOUT_DEFAULT);
807 opts.active_timeout = -1;
810 opts.add_id_to_iface = nf_cfg->add_id_to_interface;
811 if (opts.add_id_to_iface) {
812 if (opts.engine_id > 0x7f) {
813 VLOG_WARN("bridge %s: netflow port mangling may conflict "
814 "with another vswitch, choose an engine id less "
815 "than 128", br->name);
817 if (br->n_ports > 508) {
818 VLOG_WARN("bridge %s: netflow port mangling will conflict "
819 "with another port when more than 508 ports are "
824 opts.collectors.n = nf_cfg->n_targets;
825 opts.collectors.names = nf_cfg->targets;
826 if (ofproto_set_netflow(br->ofproto, &opts)) {
827 VLOG_ERR("bridge %s: problem setting netflow collectors",
831 ofproto_set_netflow(br->ofproto, NULL);
834 /* Set sFlow configuration on this bridge. */
835 if (br->cfg->sflow) {
836 const struct ovsrec_sflow *sflow_cfg = br->cfg->sflow;
837 struct ovsrec_controller **controllers;
838 struct ofproto_sflow_options oso;
839 size_t n_controllers;
841 memset(&oso, 0, sizeof oso);
843 oso.targets.n = sflow_cfg->n_targets;
844 oso.targets.names = sflow_cfg->targets;
846 oso.sampling_rate = SFL_DEFAULT_SAMPLING_RATE;
847 if (sflow_cfg->sampling) {
848 oso.sampling_rate = *sflow_cfg->sampling;
851 oso.polling_interval = SFL_DEFAULT_POLLING_INTERVAL;
852 if (sflow_cfg->polling) {
853 oso.polling_interval = *sflow_cfg->polling;
856 oso.header_len = SFL_DEFAULT_HEADER_SIZE;
857 if (sflow_cfg->header) {
858 oso.header_len = *sflow_cfg->header;
861 oso.sub_id = sflow_bridge_number++;
862 oso.agent_device = sflow_cfg->agent;
864 oso.control_ip = NULL;
865 n_controllers = bridge_get_controllers(br, &controllers);
866 for (i = 0; i < n_controllers; i++) {
867 if (controllers[i]->local_ip) {
868 oso.control_ip = controllers[i]->local_ip;
872 ofproto_set_sflow(br->ofproto, &oso);
874 /* Do not destroy oso.targets because it is owned by sflow_cfg. */
876 ofproto_set_sflow(br->ofproto, NULL);
879 /* Update the controller and related settings. It would be more
880 * straightforward to call this from bridge_reconfigure_one(), but we
881 * can't do it there for two reasons. First, and most importantly, at
882 * that point we don't know the dp_ifidx of any interfaces that have
883 * been added to the bridge (because we haven't actually added them to
884 * the datapath). Second, at that point we haven't set the datapath ID
885 * yet; when a controller is configured, resetting the datapath ID will
886 * immediately disconnect from the controller, so it's better to set
887 * the datapath ID before the controller. */
888 bridge_reconfigure_remotes(br, managers, n_managers);
890 LIST_FOR_EACH (br, node, &all_bridges) {
891 for (i = 0; i < br->n_ports; i++) {
892 struct port *port = br->ports[i];
896 for (j = 0; j < port->n_ifaces; j++) {
897 netdev_monitor_add(port->monitor, port->ifaces[j]->netdev);
900 port->miimon_next_update = 0;
903 port_update_lacp(port);
904 port_update_bonding(port);
906 for (j = 0; j < port->n_ifaces; j++) {
907 iface_update_qos(port->ifaces[j], port->cfg->qos);
911 LIST_FOR_EACH (br, node, &all_bridges) {
912 iterate_and_prune_ifaces(br, set_iface_properties, NULL);
915 LIST_FOR_EACH (br, node, &all_bridges) {
917 HMAP_FOR_EACH (iface, dp_ifidx_node, &br->ifaces) {
918 iface_update_cfm(iface);
924 /* ovs-vswitchd has completed initialization, so allow the process that
925 * forked us to exit successfully. */
926 daemonize_complete();
930 get_ovsrec_key_value(const struct ovsdb_idl_row *row,
931 const struct ovsdb_idl_column *column,
934 const struct ovsdb_datum *datum;
935 union ovsdb_atom atom;
938 datum = ovsdb_idl_get(row, column, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
939 atom.string = (char *) key;
940 idx = ovsdb_datum_find_key(datum, &atom, OVSDB_TYPE_STRING);
941 return idx == UINT_MAX ? NULL : datum->values[idx].string;
945 bridge_get_other_config(const struct ovsrec_bridge *br_cfg, const char *key)
947 return get_ovsrec_key_value(&br_cfg->header_,
948 &ovsrec_bridge_col_other_config, key);
952 bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
953 struct iface **hw_addr_iface)
959 *hw_addr_iface = NULL;
961 /* Did the user request a particular MAC? */
962 hwaddr = bridge_get_other_config(br->cfg, "hwaddr");
963 if (hwaddr && eth_addr_from_string(hwaddr, ea)) {
964 if (eth_addr_is_multicast(ea)) {
965 VLOG_ERR("bridge %s: cannot set MAC address to multicast "
966 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
967 } else if (eth_addr_is_zero(ea)) {
968 VLOG_ERR("bridge %s: cannot set MAC address to zero", br->name);
974 /* Otherwise choose the minimum non-local MAC address among all of the
976 memset(ea, 0xff, ETH_ADDR_LEN);
977 for (i = 0; i < br->n_ports; i++) {
978 struct port *port = br->ports[i];
979 uint8_t iface_ea[ETH_ADDR_LEN];
982 /* Mirror output ports don't participate. */
983 if (port->is_mirror_output_port) {
987 /* Choose the MAC address to represent the port. */
988 if (port->cfg->mac && eth_addr_from_string(port->cfg->mac, iface_ea)) {
989 /* Find the interface with this Ethernet address (if any) so that
990 * we can provide the correct devname to the caller. */
992 for (j = 0; j < port->n_ifaces; j++) {
993 struct iface *candidate = port->ifaces[j];
994 uint8_t candidate_ea[ETH_ADDR_LEN];
995 if (!netdev_get_etheraddr(candidate->netdev, candidate_ea)
996 && eth_addr_equals(iface_ea, candidate_ea)) {
1001 /* Choose the interface whose MAC address will represent the port.
1002 * The Linux kernel bonding code always chooses the MAC address of
1003 * the first slave added to a bond, and the Fedora networking
1004 * scripts always add slaves to a bond in alphabetical order, so
1005 * for compatibility we choose the interface with the name that is
1006 * first in alphabetical order. */
1007 iface = port->ifaces[0];
1008 for (j = 1; j < port->n_ifaces; j++) {
1009 struct iface *candidate = port->ifaces[j];
1010 if (strcmp(candidate->name, iface->name) < 0) {
1015 /* The local port doesn't count (since we're trying to choose its
1016 * MAC address anyway). */
1017 if (iface->dp_ifidx == ODPP_LOCAL) {
1022 error = netdev_get_etheraddr(iface->netdev, iface_ea);
1024 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1025 VLOG_ERR_RL(&rl, "failed to obtain Ethernet address of %s: %s",
1026 iface->name, strerror(error));
1031 /* Compare against our current choice. */
1032 if (!eth_addr_is_multicast(iface_ea) &&
1033 !eth_addr_is_local(iface_ea) &&
1034 !eth_addr_is_reserved(iface_ea) &&
1035 !eth_addr_is_zero(iface_ea) &&
1036 eth_addr_compare_3way(iface_ea, ea) < 0)
1038 memcpy(ea, iface_ea, ETH_ADDR_LEN);
1039 *hw_addr_iface = iface;
1042 if (eth_addr_is_multicast(ea)) {
1043 memcpy(ea, br->default_ea, ETH_ADDR_LEN);
1044 *hw_addr_iface = NULL;
1045 VLOG_WARN("bridge %s: using default bridge Ethernet "
1046 "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
1048 VLOG_DBG("bridge %s: using bridge Ethernet address "ETH_ADDR_FMT,
1049 br->name, ETH_ADDR_ARGS(ea));
1053 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
1054 * Ethernet address is 'bridge_ea'. If 'bridge_ea' is the Ethernet address of
1055 * an interface on 'br', then that interface must be passed in as
1056 * 'hw_addr_iface'; if 'bridge_ea' was derived some other way, then
1057 * 'hw_addr_iface' must be passed in as a null pointer. */
1059 bridge_pick_datapath_id(struct bridge *br,
1060 const uint8_t bridge_ea[ETH_ADDR_LEN],
1061 struct iface *hw_addr_iface)
1064 * The procedure for choosing a bridge MAC address will, in the most
1065 * ordinary case, also choose a unique MAC that we can use as a datapath
1066 * ID. In some special cases, though, multiple bridges will end up with
1067 * the same MAC address. This is OK for the bridges, but it will confuse
1068 * the OpenFlow controller, because each datapath needs a unique datapath
1071 * Datapath IDs must be unique. It is also very desirable that they be
1072 * stable from one run to the next, so that policy set on a datapath
1075 const char *datapath_id;
1078 datapath_id = bridge_get_other_config(br->cfg, "datapath-id");
1079 if (datapath_id && dpid_from_string(datapath_id, &dpid)) {
1083 if (hw_addr_iface) {
1085 if (!netdev_get_vlan_vid(hw_addr_iface->netdev, &vlan)) {
1087 * A bridge whose MAC address is taken from a VLAN network device
1088 * (that is, a network device created with vconfig(8) or similar
1089 * tool) will have the same MAC address as a bridge on the VLAN
1090 * device's physical network device.
1092 * Handle this case by hashing the physical network device MAC
1093 * along with the VLAN identifier.
1095 uint8_t buf[ETH_ADDR_LEN + 2];
1096 memcpy(buf, bridge_ea, ETH_ADDR_LEN);
1097 buf[ETH_ADDR_LEN] = vlan >> 8;
1098 buf[ETH_ADDR_LEN + 1] = vlan;
1099 return dpid_from_hash(buf, sizeof buf);
1102 * Assume that this bridge's MAC address is unique, since it
1103 * doesn't fit any of the cases we handle specially.
1108 * A purely internal bridge, that is, one that has no non-virtual
1109 * network devices on it at all, is more difficult because it has no
1110 * natural unique identifier at all.
1112 * When the host is a XenServer, we handle this case by hashing the
1113 * host's UUID with the name of the bridge. Names of bridges are
1114 * persistent across XenServer reboots, although they can be reused if
1115 * an internal network is destroyed and then a new one is later
1116 * created, so this is fairly effective.
1118 * When the host is not a XenServer, we punt by using a random MAC
1119 * address on each run.
1121 const char *host_uuid = xenserver_get_host_uuid();
1123 char *combined = xasprintf("%s,%s", host_uuid, br->name);
1124 dpid = dpid_from_hash(combined, strlen(combined));
1130 return eth_addr_to_uint64(bridge_ea);
1134 dpid_from_hash(const void *data, size_t n)
1136 uint8_t hash[SHA1_DIGEST_SIZE];
1138 BUILD_ASSERT_DECL(sizeof hash >= ETH_ADDR_LEN);
1139 sha1_bytes(data, n, hash);
1140 eth_addr_mark_random(hash);
1141 return eth_addr_to_uint64(hash);
1145 iface_refresh_status(struct iface *iface)
1149 enum netdev_flags flags;
1158 if (!netdev_get_status(iface->netdev, &sh)) {
1160 char **keys, **values;
1162 shash_to_ovs_idl_map(&sh, &keys, &values, &n);
1163 ovsrec_interface_set_status(iface->cfg, keys, values, n);
1168 ovsrec_interface_set_status(iface->cfg, NULL, NULL, 0);
1171 shash_destroy_free_data(&sh);
1173 error = netdev_get_flags(iface->netdev, &flags);
1175 ovsrec_interface_set_admin_state(iface->cfg, flags & NETDEV_UP ? "up" : "down");
1178 ovsrec_interface_set_admin_state(iface->cfg, NULL);
1181 error = netdev_get_features(iface->netdev, ¤t, NULL, NULL, NULL);
1183 ovsrec_interface_set_duplex(iface->cfg,
1184 netdev_features_is_full_duplex(current)
1186 /* warning: uint64_t -> int64_t conversion */
1187 bps = netdev_features_to_bps(current);
1188 ovsrec_interface_set_link_speed(iface->cfg, &bps, 1);
1191 ovsrec_interface_set_duplex(iface->cfg, NULL);
1192 ovsrec_interface_set_link_speed(iface->cfg, NULL, 0);
1196 ovsrec_interface_set_link_state(iface->cfg,
1197 netdev_get_carrier(iface->netdev)
1200 error = netdev_get_mtu(iface->netdev, &mtu);
1201 if (!error && mtu != INT_MAX) {
1203 ovsrec_interface_set_mtu(iface->cfg, &mtu_64, 1);
1206 ovsrec_interface_set_mtu(iface->cfg, NULL, 0);
1211 iface_refresh_cfm_stats(struct iface *iface)
1215 const struct ovsrec_monitor *mon;
1217 mon = iface->cfg->monitor;
1224 for (i = 0; i < mon->n_remote_mps; i++) {
1225 const struct ovsrec_maintenance_point *mp;
1226 const struct remote_mp *rmp;
1228 mp = mon->remote_mps[i];
1229 rmp = cfm_get_remote_mp(cfm, mp->mpid);
1231 ovsrec_maintenance_point_set_fault(mp, &rmp->fault, 1);
1234 if (hmap_is_empty(&cfm->x_remote_mps)) {
1235 ovsrec_monitor_set_unexpected_remote_mpids(mon, NULL, 0);
1238 struct remote_mp *rmp;
1239 int64_t *x_remote_mps;
1241 length = hmap_count(&cfm->x_remote_mps);
1242 x_remote_mps = xzalloc(length * sizeof *x_remote_mps);
1245 HMAP_FOR_EACH (rmp, node, &cfm->x_remote_mps) {
1246 x_remote_mps[i++] = rmp->mpid;
1249 ovsrec_monitor_set_unexpected_remote_mpids(mon, x_remote_mps, length);
1253 if (hmap_is_empty(&cfm->x_remote_maids)) {
1254 ovsrec_monitor_set_unexpected_remote_maids(mon, NULL, 0);
1257 char **x_remote_maids;
1258 struct remote_maid *rmaid;
1260 length = hmap_count(&cfm->x_remote_maids);
1261 x_remote_maids = xzalloc(length * sizeof *x_remote_maids);
1264 HMAP_FOR_EACH (rmaid, node, &cfm->x_remote_maids) {
1267 x_remote_maids[i] = xzalloc(CCM_MAID_LEN * 2 + 1);
1269 for (j = 0; j < CCM_MAID_LEN; j++) {
1270 snprintf(&x_remote_maids[i][j * 2], 3, "%02hhx",
1275 ovsrec_monitor_set_unexpected_remote_maids(mon, x_remote_maids, length);
1277 for (i = 0; i < length; i++) {
1278 free(x_remote_maids[i]);
1280 free(x_remote_maids);
1283 ovsrec_monitor_set_fault(mon, &cfm->fault, 1);
1287 iface_refresh_stats(struct iface *iface)
1293 static const struct iface_stat iface_stats[] = {
1294 { "rx_packets", offsetof(struct netdev_stats, rx_packets) },
1295 { "tx_packets", offsetof(struct netdev_stats, tx_packets) },
1296 { "rx_bytes", offsetof(struct netdev_stats, rx_bytes) },
1297 { "tx_bytes", offsetof(struct netdev_stats, tx_bytes) },
1298 { "rx_dropped", offsetof(struct netdev_stats, rx_dropped) },
1299 { "tx_dropped", offsetof(struct netdev_stats, tx_dropped) },
1300 { "rx_errors", offsetof(struct netdev_stats, rx_errors) },
1301 { "tx_errors", offsetof(struct netdev_stats, tx_errors) },
1302 { "rx_frame_err", offsetof(struct netdev_stats, rx_frame_errors) },
1303 { "rx_over_err", offsetof(struct netdev_stats, rx_over_errors) },
1304 { "rx_crc_err", offsetof(struct netdev_stats, rx_crc_errors) },
1305 { "collisions", offsetof(struct netdev_stats, collisions) },
1307 enum { N_STATS = ARRAY_SIZE(iface_stats) };
1308 const struct iface_stat *s;
1310 char *keys[N_STATS];
1311 int64_t values[N_STATS];
1314 struct netdev_stats stats;
1316 /* Intentionally ignore return value, since errors will set 'stats' to
1317 * all-1s, and we will deal with that correctly below. */
1318 netdev_get_stats(iface->netdev, &stats);
1321 for (s = iface_stats; s < &iface_stats[N_STATS]; s++) {
1322 uint64_t value = *(uint64_t *) (((char *) &stats) + s->offset);
1323 if (value != UINT64_MAX) {
1330 ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
1334 refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
1336 struct ovsdb_datum datum;
1340 get_system_stats(&stats);
1342 ovsdb_datum_from_shash(&datum, &stats);
1343 ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
1347 static inline const char *
1348 nx_role_to_str(enum nx_role role)
1353 case NX_ROLE_MASTER:
1358 return "*** INVALID ROLE ***";
1363 bridge_refresh_controller_status(const struct bridge *br)
1366 const struct ovsrec_controller *cfg;
1368 ofproto_get_ofproto_controller_info(br->ofproto, &info);
1370 OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
1371 struct ofproto_controller_info *cinfo =
1372 shash_find_data(&info, cfg->target);
1375 ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
1376 ovsrec_controller_set_role(cfg, nx_role_to_str(cinfo->role));
1377 ovsrec_controller_set_status(cfg, (char **) cinfo->pairs.keys,
1378 (char **) cinfo->pairs.values,
1381 ovsrec_controller_set_is_connected(cfg, false);
1382 ovsrec_controller_set_role(cfg, NULL);
1383 ovsrec_controller_set_status(cfg, NULL, NULL, 0);
1387 ofproto_free_ofproto_controller_info(&info);
1393 const struct ovsrec_open_vswitch *cfg;
1395 bool datapath_destroyed;
1396 bool database_changed;
1399 /* Let each bridge do the work that it needs to do. */
1400 datapath_destroyed = false;
1401 LIST_FOR_EACH (br, node, &all_bridges) {
1402 int error = bridge_run_one(br);
1404 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
1405 VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
1406 "forcing reconfiguration", br->name);
1407 datapath_destroyed = true;
1411 /* (Re)configure if necessary. */
1412 database_changed = ovsdb_idl_run(idl);
1413 cfg = ovsrec_open_vswitch_first(idl);
1415 /* Re-configure SSL. We do this on every trip through the main loop,
1416 * instead of just when the database changes, because the contents of the
1417 * key and certificate files can change without the database changing.
1419 * We do this before bridge_reconfigure() because that function might
1420 * initiate SSL connections and thus requires SSL to be configured. */
1421 if (cfg && cfg->ssl) {
1422 const struct ovsrec_ssl *ssl = cfg->ssl;
1424 stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
1425 stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
1428 if (database_changed || datapath_destroyed) {
1430 struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
1432 bridge_configure_once(cfg);
1433 bridge_reconfigure(cfg);
1435 ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
1436 ovsdb_idl_txn_commit(txn);
1437 ovsdb_idl_txn_destroy(txn); /* XXX */
1439 /* We still need to reconfigure to avoid dangling pointers to
1440 * now-destroyed ovsrec structures inside bridge data. */
1441 static const struct ovsrec_open_vswitch null_cfg;
1443 bridge_reconfigure(&null_cfg);
1447 /* Refresh system and interface stats if necessary. */
1448 if (time_msec() >= stats_timer) {
1450 struct ovsdb_idl_txn *txn;
1452 txn = ovsdb_idl_txn_create(idl);
1453 LIST_FOR_EACH (br, node, &all_bridges) {
1456 for (i = 0; i < br->n_ports; i++) {
1457 struct port *port = br->ports[i];
1460 for (j = 0; j < port->n_ifaces; j++) {
1461 struct iface *iface = port->ifaces[j];
1462 iface_refresh_stats(iface);
1463 iface_refresh_cfm_stats(iface);
1464 iface_refresh_status(iface);
1467 bridge_refresh_controller_status(br);
1469 refresh_system_stats(cfg);
1470 ovsdb_idl_txn_commit(txn);
1471 ovsdb_idl_txn_destroy(txn); /* XXX */
1474 stats_timer = time_msec() + STATS_INTERVAL;
1483 LIST_FOR_EACH (br, node, &all_bridges) {
1486 ofproto_wait(br->ofproto);
1487 if (ofproto_has_primary_controller(br->ofproto)) {
1491 mac_learning_wait(br->ml);
1493 for (i = 0; i < br->n_ports; i++) {
1494 port_wait(br->ports[i]);
1497 ovsdb_idl_wait(idl);
1498 poll_timer_wait_until(stats_timer);
1501 /* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
1502 * configuration changes. */
1504 bridge_flush(struct bridge *br)
1506 COVERAGE_INC(bridge_flush);
1508 mac_learning_flush(br->ml);
1511 /* Returns the 'br' interface for the ODPP_LOCAL port, or null if 'br' has no
1512 * such interface. */
1513 static struct iface *
1514 bridge_get_local_iface(struct bridge *br)
1518 for (i = 0; i < br->n_ports; i++) {
1519 struct port *port = br->ports[i];
1520 for (j = 0; j < port->n_ifaces; j++) {
1521 struct iface *iface = port->ifaces[j];
1522 if (iface->dp_ifidx == ODPP_LOCAL) {
1531 /* Bridge unixctl user interface functions. */
1533 bridge_unixctl_fdb_show(struct unixctl_conn *conn,
1534 const char *args, void *aux OVS_UNUSED)
1536 struct ds ds = DS_EMPTY_INITIALIZER;
1537 const struct bridge *br;
1538 const struct mac_entry *e;
1540 br = bridge_lookup(args);
1542 unixctl_command_reply(conn, 501, "no such bridge");
1546 ds_put_cstr(&ds, " port VLAN MAC Age\n");
1547 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
1548 if (e->port < 0 || e->port >= br->n_ports) {
1551 ds_put_format(&ds, "%5d %4d "ETH_ADDR_FMT" %3d\n",
1552 br->ports[e->port]->ifaces[0]->dp_ifidx,
1553 e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
1555 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1559 /* QoS unixctl user interface functions. */
1561 struct qos_unixctl_show_cbdata {
1563 struct iface *iface;
1567 qos_unixctl_show_cb(unsigned int queue_id,
1568 const struct shash *details,
1571 struct qos_unixctl_show_cbdata *data = aux;
1572 struct ds *ds = data->ds;
1573 struct iface *iface = data->iface;
1574 struct netdev_queue_stats stats;
1575 struct shash_node *node;
1578 ds_put_cstr(ds, "\n");
1580 ds_put_format(ds, "Queue %u:\n", queue_id);
1582 ds_put_cstr(ds, "Default:\n");
1585 SHASH_FOR_EACH (node, details) {
1586 ds_put_format(ds, "\t%s: %s\n", node->name, (char *)node->data);
1589 error = netdev_get_queue_stats(iface->netdev, queue_id, &stats);
1591 if (stats.tx_packets != UINT64_MAX) {
1592 ds_put_format(ds, "\ttx_packets: %"PRIu64"\n", stats.tx_packets);
1595 if (stats.tx_bytes != UINT64_MAX) {
1596 ds_put_format(ds, "\ttx_bytes: %"PRIu64"\n", stats.tx_bytes);
1599 if (stats.tx_errors != UINT64_MAX) {
1600 ds_put_format(ds, "\ttx_errors: %"PRIu64"\n", stats.tx_errors);
1603 ds_put_format(ds, "\tFailed to get statistics for queue %u: %s",
1604 queue_id, strerror(error));
1609 qos_unixctl_show(struct unixctl_conn *conn,
1610 const char *args, void *aux OVS_UNUSED)
1612 struct ds ds = DS_EMPTY_INITIALIZER;
1613 struct shash sh = SHASH_INITIALIZER(&sh);
1614 struct iface *iface;
1616 struct shash_node *node;
1617 struct qos_unixctl_show_cbdata data;
1620 iface = iface_find(args);
1622 unixctl_command_reply(conn, 501, "no such interface");
1626 netdev_get_qos(iface->netdev, &type, &sh);
1628 if (*type != '\0') {
1629 ds_put_format(&ds, "QoS: %s %s\n", iface->name, type);
1631 SHASH_FOR_EACH (node, &sh) {
1632 ds_put_format(&ds, "%s: %s\n", node->name, (char *)node->data);
1637 error = netdev_dump_queues(iface->netdev, qos_unixctl_show_cb, &data);
1640 ds_put_format(&ds, "failed to dump queues: %s", strerror(error));
1642 unixctl_command_reply(conn, 200, ds_cstr(&ds));
1644 ds_put_format(&ds, "QoS not configured on %s\n", iface->name);
1645 unixctl_command_reply(conn, 501, ds_cstr(&ds));
1648 shash_destroy_free_data(&sh);
1652 /* Bridge reconfiguration functions. */
1653 static struct bridge *
1654 bridge_create(const struct ovsrec_bridge *br_cfg)
1659 assert(!bridge_lookup(br_cfg->name));
1660 br = xzalloc(sizeof *br);
1662 error = dpif_create_and_open(br_cfg->name, br_cfg->datapath_type,
1668 dpif_flow_flush(br->dpif);
1670 error = ofproto_create(br_cfg->name, br_cfg->datapath_type, &bridge_ofhooks,
1673 VLOG_ERR("failed to create switch %s: %s", br_cfg->name,
1675 dpif_delete(br->dpif);
1676 dpif_close(br->dpif);
1681 br->name = xstrdup(br_cfg->name);
1683 br->ml = mac_learning_create();
1684 eth_addr_nicira_random(br->default_ea);
1686 hmap_init(&br->ifaces);
1688 shash_init(&br->port_by_name);
1689 shash_init(&br->iface_by_name);
1693 list_push_back(&all_bridges, &br->node);
1695 VLOG_INFO("created bridge %s on %s", br->name, dpif_name(br->dpif));
1701 bridge_destroy(struct bridge *br)
1706 while (br->n_ports > 0) {
1707 port_destroy(br->ports[br->n_ports - 1]);
1709 list_remove(&br->node);
1710 error = dpif_delete(br->dpif);
1711 if (error && error != ENOENT) {
1712 VLOG_ERR("failed to delete %s: %s",
1713 dpif_name(br->dpif), strerror(error));
1715 dpif_close(br->dpif);
1716 ofproto_destroy(br->ofproto);
1717 mac_learning_destroy(br->ml);
1718 hmap_destroy(&br->ifaces);
1719 shash_destroy(&br->port_by_name);
1720 shash_destroy(&br->iface_by_name);
1727 static struct bridge *
1728 bridge_lookup(const char *name)
1732 LIST_FOR_EACH (br, node, &all_bridges) {
1733 if (!strcmp(br->name, name)) {
1740 /* Handle requests for a listing of all flows known by the OpenFlow
1741 * stack, including those normally hidden. */
1743 bridge_unixctl_dump_flows(struct unixctl_conn *conn,
1744 const char *args, void *aux OVS_UNUSED)
1749 br = bridge_lookup(args);
1751 unixctl_command_reply(conn, 501, "Unknown bridge");
1756 ofproto_get_all_flows(br->ofproto, &results);
1758 unixctl_command_reply(conn, 200, ds_cstr(&results));
1759 ds_destroy(&results);
1762 /* "bridge/reconnect [BRIDGE]": makes BRIDGE drop all of its controller
1763 * connections and reconnect. If BRIDGE is not specified, then all bridges
1764 * drop their controller connections and reconnect. */
1766 bridge_unixctl_reconnect(struct unixctl_conn *conn,
1767 const char *args, void *aux OVS_UNUSED)
1770 if (args[0] != '\0') {
1771 br = bridge_lookup(args);
1773 unixctl_command_reply(conn, 501, "Unknown bridge");
1776 ofproto_reconnect_controllers(br->ofproto);
1778 LIST_FOR_EACH (br, node, &all_bridges) {
1779 ofproto_reconnect_controllers(br->ofproto);
1782 unixctl_command_reply(conn, 200, NULL);
1786 bridge_run_one(struct bridge *br)
1791 error = ofproto_run1(br->ofproto);
1796 mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
1798 for (i = 0; i < br->n_ports; i++) {
1799 port_run(br->ports[i]);
1802 error = ofproto_run2(br->ofproto, br->flush);
1809 bridge_get_controllers(const struct bridge *br,
1810 struct ovsrec_controller ***controllersp)
1812 struct ovsrec_controller **controllers;
1813 size_t n_controllers;
1815 controllers = br->cfg->controller;
1816 n_controllers = br->cfg->n_controller;
1818 if (n_controllers == 1 && !strcmp(controllers[0]->target, "none")) {
1824 *controllersp = controllers;
1826 return n_controllers;
1830 bridge_reconfigure_one(struct bridge *br)
1832 struct shash old_ports, new_ports;
1833 struct svec snoops, old_snoops;
1834 struct shash_node *node;
1835 enum ofproto_fail_mode fail_mode;
1838 /* Collect old ports. */
1839 shash_init(&old_ports);
1840 for (i = 0; i < br->n_ports; i++) {
1841 shash_add(&old_ports, br->ports[i]->name, br->ports[i]);
1844 /* Collect new ports. */
1845 shash_init(&new_ports);
1846 for (i = 0; i < br->cfg->n_ports; i++) {
1847 const char *name = br->cfg->ports[i]->name;
1848 if (!shash_add_once(&new_ports, name, br->cfg->ports[i])) {
1849 VLOG_WARN("bridge %s: %s specified twice as bridge port",
1854 /* If we have a controller, then we need a local port. Complain if the
1855 * user didn't specify one.
1857 * XXX perhaps we should synthesize a port ourselves in this case. */
1858 if (bridge_get_controllers(br, NULL)) {
1859 char local_name[IF_NAMESIZE];
1862 error = dpif_port_get_name(br->dpif, ODPP_LOCAL,
1863 local_name, sizeof local_name);
1864 if (!error && !shash_find(&new_ports, local_name)) {
1865 VLOG_WARN("bridge %s: controller specified but no local port "
1866 "(port named %s) defined",
1867 br->name, local_name);
1871 /* Get rid of deleted ports.
1872 * Get rid of deleted interfaces on ports that still exist. */
1873 SHASH_FOR_EACH (node, &old_ports) {
1874 struct port *port = node->data;
1875 const struct ovsrec_port *port_cfg;
1877 port_cfg = shash_find_data(&new_ports, node->name);
1881 port_del_ifaces(port, port_cfg);
1885 /* Create new ports.
1886 * Add new interfaces to existing ports.
1887 * Reconfigure existing ports. */
1888 SHASH_FOR_EACH (node, &new_ports) {
1889 struct port *port = shash_find_data(&old_ports, node->name);
1891 port = port_create(br, node->name);
1894 port_reconfigure(port, node->data);
1895 if (!port->n_ifaces) {
1896 VLOG_WARN("bridge %s: port %s has no interfaces, dropping",
1897 br->name, port->name);
1901 shash_destroy(&old_ports);
1902 shash_destroy(&new_ports);
1904 /* Set the fail-mode */
1905 fail_mode = !br->cfg->fail_mode
1906 || !strcmp(br->cfg->fail_mode, "standalone")
1907 ? OFPROTO_FAIL_STANDALONE
1908 : OFPROTO_FAIL_SECURE;
1909 if (ofproto_get_fail_mode(br->ofproto) != fail_mode
1910 && !ofproto_has_primary_controller(br->ofproto)) {
1911 ofproto_flush_flows(br->ofproto);
1913 ofproto_set_fail_mode(br->ofproto, fail_mode);
1915 /* Delete all flows if we're switching from connected to standalone or vice
1916 * versa. (XXX Should we delete all flows if we are switching from one
1917 * controller to another?) */
1919 /* Configure OpenFlow controller connection snooping. */
1921 svec_add_nocopy(&snoops, xasprintf("punix:%s/%s.snoop",
1922 ovs_rundir(), br->name));
1923 svec_init(&old_snoops);
1924 ofproto_get_snoops(br->ofproto, &old_snoops);
1925 if (!svec_equal(&snoops, &old_snoops)) {
1926 ofproto_set_snoops(br->ofproto, &snoops);
1928 svec_destroy(&snoops);
1929 svec_destroy(&old_snoops);
1931 mirror_reconfigure(br);
1934 /* Initializes 'oc' appropriately as a management service controller for
1937 * The caller must free oc->target when it is no longer needed. */
1939 bridge_ofproto_controller_for_mgmt(const struct bridge *br,
1940 struct ofproto_controller *oc)
1942 oc->target = xasprintf("punix:%s/%s.mgmt", ovs_rundir(), br->name);
1943 oc->max_backoff = 0;
1944 oc->probe_interval = 60;
1945 oc->band = OFPROTO_OUT_OF_BAND;
1947 oc->burst_limit = 0;
1950 /* Converts ovsrec_controller 'c' into an ofproto_controller in 'oc'. */
1952 bridge_ofproto_controller_from_ovsrec(const struct ovsrec_controller *c,
1953 struct ofproto_controller *oc)
1955 oc->target = c->target;
1956 oc->max_backoff = c->max_backoff ? *c->max_backoff / 1000 : 8;
1957 oc->probe_interval = c->inactivity_probe ? *c->inactivity_probe / 1000 : 5;
1958 oc->band = (!c->connection_mode || !strcmp(c->connection_mode, "in-band")
1959 ? OFPROTO_IN_BAND : OFPROTO_OUT_OF_BAND);
1960 oc->rate_limit = c->controller_rate_limit ? *c->controller_rate_limit : 0;
1961 oc->burst_limit = (c->controller_burst_limit
1962 ? *c->controller_burst_limit : 0);
1965 /* Configures the IP stack for 'br''s local interface properly according to the
1966 * configuration in 'c'. */
1968 bridge_configure_local_iface_netdev(struct bridge *br,
1969 struct ovsrec_controller *c)
1971 struct netdev *netdev;
1972 struct in_addr mask, gateway;
1974 struct iface *local_iface;
1977 /* If there's no local interface or no IP address, give up. */
1978 local_iface = bridge_get_local_iface(br);
1979 if (!local_iface || !c->local_ip || !inet_aton(c->local_ip, &ip)) {
1983 /* Bring up the local interface. */
1984 netdev = local_iface->netdev;
1985 netdev_turn_flags_on(netdev, NETDEV_UP, true);
1987 /* Configure the IP address and netmask. */
1988 if (!c->local_netmask
1989 || !inet_aton(c->local_netmask, &mask)
1991 mask.s_addr = guess_netmask(ip.s_addr);
1993 if (!netdev_set_in4(netdev, ip, mask)) {
1994 VLOG_INFO("bridge %s: configured IP address "IP_FMT", netmask "IP_FMT,
1995 br->name, IP_ARGS(&ip.s_addr), IP_ARGS(&mask.s_addr));
1998 /* Configure the default gateway. */
1999 if (c->local_gateway
2000 && inet_aton(c->local_gateway, &gateway)
2001 && gateway.s_addr) {
2002 if (!netdev_add_router(netdev, gateway)) {
2003 VLOG_INFO("bridge %s: configured gateway "IP_FMT,
2004 br->name, IP_ARGS(&gateway.s_addr));
2010 bridge_reconfigure_remotes(struct bridge *br,
2011 const struct sockaddr_in *managers,
2014 const char *disable_ib_str, *queue_id_str;
2015 bool disable_in_band = false;
2018 struct ovsrec_controller **controllers;
2019 size_t n_controllers;
2022 struct ofproto_controller *ocs;
2026 /* Check if we should disable in-band control on this bridge. */
2027 disable_ib_str = bridge_get_other_config(br->cfg, "disable-in-band");
2028 if (disable_ib_str && !strcmp(disable_ib_str, "true")) {
2029 disable_in_band = true;
2032 /* Set OpenFlow queue ID for in-band control. */
2033 queue_id_str = bridge_get_other_config(br->cfg, "in-band-queue");
2034 queue_id = queue_id_str ? strtol(queue_id_str, NULL, 10) : -1;
2035 ofproto_set_in_band_queue(br->ofproto, queue_id);
2037 if (disable_in_band) {
2038 ofproto_set_extra_in_band_remotes(br->ofproto, NULL, 0);
2040 ofproto_set_extra_in_band_remotes(br->ofproto, managers, n_managers);
2042 had_primary = ofproto_has_primary_controller(br->ofproto);
2044 n_controllers = bridge_get_controllers(br, &controllers);
2046 ocs = xmalloc((n_controllers + 1) * sizeof *ocs);
2049 bridge_ofproto_controller_for_mgmt(br, &ocs[n_ocs++]);
2050 for (i = 0; i < n_controllers; i++) {
2051 struct ovsrec_controller *c = controllers[i];
2053 if (!strncmp(c->target, "punix:", 6)
2054 || !strncmp(c->target, "unix:", 5)) {
2055 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2057 /* Prevent remote ovsdb-server users from accessing arbitrary Unix
2058 * domain sockets and overwriting arbitrary local files. */
2059 VLOG_ERR_RL(&rl, "%s: not adding Unix domain socket controller "
2060 "\"%s\" due to possibility for remote exploit",
2061 dpif_name(br->dpif), c->target);
2065 bridge_configure_local_iface_netdev(br, c);
2066 bridge_ofproto_controller_from_ovsrec(c, &ocs[n_ocs]);
2067 if (disable_in_band) {
2068 ocs[n_ocs].band = OFPROTO_OUT_OF_BAND;
2073 ofproto_set_controllers(br->ofproto, ocs, n_ocs);
2074 free(ocs[0].target); /* From bridge_ofproto_controller_for_mgmt(). */
2077 if (had_primary != ofproto_has_primary_controller(br->ofproto)) {
2078 ofproto_flush_flows(br->ofproto);
2081 /* If there are no controllers and the bridge is in standalone
2082 * mode, set up a flow that matches every packet and directs
2083 * them to OFPP_NORMAL (which goes to us). Otherwise, the
2084 * switch is in secure mode and we won't pass any traffic until
2085 * a controller has been defined and it tells us to do so. */
2087 && ofproto_get_fail_mode(br->ofproto) == OFPROTO_FAIL_STANDALONE) {
2088 union ofp_action action;
2089 struct cls_rule rule;
2091 memset(&action, 0, sizeof action);
2092 action.type = htons(OFPAT_OUTPUT);
2093 action.output.len = htons(sizeof action);
2094 action.output.port = htons(OFPP_NORMAL);
2095 cls_rule_init_catchall(&rule, 0);
2096 ofproto_add_flow(br->ofproto, &rule, &action, 1);
2101 bridge_get_all_ifaces(const struct bridge *br, struct shash *ifaces)
2106 for (i = 0; i < br->n_ports; i++) {
2107 struct port *port = br->ports[i];
2108 for (j = 0; j < port->n_ifaces; j++) {
2109 struct iface *iface = port->ifaces[j];
2110 shash_add_once(ifaces, iface->name, iface);
2112 if (port->n_ifaces > 1 && port->cfg->bond_fake_iface) {
2113 shash_add_once(ifaces, port->name, NULL);
2118 /* For robustness, in case the administrator moves around datapath ports behind
2119 * our back, we re-check all the datapath port numbers here.
2121 * This function will set the 'dp_ifidx' members of interfaces that have
2122 * disappeared to -1, so only call this function from a context where those
2123 * 'struct iface's will be removed from the bridge. Otherwise, the -1
2124 * 'dp_ifidx'es will cause trouble later when we try to send them to the
2125 * datapath, which doesn't support UINT16_MAX+1 ports. */
2127 bridge_fetch_dp_ifaces(struct bridge *br)
2129 struct dpif_port_dump dump;
2130 struct dpif_port dpif_port;
2133 /* Reset all interface numbers. */
2134 for (i = 0; i < br->n_ports; i++) {
2135 struct port *port = br->ports[i];
2136 for (j = 0; j < port->n_ifaces; j++) {
2137 struct iface *iface = port->ifaces[j];
2138 iface->dp_ifidx = -1;
2141 hmap_clear(&br->ifaces);
2143 DPIF_PORT_FOR_EACH (&dpif_port, &dump, br->dpif) {
2144 struct iface *iface = iface_lookup(br, dpif_port.name);
2146 if (iface->dp_ifidx >= 0) {
2147 VLOG_WARN("%s reported interface %s twice",
2148 dpif_name(br->dpif), dpif_port.name);
2149 } else if (iface_from_dp_ifidx(br, dpif_port.port_no)) {
2150 VLOG_WARN("%s reported interface %"PRIu16" twice",
2151 dpif_name(br->dpif), dpif_port.port_no);
2153 iface->dp_ifidx = dpif_port.port_no;
2154 hmap_insert(&br->ifaces, &iface->dp_ifidx_node,
2155 hash_int(iface->dp_ifidx, 0));
2158 iface_set_ofport(iface->cfg,
2159 (iface->dp_ifidx >= 0
2160 ? odp_port_to_ofp_port(iface->dp_ifidx)
2166 /* Bridge packet processing functions. */
2169 bond_is_tcp_hash(const struct port *port)
2171 return port->bond_mode == BM_TCP && lacp_negotiated(port->lacp);
2175 bond_hash_src(const uint8_t mac[ETH_ADDR_LEN], uint16_t vlan)
2177 return hash_bytes(mac, ETH_ADDR_LEN, vlan) & BOND_MASK;
2180 static int bond_hash_tcp(const struct flow *flow, uint16_t vlan)
2182 struct flow hash_flow;
2184 memcpy(&hash_flow, flow, sizeof hash_flow);
2185 hash_flow.vlan_tci = 0;
2187 /* The symmetric quality of this hash function is not required, but
2188 * flow_hash_symmetric_l4 already exists, and is sufficient for our
2189 * purposes, so we use it out of convenience. */
2190 return flow_hash_symmetric_l4(&hash_flow, vlan) & BOND_MASK;
2193 static struct bond_entry *
2194 lookup_bond_entry(const struct port *port, const struct flow *flow,
2197 assert(port->bond_mode != BM_AB);
2199 if (bond_is_tcp_hash(port)) {
2200 return &port->bond_hash[bond_hash_tcp(flow, vlan)];
2202 return &port->bond_hash[bond_hash_src(flow->dl_src, vlan)];
2207 bond_choose_iface(const struct port *port)
2209 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
2210 size_t i, best_down_slave = -1;
2211 long long next_delay_expiration = LLONG_MAX;
2213 for (i = 0; i < port->n_ifaces; i++) {
2214 struct iface *iface = port->ifaces[i];
2216 if (iface->enabled) {
2218 } else if (iface->delay_expires < next_delay_expiration
2219 && lacp_slave_may_enable(port->lacp, iface)) {
2220 best_down_slave = i;
2221 next_delay_expiration = iface->delay_expires;
2225 if (best_down_slave != -1) {
2226 struct iface *iface = port->ifaces[best_down_slave];
2228 VLOG_INFO_RL(&rl, "interface %s: skipping remaining %lli ms updelay "
2229 "since no other interface is up", iface->name,
2230 iface->delay_expires - time_msec());
2231 bond_enable_slave(iface, true);
2234 return best_down_slave;
2238 choose_output_iface(const struct port *port, const struct flow *flow,
2239 uint16_t vlan, uint16_t *dp_ifidx, tag_type *tags)
2241 struct iface *iface;
2243 assert(port->n_ifaces);
2244 if (port->n_ifaces == 1) {
2245 iface = port->ifaces[0];
2246 } else if (port->bond_mode == BM_AB) {
2247 if (port->active_iface < 0) {
2248 *tags |= port->no_ifaces_tag;
2251 iface = port->ifaces[port->active_iface];
2253 struct bond_entry *e = lookup_bond_entry(port, flow, vlan);
2254 if (e->iface_idx < 0 || e->iface_idx >= port->n_ifaces
2255 || !port->ifaces[e->iface_idx]->enabled) {
2256 /* XXX select interface properly. The current interface selection
2257 * is only good for testing the rebalancing code. */
2258 e->iface_idx = bond_choose_iface(port);
2259 if (e->iface_idx < 0) {
2260 *tags |= port->no_ifaces_tag;
2263 e->iface_tag = tag_create_random();
2265 *tags |= e->iface_tag;
2266 iface = port->ifaces[e->iface_idx];
2268 *dp_ifidx = iface->dp_ifidx;
2269 *tags |= iface->tag; /* Currently only used for bonding. */
2274 bond_link_status_update(struct iface *iface)
2276 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
2277 struct port *port = iface->port;
2278 bool up = iface->up && lacp_slave_may_enable(port->lacp, iface);
2279 int updelay, downdelay;
2281 updelay = port->updelay;
2282 downdelay = port->downdelay;
2284 if (lacp_negotiated(port->lacp)) {
2289 if ((up == iface->enabled) == (iface->delay_expires == LLONG_MAX)) {
2290 /* Nothing to do. */
2293 VLOG_INFO_RL(&rl, "interface %s: link state %s",
2294 iface->name, up ? "up" : "down");
2295 if (up == iface->enabled) {
2296 iface->delay_expires = LLONG_MAX;
2297 VLOG_INFO_RL(&rl, "interface %s: will not be %s",
2298 iface->name, up ? "disabled" : "enabled");
2299 } else if (up && port->active_iface < 0) {
2300 bond_enable_slave(iface, true);
2302 VLOG_INFO_RL(&rl, "interface %s: skipping %d ms updelay since no "
2303 "other interface is up", iface->name, updelay);
2306 int delay = up ? updelay : downdelay;
2307 iface->delay_expires = time_msec() + delay;
2310 "interface %s: will be %s if it stays %s for %d ms",
2312 up ? "enabled" : "disabled",
2320 bond_choose_active_iface(struct port *port)
2322 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
2324 port->active_iface = bond_choose_iface(port);
2325 port->active_iface_tag = tag_create_random();
2326 if (port->active_iface >= 0) {
2327 VLOG_INFO_RL(&rl, "port %s: active interface is now %s",
2328 port->name, port->ifaces[port->active_iface]->name);
2330 VLOG_WARN_RL(&rl, "port %s: all ports disabled, no active interface",
2336 bond_enable_slave(struct iface *iface, bool enable)
2338 struct port *port = iface->port;
2339 struct bridge *br = port->bridge;
2341 /* This acts as a recursion check. If the act of disabling a slave
2342 * causes a different slave to be enabled, the flag will allow us to
2343 * skip redundant work when we reenter this function. It must be
2344 * cleared on exit to keep things safe with multiple bonds. */
2345 static bool moving_active_iface = false;
2347 iface->delay_expires = LLONG_MAX;
2348 if (enable == iface->enabled) {
2352 iface->enabled = enable;
2353 if (!iface->enabled) {
2354 VLOG_WARN("interface %s: disabled", iface->name);
2355 ofproto_revalidate(br->ofproto, iface->tag);
2356 if (iface->port_ifidx == port->active_iface) {
2357 ofproto_revalidate(br->ofproto,
2358 port->active_iface_tag);
2360 /* Disabling a slave can lead to another slave being immediately
2361 * enabled if there will be no active slaves but one is waiting
2362 * on an updelay. In this case we do not need to run most of the
2363 * code for the newly enabled slave since there was no period
2364 * without an active slave and it is redundant with the disabling
2366 moving_active_iface = true;
2367 bond_choose_active_iface(port);
2369 bond_send_learning_packets(port);
2371 VLOG_WARN("interface %s: enabled", iface->name);
2372 if (port->active_iface < 0 && !moving_active_iface) {
2373 ofproto_revalidate(br->ofproto, port->no_ifaces_tag);
2374 bond_choose_active_iface(port);
2375 bond_send_learning_packets(port);
2377 iface->tag = tag_create_random();
2380 moving_active_iface = false;
2383 /* Attempts to make the sum of the bond slaves' statistics appear on the fake
2384 * bond interface. */
2386 bond_update_fake_iface_stats(struct port *port)
2388 struct netdev_stats bond_stats;
2389 struct netdev *bond_dev;
2392 memset(&bond_stats, 0, sizeof bond_stats);
2394 for (i = 0; i < port->n_ifaces; i++) {
2395 struct netdev_stats slave_stats;
2397 if (!netdev_get_stats(port->ifaces[i]->netdev, &slave_stats)) {
2398 /* XXX: We swap the stats here because they are swapped back when
2399 * reported by the internal device. The reason for this is
2400 * internal devices normally represent packets going into the system
2401 * but when used as fake bond device they represent packets leaving
2402 * the system. We really should do this in the internal device
2403 * itself because changing it here reverses the counts from the
2404 * perspective of the switch. However, the internal device doesn't
2405 * know what type of device it represents so we have to do it here
2407 bond_stats.tx_packets += slave_stats.rx_packets;
2408 bond_stats.tx_bytes += slave_stats.rx_bytes;
2409 bond_stats.rx_packets += slave_stats.tx_packets;
2410 bond_stats.rx_bytes += slave_stats.tx_bytes;
2414 if (!netdev_open_default(port->name, &bond_dev)) {
2415 netdev_set_stats(bond_dev, &bond_stats);
2416 netdev_close(bond_dev);
2421 bond_run(struct port *port)
2425 if (port->n_ifaces < 2) {
2429 for (i = 0; i < port->n_ifaces; i++) {
2430 bond_link_status_update(port->ifaces[i]);
2433 for (i = 0; i < port->n_ifaces; i++) {
2434 struct iface *iface = port->ifaces[i];
2435 if (time_msec() >= iface->delay_expires) {
2436 bond_enable_slave(iface, !iface->enabled);
2440 if (port->bond_fake_iface
2441 && time_msec() >= port->bond_next_fake_iface_update) {
2442 bond_update_fake_iface_stats(port);
2443 port->bond_next_fake_iface_update = time_msec() + 1000;
2448 bond_wait(struct port *port)
2452 if (port->n_ifaces < 2) {
2456 for (i = 0; i < port->n_ifaces; i++) {
2457 struct iface *iface = port->ifaces[i];
2458 if (iface->delay_expires != LLONG_MAX) {
2459 poll_timer_wait_until(iface->delay_expires);
2463 if (port->bond_fake_iface) {
2464 poll_timer_wait_until(port->bond_next_fake_iface_update);
2469 set_dst(struct dst *dst, const struct flow *flow,
2470 const struct port *in_port, const struct port *out_port,
2473 dst->vlan = (out_port->vlan >= 0 ? OFP_VLAN_NONE
2474 : in_port->vlan >= 0 ? in_port->vlan
2475 : flow->vlan_tci == 0 ? OFP_VLAN_NONE
2476 : vlan_tci_to_vid(flow->vlan_tci));
2477 return choose_output_iface(out_port, flow, dst->vlan,
2478 &dst->dp_ifidx, tags);
2482 swap_dst(struct dst *p, struct dst *q)
2484 struct dst tmp = *p;
2489 /* Moves all the dsts with vlan == 'vlan' to the front of the 'n_dsts' in
2490 * 'dsts'. (This may help performance by reducing the number of VLAN changes
2491 * that we push to the datapath. We could in fact fully sort the array by
2492 * vlan, but in most cases there are at most two different vlan tags so that's
2493 * possibly overkill.) */
2495 partition_dsts(struct dst_set *set, int vlan)
2497 struct dst *first = set->dsts;
2498 struct dst *last = set->dsts + set->n;
2500 while (first != last) {
2502 * - All dsts < first have vlan == 'vlan'.
2503 * - All dsts >= last have vlan != 'vlan'.
2504 * - first < last. */
2505 while (first->vlan == vlan) {
2506 if (++first == last) {
2511 /* Same invariants, plus one additional:
2512 * - first->vlan != vlan.
2514 while (last[-1].vlan != vlan) {
2515 if (--last == first) {
2520 /* Same invariants, plus one additional:
2521 * - last[-1].vlan == vlan.*/
2522 swap_dst(first++, --last);
2527 mirror_mask_ffs(mirror_mask_t mask)
2529 BUILD_ASSERT_DECL(sizeof(unsigned int) >= sizeof(mask));
2534 dst_set_init(struct dst_set *set)
2536 set->dsts = set->builtin;
2538 set->allocated = ARRAY_SIZE(set->builtin);
2542 dst_set_add(struct dst_set *set, const struct dst *dst)
2544 if (set->n >= set->allocated) {
2545 size_t new_allocated;
2546 struct dst *new_dsts;
2548 new_allocated = set->allocated * 2;
2549 new_dsts = xmalloc(new_allocated * sizeof *new_dsts);
2550 memcpy(new_dsts, set->dsts, set->n * sizeof *new_dsts);
2554 set->dsts = new_dsts;
2555 set->allocated = new_allocated;
2557 set->dsts[set->n++] = *dst;
2561 dst_set_free(struct dst_set *set)
2563 if (set->dsts != set->builtin) {
2569 dst_is_duplicate(const struct dst_set *set, const struct dst *test)
2572 for (i = 0; i < set->n; i++) {
2573 if (set->dsts[i].vlan == test->vlan
2574 && set->dsts[i].dp_ifidx == test->dp_ifidx) {
2582 port_trunks_vlan(const struct port *port, uint16_t vlan)
2584 return (port->vlan < 0
2585 && (!port->trunks || bitmap_is_set(port->trunks, vlan)));
2589 port_includes_vlan(const struct port *port, uint16_t vlan)
2591 return vlan == port->vlan || port_trunks_vlan(port, vlan);
2595 port_is_floodable(const struct port *port)
2599 for (i = 0; i < port->n_ifaces; i++) {
2600 if (!ofproto_port_is_floodable(port->bridge->ofproto,
2601 port->ifaces[i]->dp_ifidx)) {
2609 compose_dsts(const struct bridge *br, const struct flow *flow, uint16_t vlan,
2610 const struct port *in_port, const struct port *out_port,
2611 struct dst_set *set, tag_type *tags, uint16_t *nf_output_iface)
2613 mirror_mask_t mirrors = in_port->src_mirrors;
2618 flow_vlan = vlan_tci_to_vid(flow->vlan_tci);
2619 if (flow_vlan == 0) {
2620 flow_vlan = OFP_VLAN_NONE;
2623 if (out_port == FLOOD_PORT) {
2624 for (i = 0; i < br->n_ports; i++) {
2625 struct port *port = br->ports[i];
2627 && port_is_floodable(port)
2628 && port_includes_vlan(port, vlan)
2629 && !port->is_mirror_output_port
2630 && set_dst(&dst, flow, in_port, port, tags)) {
2631 mirrors |= port->dst_mirrors;
2632 dst_set_add(set, &dst);
2635 *nf_output_iface = NF_OUT_FLOOD;
2636 } else if (out_port && set_dst(&dst, flow, in_port, out_port, tags)) {
2637 dst_set_add(set, &dst);
2638 *nf_output_iface = dst.dp_ifidx;
2639 mirrors |= out_port->dst_mirrors;
2643 struct mirror *m = br->mirrors[mirror_mask_ffs(mirrors) - 1];
2644 if (!m->n_vlans || vlan_is_mirrored(m, vlan)) {
2646 if (set_dst(&dst, flow, in_port, m->out_port, tags)
2647 && !dst_is_duplicate(set, &dst)) {
2648 dst_set_add(set, &dst);
2651 for (i = 0; i < br->n_ports; i++) {
2652 struct port *port = br->ports[i];
2653 if (port_includes_vlan(port, m->out_vlan)
2654 && set_dst(&dst, flow, in_port, port, tags))
2656 if (port->vlan < 0) {
2657 dst.vlan = m->out_vlan;
2659 if (dst_is_duplicate(set, &dst)) {
2663 /* Use the vlan tag on the original flow instead of
2664 * the one passed in the vlan parameter. This ensures
2665 * that we compare the vlan from before any implicit
2666 * tagging tags place. This is necessary because
2667 * dst->vlan is the final vlan, after removing implicit
2669 if (port == in_port && dst.vlan == flow_vlan) {
2670 /* Don't send out input port on same VLAN. */
2673 dst_set_add(set, &dst);
2678 mirrors &= mirrors - 1;
2681 partition_dsts(set, flow_vlan);
2684 static void OVS_UNUSED
2685 print_dsts(const struct dst_set *set)
2689 for (i = 0; i < set->n; i++) {
2690 const struct dst *dst = &set->dsts[i];
2692 printf(">p%"PRIu16, dst->dp_ifidx);
2693 if (dst->vlan != OFP_VLAN_NONE) {
2694 printf("v%"PRIu16, dst->vlan);
2700 compose_actions(struct bridge *br, const struct flow *flow, uint16_t vlan,
2701 const struct port *in_port, const struct port *out_port,
2702 tag_type *tags, struct ofpbuf *actions,
2703 uint16_t *nf_output_iface)
2710 compose_dsts(br, flow, vlan, in_port, out_port, &set, tags,
2713 cur_vlan = vlan_tci_to_vid(flow->vlan_tci);
2714 if (cur_vlan == 0) {
2715 cur_vlan = OFP_VLAN_NONE;
2717 for (i = 0; i < set.n; i++) {
2718 const struct dst *dst = &set.dsts[i];
2719 if (dst->vlan != cur_vlan) {
2720 if (dst->vlan == OFP_VLAN_NONE) {
2721 nl_msg_put_flag(actions, ODP_ACTION_ATTR_STRIP_VLAN);
2724 tci = htons(dst->vlan & VLAN_VID_MASK);
2725 tci |= flow->vlan_tci & htons(VLAN_PCP_MASK);
2726 nl_msg_put_be16(actions, ODP_ACTION_ATTR_SET_DL_TCI, tci);
2728 cur_vlan = dst->vlan;
2730 nl_msg_put_u32(actions, ODP_ACTION_ATTR_OUTPUT, dst->dp_ifidx);
2735 /* Returns the effective vlan of a packet, taking into account both the
2736 * 802.1Q header and implicitly tagged ports. A value of 0 indicates that
2737 * the packet is untagged and -1 indicates it has an invalid header and
2738 * should be dropped. */
2739 static int flow_get_vlan(struct bridge *br, const struct flow *flow,
2740 struct port *in_port, bool have_packet)
2742 int vlan = vlan_tci_to_vid(flow->vlan_tci);
2743 if (in_port->vlan >= 0) {
2745 /* XXX support double tagging? */
2747 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2748 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2749 "packet received on port %s configured with "
2750 "implicit VLAN %"PRIu16,
2751 br->name, vlan, in_port->name, in_port->vlan);
2755 vlan = in_port->vlan;
2757 if (!port_includes_vlan(in_port, vlan)) {
2759 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2760 VLOG_WARN_RL(&rl, "bridge %s: dropping VLAN %d tagged "
2761 "packet received on port %s not configured for "
2763 br->name, vlan, in_port->name, vlan);
2772 /* A VM broadcasts a gratuitous ARP to indicate that it has resumed after
2773 * migration. Older Citrix-patched Linux DomU used gratuitous ARP replies to
2774 * indicate this; newer upstream kernels use gratuitous ARP requests. */
2776 is_gratuitous_arp(const struct flow *flow)
2778 return (flow->dl_type == htons(ETH_TYPE_ARP)
2779 && eth_addr_is_broadcast(flow->dl_dst)
2780 && (flow->nw_proto == ARP_OP_REPLY
2781 || (flow->nw_proto == ARP_OP_REQUEST
2782 && flow->nw_src == flow->nw_dst)));
2786 update_learning_table(struct bridge *br, const struct flow *flow, int vlan,
2787 struct port *in_port)
2789 enum grat_arp_lock_type lock_type;
2792 /* We don't want to learn from gratuitous ARP packets that are reflected
2793 * back over bond slaves so we lock the learning table. */
2794 lock_type = !is_gratuitous_arp(flow) ? GRAT_ARP_LOCK_NONE :
2795 (in_port->n_ifaces == 1) ? GRAT_ARP_LOCK_SET :
2796 GRAT_ARP_LOCK_CHECK;
2798 rev_tag = mac_learning_learn(br->ml, flow->dl_src, vlan, in_port->port_idx,
2801 /* The log messages here could actually be useful in debugging,
2802 * so keep the rate limit relatively high. */
2803 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30,
2805 VLOG_DBG_RL(&rl, "bridge %s: learned that "ETH_ADDR_FMT" is "
2806 "on port %s in VLAN %d",
2807 br->name, ETH_ADDR_ARGS(flow->dl_src),
2808 in_port->name, vlan);
2809 ofproto_revalidate(br->ofproto, rev_tag);
2813 /* Determines whether packets in 'flow' within 'br' should be forwarded or
2814 * dropped. Returns true if they may be forwarded, false if they should be
2817 * If 'have_packet' is true, it indicates that the caller is processing a
2818 * received packet. If 'have_packet' is false, then the caller is just
2819 * revalidating an existing flow because configuration has changed. Either
2820 * way, 'have_packet' only affects logging (there is no point in logging errors
2821 * during revalidation).
2823 * Sets '*in_portp' to the input port. This will be a null pointer if
2824 * flow->in_port does not designate a known input port (in which case
2825 * is_admissible() returns false).
2827 * When returning true, sets '*vlanp' to the effective VLAN of the input
2828 * packet, as returned by flow_get_vlan().
2830 * May also add tags to '*tags', although the current implementation only does
2831 * so in one special case.
2834 is_admissible(struct bridge *br, const struct flow *flow, bool have_packet,
2835 tag_type *tags, int *vlanp, struct port **in_portp)
2837 struct iface *in_iface;
2838 struct port *in_port;
2841 /* Find the interface and port structure for the received packet. */
2842 in_iface = iface_from_dp_ifidx(br, flow->in_port);
2844 /* No interface? Something fishy... */
2846 /* Odd. A few possible reasons here:
2848 * - We deleted an interface but there are still a few packets
2849 * queued up from it.
2851 * - Someone externally added an interface (e.g. with "ovs-dpctl
2852 * add-if") that we don't know about.
2854 * - Packet arrived on the local port but the local port is not
2855 * one of our bridge ports.
2857 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2859 VLOG_WARN_RL(&rl, "bridge %s: received packet on unknown "
2860 "interface %"PRIu16, br->name, flow->in_port);
2866 *in_portp = in_port = in_iface->port;
2867 *vlanp = vlan = flow_get_vlan(br, flow, in_port, have_packet);
2872 /* Drop frames for reserved multicast addresses. */
2873 if (eth_addr_is_reserved(flow->dl_dst)) {
2877 /* Drop frames on ports reserved for mirroring. */
2878 if (in_port->is_mirror_output_port) {
2880 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
2881 VLOG_WARN_RL(&rl, "bridge %s: dropping packet received on port "
2882 "%s, which is reserved exclusively for mirroring",
2883 br->name, in_port->name);
2888 /* When using LACP, do not accept packets from disabled interfaces. */
2889 if (lacp_negotiated(in_port->lacp) && !in_iface->enabled) {
2893 /* Packets received on non-LACP bonds need special attention to avoid
2895 if (in_port->n_ifaces > 1 && !lacp_negotiated(in_port->lacp)) {
2897 bool is_grat_arp_locked;
2899 if (eth_addr_is_multicast(flow->dl_dst)) {
2900 *tags |= in_port->active_iface_tag;
2901 if (in_port->active_iface != in_iface->port_ifidx) {
2902 /* Drop all multicast packets on inactive slaves. */
2907 /* Drop all packets for which we have learned a different input
2908 * port, because we probably sent the packet on one slave and got
2909 * it back on the other. Gratuitous ARP packets are an exception
2910 * to this rule: the host has moved to another switch. The exception
2911 * to the exception is if we locked the learning table to avoid
2912 * reflections on bond slaves. If this is the case, just drop the
2914 src_idx = mac_learning_lookup(br->ml, flow->dl_src, vlan,
2915 &is_grat_arp_locked);
2916 if (src_idx != -1 && src_idx != in_port->port_idx &&
2917 (!is_gratuitous_arp(flow) || is_grat_arp_locked)) {
2925 /* If the composed actions may be applied to any packet in the given 'flow',
2926 * returns true. Otherwise, the actions should only be applied to 'packet', or
2927 * not at all, if 'packet' was NULL. */
2929 process_flow(struct bridge *br, const struct flow *flow,
2930 const struct ofpbuf *packet, struct ofpbuf *actions,
2931 tag_type *tags, uint16_t *nf_output_iface)
2933 struct port *in_port;
2934 struct port *out_port;
2938 /* Check whether we should drop packets in this flow. */
2939 if (!is_admissible(br, flow, packet != NULL, tags, &vlan, &in_port)) {
2944 /* Learn source MAC (but don't try to learn from revalidation). */
2946 update_learning_table(br, flow, vlan, in_port);
2949 /* Determine output port. */
2950 out_port_idx = mac_learning_lookup_tag(br->ml, flow->dl_dst, vlan, tags,
2952 if (out_port_idx >= 0 && out_port_idx < br->n_ports) {
2953 out_port = br->ports[out_port_idx];
2954 } else if (!packet && !eth_addr_is_multicast(flow->dl_dst)) {
2955 /* If we are revalidating but don't have a learning entry then
2956 * eject the flow. Installing a flow that floods packets opens
2957 * up a window of time where we could learn from a packet reflected
2958 * on a bond and blackhole packets before the learning table is
2959 * updated to reflect the correct port. */
2962 out_port = FLOOD_PORT;
2965 /* Don't send packets out their input ports. */
2966 if (in_port == out_port) {
2972 compose_actions(br, flow, vlan, in_port, out_port, tags, actions,
2980 bridge_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
2981 struct ofpbuf *actions, tag_type *tags,
2982 uint16_t *nf_output_iface, void *br_)
2984 struct bridge *br = br_;
2986 COVERAGE_INC(bridge_process_flow);
2987 return process_flow(br, flow, packet, actions, tags, nf_output_iface);
2991 bridge_special_ofhook_cb(const struct flow *flow,
2992 const struct ofpbuf *packet, void *br_)
2994 struct iface *iface;
2995 struct bridge *br = br_;
2997 iface = iface_from_dp_ifidx(br, flow->in_port);
2999 if (cfm_should_process_flow(flow)) {
3001 if (iface && packet && iface->cfm) {
3002 COVERAGE_INC(bridge_process_cfm);
3003 cfm_process_heartbeat(iface->cfm, packet);
3006 } else if (flow->dl_type == htons(ETH_TYPE_LACP)) {
3008 if (iface && iface->port->lacp && packet) {
3009 const struct lacp_pdu *pdu = parse_lacp_packet(packet);
3012 COVERAGE_INC(bridge_process_lacp);
3013 lacp_process_pdu(iface->port->lacp, iface, pdu);
3023 bridge_account_flow_ofhook_cb(const struct flow *flow, tag_type tags,
3024 const struct nlattr *actions,
3026 uint64_t n_bytes, void *br_)
3028 struct bridge *br = br_;
3029 const struct nlattr *a;
3030 struct port *in_port;
3035 /* Feed information from the active flows back into the learning table to
3036 * ensure that table is always in sync with what is actually flowing
3037 * through the datapath.
3039 * We test that 'tags' is nonzero to ensure that only flows that include an
3040 * OFPP_NORMAL action are used for learning. This works because
3041 * bridge_normal_ofhook_cb() always sets a nonzero tag value. */
3042 if (tags && is_admissible(br, flow, false, &dummy, &vlan, &in_port)) {
3043 update_learning_table(br, flow, vlan, in_port);
3046 /* Account for bond slave utilization. */
3047 if (!br->has_bonded_ports) {
3050 NL_ATTR_FOR_EACH_UNSAFE (a, left, actions, actions_len) {
3051 if (nl_attr_type(a) == ODP_ACTION_ATTR_OUTPUT) {
3052 struct port *out_port = port_from_dp_ifidx(br, nl_attr_get_u32(a));
3053 if (out_port && out_port->n_ifaces >= 2 &&
3054 out_port->bond_mode != BM_AB) {
3055 uint16_t vlan = (flow->vlan_tci
3056 ? vlan_tci_to_vid(flow->vlan_tci)
3058 struct bond_entry *e = lookup_bond_entry(out_port, flow, vlan);
3059 e->tx_bytes += n_bytes;
3066 bridge_account_checkpoint_ofhook_cb(void *br_)
3068 struct bridge *br = br_;
3072 if (!br->has_bonded_ports) {
3077 for (i = 0; i < br->n_ports; i++) {
3078 struct port *port = br->ports[i];
3079 if (port->n_ifaces > 1 && port->bond_mode != BM_AB
3080 && now >= port->bond_next_rebalance) {
3081 port->bond_next_rebalance = now + port->bond_rebalance_interval;
3082 bond_rebalance_port(port);
3087 static struct ofhooks bridge_ofhooks = {
3088 bridge_normal_ofhook_cb,
3089 bridge_special_ofhook_cb,
3090 bridge_account_flow_ofhook_cb,
3091 bridge_account_checkpoint_ofhook_cb,
3094 /* Bonding functions. */
3096 /* Statistics for a single interface on a bonded port, used for load-based
3097 * bond rebalancing. */
3098 struct slave_balance {
3099 struct iface *iface; /* The interface. */
3100 uint64_t tx_bytes; /* Sum of hashes[*]->tx_bytes. */
3102 /* All the "bond_entry"s that are assigned to this interface, in order of
3103 * increasing tx_bytes. */
3104 struct bond_entry **hashes;
3109 bond_mode_to_string(enum bond_mode bm) {
3110 static char *bm_slb = "balance-slb";
3111 static char *bm_ab = "active-backup";
3112 static char *bm_tcp = "balance-tcp";
3115 case BM_SLB: return bm_slb;
3116 case BM_AB: return bm_ab;
3117 case BM_TCP: return bm_tcp;
3124 /* Sorts pointers to pointers to bond_entries in ascending order by the
3125 * interface to which they are assigned, and within a single interface in
3126 * ascending order of bytes transmitted. */
3128 compare_bond_entries(const void *a_, const void *b_)
3130 const struct bond_entry *const *ap = a_;
3131 const struct bond_entry *const *bp = b_;
3132 const struct bond_entry *a = *ap;
3133 const struct bond_entry *b = *bp;
3134 if (a->iface_idx != b->iface_idx) {
3135 return a->iface_idx > b->iface_idx ? 1 : -1;
3136 } else if (a->tx_bytes != b->tx_bytes) {
3137 return a->tx_bytes > b->tx_bytes ? 1 : -1;
3143 /* Sorts slave_balances so that enabled ports come first, and otherwise in
3144 * *descending* order by number of bytes transmitted. */
3146 compare_slave_balance(const void *a_, const void *b_)
3148 const struct slave_balance *a = a_;
3149 const struct slave_balance *b = b_;
3150 if (a->iface->enabled != b->iface->enabled) {
3151 return a->iface->enabled ? -1 : 1;
3152 } else if (a->tx_bytes != b->tx_bytes) {
3153 return a->tx_bytes > b->tx_bytes ? -1 : 1;
3160 swap_bals(struct slave_balance *a, struct slave_balance *b)
3162 struct slave_balance tmp = *a;
3167 /* Restores the 'n_bals' slave_balance structures in 'bals' to sorted order
3168 * given that 'p' (and only 'p') might be in the wrong location.
3170 * This function invalidates 'p', since it might now be in a different memory
3173 resort_bals(struct slave_balance *p,
3174 struct slave_balance bals[], size_t n_bals)
3177 for (; p > bals && p->tx_bytes > p[-1].tx_bytes; p--) {
3178 swap_bals(p, p - 1);
3180 for (; p < &bals[n_bals - 1] && p->tx_bytes < p[1].tx_bytes; p++) {
3181 swap_bals(p, p + 1);
3187 log_bals(const struct slave_balance *bals, size_t n_bals, struct port *port)
3189 if (VLOG_IS_DBG_ENABLED()) {
3190 struct ds ds = DS_EMPTY_INITIALIZER;
3191 const struct slave_balance *b;
3193 for (b = bals; b < bals + n_bals; b++) {
3197 ds_put_char(&ds, ',');
3199 ds_put_format(&ds, " %s %"PRIu64"kB",
3200 b->iface->name, b->tx_bytes / 1024);
3202 if (!b->iface->enabled) {
3203 ds_put_cstr(&ds, " (disabled)");
3205 if (b->n_hashes > 0) {
3206 ds_put_cstr(&ds, " (");
3207 for (i = 0; i < b->n_hashes; i++) {
3208 const struct bond_entry *e = b->hashes[i];
3210 ds_put_cstr(&ds, " + ");
3212 ds_put_format(&ds, "h%td: %"PRIu64"kB",
3213 e - port->bond_hash, e->tx_bytes / 1024);
3215 ds_put_cstr(&ds, ")");
3218 VLOG_DBG("bond %s:%s", port->name, ds_cstr(&ds));
3223 /* Shifts 'hash' from 'from' to 'to' within 'port'. */
3225 bond_shift_load(struct slave_balance *from, struct slave_balance *to,
3228 struct bond_entry *hash = from->hashes[hash_idx];
3229 struct port *port = from->iface->port;
3230 uint64_t delta = hash->tx_bytes;
3232 assert(port->bond_mode != BM_AB);
3234 VLOG_INFO("bond %s: shift %"PRIu64"kB of load (with hash %td) "
3235 "from %s to %s (now carrying %"PRIu64"kB and "
3236 "%"PRIu64"kB load, respectively)",
3237 port->name, delta / 1024, hash - port->bond_hash,
3238 from->iface->name, to->iface->name,
3239 (from->tx_bytes - delta) / 1024,
3240 (to->tx_bytes + delta) / 1024);
3242 /* Delete element from from->hashes.
3244 * We don't bother to add the element to to->hashes because not only would
3245 * it require more work, the only purpose it would be to allow that hash to
3246 * be migrated to another slave in this rebalancing run, and there is no
3247 * point in doing that. */
3248 if (hash_idx == 0) {
3251 memmove(from->hashes + hash_idx, from->hashes + hash_idx + 1,
3252 (from->n_hashes - (hash_idx + 1)) * sizeof *from->hashes);
3256 /* Shift load away from 'from' to 'to'. */
3257 from->tx_bytes -= delta;
3258 to->tx_bytes += delta;
3260 /* Arrange for flows to be revalidated. */
3261 ofproto_revalidate(port->bridge->ofproto, hash->iface_tag);
3262 hash->iface_idx = to->iface->port_ifidx;
3263 hash->iface_tag = tag_create_random();
3267 bond_rebalance_port(struct port *port)
3269 struct slave_balance *bals;
3271 struct bond_entry *hashes[BOND_MASK + 1];
3272 struct slave_balance *b, *from, *to;
3273 struct bond_entry *e;
3276 assert(port->bond_mode != BM_AB);
3278 /* Sets up 'bals' to describe each of the port's interfaces, sorted in
3279 * descending order of tx_bytes, so that bals[0] represents the most
3280 * heavily loaded slave and bals[n_bals - 1] represents the least heavily
3283 * The code is a bit tricky: to avoid dynamically allocating a 'hashes'
3284 * array for each slave_balance structure, we sort our local array of
3285 * hashes in order by slave, so that all of the hashes for a given slave
3286 * become contiguous in memory, and then we point each 'hashes' members of
3287 * a slave_balance structure to the start of a contiguous group. */
3288 n_bals = port->n_ifaces;
3289 bals = xmalloc(n_bals * sizeof *bals);
3290 for (b = bals; b < &bals[n_bals]; b++) {
3291 b->iface = port->ifaces[b - bals];
3296 for (i = 0; i <= BOND_MASK; i++) {
3297 hashes[i] = &port->bond_hash[i];
3299 qsort(hashes, BOND_MASK + 1, sizeof *hashes, compare_bond_entries);
3300 for (i = 0; i <= BOND_MASK; i++) {
3302 if (e->iface_idx >= 0 && e->iface_idx < port->n_ifaces) {
3303 b = &bals[e->iface_idx];
3304 b->tx_bytes += e->tx_bytes;
3306 b->hashes = &hashes[i];
3311 qsort(bals, n_bals, sizeof *bals, compare_slave_balance);
3312 log_bals(bals, n_bals, port);
3314 /* Discard slaves that aren't enabled (which were sorted to the back of the
3315 * array earlier). */
3316 while (!bals[n_bals - 1].iface->enabled) {
3323 /* Shift load from the most-loaded slaves to the least-loaded slaves. */
3324 to = &bals[n_bals - 1];
3325 for (from = bals; from < to; ) {
3326 uint64_t overload = from->tx_bytes - to->tx_bytes;
3327 if (overload < to->tx_bytes >> 5 || overload < 100000) {
3328 /* The extra load on 'from' (and all less-loaded slaves), compared
3329 * to that of 'to' (the least-loaded slave), is less than ~3%, or
3330 * it is less than ~1Mbps. No point in rebalancing. */
3332 } else if (from->n_hashes == 1) {
3333 /* 'from' only carries a single MAC hash, so we can't shift any
3334 * load away from it, even though we want to. */
3337 /* 'from' is carrying significantly more load than 'to', and that
3338 * load is split across at least two different hashes. Pick a hash
3339 * to migrate to 'to' (the least-loaded slave), given that doing so
3340 * must decrease the ratio of the load on the two slaves by at
3343 * The sort order we use means that we prefer to shift away the
3344 * smallest hashes instead of the biggest ones. There is little
3345 * reason behind this decision; we could use the opposite sort
3346 * order to shift away big hashes ahead of small ones. */
3349 for (i = 0; i < from->n_hashes; i++) {
3350 double old_ratio, new_ratio;
3351 uint64_t delta = from->hashes[i]->tx_bytes;
3353 if (delta == 0 || from->tx_bytes - delta == 0) {
3354 /* Pointless move. */
3358 order_swapped = from->tx_bytes - delta < to->tx_bytes + delta;
3360 if (to->tx_bytes == 0) {
3361 /* Nothing on the new slave, move it. */
3365 old_ratio = (double)from->tx_bytes / to->tx_bytes;
3366 new_ratio = (double)(from->tx_bytes - delta) /
3367 (to->tx_bytes + delta);
3369 if (new_ratio == 0) {
3370 /* Should already be covered but check to prevent division
3375 if (new_ratio < 1) {
3376 new_ratio = 1 / new_ratio;
3379 if (old_ratio - new_ratio > 0.1) {
3380 /* Would decrease the ratio, move it. */
3384 if (i < from->n_hashes) {
3385 bond_shift_load(from, to, i);
3387 /* If the result of the migration changed the relative order of
3388 * 'from' and 'to' swap them back to maintain invariants. */
3389 if (order_swapped) {
3390 swap_bals(from, to);
3393 /* Re-sort 'bals'. Note that this may make 'from' and 'to'
3394 * point to different slave_balance structures. It is only
3395 * valid to do these two operations in a row at all because we
3396 * know that 'from' will not move past 'to' and vice versa. */
3397 resort_bals(from, bals, n_bals);
3398 resort_bals(to, bals, n_bals);
3405 /* Implement exponentially weighted moving average. A weight of 1/2 causes
3406 * historical data to decay to <1% in 7 rebalancing runs. */
3407 for (e = &port->bond_hash[0]; e <= &port->bond_hash[BOND_MASK]; e++) {
3416 bond_send_learning_packets(struct port *port)
3418 struct bridge *br = port->bridge;
3419 struct mac_entry *e;
3420 struct ofpbuf packet;
3421 int error, n_packets, n_errors;
3423 if (!port->n_ifaces || port->active_iface < 0 || bond_is_tcp_hash(port)) {
3427 ofpbuf_init(&packet, 128);
3428 error = n_packets = n_errors = 0;
3429 LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
3430 union ofp_action actions[2], *a;
3436 if (e->port == port->port_idx) {
3440 compose_benign_packet(&packet, "Open vSwitch Bond Failover", 0xf177,
3442 flow_extract(&packet, 0, ODPP_NONE, &flow);
3444 if (!choose_output_iface(port, &flow, e->vlan, &dp_ifidx, &tags)) {
3448 /* Compose actions. */
3449 memset(actions, 0, sizeof actions);
3452 a->vlan_vid.type = htons(OFPAT_SET_VLAN_VID);
3453 a->vlan_vid.len = htons(sizeof *a);
3454 a->vlan_vid.vlan_vid = htons(e->vlan);
3457 a->output.type = htons(OFPAT_OUTPUT);
3458 a->output.len = htons(sizeof *a);
3459 a->output.port = htons(odp_port_to_ofp_port(dp_ifidx));
3464 retval = ofproto_send_packet(br->ofproto, &flow, actions, a - actions,
3471 ofpbuf_uninit(&packet);
3474 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
3475 VLOG_WARN_RL(&rl, "bond %s: %d errors sending %d gratuitous learning "
3476 "packets, last error was: %s",
3477 port->name, n_errors, n_packets, strerror(error));
3479 VLOG_DBG("bond %s: sent %d gratuitous learning packets",
3480 port->name, n_packets);
3484 /* Bonding unixctl user interface functions. */
3487 bond_unixctl_list(struct unixctl_conn *conn,
3488 const char *args OVS_UNUSED, void *aux OVS_UNUSED)
3490 struct ds ds = DS_EMPTY_INITIALIZER;
3491 const struct bridge *br;
3493 ds_put_cstr(&ds, "bridge\tbond\ttype\tslaves\n");
3495 LIST_FOR_EACH (br, node, &all_bridges) {
3498 for (i = 0; i < br->n_ports; i++) {
3499 const struct port *port = br->ports[i];
3500 if (port->n_ifaces > 1) {
3503 ds_put_format(&ds, "%s\t%s\t%s\t", br->name, port->name,
3504 bond_mode_to_string(port->bond_mode));
3505 for (j = 0; j < port->n_ifaces; j++) {
3506 const struct iface *iface = port->ifaces[j];
3508 ds_put_cstr(&ds, ", ");
3510 ds_put_cstr(&ds, iface->name);
3512 ds_put_char(&ds, '\n');
3516 unixctl_command_reply(conn, 200, ds_cstr(&ds));
3520 static struct port *
3521 bond_find(const char *name)
3523 const struct bridge *br;
3525 LIST_FOR_EACH (br, node, &all_bridges) {
3528 for (i = 0; i < br->n_ports; i++) {
3529 struct port *port = br->ports[i];
3530 if (!strcmp(port->name, name) && port->n_ifaces > 1) {
3539 bond_unixctl_show(struct unixctl_conn *conn,
3540 const char *args, void *aux OVS_UNUSED)
3542 struct ds ds = DS_EMPTY_INITIALIZER;
3543 const struct port *port;
3546 port = bond_find(args);
3548 unixctl_command_reply(conn, 501, "no such bond");
3552 ds_put_format(&ds, "bond_mode: %s\n",
3553 bond_mode_to_string(port->bond_mode));
3556 ds_put_format(&ds, "lacp: %s\n",
3557 port->lacp_active ? "active" : "passive");
3559 ds_put_cstr(&ds, "lacp: off\n");
3562 if (port->bond_mode != BM_AB) {
3563 ds_put_format(&ds, "bond-hash-algorithm: %s\n",
3564 bond_is_tcp_hash(port) ? "balance-tcp" : "balance-slb");
3568 ds_put_format(&ds, "bond-detect-mode: %s\n",
3569 port->monitor ? "carrier" : "miimon");
3571 if (!port->monitor) {
3572 ds_put_format(&ds, "bond-miimon-interval: %lld\n",
3573 port->miimon_interval);
3576 ds_put_format(&ds, "updelay: %d ms\n", port->updelay);
3577 ds_put_format(&ds, "downdelay: %d ms\n", port->downdelay);
3579 if (port->bond_mode != BM_AB) {
3580 ds_put_format(&ds, "next rebalance: %lld ms\n",
3581 port->bond_next_rebalance - time_msec());
3584 for (j = 0; j < port->n_ifaces; j++) {
3585 const struct iface *iface = port->ifaces[j];
3586 struct bond_entry *be;
3590 ds_put_format(&ds, "\nslave %s: %s\n",
3591 iface->name, iface->enabled ? "enabled" : "disabled");
3592 if (j == port->active_iface) {
3593 ds_put_cstr(&ds, "\tactive slave\n");
3595 if (iface->delay_expires != LLONG_MAX) {
3596 ds_put_format(&ds, "\t%s expires in %lld ms\n",
3597 iface->enabled ? "downdelay" : "updelay",
3598 iface->delay_expires - time_msec());
3601 if (port->bond_mode == BM_AB) {
3606 memset(&flow, 0, sizeof flow);
3607 for (be = port->bond_hash; be <= &port->bond_hash[BOND_MASK]; be++) {
3608 int hash = be - port->bond_hash;
3609 struct mac_entry *me;
3611 if (be->iface_idx != j) {
3615 ds_put_format(&ds, "\thash %d: %"PRIu64" kB load\n",
3616 hash, be->tx_bytes / 1024);
3618 if (port->bond_mode != BM_SLB) {
3623 LIST_FOR_EACH (me, lru_node, &port->bridge->ml->lrus) {
3627 memcpy(flow.dl_src, me->mac, ETH_ADDR_LEN);
3628 if (bond_hash_src(me->mac, me->vlan) == hash
3629 && me->port != port->port_idx
3630 && choose_output_iface(port, &flow, me->vlan,
3632 && dp_ifidx == iface->dp_ifidx)
3634 ds_put_format(&ds, "\t\t"ETH_ADDR_FMT"\n",
3635 ETH_ADDR_ARGS(me->mac));
3640 unixctl_command_reply(conn, 200, ds_cstr(&ds));
3645 bond_unixctl_migrate(struct unixctl_conn *conn, const char *args_,
3646 void *aux OVS_UNUSED)
3648 char *args = (char *) args_;
3649 char *save_ptr = NULL;
3650 char *bond_s, *hash_s, *slave_s;
3652 struct iface *iface;
3653 struct bond_entry *entry;
3656 bond_s = strtok_r(args, " ", &save_ptr);
3657 hash_s = strtok_r(NULL, " ", &save_ptr);
3658 slave_s = strtok_r(NULL, " ", &save_ptr);
3660 unixctl_command_reply(conn, 501,
3661 "usage: bond/migrate BOND HASH SLAVE");
3665 port = bond_find(bond_s);
3667 unixctl_command_reply(conn, 501, "no such bond");
3671 if (port->bond_mode != BM_SLB) {
3672 unixctl_command_reply(conn, 501, "not an SLB bond");
3676 if (strspn(hash_s, "0123456789") == strlen(hash_s)) {
3677 hash = atoi(hash_s) & BOND_MASK;
3679 unixctl_command_reply(conn, 501, "bad hash");
3683 iface = port_lookup_iface(port, slave_s);
3685 unixctl_command_reply(conn, 501, "no such slave");
3689 if (!iface->enabled) {
3690 unixctl_command_reply(conn, 501, "cannot migrate to disabled slave");
3694 entry = &port->bond_hash[hash];
3695 ofproto_revalidate(port->bridge->ofproto, entry->iface_tag);
3696 entry->iface_idx = iface->port_ifidx;
3697 entry->iface_tag = tag_create_random();
3698 unixctl_command_reply(conn, 200, "migrated");
3702 bond_unixctl_set_active_slave(struct unixctl_conn *conn, const char *args_,
3703 void *aux OVS_UNUSED)
3705 char *args = (char *) args_;
3706 char *save_ptr = NULL;
3707 char *bond_s, *slave_s;
3709 struct iface *iface;
3711 bond_s = strtok_r(args, " ", &save_ptr);
3712 slave_s = strtok_r(NULL, " ", &save_ptr);
3714 unixctl_command_reply(conn, 501,
3715 "usage: bond/set-active-slave BOND SLAVE");
3719 port = bond_find(bond_s);
3721 unixctl_command_reply(conn, 501, "no such bond");
3725 iface = port_lookup_iface(port, slave_s);
3727 unixctl_command_reply(conn, 501, "no such slave");
3731 if (!iface->enabled) {
3732 unixctl_command_reply(conn, 501, "cannot make disabled slave active");
3736 if (port->active_iface != iface->port_ifidx) {
3737 ofproto_revalidate(port->bridge->ofproto, port->active_iface_tag);
3738 port->active_iface = iface->port_ifidx;
3739 port->active_iface_tag = tag_create_random();
3740 VLOG_INFO("port %s: active interface is now %s",
3741 port->name, iface->name);
3742 bond_send_learning_packets(port);
3743 unixctl_command_reply(conn, 200, "done");
3745 unixctl_command_reply(conn, 200, "no change");
3750 enable_slave(struct unixctl_conn *conn, const char *args_, bool enable)
3752 char *args = (char *) args_;
3753 char *save_ptr = NULL;
3754 char *bond_s, *slave_s;
3756 struct iface *iface;
3758 bond_s = strtok_r(args, " ", &save_ptr);
3759 slave_s = strtok_r(NULL, " ", &save_ptr);
3761 unixctl_command_reply(conn, 501,
3762 "usage: bond/enable/disable-slave BOND SLAVE");
3766 port = bond_find(bond_s);
3768 unixctl_command_reply(conn, 501, "no such bond");
3772 iface = port_lookup_iface(port, slave_s);
3774 unixctl_command_reply(conn, 501, "no such slave");
3778 bond_enable_slave(iface, enable);
3779 unixctl_command_reply(conn, 501, enable ? "enabled" : "disabled");
3783 bond_unixctl_enable_slave(struct unixctl_conn *conn, const char *args,
3784 void *aux OVS_UNUSED)
3786 enable_slave(conn, args, true);
3790 bond_unixctl_disable_slave(struct unixctl_conn *conn, const char *args,
3791 void *aux OVS_UNUSED)
3793 enable_slave(conn, args, false);
3797 bond_unixctl_hash(struct unixctl_conn *conn, const char *args_,
3798 void *aux OVS_UNUSED)
3800 char *args = (char *) args_;
3801 uint8_t mac[ETH_ADDR_LEN];
3805 char *mac_s, *vlan_s;
3806 char *save_ptr = NULL;
3808 mac_s = strtok_r(args, " ", &save_ptr);
3809 vlan_s = strtok_r(NULL, " ", &save_ptr);
3812 if (sscanf(vlan_s, "%u", &vlan) != 1) {
3813 unixctl_command_reply(conn, 501, "invalid vlan");
3817 vlan = OFP_VLAN_NONE;
3820 if (sscanf(mac_s, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
3821 == ETH_ADDR_SCAN_COUNT) {
3822 hash = bond_hash_src(mac, vlan);
3824 hash_cstr = xasprintf("%u", hash);
3825 unixctl_command_reply(conn, 200, hash_cstr);
3828 unixctl_command_reply(conn, 501, "invalid mac");
3835 unixctl_command_register("bond/list", bond_unixctl_list, NULL);
3836 unixctl_command_register("bond/show", bond_unixctl_show, NULL);
3837 unixctl_command_register("bond/migrate", bond_unixctl_migrate, NULL);
3838 unixctl_command_register("bond/set-active-slave",
3839 bond_unixctl_set_active_slave, NULL);
3840 unixctl_command_register("bond/enable-slave", bond_unixctl_enable_slave,
3842 unixctl_command_register("bond/disable-slave", bond_unixctl_disable_slave,
3844 unixctl_command_register("bond/hash", bond_unixctl_hash, NULL);
3847 /* Port functions. */
3850 lacp_send_pdu_cb(void *aux, const struct lacp_pdu *pdu)
3852 struct iface *iface = aux;
3853 uint8_t ea[ETH_ADDR_LEN];
3856 error = netdev_get_etheraddr(iface->netdev, ea);
3858 struct ofpbuf packet;
3860 ofpbuf_init(&packet, ETH_HEADER_LEN + LACP_PDU_LEN);
3861 compose_lacp_packet(&packet, ea, pdu);
3862 iface_send_packet(iface, &packet);
3863 ofpbuf_uninit(&packet);
3865 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 10);
3866 VLOG_ERR_RL(&rl, "iface %s: failed to obtain Ethernet address "
3867 "(%s)", iface->name, strerror(error));
3872 port_run(struct port *port)
3876 if (port->monitor) {
3879 /* Track carrier going up and down on interfaces. */
3880 while (!netdev_monitor_poll(port->monitor, &devname)) {
3881 struct iface *iface;
3883 iface = port_lookup_iface(port, devname);
3885 iface_update_carrier(iface, netdev_get_carrier(iface->netdev));
3889 } else if (time_msec() >= port->miimon_next_update) {
3891 for (i = 0; i < port->n_ifaces; i++) {
3892 struct iface *iface = port->ifaces[i];
3893 iface_update_carrier(iface, netdev_get_miimon(iface->netdev));
3895 port->miimon_next_update = time_msec() + port->miimon_interval;
3899 for (i = 0; i < port->n_ifaces; i++) {
3900 struct iface *iface = port->ifaces[i];
3901 lacp_slave_enable(port->lacp, iface, iface->enabled);
3904 lacp_run(port->lacp, lacp_send_pdu_cb);
3909 for (i = 0; i < port->n_ifaces; i++) {
3910 struct iface *iface = port->ifaces[i];
3913 struct ofpbuf *packet = cfm_run(iface->cfm);
3915 iface_send_packet(iface, packet);
3916 ofpbuf_uninit(packet);
3924 port_wait(struct port *port)
3928 if (port->monitor) {
3929 netdev_monitor_poll_wait(port->monitor);
3931 poll_timer_wait_until(port->miimon_next_update);
3935 lacp_wait(port->lacp);
3940 for (i = 0; i < port->n_ifaces; i++) {
3941 struct iface *iface = port->ifaces[i];
3943 cfm_wait(iface->cfm);
3948 static struct port *
3949 port_create(struct bridge *br, const char *name)
3953 port = xzalloc(sizeof *port);
3955 port->port_idx = br->n_ports;
3957 port->trunks = NULL;
3958 port->name = xstrdup(name);
3959 port->active_iface = -1;
3961 if (br->n_ports >= br->allocated_ports) {
3962 br->ports = x2nrealloc(br->ports, &br->allocated_ports,
3965 br->ports[br->n_ports++] = port;
3966 shash_add_assert(&br->port_by_name, port->name, port);
3968 VLOG_INFO("created port %s on bridge %s", port->name, br->name);
3975 get_port_other_config(const struct ovsrec_port *port, const char *key,
3976 const char *default_value)
3980 value = get_ovsrec_key_value(&port->header_, &ovsrec_port_col_other_config,
3982 return value ? value : default_value;
3986 get_interface_other_config(const struct ovsrec_interface *iface,
3987 const char *key, const char *default_value)
3991 value = get_ovsrec_key_value(&iface->header_,
3992 &ovsrec_interface_col_other_config, key);
3993 return value ? value : default_value;
3997 port_del_ifaces(struct port *port, const struct ovsrec_port *cfg)
3999 struct shash new_ifaces;
4002 /* Collect list of new interfaces. */
4003 shash_init(&new_ifaces);
4004 for (i = 0; i < cfg->n_interfaces; i++) {
4005 const char *name = cfg->interfaces[i]->name;
4006 shash_add_once(&new_ifaces, name, NULL);
4009 /* Get rid of deleted interfaces. */
4010 for (i = 0; i < port->n_ifaces; ) {
4011 if (!shash_find(&new_ifaces, cfg->interfaces[i]->name)) {
4012 iface_destroy(port->ifaces[i]);
4018 shash_destroy(&new_ifaces);
4022 port_reconfigure(struct port *port, const struct ovsrec_port *cfg)
4024 const char *detect_mode;
4025 struct shash new_ifaces;
4026 long long int next_rebalance, miimon_next_update, lacp_priority;
4027 unsigned long *trunks;
4033 /* Update settings. */
4034 port->updelay = cfg->bond_updelay;
4035 if (port->updelay < 0) {
4038 port->downdelay = cfg->bond_downdelay;
4039 if (port->downdelay < 0) {
4040 port->downdelay = 0;
4042 port->bond_rebalance_interval = atoi(
4043 get_port_other_config(cfg, "bond-rebalance-interval", "10000"));
4044 if (port->bond_rebalance_interval < 1000) {
4045 port->bond_rebalance_interval = 1000;
4047 next_rebalance = time_msec() + port->bond_rebalance_interval;
4048 if (port->bond_next_rebalance > next_rebalance) {
4049 port->bond_next_rebalance = next_rebalance;
4052 detect_mode = get_port_other_config(cfg, "bond-detect-mode",
4055 netdev_monitor_destroy(port->monitor);
4056 port->monitor = NULL;
4058 if (strcmp(detect_mode, "miimon")) {
4059 port->monitor = netdev_monitor_create();
4061 if (strcmp(detect_mode, "carrier")) {
4062 VLOG_WARN("port %s: unsupported bond-detect-mode %s, "
4063 "defaulting to carrier", port->name, detect_mode);
4067 port->miimon_interval = atoi(
4068 get_port_other_config(cfg, "bond-miimon-interval", "200"));
4069 if (port->miimon_interval < 100) {
4070 port->miimon_interval = 100;
4072 miimon_next_update = time_msec() + port->miimon_interval;
4073 if (port->miimon_next_update > miimon_next_update) {
4074 port->miimon_next_update = miimon_next_update;
4077 if (!port->cfg->bond_mode ||
4078 !strcmp(port->cfg->bond_mode, bond_mode_to_string(BM_SLB))) {
4079 port->bond_mode = BM_SLB;
4080 } else if (!strcmp(port->cfg->bond_mode, bond_mode_to_string(BM_AB))) {
4081 port->bond_mode = BM_AB;
4082 } else if (!strcmp(port->cfg->bond_mode, bond_mode_to_string(BM_TCP))) {
4083 port->bond_mode = BM_TCP;
4085 port->bond_mode = BM_SLB;
4086 VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
4087 port->name, port->cfg->bond_mode,
4088 bond_mode_to_string(port->bond_mode));
4091 /* Add new interfaces and update 'cfg' member of existing ones. */
4092 shash_init(&new_ifaces);
4093 for (i = 0; i < cfg->n_interfaces; i++) {
4094 const struct ovsrec_interface *if_cfg = cfg->interfaces[i];
4095 struct iface *iface;
4097 if (!shash_add_once(&new_ifaces, if_cfg->name, NULL)) {
4098 VLOG_WARN("port %s: %s specified twice as port interface",
4099 port->name, if_cfg->name);
4100 iface_set_ofport(if_cfg, -1);
4104 iface = iface_lookup(port->bridge, if_cfg->name);
4106 if (iface->port != port) {
4107 VLOG_ERR("bridge %s: %s interface is on multiple ports, "
4109 port->bridge->name, if_cfg->name, iface->port->name);
4112 iface->cfg = if_cfg;
4114 iface = iface_create(port, if_cfg);
4117 /* Determine interface type. The local port always has type
4118 * "internal". Other ports take their type from the database and
4119 * default to "system" if none is specified. */
4120 iface->type = (!strcmp(if_cfg->name, port->bridge->name) ? "internal"
4121 : if_cfg->type[0] ? if_cfg->type
4125 atoi(get_interface_other_config(if_cfg, "lacp-port-priority",
4128 if (lacp_priority <= 0 || lacp_priority > UINT16_MAX) {
4129 iface->lacp_priority = UINT16_MAX;
4131 iface->lacp_priority = lacp_priority;
4134 shash_destroy(&new_ifaces);
4136 port->lacp_fast = !strcmp(get_port_other_config(cfg, "lacp-time", "slow"),
4140 atoi(get_port_other_config(cfg, "lacp-system-priority", "0"));
4142 if (lacp_priority <= 0 || lacp_priority > UINT16_MAX) {
4143 /* Prefer bondable links if unspecified. */
4144 port->lacp_priority = port->n_ifaces > 1 ? UINT16_MAX - 1 : UINT16_MAX;
4146 port->lacp_priority = lacp_priority;
4149 if (!port->cfg->lacp) {
4150 /* XXX when LACP implementation has been sufficiently tested, enable by
4151 * default and make active on bonded ports. */
4152 lacp_destroy(port->lacp);
4154 } else if (!strcmp(port->cfg->lacp, "off")) {
4155 lacp_destroy(port->lacp);
4157 } else if (!strcmp(port->cfg->lacp, "active")) {
4159 port->lacp = lacp_create();
4161 port->lacp_active = true;
4162 } else if (!strcmp(port->cfg->lacp, "passive")) {
4164 port->lacp = lacp_create();
4166 port->lacp_active = false;
4168 VLOG_WARN("port %s: unknown LACP mode %s",
4169 port->name, port->cfg->lacp);
4170 lacp_destroy(port->lacp);
4177 if (port->n_ifaces < 2) {
4179 if (vlan >= 0 && vlan <= 4095) {
4180 VLOG_DBG("port %s: assigning VLAN tag %d", port->name, vlan);
4185 /* It's possible that bonded, VLAN-tagged ports make sense. Maybe
4186 * they even work as-is. But they have not been tested. */
4187 VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
4191 if (port->vlan != vlan) {
4193 bridge_flush(port->bridge);
4196 /* Get trunked VLANs. */
4198 if (vlan < 0 && cfg->n_trunks) {
4201 trunks = bitmap_allocate(4096);
4203 for (i = 0; i < cfg->n_trunks; i++) {
4204 int trunk = cfg->trunks[i];
4206 bitmap_set1(trunks, trunk);
4212 VLOG_ERR("port %s: invalid values for %zu trunk VLANs",
4213 port->name, cfg->n_trunks);
4215 if (n_errors == cfg->n_trunks) {
4216 VLOG_ERR("port %s: no valid trunks, trunking all VLANs",
4218 bitmap_free(trunks);
4221 } else if (vlan >= 0 && cfg->n_trunks) {
4222 VLOG_ERR("port %s: ignoring trunks in favor of implicit vlan",
4226 ? port->trunks != NULL
4227 : port->trunks == NULL || !bitmap_equal(trunks, port->trunks, 4096)) {
4228 bridge_flush(port->bridge);
4230 bitmap_free(port->trunks);
4231 port->trunks = trunks;
4235 port_destroy(struct port *port)
4238 struct bridge *br = port->bridge;
4242 for (i = 0; i < MAX_MIRRORS; i++) {
4243 struct mirror *m = br->mirrors[i];
4244 if (m && m->out_port == port) {
4249 while (port->n_ifaces > 0) {
4250 iface_destroy(port->ifaces[port->n_ifaces - 1]);
4253 shash_find_and_delete_assert(&br->port_by_name, port->name);
4255 del = br->ports[port->port_idx] = br->ports[--br->n_ports];
4256 del->port_idx = port->port_idx;
4258 VLOG_INFO("destroyed port %s on bridge %s", port->name, br->name);
4260 netdev_monitor_destroy(port->monitor);
4262 bitmap_free(port->trunks);
4269 static struct port *
4270 port_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
4272 struct iface *iface = iface_from_dp_ifidx(br, dp_ifidx);
4273 return iface ? iface->port : NULL;
4276 static struct port *
4277 port_lookup(const struct bridge *br, const char *name)
4279 return shash_find_data(&br->port_by_name, name);
4282 static struct iface *
4283 port_lookup_iface(const struct port *port, const char *name)
4285 struct iface *iface = iface_lookup(port->bridge, name);
4286 return iface && iface->port == port ? iface : NULL;
4290 port_update_lacp(struct port *port)
4295 lacp_configure(port->lacp, port->name,
4296 port->bridge->ea, port->lacp_priority,
4297 port->lacp_active, port->lacp_fast);
4299 for (i = 0; i < port->n_ifaces; i++) {
4300 struct iface *iface = port->ifaces[i];
4301 lacp_slave_register(port->lacp, iface, iface->name,
4302 iface->dp_ifidx, iface->lacp_priority);
4308 port_update_bonding(struct port *port)
4310 if (port->n_ifaces < 2) {
4311 /* Not a bonded port. */
4312 free(port->bond_hash);
4313 port->bond_hash = NULL;
4314 port->bond_fake_iface = false;
4315 port->active_iface = -1;
4316 port->no_ifaces_tag = 0;
4320 if (port->bond_mode != BM_AB && !port->bond_hash) {
4321 port->bond_hash = xcalloc(BOND_MASK + 1, sizeof *port->bond_hash);
4322 for (i = 0; i <= BOND_MASK; i++) {
4323 struct bond_entry *e = &port->bond_hash[i];
4327 port->bond_next_rebalance
4328 = time_msec() + port->bond_rebalance_interval;
4329 } else if (port->bond_mode == BM_AB) {
4330 free(port->bond_hash);
4331 port->bond_hash = NULL;
4334 if (!port->no_ifaces_tag) {
4335 port->no_ifaces_tag = tag_create_random();
4338 if (port->active_iface < 0) {
4339 bond_choose_active_iface(port);
4342 port->bond_fake_iface = port->cfg->bond_fake_iface;
4343 if (port->bond_fake_iface) {
4344 port->bond_next_fake_iface_update = time_msec();
4350 /* Interface functions. */
4353 iface_send_packet(struct iface *iface, struct ofpbuf *packet)
4356 union ofp_action action;
4358 memset(&action, 0, sizeof action);
4359 action.output.type = htons(OFPAT_OUTPUT);
4360 action.output.len = htons(sizeof action);
4361 action.output.port = htons(odp_port_to_ofp_port(iface->dp_ifidx));
4363 flow_extract(packet, 0, ODPP_NONE, &flow);
4365 if (ofproto_send_packet(iface->port->bridge->ofproto, &flow, &action, 1,
4367 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
4368 VLOG_WARN_RL(&rl, "interface %s: Failed to send packet.", iface->name);
4372 static struct iface *
4373 iface_create(struct port *port, const struct ovsrec_interface *if_cfg)
4375 struct bridge *br = port->bridge;
4376 struct iface *iface;
4377 char *name = if_cfg->name;
4379 iface = xzalloc(sizeof *iface);
4381 iface->port_ifidx = port->n_ifaces;
4382 iface->name = xstrdup(name);
4383 iface->dp_ifidx = -1;
4384 iface->tag = tag_create_random();
4385 iface->delay_expires = LLONG_MAX;
4386 iface->netdev = NULL;
4387 iface->cfg = if_cfg;
4389 shash_add_assert(&br->iface_by_name, iface->name, iface);
4391 if (port->n_ifaces >= port->allocated_ifaces) {
4392 port->ifaces = x2nrealloc(port->ifaces, &port->allocated_ifaces,
4393 sizeof *port->ifaces);
4395 port->ifaces[port->n_ifaces++] = iface;
4396 if (port->n_ifaces > 1) {
4397 br->has_bonded_ports = true;
4400 VLOG_DBG("attached network device %s to port %s", iface->name, port->name);
4408 iface_destroy(struct iface *iface)
4411 struct port *port = iface->port;
4412 struct bridge *br = port->bridge;
4413 bool del_active = port->active_iface == iface->port_ifidx;
4416 if (iface->port->lacp) {
4417 lacp_slave_unregister(iface->port->lacp, iface);
4420 if (port->monitor && iface->netdev) {
4421 netdev_monitor_remove(port->monitor, iface->netdev);
4424 shash_find_and_delete_assert(&br->iface_by_name, iface->name);
4426 if (iface->dp_ifidx >= 0) {
4427 hmap_remove(&br->ifaces, &iface->dp_ifidx_node);
4430 del = port->ifaces[iface->port_ifidx] = port->ifaces[--port->n_ifaces];
4431 del->port_ifidx = iface->port_ifidx;
4433 netdev_close(iface->netdev);
4436 ofproto_revalidate(port->bridge->ofproto, port->active_iface_tag);
4437 bond_choose_active_iface(port);
4438 bond_send_learning_packets(port);
4441 cfm_destroy(iface->cfm);
4446 bridge_flush(port->bridge);
4450 static struct iface *
4451 iface_lookup(const struct bridge *br, const char *name)
4453 return shash_find_data(&br->iface_by_name, name);
4456 static struct iface *
4457 iface_find(const char *name)
4459 const struct bridge *br;
4461 LIST_FOR_EACH (br, node, &all_bridges) {
4462 struct iface *iface = iface_lookup(br, name);
4471 static struct iface *
4472 iface_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
4474 struct iface *iface;
4476 HMAP_FOR_EACH_IN_BUCKET (iface, dp_ifidx_node,
4477 hash_int(dp_ifidx, 0), &br->ifaces) {
4478 if (iface->dp_ifidx == dp_ifidx) {
4485 /* Set Ethernet address of 'iface', if one is specified in the configuration
4488 iface_set_mac(struct iface *iface)
4490 uint8_t ea[ETH_ADDR_LEN];
4492 if (iface->cfg->mac && eth_addr_from_string(iface->cfg->mac, ea)) {
4493 if (eth_addr_is_multicast(ea)) {
4494 VLOG_ERR("interface %s: cannot set MAC to multicast address",
4496 } else if (iface->dp_ifidx == ODPP_LOCAL) {
4497 VLOG_ERR("ignoring iface.%s.mac; use bridge.%s.mac instead",
4498 iface->name, iface->name);
4500 int error = netdev_set_etheraddr(iface->netdev, ea);
4502 VLOG_ERR("interface %s: setting MAC failed (%s)",
4503 iface->name, strerror(error));
4509 /* Sets the ofport column of 'if_cfg' to 'ofport'. */
4511 iface_set_ofport(const struct ovsrec_interface *if_cfg, int64_t ofport)
4514 ovsrec_interface_set_ofport(if_cfg, &ofport, 1);
4518 /* Adds the 'n' key-value pairs in 'keys' in 'values' to 'shash'.
4520 * The value strings in '*shash' are taken directly from values[], not copied,
4521 * so the caller should not modify or free them. */
4523 shash_from_ovs_idl_map(char **keys, char **values, size_t n,
4524 struct shash *shash)
4529 for (i = 0; i < n; i++) {
4530 shash_add(shash, keys[i], values[i]);
4534 /* Creates 'keys' and 'values' arrays from 'shash'.
4536 * Sets 'keys' and 'values' to heap allocated arrays representing the key-value
4537 * pairs in 'shash'. The caller takes ownership of 'keys' and 'values'. They
4538 * are populated with with strings taken directly from 'shash' and thus have
4539 * the same ownership of the key-value pairs in shash.
4542 shash_to_ovs_idl_map(struct shash *shash,
4543 char ***keys, char ***values, size_t *n)
4547 struct shash_node *sn;
4549 count = shash_count(shash);
4551 k = xmalloc(count * sizeof *k);
4552 v = xmalloc(count * sizeof *v);
4555 SHASH_FOR_EACH(sn, shash) {
4566 struct iface_delete_queues_cbdata {
4567 struct netdev *netdev;
4568 const struct ovsdb_datum *queues;
4572 queue_ids_include(const struct ovsdb_datum *queues, int64_t target)
4574 union ovsdb_atom atom;
4576 atom.integer = target;
4577 return ovsdb_datum_find_key(queues, &atom, OVSDB_TYPE_INTEGER) != UINT_MAX;
4581 iface_delete_queues(unsigned int queue_id,
4582 const struct shash *details OVS_UNUSED, void *cbdata_)
4584 struct iface_delete_queues_cbdata *cbdata = cbdata_;
4586 if (!queue_ids_include(cbdata->queues, queue_id)) {
4587 netdev_delete_queue(cbdata->netdev, queue_id);
4592 iface_update_carrier(struct iface *iface, bool carrier)
4594 if (carrier == iface->up) {
4598 iface->up = carrier;
4599 if (iface->port->lacp) {
4600 lacp_slave_carrier_changed(iface->port->lacp, iface);
4605 iface_update_qos(struct iface *iface, const struct ovsrec_qos *qos)
4607 if (!qos || qos->type[0] == '\0') {
4608 netdev_set_qos(iface->netdev, NULL, NULL);
4610 struct iface_delete_queues_cbdata cbdata;
4611 struct shash details;
4614 /* Configure top-level Qos for 'iface'. */
4615 shash_from_ovs_idl_map(qos->key_other_config, qos->value_other_config,
4616 qos->n_other_config, &details);
4617 netdev_set_qos(iface->netdev, qos->type, &details);
4618 shash_destroy(&details);
4620 /* Deconfigure queues that were deleted. */
4621 cbdata.netdev = iface->netdev;
4622 cbdata.queues = ovsrec_qos_get_queues(qos, OVSDB_TYPE_INTEGER,
4624 netdev_dump_queues(iface->netdev, iface_delete_queues, &cbdata);
4626 /* Configure queues for 'iface'. */
4627 for (i = 0; i < qos->n_queues; i++) {
4628 const struct ovsrec_queue *queue = qos->value_queues[i];
4629 unsigned int queue_id = qos->key_queues[i];
4631 shash_from_ovs_idl_map(queue->key_other_config,
4632 queue->value_other_config,
4633 queue->n_other_config, &details);
4634 netdev_set_queue(iface->netdev, queue_id, &details);
4635 shash_destroy(&details);
4641 iface_update_cfm(struct iface *iface)
4645 uint16_t *remote_mps;
4646 struct ovsrec_monitor *mon;
4647 uint8_t ea[ETH_ADDR_LEN], maid[CCM_MAID_LEN];
4649 mon = iface->cfg->monitor;
4652 cfm_destroy(iface->cfm);
4657 if (netdev_get_etheraddr(iface->netdev, ea)) {
4658 VLOG_WARN("interface %s: Failed to get ethernet address. "
4659 "Skipping Monitor.", iface->name);
4663 if (!cfm_generate_maid(mon->md_name, mon->ma_name, maid)) {
4664 VLOG_WARN("interface %s: Failed to generate MAID.", iface->name);
4669 iface->cfm = cfm_create();
4673 cfm->mpid = mon->mpid;
4674 cfm->interval = mon->interval ? *mon->interval : 1000;
4676 memcpy(cfm->eth_src, ea, sizeof cfm->eth_src);
4677 memcpy(cfm->maid, maid, sizeof cfm->maid);
4679 remote_mps = xzalloc(mon->n_remote_mps * sizeof *remote_mps);
4680 for(i = 0; i < mon->n_remote_mps; i++) {
4681 remote_mps[i] = mon->remote_mps[i]->mpid;
4683 cfm_update_remote_mps(cfm, remote_mps, mon->n_remote_mps);
4686 if (!cfm_configure(iface->cfm)) {
4687 cfm_destroy(iface->cfm);
4692 /* Port mirroring. */
4694 static struct mirror *
4695 mirror_find_by_uuid(struct bridge *br, const struct uuid *uuid)
4699 for (i = 0; i < MAX_MIRRORS; i++) {
4700 struct mirror *m = br->mirrors[i];
4701 if (m && uuid_equals(uuid, &m->uuid)) {
4709 mirror_reconfigure(struct bridge *br)
4711 unsigned long *rspan_vlans;
4714 /* Get rid of deleted mirrors. */
4715 for (i = 0; i < MAX_MIRRORS; i++) {
4716 struct mirror *m = br->mirrors[i];
4718 const struct ovsdb_datum *mc;
4719 union ovsdb_atom atom;
4721 mc = ovsrec_bridge_get_mirrors(br->cfg, OVSDB_TYPE_UUID);
4722 atom.uuid = br->mirrors[i]->uuid;
4723 if (ovsdb_datum_find_key(mc, &atom, OVSDB_TYPE_UUID) == UINT_MAX) {
4729 /* Add new mirrors and reconfigure existing ones. */
4730 for (i = 0; i < br->cfg->n_mirrors; i++) {
4731 struct ovsrec_mirror *cfg = br->cfg->mirrors[i];
4732 struct mirror *m = mirror_find_by_uuid(br, &cfg->header_.uuid);
4734 mirror_reconfigure_one(m, cfg);
4736 mirror_create(br, cfg);
4740 /* Update port reserved status. */
4741 for (i = 0; i < br->n_ports; i++) {
4742 br->ports[i]->is_mirror_output_port = false;
4744 for (i = 0; i < MAX_MIRRORS; i++) {
4745 struct mirror *m = br->mirrors[i];
4746 if (m && m->out_port) {
4747 m->out_port->is_mirror_output_port = true;
4751 /* Update flooded vlans (for RSPAN). */
4753 if (br->cfg->n_flood_vlans) {
4754 rspan_vlans = bitmap_allocate(4096);
4756 for (i = 0; i < br->cfg->n_flood_vlans; i++) {
4757 int64_t vlan = br->cfg->flood_vlans[i];
4758 if (vlan >= 0 && vlan < 4096) {
4759 bitmap_set1(rspan_vlans, vlan);
4760 VLOG_INFO("bridge %s: disabling learning on vlan %"PRId64,
4763 VLOG_ERR("bridge %s: invalid value %"PRId64 "for flood VLAN",
4768 if (mac_learning_set_flood_vlans(br->ml, rspan_vlans)) {
4774 mirror_create(struct bridge *br, struct ovsrec_mirror *cfg)
4779 for (i = 0; ; i++) {
4780 if (i >= MAX_MIRRORS) {
4781 VLOG_WARN("bridge %s: maximum of %d port mirrors reached, "
4782 "cannot create %s", br->name, MAX_MIRRORS, cfg->name);
4785 if (!br->mirrors[i]) {
4790 VLOG_INFO("created port mirror %s on bridge %s", cfg->name, br->name);
4793 br->mirrors[i] = m = xzalloc(sizeof *m);
4796 m->name = xstrdup(cfg->name);
4797 shash_init(&m->src_ports);
4798 shash_init(&m->dst_ports);
4804 mirror_reconfigure_one(m, cfg);
4808 mirror_destroy(struct mirror *m)
4811 struct bridge *br = m->bridge;
4814 for (i = 0; i < br->n_ports; i++) {
4815 br->ports[i]->src_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
4816 br->ports[i]->dst_mirrors &= ~(MIRROR_MASK_C(1) << m->idx);
4819 shash_destroy(&m->src_ports);
4820 shash_destroy(&m->dst_ports);
4823 m->bridge->mirrors[m->idx] = NULL;
4832 mirror_collect_ports(struct mirror *m, struct ovsrec_port **ports, int n_ports,
4833 struct shash *names)
4837 for (i = 0; i < n_ports; i++) {
4838 const char *name = ports[i]->name;
4839 if (port_lookup(m->bridge, name)) {
4840 shash_add_once(names, name, NULL);
4842 VLOG_WARN("bridge %s: mirror %s cannot match on nonexistent "
4843 "port %s", m->bridge->name, m->name, name);
4849 mirror_collect_vlans(struct mirror *m, const struct ovsrec_mirror *cfg,
4855 *vlans = xmalloc(sizeof **vlans * cfg->n_select_vlan);
4857 for (i = 0; i < cfg->n_select_vlan; i++) {
4858 int64_t vlan = cfg->select_vlan[i];
4859 if (vlan < 0 || vlan > 4095) {
4860 VLOG_WARN("bridge %s: mirror %s selects invalid VLAN %"PRId64,
4861 m->bridge->name, m->name, vlan);
4863 (*vlans)[n_vlans++] = vlan;
4870 vlan_is_mirrored(const struct mirror *m, int vlan)
4874 for (i = 0; i < m->n_vlans; i++) {
4875 if (m->vlans[i] == vlan) {
4883 port_trunks_any_mirrored_vlan(const struct mirror *m, const struct port *p)
4887 for (i = 0; i < m->n_vlans; i++) {
4888 if (port_trunks_vlan(p, m->vlans[i])) {
4896 mirror_reconfigure_one(struct mirror *m, struct ovsrec_mirror *cfg)
4898 struct shash src_ports, dst_ports;
4899 mirror_mask_t mirror_bit;
4900 struct port *out_port;
4907 if (strcmp(cfg->name, m->name)) {
4909 m->name = xstrdup(cfg->name);
4912 /* Get output port. */
4913 if (cfg->output_port) {
4914 out_port = port_lookup(m->bridge, cfg->output_port->name);
4916 VLOG_ERR("bridge %s: mirror %s outputs to port not on bridge",
4917 m->bridge->name, m->name);
4923 if (cfg->output_vlan) {
4924 VLOG_ERR("bridge %s: mirror %s specifies both output port and "
4925 "output vlan; ignoring output vlan",
4926 m->bridge->name, m->name);
4928 } else if (cfg->output_vlan) {
4930 out_vlan = *cfg->output_vlan;
4932 VLOG_ERR("bridge %s: mirror %s does not specify output; ignoring",
4933 m->bridge->name, m->name);
4938 shash_init(&src_ports);
4939 shash_init(&dst_ports);
4940 if (cfg->select_all) {
4941 for (i = 0; i < m->bridge->n_ports; i++) {
4942 const char *name = m->bridge->ports[i]->name;
4943 shash_add_once(&src_ports, name, NULL);
4944 shash_add_once(&dst_ports, name, NULL);
4949 /* Get ports, and drop duplicates and ports that don't exist. */
4950 mirror_collect_ports(m, cfg->select_src_port, cfg->n_select_src_port,
4952 mirror_collect_ports(m, cfg->select_dst_port, cfg->n_select_dst_port,
4955 /* Get all the vlans, and drop duplicate and invalid vlans. */
4956 n_vlans = mirror_collect_vlans(m, cfg, &vlans);
4959 /* Update mirror data. */
4960 if (!shash_equal_keys(&m->src_ports, &src_ports)
4961 || !shash_equal_keys(&m->dst_ports, &dst_ports)
4962 || m->n_vlans != n_vlans
4963 || memcmp(m->vlans, vlans, sizeof *vlans * n_vlans)
4964 || m->out_port != out_port
4965 || m->out_vlan != out_vlan) {
4966 bridge_flush(m->bridge);
4968 shash_swap(&m->src_ports, &src_ports);
4969 shash_swap(&m->dst_ports, &dst_ports);
4972 m->n_vlans = n_vlans;
4973 m->out_port = out_port;
4974 m->out_vlan = out_vlan;
4977 mirror_bit = MIRROR_MASK_C(1) << m->idx;
4978 for (i = 0; i < m->bridge->n_ports; i++) {
4979 struct port *port = m->bridge->ports[i];
4981 if (shash_find(&m->src_ports, port->name)
4984 ? port_trunks_any_mirrored_vlan(m, port)
4985 : vlan_is_mirrored(m, port->vlan)))) {
4986 port->src_mirrors |= mirror_bit;
4988 port->src_mirrors &= ~mirror_bit;
4991 if (shash_find(&m->dst_ports, port->name)) {
4992 port->dst_mirrors |= mirror_bit;
4994 port->dst_mirrors &= ~mirror_bit;
4999 shash_destroy(&src_ports);
5000 shash_destroy(&dst_ports);