+static void
+refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
+{
+ struct ovsdb_datum datum;
+ struct shash stats;
+
+ shash_init(&stats);
+ get_system_stats(&stats);
+
+ ovsdb_datum_from_shash(&datum, &stats);
+ ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
+ &datum);
+}
+
+static inline const char *
+nx_role_to_str(enum nx_role role)
+{
+ switch (role) {
+ case NX_ROLE_OTHER:
+ return "other";
+ case NX_ROLE_MASTER:
+ return "master";
+ case NX_ROLE_SLAVE:
+ return "slave";
+ default:
+ return "*** INVALID ROLE ***";
+ }
+}
+
+static void
+bridge_refresh_controller_status(const struct bridge *br)
+{
+ struct shash info;
+ const struct ovsrec_controller *cfg;
+
+ ofproto_get_ofproto_controller_info(br->ofproto, &info);
+
+ OVSREC_CONTROLLER_FOR_EACH(cfg, idl) {
+ struct ofproto_controller_info *cinfo =
+ shash_find_data(&info, cfg->target);
+
+ if (cinfo) {
+ ovsrec_controller_set_is_connected(cfg, cinfo->is_connected);
+ ovsrec_controller_set_role(cfg, nx_role_to_str(cinfo->role));
+ ovsrec_controller_set_status(cfg, (char **) cinfo->pairs.keys,
+ (char **) cinfo->pairs.values,
+ cinfo->pairs.n);
+ } else {
+ ovsrec_controller_set_is_connected(cfg, false);
+ ovsrec_controller_set_role(cfg, NULL);
+ ovsrec_controller_set_status(cfg, NULL, NULL, 0);
+ }
+ }
+
+ ofproto_free_ofproto_controller_info(&info);
+}
+
+void
+bridge_run(void)
+{
+ const struct ovsrec_open_vswitch *cfg;
+
+ bool datapath_destroyed;
+ bool database_changed;
+ struct bridge *br;
+
+ /* Let each bridge do the work that it needs to do. */
+ datapath_destroyed = false;
+ LIST_FOR_EACH (br, node, &all_bridges) {
+ int error = bridge_run_one(br);
+ if (error) {
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
+ VLOG_ERR_RL(&rl, "bridge %s: datapath was destroyed externally, "
+ "forcing reconfiguration", br->name);
+ datapath_destroyed = true;
+ }
+ }
+
+ /* (Re)configure if necessary. */
+ database_changed = ovsdb_idl_run(idl);
+ cfg = ovsrec_open_vswitch_first(idl);
+#ifdef HAVE_OPENSSL
+ /* Re-configure SSL. We do this on every trip through the main loop,
+ * instead of just when the database changes, because the contents of the
+ * key and certificate files can change without the database changing.
+ *
+ * We do this before bridge_reconfigure() because that function might
+ * initiate SSL connections and thus requires SSL to be configured. */
+ if (cfg && cfg->ssl) {
+ const struct ovsrec_ssl *ssl = cfg->ssl;
+
+ stream_ssl_set_key_and_cert(ssl->private_key, ssl->certificate);
+ stream_ssl_set_ca_cert_file(ssl->ca_cert, ssl->bootstrap_ca_cert);
+ }
+#endif
+ if (database_changed || datapath_destroyed) {
+ if (cfg) {
+ struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
+
+ bridge_configure_once(cfg);
+ bridge_reconfigure(cfg);
+
+ ovsrec_open_vswitch_set_cur_cfg(cfg, cfg->next_cfg);
+ ovsdb_idl_txn_commit(txn);
+ ovsdb_idl_txn_destroy(txn); /* XXX */
+ } else {
+ /* We still need to reconfigure to avoid dangling pointers to
+ * now-destroyed ovsrec structures inside bridge data. */
+ static const struct ovsrec_open_vswitch null_cfg;
+
+ bridge_reconfigure(&null_cfg);
+ }
+ }
+
+ /* Refresh system and interface stats if necessary. */
+ if (time_msec() >= stats_timer) {
+ if (cfg) {
+ struct ovsdb_idl_txn *txn;
+
+ txn = ovsdb_idl_txn_create(idl);
+ LIST_FOR_EACH (br, node, &all_bridges) {
+ size_t i;
+
+ for (i = 0; i < br->n_ports; i++) {
+ struct port *port = br->ports[i];
+ size_t j;
+
+ for (j = 0; j < port->n_ifaces; j++) {
+ struct iface *iface = port->ifaces[j];
+ iface_refresh_stats(iface);
+ iface_refresh_cfm_stats(iface);
+ iface_refresh_status(iface);
+ }
+ }
+ bridge_refresh_controller_status(br);
+ }
+ refresh_system_stats(cfg);
+ ovsdb_idl_txn_commit(txn);
+ ovsdb_idl_txn_destroy(txn); /* XXX */
+ }
+
+ stats_timer = time_msec() + STATS_INTERVAL;
+ }
+}
+
+void
+bridge_wait(void)
+{
+ struct bridge *br;
+ struct iface *iface;
+
+ LIST_FOR_EACH (br, node, &all_bridges) {
+ ofproto_wait(br->ofproto);
+ if (ofproto_has_primary_controller(br->ofproto)) {
+ continue;
+ }
+
+ mac_learning_wait(br->ml);
+ lacp_wait(br);
+ bond_wait(br);
+
+ HMAP_FOR_EACH (iface, dp_ifidx_node, &br->ifaces) {
+ if (iface->cfm) {
+ cfm_wait(iface->cfm);
+ }
+ }
+ }
+ ovsdb_idl_wait(idl);
+ poll_timer_wait_until(stats_timer);
+}
+
+/* Forces 'br' to revalidate all of its flows. This is appropriate when 'br''s
+ * configuration changes. */
+static void
+bridge_flush(struct bridge *br)
+{
+ COVERAGE_INC(bridge_flush);
+ br->flush = true;
+ mac_learning_flush(br->ml);
+}
+
+/* Returns the 'br' interface for the ODPP_LOCAL port, or null if 'br' has no
+ * such interface. */
+static struct iface *
+bridge_get_local_iface(struct bridge *br)
+{
+ size_t i, j;
+
+ for (i = 0; i < br->n_ports; i++) {
+ struct port *port = br->ports[i];
+ for (j = 0; j < port->n_ifaces; j++) {
+ struct iface *iface = port->ifaces[j];
+ if (iface->dp_ifidx == ODPP_LOCAL) {
+ return iface;
+ }
+ }
+ }
+
+ return NULL;
+}
+\f
+/* Bridge unixctl user interface functions. */
+static void
+bridge_unixctl_fdb_show(struct unixctl_conn *conn,
+ const char *args, void *aux OVS_UNUSED)
+{
+ struct ds ds = DS_EMPTY_INITIALIZER;
+ const struct bridge *br;
+ const struct mac_entry *e;
+
+ br = bridge_lookup(args);
+ if (!br) {