+/* Configures 'netdev' based on the "options" column in 'iface_cfg'.
+ * Returns 0 if successful, otherwise a positive errno value. */
+static int
+iface_set_netdev_config(const struct ovsrec_interface *iface_cfg,
+ struct netdev *netdev)
+{
+ int error;
+
+ error = netdev_set_config(netdev, &iface_cfg->options);
+ if (error) {
+ VLOG_WARN("could not configure network device %s (%s)",
+ iface_cfg->name, strerror(error));
+ }
+ return error;
+}
+
+/* This function determines whether 'ofproto_port', which is attached to
+ * br->ofproto's datapath, is one that we want in 'br'.
+ *
+ * If it is, it returns true, after creating an iface (if necessary),
+ * configuring the iface's netdev according to the iface's options, and setting
+ * iface's ofp_port member to 'ofproto_port->ofp_port'.
+ *
+ * If, on the other hand, 'port' should be removed, it returns false. The
+ * caller should later detach the port from br->ofproto. */
+static bool
+bridge_refresh_one_ofp_port(struct bridge *br,
+ const struct ofproto_port *ofproto_port)
+{
+ const char *name = ofproto_port->name;
+ const char *type = ofproto_port->type;
+ uint16_t ofp_port = ofproto_port->ofp_port;
+
+ struct iface *iface = iface_lookup(br, name);
+ if (iface) {
+ /* Check that the name-to-number mapping is one-to-one. */
+ if (iface->ofp_port >= 0) {
+ VLOG_WARN("bridge %s: interface %s reported twice",
+ br->name, name);
+ return false;
+ } else if (iface_from_ofp_port(br, ofp_port)) {
+ VLOG_WARN("bridge %s: interface %"PRIu16" reported twice",
+ br->name, ofp_port);
+ return false;
+ }
+
+ /* There's a configured interface named 'name'. */
+ if (strcmp(type, iface->type)
+ || iface_set_netdev_config(iface->cfg, iface->netdev)) {
+ /* It's the wrong type, or it's the right type but can't be
+ * configured as the user requested, so we must destroy it. */
+ return false;
+ } else {
+ /* It's the right type and configured correctly. keep it. */
+ iface_set_ofp_port(iface, ofp_port);
+ return true;
+ }
+ } else if (bridge_has_bond_fake_iface(br, name)
+ && !strcmp(type, "internal")) {
+ /* It's a bond fake iface. Keep it. */
+ return true;
+ } else {
+ /* There's no configured interface named 'name', but there might be an
+ * interface of that name queued to be created.
+ *
+ * If there is, and it has the correct type, then try to configure it
+ * and add it. If that's successful, we'll keep it. Otherwise, we'll
+ * delete it and later try to re-add it. */
+ struct if_cfg *if_cfg = if_cfg_lookup(br, name);
+ return (if_cfg
+ && !strcmp(type, iface_get_type(if_cfg->cfg, br->cfg))
+ && iface_create(br, if_cfg, ofp_port));
+ }
+}
+
+/* Update bridges "if_cfg"s, "struct port"s, and "struct iface"s to be
+ * consistent with the ofp_ports in "br->ofproto". */
+static void
+bridge_refresh_ofp_port(struct bridge *br)
+{
+ struct ofproto_port_dump dump;
+ struct ofproto_port ofproto_port;
+ struct port *port, *port_next;
+
+ /* Clear each "struct iface"s ofp_port so we can get its correct value. */
+ hmap_clear(&br->ifaces);
+ HMAP_FOR_EACH (port, hmap_node, &br->ports) {
+ struct iface *iface;
+
+ LIST_FOR_EACH (iface, port_elem, &port->ifaces) {
+ iface->ofp_port = -1;
+ }
+ }
+
+ /* Obtain the correct "ofp_port"s from ofproto. Find any if_cfg's which
+ * already exist in the datapath and promote them to full fledged "struct
+ * iface"s. Mark ports in the datapath which don't belong as garbage. */
+ OFPROTO_PORT_FOR_EACH (&ofproto_port, &dump, br->ofproto) {
+ if (!bridge_refresh_one_ofp_port(br, &ofproto_port)) {
+ struct ofpp_garbage *garbage = xmalloc(sizeof *garbage);
+ garbage->ofp_port = ofproto_port.ofp_port;
+ list_push_front(&br->ofpp_garbage, &garbage->list_node);
+ }
+ }
+
+ /* Some ifaces may not have "ofp_port"s in ofproto and therefore don't
+ * deserve to have "struct iface"s. Demote these to "if_cfg"s so that
+ * later they can be added to ofproto. */
+ HMAP_FOR_EACH_SAFE (port, port_next, hmap_node, &br->ports) {
+ struct iface *iface, *iface_next;
+
+ LIST_FOR_EACH_SAFE (iface, iface_next, port_elem, &port->ifaces) {
+ if (iface->ofp_port < 0) {
+ bridge_queue_if_cfg(br, iface->cfg, port->cfg);
+ iface_destroy(iface);
+ }
+ }
+
+ if (list_is_empty(&port->ifaces)) {
+ port_destroy(port);
+ }
+ }
+}
+
+/* Opens a network device for 'iface_cfg' and configures it. If '*ofp_portp'
+ * is negative, adds the network device to br->ofproto and stores the OpenFlow
+ * port number in '*ofp_portp'; otherwise leaves br->ofproto and '*ofp_portp'
+ * untouched.
+ *
+ * If successful, returns 0 and stores the network device in '*netdevp'. On
+ * failure, returns a positive errno value and stores NULL in '*netdevp'. */
+static int
+iface_do_create(const struct bridge *br,
+ const struct ovsrec_interface *iface_cfg,
+ const struct ovsrec_port *port_cfg,
+ int *ofp_portp, struct netdev **netdevp)
+{
+ struct netdev *netdev;
+ int error;
+
+ error = netdev_open(iface_cfg->name,
+ iface_get_type(iface_cfg, br->cfg), &netdev);
+ if (error) {
+ VLOG_WARN("could not open network device %s (%s)",
+ iface_cfg->name, strerror(error));
+ goto error;
+ }
+
+ error = iface_set_netdev_config(iface_cfg, netdev);
+ if (error) {
+ goto error;
+ }
+
+ if (*ofp_portp < 0) {
+ uint16_t ofp_port;
+
+ error = ofproto_port_add(br->ofproto, netdev, &ofp_port);
+ if (error) {
+ goto error;
+ }
+ *ofp_portp = ofp_port;
+
+ VLOG_INFO("bridge %s: added interface %s on port %d",
+ br->name, iface_cfg->name, *ofp_portp);
+ } else {
+ VLOG_DBG("bridge %s: interface %s is on port %d",
+ br->name, iface_cfg->name, *ofp_portp);
+ }
+
+ if (port_cfg->vlan_mode && !strcmp(port_cfg->vlan_mode, "splinter")) {
+ netdev_turn_flags_on(netdev, NETDEV_UP, true);
+ }
+
+ *netdevp = netdev;
+ return 0;
+
+error:
+ *netdevp = NULL;
+ netdev_close(netdev);
+ return error;
+}
+
+/* Creates a new iface on 'br' based on 'if_cfg'. The new iface has OpenFlow
+ * port number 'ofp_port'. If ofp_port is negative, an OpenFlow port is
+ * automatically allocated for the iface. Takes ownership of and
+ * deallocates 'if_cfg'.
+ *
+ * Return true if an iface is successfully created, false otherwise. */
+static bool
+iface_create(struct bridge *br, struct if_cfg *if_cfg, int ofp_port)
+{
+ const struct ovsrec_interface *iface_cfg = if_cfg->cfg;
+ const struct ovsrec_port *port_cfg = if_cfg->parent;
+
+ struct netdev *netdev;
+ struct iface *iface;
+ struct port *port;
+ int error;
+
+ /* Get rid of 'if_cfg' itself. We already copied out the interesting
+ * bits. */
+ hmap_remove(&br->if_cfg_todo, &if_cfg->hmap_node);
+ free(if_cfg);
+
+ /* Do the bits that can fail up front.
+ *
+ * It's a bit dangerous to call bridge_run_fast() here as ofproto's
+ * internal datastructures may not be consistent. Eventually, when port
+ * additions and deletions are cheaper, these calls should be removed. */
+ bridge_run_fast();
+ assert(!iface_lookup(br, iface_cfg->name));
+ error = iface_do_create(br, iface_cfg, port_cfg, &ofp_port, &netdev);
+ bridge_run_fast();
+ if (error) {
+ iface_clear_db_record(iface_cfg);
+ return false;
+ }
+
+ /* Get or create the port structure. */
+ port = port_lookup(br, port_cfg->name);
+ if (!port) {
+ port = port_create(br, port_cfg);
+ }
+
+ /* Create the iface structure. */
+ iface = xzalloc(sizeof *iface);
+ list_push_back(&port->ifaces, &iface->port_elem);
+ hmap_insert(&br->iface_by_name, &iface->name_node,
+ hash_string(iface_cfg->name, 0));
+ iface->port = port;
+ iface->name = xstrdup(iface_cfg->name);
+ iface->ofp_port = -1;
+ iface->netdev = netdev;
+ iface->type = iface_get_type(iface_cfg, br->cfg);
+ iface->cfg = iface_cfg;
+
+ iface_set_ofp_port(iface, ofp_port);
+
+ /* Populate initial status in database. */
+ iface_refresh_stats(iface);
+ iface_refresh_status(iface);
+
+ /* Add bond fake iface if necessary. */
+ if (port_is_bond_fake_iface(port)) {
+ struct ofproto_port ofproto_port;
+
+ if (ofproto_port_query_by_name(br->ofproto, port->name,
+ &ofproto_port)) {
+ struct netdev *netdev;
+ int error;
+
+ error = netdev_open(port->name, "internal", &netdev);
+ if (!error) {
+ ofproto_port_add(br->ofproto, netdev, NULL);
+ netdev_close(netdev);
+ } else {
+ VLOG_WARN("could not open network device %s (%s)",
+ port->name, strerror(error));
+ }
+ } else {
+ /* Already exists, nothing to do. */
+ ofproto_port_destroy(&ofproto_port);
+ }
+ }
+
+ return true;
+}
+
+/* Set Flow eviction threshold */
+static void
+bridge_configure_flow_eviction_threshold(struct bridge *br)
+{
+ const char *threshold_str;
+ unsigned threshold;
+
+ threshold_str = smap_get(&br->cfg->other_config,
+ "flow-eviction-threshold");
+ if (threshold_str) {
+ threshold = strtoul(threshold_str, NULL, 10);
+ } else {
+ threshold = OFPROTO_FLOW_EVICTON_THRESHOLD_DEFAULT;
+ }
+ ofproto_set_flow_eviction_threshold(br->ofproto, threshold);
+}
+
+/* Set forward BPDU option. */
+static void
+bridge_configure_forward_bpdu(struct bridge *br)