+/* Update interface and mirror statistics if necessary. */
+static void
+run_stats_update(void)
+{
+ static struct ovsdb_idl_txn *stats_txn;
+ const struct ovsrec_open_vswitch *cfg = ovsrec_open_vswitch_first(idl);
+ int stats_interval;
+
+ if (!cfg) {
+ return;
+ }
+
+ /* Statistics update interval should always be greater than or equal to
+ * 5000 ms. */
+ stats_interval = MAX(smap_get_int(&cfg->other_config,
+ "stats-update-interval",
+ 5000), 5000);
+ if (stats_timer_interval != stats_interval) {
+ stats_timer_interval = stats_interval;
+ stats_timer = LLONG_MIN;
+ }
+
+ if (time_msec() >= stats_timer) {
+ enum ovsdb_idl_txn_status status;
+
+ /* Rate limit the update. Do not start a new update if the
+ * previous one is not done. */
+ if (!stats_txn) {
+ struct bridge *br;
+
+ stats_txn = ovsdb_idl_txn_create(idl);
+ HMAP_FOR_EACH (br, node, &all_bridges) {
+ struct port *port;
+ struct mirror *m;
+
+ HMAP_FOR_EACH (port, hmap_node, &br->ports) {
+ struct iface *iface;
+
+ LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
+ iface_refresh_stats(iface);
+ }
+ port_refresh_stp_stats(port);
+ }
+ HMAP_FOR_EACH (m, hmap_node, &br->mirrors) {
+ mirror_refresh_stats(m);
+ }
+ }
+ refresh_controller_status();
+ }
+
+ status = ovsdb_idl_txn_commit(stats_txn);
+ if (status != TXN_INCOMPLETE) {
+ stats_timer = time_msec() + stats_timer_interval;
+ ovsdb_idl_txn_destroy(stats_txn);
+ stats_txn = NULL;
+ }
+ }
+}
+
+/* Update bridge/port/interface status if necessary. */
+static void
+run_status_update(void)
+{
+ if (!status_txn) {
+ uint64_t seq;
+
+ /* Rate limit the update. Do not start a new update if the
+ * previous one is not done. */
+ seq = seq_read(connectivity_seq_get());
+ if (seq != connectivity_seqno || status_txn_try_again) {
+ struct bridge *br;
+
+ connectivity_seqno = seq;
+ status_txn = ovsdb_idl_txn_create(idl);
+ HMAP_FOR_EACH (br, node, &all_bridges) {
+ struct port *port;
+
+ br_refresh_stp_status(br);
+ br_refresh_rstp_status(br);
+ HMAP_FOR_EACH (port, hmap_node, &br->ports) {
+ struct iface *iface;
+
+ port_refresh_stp_status(port);
+ port_refresh_rstp_status(port);
+ port_refresh_bond_status(port, status_txn_try_again);
+ LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
+ iface_refresh_netdev_status(iface);
+ iface_refresh_ofproto_status(iface);
+ }
+ }
+ }
+ }
+ }
+
+ /* Commit the transaction and get the status. If the transaction finishes,
+ * then destroy the transaction. Otherwise, keep it so that we can check
+ * progress the next time that this function is called. */
+ if (status_txn) {
+ enum ovsdb_idl_txn_status status;
+
+ status = ovsdb_idl_txn_commit(status_txn);
+ if (status != TXN_INCOMPLETE) {
+ ovsdb_idl_txn_destroy(status_txn);
+ status_txn = NULL;
+
+ /* Sets the 'status_txn_try_again' if the transaction fails. */
+ if (status == TXN_SUCCESS || status == TXN_UNCHANGED) {
+ status_txn_try_again = false;
+ } else {
+ status_txn_try_again = true;
+ }
+ }
+ }
+}
+
+static void
+status_update_wait(void)
+{
+ /* This prevents the process from constantly waking up on
+ * connectivity seq, when there is no connection to ovsdb. */
+ if (!ovsdb_idl_has_lock(idl)) {
+ return;
+ }
+
+ /* If the 'status_txn' is non-null (transaction incomplete), waits for the
+ * transaction to complete. If the status update to database needs to be
+ * run again (transaction fails), registers a timeout in
+ * 'STATUS_CHECK_AGAIN_MSEC'. Otherwise, waits on the global connectivity
+ * sequence number. */
+ if (status_txn) {
+ ovsdb_idl_txn_wait(status_txn);
+ } else if (status_txn_try_again) {
+ poll_timer_wait_until(time_msec() + STATUS_CHECK_AGAIN_MSEC);
+ } else {
+ seq_wait(connectivity_seq_get(), connectivity_seqno);
+ }
+}
+