+\f
+/* Linux VLAN device support (e.g. "eth0.10" for VLAN 10.)
+ *
+ * This is deprecated. It is only for compatibility with broken device drivers
+ * in old versions of Linux that do not properly support VLANs when VLAN
+ * devices are not used. When broken device drivers are no longer in
+ * widespread use, we will delete these interfaces. */
+
+static struct ovsrec_port **recs;
+static size_t n_recs, allocated_recs;
+
+/* Adds 'rec' to a list of recs that have to be destroyed when the VLAN
+ * splinters are reconfigured. */
+static void
+register_rec(struct ovsrec_port *rec)
+{
+ if (n_recs >= allocated_recs) {
+ recs = x2nrealloc(recs, &allocated_recs, sizeof *recs);
+ }
+ recs[n_recs++] = rec;
+}
+
+/* Frees all of the ports registered with register_reg(). */
+static void
+free_registered_recs(void)
+{
+ size_t i;
+
+ for (i = 0; i < n_recs; i++) {
+ struct ovsrec_port *port = recs[i];
+ size_t j;
+
+ for (j = 0; j < port->n_interfaces; j++) {
+ struct ovsrec_interface *iface = port->interfaces[j];
+ free(iface->name);
+ free(iface);
+ }
+
+ smap_destroy(&port->other_config);
+ free(port->interfaces);
+ free(port->name);
+ free(port->tag);
+ free(port);
+ }
+ n_recs = 0;
+}
+
+/* Returns true if VLAN splinters are enabled on 'iface_cfg', false
+ * otherwise. */
+static bool
+vlan_splinters_is_enabled(const struct ovsrec_interface *iface_cfg)
+{
+ return smap_get_bool(&iface_cfg->other_config, "enable-vlan-splinters",
+ false);
+}
+
+/* Figures out the set of VLANs that are in use for the purpose of VLAN
+ * splinters.
+ *
+ * If VLAN splinters are enabled on at least one interface and any VLANs are in
+ * use, returns a 4096-bit bitmap with a 1-bit for each in-use VLAN (bits 0 and
+ * 4095 will not be set). The caller is responsible for freeing the bitmap,
+ * with free().
+ *
+ * If VLANs splinters are not enabled on any interface or if no VLANs are in
+ * use, returns NULL.
+ *
+ * Updates 'vlan_splinters_enabled_anywhere'. */
+static unsigned long int *
+collect_splinter_vlans(const struct ovsrec_open_vswitch *ovs_cfg)
+{
+ unsigned long int *splinter_vlans;
+ struct sset splinter_ifaces;
+ const char *real_dev_name;
+ struct shash *real_devs;
+ struct shash_node *node;
+ struct bridge *br;
+ size_t i;
+
+ /* Free space allocated for synthesized ports and interfaces, since we're
+ * in the process of reconstructing all of them. */
+ free_registered_recs();
+
+ 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;
+
+ for (j = 0; j < br_cfg->n_ports; j++) {
+ struct ovsrec_port *port_cfg = br_cfg->ports[j];
+ int k;
+
+ for (k = 0; k < port_cfg->n_interfaces; k++) {
+ 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);
+ 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);
+ }
+ }
+ }
+
+ if (!vlan_splinters_enabled_anywhere) {
+ free(splinter_vlans);
+ sset_destroy(&splinter_ifaces);
+ return NULL;
+ }
+
+ HMAP_FOR_EACH (br, node, &all_bridges) {
+ if (br->ofproto) {
+ ofproto_get_vlan_usage(br->ofproto, splinter_vlans);
+ }
+ }
+
+ /* Don't allow VLANs 0 or 4095 to be splintered. VLAN 0 should appear on
+ * the real device. VLAN 4095 is reserved and Linux doesn't allow a VLAN
+ * device to be created for it. */
+ bitmap_set0(splinter_vlans, 0);
+ bitmap_set0(splinter_vlans, 4095);
+
+ /* Delete all VLAN devices that we don't need. */
+ vlandev_refresh();
+ real_devs = vlandev_get_real_devs();
+ SHASH_FOR_EACH (node, real_devs) {
+ const struct vlan_real_dev *real_dev = node->data;
+ const struct vlan_dev *vlan_dev;
+ bool real_dev_has_splinters;
+
+ real_dev_has_splinters = sset_contains(&splinter_ifaces,
+ real_dev->name);
+ HMAP_FOR_EACH (vlan_dev, hmap_node, &real_dev->vlan_devs) {
+ if (!real_dev_has_splinters
+ || !bitmap_is_set(splinter_vlans, vlan_dev->vid)) {
+ struct netdev *netdev;
+
+ if (!netdev_open(vlan_dev->name, "system", &netdev)) {
+ if (!netdev_get_in4(netdev, NULL, NULL) ||
+ !netdev_get_in6(netdev, NULL)) {
+ vlandev_del(vlan_dev->name);
+ } else {
+ /* It has an IP address configured, so we don't own
+ * it. Don't delete it. */
+ }
+ netdev_close(netdev);
+ }
+ }
+
+ }
+ }
+
+ /* Add all VLAN devices that we need. */
+ SSET_FOR_EACH (real_dev_name, &splinter_ifaces) {
+ int vid;
+
+ BITMAP_FOR_EACH_1 (vid, 4096, splinter_vlans) {
+ if (!vlandev_get_name(real_dev_name, vid)) {
+ vlandev_add(real_dev_name, vid);
+ }
+ }
+ }
+
+ vlandev_refresh();
+
+ sset_destroy(&splinter_ifaces);
+
+ if (bitmap_scan(splinter_vlans, 0, 4096) >= 4096) {
+ free(splinter_vlans);
+ return NULL;
+ }
+ return splinter_vlans;
+}
+
+/* Pushes the configure of VLAN splinter port 'port' (e.g. eth0.9) down to
+ * ofproto. */
+static void
+configure_splinter_port(struct port *port)
+{
+ struct ofproto *ofproto = port->bridge->ofproto;
+ uint16_t realdev_ofp_port;
+ const char *realdev_name;
+ struct iface *vlandev, *realdev;
+
+ ofproto_bundle_unregister(port->bridge->ofproto, port);
+
+ vlandev = CONTAINER_OF(list_front(&port->ifaces), struct iface,
+ port_elem);
+
+ realdev_name = smap_get(&port->cfg->other_config, "realdev");
+ realdev = iface_lookup(port->bridge, realdev_name);
+ realdev_ofp_port = realdev ? realdev->ofp_port : 0;
+
+ ofproto_port_set_realdev(ofproto, vlandev->ofp_port, realdev_ofp_port,
+ *port->cfg->tag);
+}
+
+static struct ovsrec_port *
+synthesize_splinter_port(const char *real_dev_name,
+ const char *vlan_dev_name, int vid)
+{
+ struct ovsrec_interface *iface;
+ struct ovsrec_port *port;
+
+ iface = xmalloc(sizeof *iface);
+ ovsrec_interface_init(iface);
+ iface->name = xstrdup(vlan_dev_name);
+ iface->type = "system";
+
+ port = xmalloc(sizeof *port);
+ ovsrec_port_init(port);
+ port->interfaces = xmemdup(&iface, sizeof iface);
+ port->n_interfaces = 1;
+ port->name = xstrdup(vlan_dev_name);
+ port->vlan_mode = "splinter";
+ port->tag = xmalloc(sizeof *port->tag);
+ *port->tag = vid;
+
+ smap_add(&port->other_config, "realdev", real_dev_name);
+
+ register_rec(port);
+ return port;
+}
+
+/* For each interface with 'br' that has VLAN splinters enabled, adds a
+ * corresponding ovsrec_port to 'ports' for each splinter VLAN marked with a
+ * 1-bit in the 'splinter_vlans' bitmap. */
+static void
+add_vlan_splinter_ports(struct bridge *br,
+ const unsigned long int *splinter_vlans,
+ struct shash *ports)
+{
+ size_t i;
+
+ /* We iterate through 'br->cfg->ports' instead of 'ports' here because
+ * we're modifying 'ports'. */
+ for (i = 0; i < br->cfg->n_ports; i++) {
+ const char *name = br->cfg->ports[i]->name;
+ struct ovsrec_port *port_cfg = shash_find_data(ports, name);
+ size_t j;
+
+ for (j = 0; j < port_cfg->n_interfaces; j++) {
+ struct ovsrec_interface *iface_cfg = port_cfg->interfaces[j];
+
+ if (vlan_splinters_is_enabled(iface_cfg)) {
+ const char *real_dev_name;
+ uint16_t vid;
+
+ real_dev_name = iface_cfg->name;
+ BITMAP_FOR_EACH_1 (vid, 4096, splinter_vlans) {
+ const char *vlan_dev_name;
+
+ vlan_dev_name = vlandev_get_name(real_dev_name, vid);
+ if (vlan_dev_name
+ && !shash_find(ports, vlan_dev_name)) {
+ shash_add(ports, vlan_dev_name,
+ synthesize_splinter_port(
+ real_dev_name, vlan_dev_name, vid));
+ }
+ }
+ }
+ }
+ }
+}
+
+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);
+}