vswitchd: Also consider access port VLANs as "in use" for VLAN splinters.
[sliver-openvswitch.git] / vswitchd / bridge.c
index 32885eb..44a9d1d 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks
+/* Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
 #include "dynamic-string.h"
 #include "hash.h"
 #include "hmap.h"
+#include "hmapx.h"
 #include "jsonrpc.h"
 #include "lacp.h"
 #include "list.h"
@@ -78,6 +79,7 @@ struct mirror {
     struct hmap_node hmap_node; /* In struct bridge's "mirrors" hmap. */
     struct bridge *bridge;
     char *name;
+    const struct ovsrec_mirror *cfg;
 };
 
 struct port {
@@ -192,7 +194,8 @@ static void bridge_configure_mirrors(struct bridge *);
 static struct mirror *mirror_create(struct bridge *,
                                     const struct ovsrec_mirror *);
 static void mirror_destroy(struct mirror *);
-static bool mirror_configure(struct mirror *, const struct ovsrec_mirror *);
+static bool mirror_configure(struct mirror *);
+static void mirror_refresh_stats(struct mirror *);
 
 static void iface_configure_lacp(struct iface *, struct lacp_slave_settings *);
 static struct iface *iface_create(struct port *port,
@@ -291,6 +294,7 @@ bridge_init(const char *remote)
     ovsdb_idl_omit(idl, &ovsrec_queue_col_external_ids);
 
     ovsdb_idl_omit(idl, &ovsrec_mirror_col_external_ids);
+    ovsdb_idl_omit_alert(idl, &ovsrec_mirror_col_statistics);
 
     ovsdb_idl_omit(idl, &ovsrec_netflow_col_external_ids);
 
@@ -441,6 +445,10 @@ bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
     HMAP_FOR_EACH (br, node, &all_bridges) {
         struct port *port;
 
+        /* We need the datapath ID early to allow LACP ports to use it as the
+         * default system ID. */
+        bridge_configure_datapath_id(br);
+
         HMAP_FOR_EACH (port, hmap_node, &br->ports) {
             struct iface *iface;
 
@@ -453,7 +461,6 @@ bridge_reconfigure(const struct ovsrec_open_vswitch *ovs_cfg)
             }
         }
         bridge_configure_mirrors(br);
-        bridge_configure_datapath_id(br);
         bridge_configure_flow_eviction_threshold(br);
         bridge_configure_forward_bpdu(br);
         bridge_configure_remotes(br, managers, n_managers);
@@ -532,17 +539,8 @@ port_configure(struct port *port)
 
     /* Get VLAN tag. */
     s.vlan = -1;
-    if (cfg->tag) {
-        if (list_is_short(&port->ifaces)) {
-            if (*cfg->tag >= 0 && *cfg->tag <= 4095) {
-                s.vlan = *cfg->tag;
-            }
-        } else {
-            /* It's possible that bonded, VLAN-tagged ports make sense.  Maybe
-             * they even work as-is.  But they have not been tested. */
-            VLOG_WARN("port %s: VLAN tags not supported on bonded ports",
-                      port->name);
-        }
+    if (cfg->tag && *cfg->tag >= 0 && *cfg->tag <= 4095) {
+        s.vlan = *cfg->tag;
     }
 
     /* Get VLAN trunks. */
@@ -1275,10 +1273,12 @@ static void
 bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
                           struct iface **hw_addr_iface)
 {
+    struct hmapx mirror_output_ports;
     const char *hwaddr;
     struct port *port;
     bool found_addr = false;
     int error;
+    int i;
 
     *hw_addr_iface = NULL;
 
@@ -1295,6 +1295,18 @@ bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
         }
     }
 
+    /* Mirror output ports don't participate in picking the local hardware
+     * address.  ofproto can't help us find out whether a given port is a
+     * mirror output because we haven't configured mirrors yet, so we need to
+     * accumulate them ourselves. */
+    hmapx_init(&mirror_output_ports);
+    for (i = 0; i < br->cfg->n_mirrors; i++) {
+        struct ovsrec_mirror *m = br->cfg->mirrors[i];
+        if (m->output_port) {
+            hmapx_add(&mirror_output_ports, m->output_port);
+        }
+    }
+
     /* Otherwise choose the minimum non-local MAC address among all of the
      * interfaces. */
     HMAP_FOR_EACH (port, hmap_node, &br->ports) {
@@ -1303,7 +1315,7 @@ bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
         struct iface *iface;
 
         /* Mirror output ports don't participate. */
-        if (ofproto_is_mirror_output_bundle(br->ofproto, port)) {
+        if (hmapx_contains(&mirror_output_ports, port->cfg)) {
             continue;
         }
 
@@ -1366,6 +1378,8 @@ bridge_pick_local_hw_addr(struct bridge *br, uint8_t ea[ETH_ADDR_LEN],
         VLOG_WARN("bridge %s: using default bridge Ethernet "
                   "address "ETH_ADDR_FMT, br->name, ETH_ADDR_ARGS(ea));
     }
+
+    hmapx_destroy(&mirror_output_ports);
 }
 
 /* Choose and returns the datapath ID for bridge 'br' given that the bridge
@@ -1779,13 +1793,28 @@ refresh_cfm_stats(void)
     }
 }
 
+/* Performs periodic activity required by bridges that needs to be done with
+ * the least possible latency.
+ *
+ * It makes sense to call this function a couple of times per poll loop, to
+ * provide a significant performance boost on some benchmarks with ofprotos
+ * that use the ofproto-dpif implementation. */
+void
+bridge_run_fast(void)
+{
+    struct bridge *br;
+
+    HMAP_FOR_EACH (br, node, &all_bridges) {
+        ofproto_run_fast(br->ofproto);
+    }
+}
+
 void
 bridge_run(void)
 {
     const struct ovsrec_open_vswitch *cfg;
 
     bool vlan_splinters_changed;
-    bool datapath_destroyed;
     bool database_changed;
     struct bridge *br;
 
@@ -1808,15 +1837,8 @@ bridge_run(void)
     cfg = ovsrec_open_vswitch_first(idl);
 
     /* Let each bridge do the work that it needs to do. */
-    datapath_destroyed = false;
     HMAP_FOR_EACH (br, node, &all_bridges) {
-        int error = ofproto_run(br->ofproto);
-        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;
-        }
+        ofproto_run(br->ofproto);
     }
 
     /* Re-configure SSL.  We do this on every trip through the main loop,
@@ -1844,7 +1866,7 @@ bridge_run(void)
         }
     }
 
-    if (database_changed || datapath_destroyed || vlan_splinters_changed) {
+    if (database_changed || vlan_splinters_changed) {
         if (cfg) {
             struct ovsdb_idl_txn *txn = ovsdb_idl_txn_create(idl);
 
@@ -1870,6 +1892,7 @@ bridge_run(void)
             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;
@@ -1879,6 +1902,11 @@ bridge_run(void)
                         iface_refresh_status(iface);
                     }
                 }
+
+                HMAP_FOR_EACH (m, hmap_node, &br->mirrors) {
+                    mirror_refresh_stats(m);
+                }
+
             }
             refresh_system_stats(cfg);
             refresh_controller_status();
@@ -2623,7 +2651,7 @@ enable_lacp(struct port *port, bool *activep)
 static struct lacp_settings *
 port_configure_lacp(struct port *port, struct lacp_settings *s)
 {
-    const char *lacp_time;
+    const char *lacp_time, *system_id;
     long long int custom_time;
     int priority;
 
@@ -2632,7 +2660,18 @@ port_configure_lacp(struct port *port, struct lacp_settings *s)
     }
 
     s->name = port->name;
-    memcpy(s->id, port->bridge->ea, ETH_ADDR_LEN);
+
+    system_id = get_port_other_config(port->cfg, "lacp-system-id", NULL);
+    if (!system_id
+        || sscanf(system_id, ETH_ADDR_SCAN_FMT,
+                  ETH_ADDR_SCAN_ARGS(s->id)) != ETH_ADDR_SCAN_COUNT) {
+        memcpy(s->id, port->bridge->ea, ETH_ADDR_LEN);
+    }
+
+    if (eth_addr_is_zero(s->id)) {
+        VLOG_WARN("port %s: Invalid zero LACP system ID.", port->name);
+        return NULL;
+    }
 
     /* Prefer bondable links if unspecified. */
     priority = atoi(get_port_other_config(port->cfg, "lacp-system-priority",
@@ -2645,7 +2684,6 @@ port_configure_lacp(struct port *port, struct lacp_settings *s)
                                                  "lacp-heartbeat",
                                                  "false"), "true");
 
-
     lacp_time = get_port_other_config(port->cfg, "lacp-time", "slow");
     custom_time = atoi(lacp_time);
     if (!strcmp(lacp_time, "fast")) {
@@ -2702,11 +2740,21 @@ port_configure_bond(struct port *port, struct bond_settings *s,
 
     s->name = port->name;
     s->balance = BM_SLB;
-    if (port->cfg->bond_mode
-        && !bond_mode_from_string(&s->balance, port->cfg->bond_mode)) {
-        VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
-                  port->name, port->cfg->bond_mode,
-                  bond_mode_to_string(s->balance));
+    if (port->cfg->bond_mode) {
+        if (!bond_mode_from_string(&s->balance, port->cfg->bond_mode)) {
+            VLOG_WARN("port %s: unknown bond_mode %s, defaulting to %s",
+                      port->name, port->cfg->bond_mode,
+                      bond_mode_to_string(s->balance));
+        }
+    } else {
+        static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
+
+        /* XXX: Post version 1.4.*, change the default bond_mode to
+         * active-backup.  Until then, warn that the change is imminent. */
+        VLOG_WARN_RL(&rl, "port %s: Using the default bond_mode %s. Note that"
+                     " in future versions, the default bond_mode is expected"
+                     " to change to active-backup", port->name,
+                     bond_mode_to_string(s->balance));
     }
     if (s->balance == BM_SLB && port->bridge->cfg->n_flood_vlans) {
         VLOG_WARN("port %s: SLB bonds are incompatible with flood_vlans, "
@@ -3145,7 +3193,8 @@ bridge_configure_mirrors(struct bridge *br)
         if (!m) {
             m = mirror_create(br, cfg);
         }
-        if (!mirror_configure(m, cfg)) {
+        m->cfg = cfg;
+        if (!mirror_configure(m)) {
             mirror_destroy(m);
         }
     }
@@ -3211,8 +3260,9 @@ mirror_collect_ports(struct mirror *m,
 }
 
 static bool
-mirror_configure(struct mirror *m, const struct ovsrec_mirror *cfg)
+mirror_configure(struct mirror *m)
 {
+    const struct ovsrec_mirror *cfg = m->cfg;
     struct ofproto_mirror_settings s;
 
     /* Set name. */
@@ -3356,8 +3406,13 @@ collect_splinter_vlans(const struct ovsrec_open_vswitch *ovs_cfg)
     struct bridge *br;
     size_t i;
 
-    splinter_vlans = NULL;
+    /* Free space allocated for synthesized ports and interfaces, since we're
+     * in the process of reconstructing all of them. */
+    free_registered_blocks();
+
+    splinter_vlans = bitmap_allocate(4096);
     sset_init(&splinter_ifaces);
+    vlan_splinters_enabled_anywhere = false;
     for (i = 0; i < ovs_cfg->n_bridges; i++) {
         struct ovsrec_bridge *br_cfg = ovs_cfg->bridges[i];
         size_t j;
@@ -3370,21 +3425,22 @@ collect_splinter_vlans(const struct ovsrec_open_vswitch *ovs_cfg)
                 struct ovsrec_interface *iface_cfg = port_cfg->interfaces[k];
 
                 if (vlan_splinters_is_enabled(iface_cfg)) {
+                    vlan_splinters_enabled_anywhere = true;
                     sset_add(&splinter_ifaces, iface_cfg->name);
-
-                    if (!splinter_vlans) {
-                        splinter_vlans = bitmap_allocate(4096);
-                    }
                     vlan_bitmap_from_array__(port_cfg->trunks,
                                              port_cfg->n_trunks,
                                              splinter_vlans);
                 }
             }
+
+            if (port_cfg->tag && *port_cfg->tag > 0 && *port_cfg->tag < 4095) {
+                bitmap_set1(splinter_vlans, *port_cfg->tag);
+            }
         }
     }
 
-    vlan_splinters_enabled_anywhere = splinter_vlans != NULL;
-    if (!splinter_vlans) {
+    if (!vlan_splinters_enabled_anywhere) {
+        free(splinter_vlans);
         sset_destroy(&splinter_ifaces);
         return NULL;
     }
@@ -3523,8 +3579,6 @@ add_vlan_splinter_ports(struct bridge *br,
 {
     size_t i;
 
-    free_registered_blocks();
-
     /* We iterate through 'br->cfg->ports' instead of 'ports' here because
      * we're modifying 'ports'. */
     for (i = 0; i < br->cfg->n_ports; i++) {
@@ -3555,3 +3609,31 @@ add_vlan_splinter_ports(struct bridge *br,
         }
     }
 }
+
+static void
+mirror_refresh_stats(struct mirror *m)
+{
+    struct ofproto *ofproto = m->bridge->ofproto;
+    uint64_t tx_packets, tx_bytes;
+    char *keys[2];
+    int64_t values[2];
+    size_t stat_cnt = 0;
+
+    if (ofproto_mirror_get_stats(ofproto, m, &tx_packets, &tx_bytes)) {
+        ovsrec_mirror_set_statistics(m->cfg, NULL, NULL, 0);
+        return;
+    }
+
+    if (tx_packets != UINT64_MAX) {
+        keys[stat_cnt] = "tx_packets";
+        values[stat_cnt] = tx_packets;
+        stat_cnt++;
+    }
+    if (tx_bytes != UINT64_MAX) {
+        keys[stat_cnt] = "tx_bytes";
+        values[stat_cnt] = tx_bytes;
+        stat_cnt++;
+    }
+
+    ovsrec_mirror_set_statistics(m->cfg, keys, values, stat_cnt);
+}