-/* Copyright (c) 2015 Nicira, Inc.
+/* Copyright (c) 2015, 2016 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <config.h>
#include "binding.h"
+#include "lib/bitmap.h"
+#include "lib/hmap.h"
+#include "lib/poll-loop.h"
#include "lib/sset.h"
#include "lib/util.h"
#include "lib/vswitch-idl.h"
VLOG_DEFINE_THIS_MODULE(binding);
+/* A set of the iface-id values of local interfaces on this chassis. */
+static struct sset local_ids = SSET_INITIALIZER(&local_ids);
+
+/* When this gets set to true, the next run will re-check all binding records. */
+static bool process_full_binding = false;
+
void
-binding_init(struct controller_ctx *ctx)
+binding_reset_processing(void)
{
- ovsdb_idl_add_table(ctx->ovs_idl, &ovsrec_table_open_vswitch);
- ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_open_vswitch_col_bridges);
+ process_full_binding = true;
+}
- ovsdb_idl_add_table(ctx->ovs_idl, &ovsrec_table_bridge);
- ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_bridge_col_name);
- ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_bridge_col_ports);
+void
+binding_register_ovs_idl(struct ovsdb_idl *ovs_idl)
+{
+ ovsdb_idl_add_table(ovs_idl, &ovsrec_table_open_vswitch);
+ ovsdb_idl_add_column(ovs_idl, &ovsrec_open_vswitch_col_bridges);
- ovsdb_idl_add_table(ctx->ovs_idl, &ovsrec_table_port);
- ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_port_col_name);
- ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_port_col_interfaces);
+ ovsdb_idl_add_table(ovs_idl, &ovsrec_table_bridge);
+ ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_name);
+ ovsdb_idl_add_column(ovs_idl, &ovsrec_bridge_col_ports);
- ovsdb_idl_add_table(ctx->ovs_idl, &ovsrec_table_interface);
- ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_interface_col_name);
- ovsdb_idl_add_column(ctx->ovs_idl, &ovsrec_interface_col_external_ids);
+ ovsdb_idl_add_table(ovs_idl, &ovsrec_table_port);
+ ovsdb_idl_add_column(ovs_idl, &ovsrec_port_col_name);
+ ovsdb_idl_add_column(ovs_idl, &ovsrec_port_col_interfaces);
+
+ ovsdb_idl_add_table(ovs_idl, &ovsrec_table_interface);
+ ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_name);
+ ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_external_ids);
+ ovsdb_idl_add_column(ovs_idl, &ovsrec_interface_col_ingress_policing_rate);
+ ovsdb_idl_add_column(ovs_idl,
+ &ovsrec_interface_col_ingress_policing_burst);
}
-static void
-get_local_iface_ids(struct controller_ctx *ctx, struct sset *lports)
+static bool
+get_local_iface_ids(const struct ovsrec_bridge *br_int,
+ struct shash *lport_to_iface)
{
int i;
+ bool changed = false;
- for (i = 0; i < ctx->br_int->n_ports; i++) {
- const struct ovsrec_port *port_rec = ctx->br_int->ports[i];
+ struct sset old_local_ids = SSET_INITIALIZER(&old_local_ids);
+ sset_clone(&old_local_ids, &local_ids);
+
+ for (i = 0; i < br_int->n_ports; i++) {
+ const struct ovsrec_port *port_rec = br_int->ports[i];
const char *iface_id;
int j;
- if (!strcmp(port_rec->name, ctx->br_int_name)) {
+ if (!strcmp(port_rec->name, br_int->name)) {
continue;
}
if (!iface_id) {
continue;
}
- sset_add(lports, iface_id);
+ shash_add(lport_to_iface, iface_id, iface_rec);
+ if (!sset_find_and_delete(&old_local_ids, iface_id)) {
+ sset_add(&local_ids, iface_id);
+ changed = true;
+ }
}
}
+
+ /* Any item left in old_local_ids is an ID for an interface
+ * that has been removed. */
+ if (!changed && !sset_is_empty(&old_local_ids)) {
+ changed = true;
+ }
+
+ sset_destroy(&old_local_ids);
+
+ return changed;
}
-void
-binding_run(struct controller_ctx *ctx)
+static struct local_datapath *
+local_datapath_lookup_by_uuid(struct hmap *hmap_p, const struct uuid *uuid)
{
- const struct sbrec_chassis *chassis_rec;
- const struct sbrec_binding *binding_rec;
- struct ovsdb_idl_txn *txn;
- struct sset lports, all_lports;
- const char *name;
- int retval;
+ struct local_datapath *ld;
+ HMAP_FOR_EACH_WITH_HASH(ld, uuid_hmap_node, uuid_hash(uuid), hmap_p) {
+ if (uuid_equals(&ld->uuid, uuid)) {
+ return ld;
+ }
+ }
+ return NULL;
+}
- chassis_rec = get_chassis_by_name(ctx->ovnsb_idl, ctx->chassis_id);
- if (!chassis_rec) {
+static void
+remove_local_datapath(struct hmap *local_datapaths, struct local_datapath *ld)
+{
+ if (ld->logical_port) {
+ free(ld->logical_port);
+ ld->logical_port = NULL;
+ }
+ hmap_remove(local_datapaths, &ld->hmap_node);
+ free(ld);
+}
+
+static void
+add_local_datapath(struct hmap *local_datapaths,
+ const struct sbrec_port_binding *binding_rec)
+{
+ if (get_local_datapath(local_datapaths,
+ binding_rec->datapath->tunnel_key)) {
return;
}
- sset_init(&lports);
- sset_init(&all_lports);
- get_local_iface_ids(ctx, &lports);
- sset_clone(&all_lports, &lports);
-
- txn = ovsdb_idl_txn_create(ctx->ovnsb_idl);
- ovsdb_idl_txn_add_comment(txn,
- "ovn-controller: updating bindings for '%s'",
- ctx->chassis_id);
-
- SBREC_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
- if (sset_find_and_delete(&lports, binding_rec->logical_port) ||
- (binding_rec->parent_port && binding_rec->parent_port[0] &&
- sset_contains(&all_lports, binding_rec->parent_port))) {
- if (binding_rec->chassis == chassis_rec) {
- continue;
- }
+ struct local_datapath *ld = xzalloc(sizeof *ld);
+ ld->logical_port = xstrdup(binding_rec->logical_port);
+ memcpy(&ld->uuid, &binding_rec->header_.uuid, sizeof ld->uuid);
+ hmap_insert(local_datapaths, &ld->hmap_node,
+ binding_rec->datapath->tunnel_key);
+}
+
+static void
+update_qos(const struct ovsrec_interface *iface_rec,
+ const struct sbrec_port_binding *pb)
+{
+ int rate = smap_get_int(&pb->options, "policing_rate", 0);
+ int burst = smap_get_int(&pb->options, "policing_burst", 0);
+
+ ovsrec_interface_set_ingress_policing_rate(iface_rec, MAX(0, rate));
+ ovsrec_interface_set_ingress_policing_burst(iface_rec, MAX(0, burst));
+}
+
+static void
+consider_local_datapath(struct controller_ctx *ctx,
+ const struct sbrec_chassis *chassis_rec,
+ const struct sbrec_port_binding *binding_rec,
+ struct hmap *local_datapaths,
+ struct shash *lport_to_iface)
+{
+ const struct ovsrec_interface *iface_rec
+ = shash_find_data(lport_to_iface, binding_rec->logical_port);
+
+ if (iface_rec
+ || (binding_rec->parent_port && binding_rec->parent_port[0] &&
+ sset_contains(&local_ids, binding_rec->parent_port))) {
+ add_local_datapath(local_datapaths, binding_rec);
+ if (iface_rec && ctx->ovs_idl_txn) {
+ update_qos(iface_rec, binding_rec);
+ }
+ if (binding_rec->chassis == chassis_rec) {
+ return;
+ }
+ if (ctx->ovnsb_idl_txn) {
if (binding_rec->chassis) {
- VLOG_INFO("Changing chassis for lport %s from %s to %s",
+ VLOG_INFO("Changing chassis for lport %s from %s to %s.",
binding_rec->logical_port,
binding_rec->chassis->name,
chassis_rec->name);
+ } else {
+ VLOG_INFO("Claiming lport %s for this chassis.",
+ binding_rec->logical_port);
}
- sbrec_binding_set_chassis(binding_rec, chassis_rec);
- } else if (binding_rec->chassis == chassis_rec) {
- sbrec_binding_set_chassis(binding_rec, NULL);
+ sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
+ }
+ } else if (!strcmp(binding_rec->type, "l2gateway")) {
+ const char *chassis_id = smap_get(&binding_rec->options,
+ "l2gateway-chassis");
+ if (!chassis_id || strcmp(chassis_id, chassis_rec->name)) {
+ if (binding_rec->chassis == chassis_rec && ctx->ovnsb_idl_txn) {
+ VLOG_INFO("Releasing l2gateway port %s from this chassis.",
+ binding_rec->logical_port);
+ sbrec_port_binding_set_chassis(binding_rec, NULL);
+ }
+ return;
}
- }
-
- retval = ovsdb_idl_txn_commit_block(txn);
- if (retval == TXN_ERROR) {
- VLOG_INFO("Problem committing binding information: %s",
- ovsdb_idl_txn_status_to_string(retval));
- }
- ovsdb_idl_txn_destroy(txn);
+ if (binding_rec->chassis == chassis_rec) {
+ return;
+ }
- SSET_FOR_EACH (name, &lports) {
- VLOG_DBG("No binding record for lport %s", name);
+ if (!strcmp(chassis_id, chassis_rec->name) && ctx->ovnsb_idl_txn) {
+ VLOG_INFO("Claiming l2gateway port %s for this chassis.",
+ binding_rec->logical_port);
+ sbrec_port_binding_set_chassis(binding_rec, chassis_rec);
+ add_local_datapath(local_datapaths, binding_rec);
+ }
+ } else if (chassis_rec && binding_rec->chassis == chassis_rec
+ && strcmp(binding_rec->type, "gateway")) {
+ if (ctx->ovnsb_idl_txn) {
+ VLOG_INFO("Releasing lport %s from this chassis.",
+ binding_rec->logical_port);
+ sbrec_port_binding_set_chassis(binding_rec, NULL);
+ }
}
- sset_destroy(&lports);
- sset_destroy(&all_lports);
}
void
-binding_destroy(struct controller_ctx *ctx)
+binding_run(struct controller_ctx *ctx, const struct ovsrec_bridge *br_int,
+ const char *chassis_id, struct hmap *local_datapaths)
{
const struct sbrec_chassis *chassis_rec;
- int retval = TXN_TRY_AGAIN;
-
- ovs_assert(ctx->ovnsb_idl);
+ const struct sbrec_port_binding *binding_rec;
+ struct shash lport_to_iface = SHASH_INITIALIZER(&lport_to_iface);
- chassis_rec = get_chassis_by_name(ctx->ovnsb_idl, ctx->chassis_id);
+ chassis_rec = get_chassis(ctx->ovnsb_idl, chassis_id);
if (!chassis_rec) {
return;
}
- while (retval != TXN_SUCCESS && retval != TXN_UNCHANGED) {
- const struct sbrec_binding *binding_rec;
- struct ovsdb_idl_txn *txn;
-
- txn = ovsdb_idl_txn_create(ctx->ovnsb_idl);
- ovsdb_idl_txn_add_comment(txn,
- "ovn-controller: removing all bindings for '%s'",
- ctx->chassis_id);
+ if (br_int) {
+ if (ctx->ovnsb_idl_txn && get_local_iface_ids(br_int, &lport_to_iface)) {
+ process_full_binding = true;
+ }
+ } else {
+ /* We have no integration bridge, therefore no local logical ports.
+ * We'll remove our chassis from all port binding records below. */
+ process_full_binding = true;
+ }
- SBREC_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
- if (binding_rec->chassis == chassis_rec) {
- sbrec_binding_set_chassis(binding_rec, NULL);
+ /* Run through each binding record to see if it is resident on this
+ * chassis and update the binding accordingly. This includes both
+ * directly connected logical ports and children of those ports. */
+ if (process_full_binding) {
+ struct hmap keep_local_datapath_by_uuid =
+ HMAP_INITIALIZER(&keep_local_datapath_by_uuid);
+ SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
+ consider_local_datapath(ctx, chassis_rec, binding_rec,
+ local_datapaths, &lport_to_iface);
+ struct local_datapath *ld = xzalloc(sizeof *ld);
+ memcpy(&ld->uuid, &binding_rec->header_.uuid, sizeof ld->uuid);
+ hmap_insert(&keep_local_datapath_by_uuid, &ld->uuid_hmap_node,
+ uuid_hash(&ld->uuid));
+ }
+ struct local_datapath *old_ld, *next;
+ HMAP_FOR_EACH_SAFE (old_ld, next, hmap_node, local_datapaths) {
+ if (!local_datapath_lookup_by_uuid(&keep_local_datapath_by_uuid,
+ &old_ld->uuid)) {
+ remove_local_datapath(local_datapaths, old_ld);
}
}
-
- retval = ovsdb_idl_txn_commit_block(txn);
- if (retval == TXN_ERROR) {
- VLOG_INFO("Problem removing bindings: %s",
- ovsdb_idl_txn_status_to_string(retval));
+ hmap_destroy(&keep_local_datapath_by_uuid);
+ process_full_binding = false;
+ } else {
+ SBREC_PORT_BINDING_FOR_EACH_TRACKED(binding_rec, ctx->ovnsb_idl) {
+ if (sbrec_port_binding_is_deleted(binding_rec)) {
+ /* If a port binding was bound to this chassis and removed before
+ * the ovs interface was removed, we'll catch that here and trigger
+ * a full bindings refresh. This is to see if we need to clear
+ * an entry out of local_datapaths. */
+ if (binding_rec->chassis == chassis_rec) {
+ process_full_binding = true;
+ poll_immediate_wake();
+ }
+ } else {
+ consider_local_datapath(ctx, chassis_rec, binding_rec,
+ local_datapaths, &lport_to_iface);
+ }
}
+ }
- ovsdb_idl_txn_destroy(txn);
+ shash_destroy(&lport_to_iface);
+}
+
+/* Returns true if the database is all cleaned up, false if more work is
+ * required. */
+bool
+binding_cleanup(struct controller_ctx *ctx, const char *chassis_id)
+{
+ if (!ctx->ovnsb_idl_txn) {
+ return false;
+ }
+
+ if (!chassis_id) {
+ return true;
+ }
+
+ const struct sbrec_chassis *chassis_rec
+ = get_chassis(ctx->ovnsb_idl, chassis_id);
+ if (!chassis_rec) {
+ return true;
+ }
+
+ ovsdb_idl_txn_add_comment(
+ ctx->ovnsb_idl_txn,
+ "ovn-controller: removing all port bindings for '%s'", chassis_id);
+
+ const struct sbrec_port_binding *binding_rec;
+ bool any_changes = false;
+ SBREC_PORT_BINDING_FOR_EACH(binding_rec, ctx->ovnsb_idl) {
+ if (binding_rec->chassis == chassis_rec) {
+ sbrec_port_binding_set_chassis(binding_rec, NULL);
+ any_changes = true;
+ }
}
+ return !any_changes;
}