+/* Returns true if 'filename' is known to be already open as a database,
+ * false if not.
+ *
+ * "False negatives" are possible. */
+static bool
+is_already_open(struct server_config *config OVS_UNUSED,
+ const char *filename OVS_UNUSED)
+{
+#ifndef _WIN32
+ struct stat s;
+
+ if (!stat(filename, &s)) {
+ struct shash_node *node;
+
+ SHASH_FOR_EACH (node, config->all_dbs) {
+ struct db *db = node->data;
+ struct stat s2;
+
+ if (!stat(db->filename, &s2)
+ && s.st_dev == s2.st_dev
+ && s.st_ino == s2.st_ino) {
+ return true;
+ }
+ }
+ }
+#endif /* !_WIN32 */
+
+ return false;
+}
+
+static char *
+open_db(struct server_config *config, const char *filename)
+{
+ struct ovsdb_error *db_error;
+ struct db *db;
+ char *error;
+
+ /* If we know that the file is already open, return a good error message.
+ * Otherwise, if the file is open, we'll fail later on with a harder to
+ * interpret file locking error. */
+ if (is_already_open(config, filename)) {
+ return xasprintf("%s: already open", filename);
+ }
+
+ db = xzalloc(sizeof *db);
+ db->filename = xstrdup(filename);
+
+ db_error = ovsdb_file_open(db->filename, false, &db->db, &db->file);
+ if (db_error) {
+ error = ovsdb_error_to_string(db_error);
+ } else if (!ovsdb_jsonrpc_server_add_db(config->jsonrpc, db->db)) {
+ error = xasprintf("%s: duplicate database name", db->db->schema->name);
+ } else {
+ shash_add_assert(config->all_dbs, db->db->schema->name, db);
+ return NULL;
+ }
+
+ ovsdb_error_destroy(db_error);
+ ovsdb_destroy(db->db);
+ free(db->filename);
+ free(db);
+ return error;
+}
+
+static const struct db *
+find_db(const struct shash *all_dbs, const char *db_name)
+{
+ struct shash_node *node;
+
+ SHASH_FOR_EACH(node, all_dbs) {
+ struct db *db = node->data;
+ if (!strcmp(db->db->schema->name, db_name)) {
+ return db;
+ }
+ }
+
+ return NULL;
+}
+
+static char * WARN_UNUSED_RESULT
+parse_db_column__(const struct shash *all_dbs,
+ const char *name_, char *name,
+ const struct db **dbp,
+ const struct ovsdb_table **tablep,
+ const struct ovsdb_column **columnp)
+{
+ const char *db_name, *table_name, *column_name;
+ const struct ovsdb_column *column;
+ const struct ovsdb_table *table;
+ const char *tokens[3];
+ char *save_ptr = NULL;
+ const struct db *db;
+
+ *dbp = NULL;
+ *tablep = NULL;
+ *columnp = NULL;
+
+ strtok_r(name, ":", &save_ptr); /* "db:" */
+ tokens[0] = strtok_r(NULL, ",", &save_ptr);
+ tokens[1] = strtok_r(NULL, ",", &save_ptr);
+ tokens[2] = strtok_r(NULL, ",", &save_ptr);
+ if (!tokens[0] || !tokens[1] || !tokens[2]) {
+ return xasprintf("\"%s\": invalid syntax", name_);
+ }
+
+ db_name = tokens[0];
+ table_name = tokens[1];
+ column_name = tokens[2];
+
+ db = find_db(all_dbs, tokens[0]);
+ if (!db) {
+ return xasprintf("\"%s\": no database named %s", name_, db_name);
+ }
+
+ table = ovsdb_get_table(db->db, table_name);
+ if (!table) {
+ return xasprintf("\"%s\": no table named %s", name_, table_name);
+ }
+
+ column = ovsdb_table_schema_get_column(table->schema, column_name);
+ if (!column) {
+ return xasprintf("\"%s\": table \"%s\" has no column \"%s\"",
+ name_, table_name, column_name);
+ }
+
+ *dbp = db;
+ *columnp = column;
+ *tablep = table;
+ return NULL;
+}
+
+/* Returns NULL if successful, otherwise a malloc()'d string describing the
+ * error. */
+static char * WARN_UNUSED_RESULT
+parse_db_column(const struct shash *all_dbs,
+ const char *name_,
+ const struct db **dbp,
+ const struct ovsdb_table **tablep,
+ const struct ovsdb_column **columnp)
+{
+ char *name = xstrdup(name_);
+ char *retval = parse_db_column__(all_dbs, name_, name,
+ dbp, tablep, columnp);
+ free(name);
+ return retval;
+}
+
+/* Returns NULL if successful, otherwise a malloc()'d string describing the
+ * error. */
+static char * WARN_UNUSED_RESULT
+parse_db_string_column(const struct shash *all_dbs,
+ const char *name,
+ const struct db **dbp,
+ const struct ovsdb_table **tablep,
+ const struct ovsdb_column **columnp)
+{
+ char *retval;
+
+ retval = parse_db_column(all_dbs, name, dbp, tablep, columnp);
+ if (retval) {
+ return retval;
+ }
+
+ if ((*columnp)->type.key.type != OVSDB_TYPE_STRING
+ || (*columnp)->type.value.type != OVSDB_TYPE_VOID) {
+ return xasprintf("\"%s\": table \"%s\" column \"%s\" is "
+ "not string or set of strings",
+ name, (*tablep)->schema->name, (*columnp)->name);
+ }
+
+ return NULL;
+}
+
+static const char *
+query_db_string(const struct shash *all_dbs, const char *name,
+ struct ds *errors)
+{
+ if (!name || strncmp(name, "db:", 3)) {
+ return name;
+ } else {
+ const struct ovsdb_column *column;
+ const struct ovsdb_table *table;
+ const struct ovsdb_row *row;
+ const struct db *db;
+ char *retval;
+
+ retval = parse_db_string_column(all_dbs, name,
+ &db, &table, &column);
+ if (retval) {
+ ds_put_format(errors, "%s\n", retval);
+ return NULL;
+ }
+
+ HMAP_FOR_EACH (row, hmap_node, &table->rows) {
+ const struct ovsdb_datum *datum;
+ size_t i;
+
+ datum = &row->fields[column->index];
+ for (i = 0; i < datum->n; i++) {
+ if (datum->keys[i].string[0]) {
+ return datum->keys[i].string;
+ }
+ }
+ }
+ return NULL;
+ }
+}
+
+static struct ovsdb_jsonrpc_options *
+add_remote(struct shash *remotes, const char *target)
+{
+ struct ovsdb_jsonrpc_options *options;
+
+ options = shash_find_data(remotes, target);
+ if (!options) {
+ options = ovsdb_jsonrpc_default_options(target);
+ shash_add(remotes, target, options);
+ }
+
+ return options;
+}
+
+static struct ovsdb_datum *
+get_datum(struct ovsdb_row *row, const char *column_name,
+ const enum ovsdb_atomic_type key_type,
+ const enum ovsdb_atomic_type value_type,
+ const size_t n_max)
+{
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
+ const struct ovsdb_table_schema *schema = row->table->schema;
+ const struct ovsdb_column *column;
+
+ column = ovsdb_table_schema_get_column(schema, column_name);
+ if (!column) {
+ VLOG_DBG_RL(&rl, "Table `%s' has no `%s' column",
+ schema->name, column_name);
+ return NULL;
+ }
+
+ if (column->type.key.type != key_type
+ || column->type.value.type != value_type
+ || column->type.n_max != n_max) {
+ if (!VLOG_DROP_DBG(&rl)) {
+ char *type_name = ovsdb_type_to_english(&column->type);
+ VLOG_DBG("Table `%s' column `%s' has type %s, not expected "
+ "key type %s, value type %s, max elements %"PRIuSIZE".",
+ schema->name, column_name, type_name,
+ ovsdb_atomic_type_to_string(key_type),
+ ovsdb_atomic_type_to_string(value_type),
+ n_max);
+ free(type_name);
+ }
+ return NULL;
+ }
+
+ return &row->fields[column->index];
+}
+
+/* Read string-string key-values from a map. Returns the value associated with
+ * 'key', if found, or NULL */
+static const char *
+read_map_string_column(const struct ovsdb_row *row, const char *column_name,
+ const char *key)
+{
+ const struct ovsdb_datum *datum;
+ union ovsdb_atom *atom_key = NULL, *atom_value = NULL;
+ size_t i;
+
+ datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name,
+ OVSDB_TYPE_STRING, OVSDB_TYPE_STRING, UINT_MAX);
+
+ if (!datum) {
+ return NULL;
+ }
+
+ for (i = 0; i < datum->n; i++) {
+ atom_key = &datum->keys[i];
+ if (!strcmp(atom_key->string, key)){
+ atom_value = &datum->values[i];
+ break;
+ }
+ }
+
+ return atom_value ? atom_value->string : NULL;
+}
+
+static const union ovsdb_atom *
+read_column(const struct ovsdb_row *row, const char *column_name,
+ enum ovsdb_atomic_type type)
+{
+ const struct ovsdb_datum *datum;
+
+ datum = get_datum(CONST_CAST(struct ovsdb_row *, row), column_name, type,
+ OVSDB_TYPE_VOID, 1);
+ return datum && datum->n ? datum->keys : NULL;
+}
+
+static bool
+read_integer_column(const struct ovsdb_row *row, const char *column_name,
+ long long int *integerp)
+{
+ const union ovsdb_atom *atom;
+
+ atom = read_column(row, column_name, OVSDB_TYPE_INTEGER);
+ *integerp = atom ? atom->integer : 0;
+ return atom != NULL;
+}
+
+static bool
+read_string_column(const struct ovsdb_row *row, const char *column_name,
+ const char **stringp)
+{
+ const union ovsdb_atom *atom;
+
+ atom = read_column(row, column_name, OVSDB_TYPE_STRING);
+ *stringp = atom ? atom->string : NULL;
+ return atom != NULL;
+}
+
+static void
+write_bool_column(struct ovsdb_row *row, const char *column_name, bool value)
+{
+ const struct ovsdb_column *column;
+ struct ovsdb_datum *datum;
+
+ column = ovsdb_table_schema_get_column(row->table->schema, column_name);
+ datum = get_datum(row, column_name, OVSDB_TYPE_BOOLEAN,
+ OVSDB_TYPE_VOID, 1);
+ if (!datum) {
+ return;
+ }
+
+ if (datum->n != 1) {
+ ovsdb_datum_destroy(datum, &column->type);
+
+ datum->n = 1;
+ datum->keys = xmalloc(sizeof *datum->keys);
+ datum->values = NULL;
+ }
+
+ datum->keys[0].boolean = value;
+}
+
+static void
+write_string_string_column(struct ovsdb_row *row, const char *column_name,
+ char **keys, char **values, size_t n)
+{
+ const struct ovsdb_column *column;
+ struct ovsdb_datum *datum;
+ size_t i;
+
+ column = ovsdb_table_schema_get_column(row->table->schema, column_name);
+ datum = get_datum(row, column_name, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING,
+ UINT_MAX);
+ if (!datum) {
+ for (i = 0; i < n; i++) {
+ free(keys[i]);
+ free(values[i]);
+ }
+ return;
+ }
+
+ /* Free existing data. */
+ ovsdb_datum_destroy(datum, &column->type);
+
+ /* Allocate space for new values. */
+ datum->n = n;
+ datum->keys = xmalloc(n * sizeof *datum->keys);
+ datum->values = xmalloc(n * sizeof *datum->values);
+
+ for (i = 0; i < n; ++i) {
+ datum->keys[i].string = keys[i];
+ datum->values[i].string = values[i];
+ }
+
+ /* Sort and check constraints. */
+ ovsdb_datum_sort_assert(datum, column->type.key.type);
+}
+
+/* Adds a remote and options to 'remotes', based on the Manager table row in
+ * 'row'. */
+static void
+add_manager_options(struct shash *remotes, const struct ovsdb_row *row)
+{
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 1);
+ struct ovsdb_jsonrpc_options *options;
+ long long int max_backoff, probe_interval;
+ const char *target, *dscp_string;
+
+ if (!read_string_column(row, "target", &target) || !target) {
+ VLOG_INFO_RL(&rl, "Table `%s' has missing or invalid `target' column",
+ row->table->schema->name);
+ return;
+ }
+
+ options = add_remote(remotes, target);
+ if (read_integer_column(row, "max_backoff", &max_backoff)) {
+ options->max_backoff = max_backoff;
+ }
+ if (read_integer_column(row, "inactivity_probe", &probe_interval)) {
+ options->probe_interval = probe_interval;
+ }
+
+ options->dscp = DSCP_DEFAULT;
+ dscp_string = read_map_string_column(row, "other_config", "dscp");
+ if (dscp_string) {
+ int dscp = atoi(dscp_string);
+ if (dscp >= 0 && dscp <= 63) {
+ options->dscp = dscp;
+ }
+ }
+}
+
+static void
+query_db_remotes(const char *name, const struct shash *all_dbs,
+ struct shash *remotes, struct ds *errors)
+{
+ const struct ovsdb_column *column;
+ const struct ovsdb_table *table;
+ const struct ovsdb_row *row;
+ const struct db *db;
+ char *retval;
+
+ retval = parse_db_column(all_dbs, name, &db, &table, &column);
+ if (retval) {
+ ds_put_format(errors, "%s\n", retval);
+ free(retval);
+ return;
+ }
+
+ if (column->type.key.type == OVSDB_TYPE_STRING
+ && column->type.value.type == OVSDB_TYPE_VOID) {
+ HMAP_FOR_EACH (row, hmap_node, &table->rows) {
+ const struct ovsdb_datum *datum;
+ size_t i;
+
+ datum = &row->fields[column->index];
+ for (i = 0; i < datum->n; i++) {
+ add_remote(remotes, datum->keys[i].string);
+ }
+ }
+ } else if (column->type.key.type == OVSDB_TYPE_UUID
+ && column->type.key.u.uuid.refTable
+ && column->type.value.type == OVSDB_TYPE_VOID) {
+ const struct ovsdb_table *ref_table = column->type.key.u.uuid.refTable;
+ HMAP_FOR_EACH (row, hmap_node, &table->rows) {
+ const struct ovsdb_datum *datum;
+ size_t i;
+
+ datum = &row->fields[column->index];
+ for (i = 0; i < datum->n; i++) {
+ const struct ovsdb_row *ref_row;
+
+ ref_row = ovsdb_table_get_row(ref_table, &datum->keys[i].uuid);
+ if (ref_row) {
+ add_manager_options(remotes, ref_row);
+ }
+ }
+ }
+ }
+}
+