+ ovsdb_idl_txn_add_comment(txn, "ovs-brcompatd: addbr %s", br_name);
+
+ iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
+ ovsrec_interface_set_name(iface, br_name);
+
+ port = ovsrec_port_insert(txn_from_openvswitch(ovs));
+ ovsrec_port_set_name(port, br_name);
+ ovsrec_port_set_interfaces(port, &iface, 1);
+
+ br = ovsrec_bridge_insert(txn_from_openvswitch(ovs));
+ ovsrec_bridge_set_name(br, br_name);
+ ovsrec_bridge_set_ports(br, &port, 1);
+
+ ovs_insert_bridge(ovs, br);
+
+ return commit_txn(txn, true);
+}
+
+static void
+add_port(const struct ovsrec_open_vswitch *ovs,
+ const struct ovsrec_bridge *br, const char *port_name)
+{
+ struct ovsrec_interface *iface;
+ struct ovsrec_port *port;
+ struct ovsrec_port **ports;
+ size_t i;
+
+ /* xxx Check conflicts? */
+ iface = ovsrec_interface_insert(txn_from_openvswitch(ovs));
+ ovsrec_interface_set_name(iface, port_name);
+
+ port = ovsrec_port_insert(txn_from_openvswitch(ovs));
+ ovsrec_port_set_name(port, port_name);
+ ovsrec_port_set_interfaces(port, &iface, 1);
+
+ ports = xmalloc(sizeof *br->ports * (br->n_ports + 1));
+ for (i = 0; i < br->n_ports; i++) {
+ ports[i] = br->ports[i];
+ }
+ ports[br->n_ports] = port;
+ ovsrec_bridge_set_ports(br, ports, br->n_ports + 1);
+ free(ports);
+}
+
+/* Deletes 'port' from 'br'.
+ *
+ * After calling this function, 'port' must not be referenced again. */
+static void
+del_port(const struct ovsrec_bridge *br, const struct ovsrec_port *port)
+{
+ struct ovsrec_port **ports;
+ size_t i, n;
+
+ /* Remove 'port' from the bridge's list of ports. */
+ ports = xmalloc(sizeof *br->ports * br->n_ports);
+ for (i = n = 0; i < br->n_ports; i++) {
+ if (br->ports[i] != port) {
+ ports[n++] = br->ports[i];
+ }
+ }
+ ovsrec_bridge_set_ports(br, ports, n);
+ free(ports);
+
+ /* Delete all of the port's interfaces. */
+ for (i = 0; i < port->n_interfaces; i++) {
+ ovsrec_interface_delete(port->interfaces[i]);
+ }
+
+ /* Delete the port itself. */
+ ovsrec_port_delete(port);
+}
+
+/* Delete 'iface' from 'port' (which must be within 'br'). If 'iface' was
+ * 'port''s only interface, delete 'port' from 'br' also.
+ *
+ * After calling this function, 'iface' must not be referenced again. */
+static void
+del_interface(const struct ovsrec_bridge *br,
+ const struct ovsrec_port *port,
+ const struct ovsrec_interface *iface)
+{
+ if (port->n_interfaces == 1) {
+ del_port(br, port);
+ } else {
+ struct ovsrec_interface **ifaces;
+ size_t i, n;
+
+ ifaces = xmalloc(sizeof *port->interfaces * port->n_interfaces);
+ for (i = n = 0; i < port->n_interfaces; i++) {
+ if (port->interfaces[i] != iface) {
+ ifaces[n++] = port->interfaces[i];
+ }
+ }
+ ovsrec_port_set_interfaces(port, ifaces, n);
+ free(ifaces);
+ ovsrec_interface_delete(iface);
+ }