-/* Copyright (c) 2009, 2010 Nicira Networks
+/* Copyright (c) 2009, 2010, 2011, 2012, 2014, 2016, 2017 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 "ovsdb-data.h"
-#include <assert.h>
#include <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
-#include "dynamic-string.h"
+#include "openvswitch/dynamic-string.h"
#include "hash.h"
+#include "ovs-thread.h"
#include "ovsdb-error.h"
-#include "json.h"
-#include "shash.h"
+#include "ovsdb-parser.h"
+#include "openvswitch/json.h"
+#include "openvswitch/shash.h"
+#include "smap.h"
#include "sort.h"
#include "unicode.h"
+#include "util.h"
static struct json *
wrap_json(const char *name, struct json *wrapped)
/* Initializes 'atom' with the default value of the given 'type'.
*
- * The default value for an atom is as defined in ovsdb/SPECS:
+ * The default value for an atom is as defined in RFC 7047:
*
* - "integer" or "real": 0
*
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
atom->integer = 0;
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
ovsdb_atom_default(enum ovsdb_atomic_type type)
{
static union ovsdb_atom default_atoms[OVSDB_N_TYPES];
- static bool inited;
+ static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
- if (!inited) {
+ if (ovsthread_once_start(&once)) {
int i;
for (i = 0; i < OVSDB_N_TYPES; i++) {
ovsdb_atom_init_default(&default_atoms[i], i);
}
}
- inited = true;
+ ovsthread_once_done(&once);
}
- assert(ovsdb_atomic_type_is_valid(type));
+ ovs_assert(ovsdb_atomic_type_is_valid(type));
return &default_atoms[type];
}
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return atom->integer == 0;
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
new->integer = old->integer;
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return hash_int(atom->integer, basis);
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return a->integer < b->integer ? -1 : a->integer > b->integer;
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
|| (name && strcmp(json->u.array.elems[0]->u.string, name))
|| json->u.array.elems[1]->type != value_type)
{
+ *value = NULL;
return ovsdb_syntax_error(json, NULL, "expected [\"%s\", <%s>]", name,
json_type_to_string(value_type));
}
return NULL;
}
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static void
+ovsdb_symbol_referenced(struct ovsdb_symbol *symbol,
+ const struct ovsdb_base_type *base)
+{
+ ovs_assert(base->type == OVSDB_TYPE_UUID);
+
+ if (base->u.uuid.refTableName) {
+ switch (base->u.uuid.refType) {
+ case OVSDB_REF_STRONG:
+ symbol->strong_ref = true;
+ break;
+ case OVSDB_REF_WEAK:
+ symbol->weak_ref = true;
+ break;
+ }
+ }
+}
+
+static union ovsdb_atom *
+alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
+{
+ if (type != OVSDB_TYPE_VOID && n) {
+ union ovsdb_atom *atoms;
+ unsigned int i;
+
+ atoms = xmalloc(n * sizeof *atoms);
+ for (i = 0; i < n; i++) {
+ ovsdb_atom_init_default(&atoms[i], type);
+ }
+ return atoms;
+ } else {
+ /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
+ * treated as xmalloc(1). */
+ return NULL;
+ }
+}
+
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json,
- struct ovsdb_symbol_table *symtab)
+ struct ovsdb_symbol_table *symtab,
+ const struct ovsdb_base_type *base)
{
struct ovsdb_error *error0;
const struct json *value;
error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value);
if (!error1) {
- const char *name = json_string(value);
+ struct ovsdb_symbol *symbol;
ovsdb_error_destroy(error0);
- *uuid = ovsdb_symbol_table_insert(symtab, name)->uuid;
+ if (!ovsdb_parser_is_id(json_string(value))) {
+ return ovsdb_syntax_error(json, NULL, "named-uuid string is "
+ "not a valid <id>");
+ }
+
+ symbol = ovsdb_symbol_table_insert(symtab, json_string(value));
+ *uuid = symbol->uuid;
+ ovsdb_symbol_referenced(symbol, base);
return NULL;
}
ovsdb_error_destroy(error1);
return error0;
}
-static struct ovsdb_error * WARN_UNUSED_RESULT
-ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
+ovsdb_atom_from_json__(union ovsdb_atom *atom,
+ const struct ovsdb_base_type *base,
const struct json *json,
struct ovsdb_symbol_table *symtab)
{
+ enum ovsdb_atomic_type type = base->type;
+
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
if (json->type == JSON_INTEGER) {
break;
case OVSDB_TYPE_UUID:
- return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab);
+ return ovsdb_atom_parse_uuid(&atom->uuid, json, symtab, base);
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
return ovsdb_syntax_error(json, NULL, "expected %s",
* Violations of constraints expressed by 'base' are treated as errors.
*
* If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
- * ovsdb/SPECS for information about this, and for the syntax that this
- * function accepts. */
+ * RFC 7047 for information about this, and for the syntax that this function
+ * accepts. If 'base' is a reference and a symbol is parsed, then the symbol's
+ * 'strong_ref' or 'weak_ref' member is set to true, as appropriate. */
struct ovsdb_error *
ovsdb_atom_from_json(union ovsdb_atom *atom,
const struct ovsdb_base_type *base,
{
struct ovsdb_error *error;
- error = ovsdb_atom_from_json__(atom, base->type, json, symtab);
+ error = ovsdb_atom_from_json__(atom, base, json, symtab);
if (error) {
return error;
}
/* Converts 'atom', of the specified 'type', to JSON format, and returns the
* JSON. The caller is responsible for freeing the returned JSON.
*
- * Refer to ovsdb/SPECS for the format of the JSON that this function
- * produces. */
+ * Refer to RFC 7047 for the format of the JSON that this function produces. */
struct json *
ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
return json_integer_create(atom->integer);
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
static char *
-ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
- const char *s, struct ovsdb_symbol_table *symtab)
+ovsdb_atom_from_string__(union ovsdb_atom *atom,
+ union ovsdb_atom **range_end_atom,
+ const struct ovsdb_base_type *base, const char *s,
+ struct ovsdb_symbol_table *symtab)
{
+ enum ovsdb_atomic_type type = base->type;
+
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER: {
- long long int integer;
- if (!str_to_llong(s, 10, &integer)) {
- return xasprintf("\"%s\" is not a valid integer", s);
+ long long int integer, end;
+ if (range_end_atom
+ && str_to_llong_range(s, 10, &integer, &end)) {
+ if (end < integer) {
+ return xasprintf("\"%s\" is not a valid range. "
+ "Range end cannot be before start.", s);
+ }
+ *range_end_atom = alloc_default_atoms(type, 1);
+ if (!(*range_end_atom)) {
+ return xasprintf("\"%s\" is not a valid range", s);
+ }
+ (*range_end_atom)->integer = end;
+ } else if (!str_to_llong(s, 10, &integer)) {
+ return xasprintf("\"%s\" is not a valid integer or range", s);
}
atom->integer = integer;
}
case OVSDB_TYPE_UUID:
if (*s == '@') {
- atom->uuid = ovsdb_symbol_table_insert(symtab, s)->uuid;
+ struct ovsdb_symbol *symbol = ovsdb_symbol_table_insert(symtab, s);
+ atom->uuid = symbol->uuid;
+ ovsdb_symbol_referenced(symbol, base);
} else if (!uuid_from_string(&atom->uuid, s)) {
return xasprintf("\"%s\" is not a valid UUID", s);
}
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
return NULL;
}
-/* Initializes 'atom' to a value of type 'base' parsed from 's', which takes
- * one of the following forms:
+/* Initializes 'atom' and optionally 'range_end_atom' to a value of type 'base'
+ * parsed from 's', which takes one of the following forms:
*
- * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign.
+ * - OVSDB_TYPE_INTEGER: A decimal integer optionally preceded by a sign
+ * or two decimal integers optionally preceded by a sign and separated
+ * by a hyphen, representing inclusive range of integers
+ * ['atom', 'range_end_atom'].
*
* - OVSDB_TYPE_REAL: A floating-point number in the format accepted by
* strtod().
* then an identifier beginning with '@' is also acceptable. If the
* named identifier is already in 'symtab', then the associated UUID is
* used; otherwise, a new, random UUID is used and added to the symbol
- * table.
+ * table. If 'base' is a reference and a symbol is parsed, then the
+ * symbol's 'strong_ref' or 'weak_ref' member is set to true, as
+ * appropriate.
*
* Returns a null pointer if successful, otherwise an error message describing
* the problem. On failure, the contents of 'atom' are indeterminate. The
* caller is responsible for freeing the atom or the error.
+ *
+ * Does not attempt to parse range if 'range_end_atom' is a null pointer.
+ * Dynamically allocates ovdsb_atom and stores its address in '*range_end_atom'
+ * if successfully parses range. Caller is responsible for deallocating
+ * the memory by calling 'ovsdb_atom_destroy' and then 'free' on the address.
+ * Does not allocate memory and sets '*range_end_atom' to a null pointer
+ * if does not parse a range or fails for any reason.
*/
char *
ovsdb_atom_from_string(union ovsdb_atom *atom,
+ union ovsdb_atom **range_end_atom,
const struct ovsdb_base_type *base, const char *s,
struct ovsdb_symbol_table *symtab)
{
struct ovsdb_error *error;
char *msg;
- msg = ovsdb_atom_from_string__(atom, base->type, s, symtab);
+ if (range_end_atom) {
+ *range_end_atom = NULL;
+ }
+
+ msg = ovsdb_atom_from_string__(atom, range_end_atom, base, s, symtab);
if (msg) {
return msg;
}
error = ovsdb_atom_check_constraints(atom, base);
+
+ if (!error && range_end_atom && *range_end_atom) {
+ /* Check range constraints */
+ int64_t start = atom->integer;
+ int64_t end = (*range_end_atom)->integer;
+ if (base->enum_) {
+ for (int64_t i = start + 1; i <= end; i++) {
+ union ovsdb_atom ai = { .integer = i };
+ error = ovsdb_atom_check_constraints(&ai, base);
+ if (error) {
+ break;
+ }
+ }
+ } else {
+ error = ovsdb_atom_check_constraints(*range_end_atom, base);
+ }
+
+ if (!error) {
+ error = ovsdb_atom_range_check_size(start, end);
+ }
+ }
+
if (error) {
- msg = ovsdb_error_to_string(error);
- ovsdb_error_destroy(error);
+ ovsdb_atom_destroy(atom, base->type);
+ if (range_end_atom && *range_end_atom) {
+ ovsdb_atom_destroy(*range_end_atom, base->type);
+ free(*range_end_atom);
+ *range_end_atom = NULL;
+ }
+ msg = ovsdb_error_to_string_free(error);
}
return msg;
}
{
switch (type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
ds_put_format(out, "%"PRId64, atom->integer);
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
+ }
+}
+
+/* Appends 'atom' (which has the given 'type') to 'out', in a bare string
+ * format that cannot be parsed uniformly back into a datum but is easier for
+ * shell scripts, etc., to deal with. */
+void
+ovsdb_atom_to_bare(const union ovsdb_atom *atom, enum ovsdb_atomic_type type,
+ struct ds *out)
+{
+ if (type == OVSDB_TYPE_STRING) {
+ ds_put_cstr(out, atom->string);
+ } else {
+ ovsdb_atom_to_string(atom, type, out);
}
}
struct ovsdb_error *error;
error = ovsdb_error("constraint violation",
- "\"%s\" is not a valid UTF-8 string: %s",
- s, msg);
+ "not a valid UTF-8 string: %s", msg);
free(msg);
return error;
}
if (n_chars < c->minLen) {
return ovsdb_error(
"constraint violation",
- "\"%s\" length %zu is less than minimum allowed "
+ "\"%s\" length %"PRIuSIZE" is less than minimum allowed "
"length %u", s, n_chars, c->minLen);
} else if (n_chars > c->maxLen) {
return ovsdb_error(
"constraint violation",
- "\"%s\" length %zu is greater than maximum allowed "
+ "\"%s\" length %"PRIuSIZE" is greater than maximum allowed "
"length %u", s, n_chars, c->maxLen);
}
switch (base->type) {
case OVSDB_TYPE_VOID:
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_INTEGER:
if (atom->integer >= base->u.integer.min
"value %"PRId64,
atom->integer, base->u.integer.max);
}
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_REAL:
if (atom->real >= base->u.real.min && atom->real <= base->u.real.max) {
DBL_DIG, atom->real,
DBL_DIG, base->u.real.max);
}
- NOT_REACHED();
+ OVS_NOT_REACHED();
case OVSDB_TYPE_BOOLEAN:
return NULL;
case OVSDB_N_TYPES:
default:
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
\f
-static union ovsdb_atom *
-alloc_default_atoms(enum ovsdb_atomic_type type, size_t n)
-{
- if (type != OVSDB_TYPE_VOID && n) {
- union ovsdb_atom *atoms;
- unsigned int i;
-
- atoms = xmalloc(n * sizeof *atoms);
- for (i = 0; i < n; i++) {
- ovsdb_atom_init_default(&atoms[i], type);
- }
- return atoms;
- } else {
- /* Avoid wasting memory in the n == 0 case, because xmalloc(0) is
- * treated as xmalloc(1). */
- return NULL;
- }
-}
-
/* Initializes 'datum' as an empty datum. (An empty datum can be treated as
* any type.) */
void
/* Initializes 'datum' as a datum that has the default value for 'type'.
*
- * The default value for a particular type is as defined in ovsdb/SPECS:
+ * The default value for a particular type is as defined in RFC 7047:
*
* - If n_min is 0, then the default value is the empty set (or map).
*
int kt = type->key.type;
int vt = type->value.type;
- assert(ovsdb_type_is_valid(type));
+ ovs_assert(ovsdb_type_is_valid(type));
d = &default_data[kt][vt];
if (!d->n) {
d->n = 1;
- d->keys = (union ovsdb_atom *) ovsdb_atom_default(kt);
+ d->keys = CONST_CAST(union ovsdb_atom *, ovsdb_atom_default(kt));
if (vt != OVSDB_TYPE_VOID) {
- d->values = (union ovsdb_atom *) ovsdb_atom_default(vt);
+ d->values = CONST_CAST(union ovsdb_atom *,
+ ovsdb_atom_default(vt));
}
}
return d;
} else {
- NOT_REACHED();
+ OVS_NOT_REACHED();
}
}
struct ovsdb_datum_sort_cbdata {
enum ovsdb_atomic_type key_type;
+ enum ovsdb_atomic_type value_type;
struct ovsdb_datum *datum;
};
ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_)
{
struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
+ int retval;
+
+ retval = ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
+ &cbdata->datum->keys[b],
+ cbdata->key_type);
+ if (retval || cbdata->value_type == OVSDB_TYPE_VOID) {
+ return retval;
+ }
- return ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
- &cbdata->datum->keys[b],
- cbdata->key_type);
+ return ovsdb_atom_compare_3way(&cbdata->datum->values[a],
+ &cbdata->datum->values[b],
+ cbdata->value_type);
}
static void
}
}
+static void
+ovsdb_datum_sort__(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type,
+ enum ovsdb_atomic_type value_type)
+{
+ struct ovsdb_datum_sort_cbdata cbdata;
+
+ cbdata.key_type = key_type;
+ cbdata.value_type = value_type;
+ cbdata.datum = datum;
+ sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
+ &cbdata);
+}
+
/* The keys in an ovsdb_datum must be unique and in sorted order. Most
* functions that modify an ovsdb_datum maintain these invariants. For those
* that don't, this function checks and restores these invariants for 'datum',
struct ovsdb_error *
ovsdb_datum_sort(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type)
{
+ size_t i;
+
if (datum->n < 2) {
return NULL;
- } else {
- struct ovsdb_datum_sort_cbdata cbdata;
- size_t i;
+ }
- cbdata.key_type = key_type;
- cbdata.datum = datum;
- sort(datum->n, ovsdb_datum_sort_compare_cb, ovsdb_datum_sort_swap_cb,
- &cbdata);
-
- for (i = 0; i < datum->n - 1; i++) {
- if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
- key_type)) {
- if (datum->values) {
- return ovsdb_error(NULL, "map contains duplicate key");
- } else {
- return ovsdb_error(NULL, "set contains duplicate");
- }
+ ovsdb_datum_sort__(datum, key_type, OVSDB_TYPE_VOID);
+
+ for (i = 0; i < datum->n - 1; i++) {
+ if (ovsdb_atom_equals(&datum->keys[i], &datum->keys[i + 1],
+ key_type)) {
+ if (datum->values) {
+ return ovsdb_error(NULL, "map contains duplicate key");
+ } else {
+ return ovsdb_error(NULL, "set contains duplicate");
}
}
-
- return NULL;
}
+ return NULL;
}
/* This function is the same as ovsdb_datum_sort(), except that the caller
{
struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type);
if (error) {
- NOT_REACHED();
+ OVS_NOT_REACHED();
+ }
+}
+
+/* This is similar to ovsdb_datum_sort(), except that it drops duplicate keys
+ * instead of reporting an error. In a map type, the smallest value among a
+ * group of duplicate pairs is retained and the others are dropped.
+ *
+ * Returns the number of keys (or pairs) that were dropped. */
+size_t
+ovsdb_datum_sort_unique(struct ovsdb_datum *datum,
+ enum ovsdb_atomic_type key_type,
+ enum ovsdb_atomic_type value_type)
+{
+ size_t src, dst;
+
+ if (datum->n < 2) {
+ return 0;
+ }
+
+ ovsdb_datum_sort__(datum, key_type, value_type);
+
+ dst = 1;
+ for (src = 1; src < datum->n; src++) {
+ if (ovsdb_atom_equals(&datum->keys[src], &datum->keys[dst - 1],
+ key_type)) {
+ ovsdb_atom_destroy(&datum->keys[src], key_type);
+ if (value_type != OVSDB_TYPE_VOID) {
+ ovsdb_atom_destroy(&datum->values[src], value_type);
+ }
+ } else {
+ if (src != dst) {
+ datum->keys[dst] = datum->keys[src];
+ if (value_type != OVSDB_TYPE_VOID) {
+ datum->values[dst] = datum->values[src];
+ }
+ }
+ dst++;
+ }
}
+ datum->n = dst;
+ return datum->n - src;
}
/* Checks that each of the atoms in 'datum' conforms to the constraints
return NULL;
}
-/* Parses 'json' as a datum of the type described by 'type'. If successful,
- * returns NULL and initializes 'datum' with the parsed datum. On failure,
- * returns an error and the contents of 'datum' are indeterminate. The caller
- * is responsible for freeing the error or the datum that is returned.
- *
- * Violations of constraints expressed by 'type' are treated as errors.
- *
- * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
- * ovsdb/SPECS for information about this, and for the syntax that this
- * function accepts. */
-struct ovsdb_error *
-ovsdb_datum_from_json(struct ovsdb_datum *datum,
- const struct ovsdb_type *type,
- const struct json *json,
- struct ovsdb_symbol_table *symtab)
+static struct ovsdb_error *
+ovsdb_datum_from_json__(struct ovsdb_datum *datum,
+ const struct ovsdb_type *type,
+ const struct json *json,
+ struct ovsdb_symbol_table *symtab)
{
struct ovsdb_error *error;
n = inner->u.array.n;
if (n < type->n_min || n > type->n_max) {
return ovsdb_syntax_error(json, NULL, "%s must have %u to "
- "%u members but %zu are present",
+ "%u members but %"PRIuSIZE" are present",
class, type->n_min, type->n_max, n);
}
datum->n++;
}
-
- error = ovsdb_datum_sort(datum, type->key.type);
- if (error) {
- goto error;
- }
-
return NULL;
error:
}
}
-
-/* Converts 'datum', of the specified 'type', to JSON format, and returns the
- * JSON. The caller is responsible for freeing the returned JSON.
+/* Parses 'json' as a datum of the type described by 'type'. If successful,
+ * returns NULL and initializes 'datum' with the parsed datum. On failure,
+ * returns an error and the contents of 'datum' are indeterminate. The caller
+ * is responsible for freeing the error or the datum that is returned.
*
- * 'type' constraints on datum->n are ignored.
+ * Violations of constraints expressed by 'type' are treated as errors.
*
- * Refer to ovsdb/SPECS for the format of the JSON that this function
- * produces. */
-struct json *
-ovsdb_datum_to_json(const struct ovsdb_datum *datum,
- const struct ovsdb_type *type)
+ * If 'symtab' is nonnull, then named UUIDs in 'symtab' are accepted. Refer to
+ * RFC 7047 for information about this, and for the syntax that this function
+ * accepts. */
+struct ovsdb_error * OVS_WARN_UNUSED_RESULT
+ovsdb_datum_from_json(struct ovsdb_datum *datum,
+ const struct ovsdb_type *type,
+ const struct json *json,
+ struct ovsdb_symbol_table *symtab)
+{
+ struct ovsdb_error *error;
+
+ error = ovsdb_datum_from_json__(datum, type, json, symtab);
+ if (error) {
+ return error;
+ }
+
+ error = ovsdb_datum_sort(datum, type->key.type);
+ if (error) {
+ ovsdb_datum_destroy(datum, type);
+ }
+ return error;
+}
+
+/* Parses 'json' as a datum of the type described by 'type' for internal
+ * use. This function is similar to 'ovsdb_datum_from_json', except the
+ * member size of set or map is not checked.
+ *
+ * The datum generated should be used then discard. It is not suitable
+ * for storing into IDL because of the possible member size violation. */
+struct ovsdb_error * OVS_WARN_UNUSED_RESULT
+ovsdb_transient_datum_from_json(struct ovsdb_datum *datum,
+ const struct ovsdb_type *type,
+ const struct json *json)
+{
+ struct ovsdb_type relaxed_type = *type;
+
+ relaxed_type.n_min = 0;
+ relaxed_type.n_max = UINT_MAX;
+
+ return ovsdb_datum_from_json(datum, &relaxed_type, json, NULL);
+}
+
+/* Parses 'json' as a datum of the type described by 'type', but ignoring all
+ * constraints. */
+struct ovsdb_error * OVS_WARN_UNUSED_RESULT
+ovsdb_unconstrained_datum_from_json(struct ovsdb_datum *datum,
+ const struct ovsdb_type *type,
+ const struct json *json)
+{
+ struct ovsdb_type relaxed_type;
+
+ ovsdb_base_type_init(&relaxed_type.key, type->key.type);
+ ovsdb_base_type_init(&relaxed_type.value, type->value.type);
+ relaxed_type.n_min = 0;
+ relaxed_type.n_max = UINT_MAX;
+
+ return ovsdb_datum_from_json(datum, &relaxed_type, json, NULL);
+}
+
+static struct json *
+ovsdb_base_to_json(const union ovsdb_atom *atom,
+ const struct ovsdb_base_type *base,
+ bool use_row_names)
+{
+ if (!use_row_names
+ || base->type != OVSDB_TYPE_UUID
+ || !base->u.uuid.refTableName) {
+ return ovsdb_atom_to_json(atom, base->type);
+ } else {
+ return json_array_create_2(
+ json_string_create("named-uuid"),
+ json_string_create_nocopy(ovsdb_data_row_name(&atom->uuid)));
+ }
+}
+
+static struct json *
+ovsdb_datum_to_json__(const struct ovsdb_datum *datum,
+ const struct ovsdb_type *type,
+ bool use_row_names)
{
- if (datum->n == 1 && !ovsdb_type_is_map(type)) {
- return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
- } else if (type->value.type == OVSDB_TYPE_VOID) {
+ if (ovsdb_type_is_map(type)) {
struct json **elems;
size_t i;
elems = xmalloc(datum->n * sizeof *elems);
for (i = 0; i < datum->n; i++) {
- elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
+ elems[i] = json_array_create_2(
+ ovsdb_base_to_json(&datum->keys[i], &type->key,
+ use_row_names),
+ ovsdb_base_to_json(&datum->values[i], &type->value,
+ use_row_names));
}
- return wrap_json("set", json_array_create(elems, datum->n));
+ return wrap_json("map", json_array_create(elems, datum->n));
+ } else if (datum->n == 1) {
+ return ovsdb_base_to_json(&datum->keys[0], &type->key, use_row_names);
} else {
struct json **elems;
size_t i;
elems = xmalloc(datum->n * sizeof *elems);
for (i = 0; i < datum->n; i++) {
- elems[i] = json_array_create_2(
- ovsdb_atom_to_json(&datum->keys[i], type->key.type),
- ovsdb_atom_to_json(&datum->values[i], type->value.type));
+ elems[i] = ovsdb_base_to_json(&datum->keys[i], &type->key,
+ use_row_names);
}
- return wrap_json("map", json_array_create(elems, datum->n));
+ return wrap_json("set", json_array_create(elems, datum->n));
}
}
+/* Converts 'datum', of the specified 'type', to JSON format, and returns the
+ * JSON. The caller is responsible for freeing the returned JSON.
+ *
+ * 'type' constraints on datum->n are ignored.
+ *
+ * Refer to RFC 7047 for the format of the JSON that this function produces. */
+struct json *
+ovsdb_datum_to_json(const struct ovsdb_datum *datum,
+ const struct ovsdb_type *type)
+{
+ return ovsdb_datum_to_json__(datum, type, false);
+}
+
+struct json *
+ovsdb_datum_to_json_with_row_names(const struct ovsdb_datum *datum,
+ const struct ovsdb_type *type)
+{
+ return ovsdb_datum_to_json__(datum, type, true);
+}
+
static const char *
skip_spaces(const char *p)
{
static char *
parse_atom_token(const char **s, const struct ovsdb_base_type *base,
- union ovsdb_atom *atom, struct ovsdb_symbol_table *symtab)
+ union ovsdb_atom *atom, union ovsdb_atom **range_end_atom,
+ struct ovsdb_symbol_table *symtab)
{
char *token, *error;
error = ovsdb_token_parse(s, &token);
if (!error) {
- error = ovsdb_atom_from_string(atom, base, token, symtab);
+ error = ovsdb_atom_from_string(atom, range_end_atom,
+ base, token, symtab);
free(token);
}
return error;
static char *
parse_key_value(const char **s, const struct ovsdb_type *type,
union ovsdb_atom *key, union ovsdb_atom *value,
- struct ovsdb_symbol_table *symtab)
+ struct ovsdb_symbol_table *symtab,
+ union ovsdb_atom **range_end_key)
{
const char *start = *s;
char *error;
- error = parse_atom_token(s, &type->key, key, symtab);
+ error = parse_atom_token(s, &type->key, key, range_end_key, symtab);
+
if (!error && type->value.type != OVSDB_TYPE_VOID) {
*s = skip_spaces(*s);
if (**s == '=') {
(*s)++;
*s = skip_spaces(*s);
- error = parse_atom_token(s, &type->value, value, symtab);
+ error = parse_atom_token(s, &type->value, value, NULL, symtab);
} else {
error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
start, **s);
}
if (error) {
ovsdb_atom_destroy(key, type->key.type);
+ if (range_end_key && *range_end_key) {
+ ovsdb_atom_destroy(*range_end_key, type->key.type);
+ free(*range_end_key);
+ *range_end_key = NULL;
+ }
}
}
return error;
}
static void
-free_key_value(const struct ovsdb_type *type,
- union ovsdb_atom *key, union ovsdb_atom *value)
+free_key_value_range(const struct ovsdb_type *type,
+ union ovsdb_atom *key, union ovsdb_atom *value,
+ union ovsdb_atom **range_end_atom)
{
ovsdb_atom_destroy(key, type->key.type);
if (type->value.type != OVSDB_TYPE_VOID) {
ovsdb_atom_destroy(value, type->value.type);
}
+ if (range_end_atom && *range_end_atom) {
+ ovsdb_atom_destroy(*range_end_atom, type->key.type);
+ free(*range_end_atom);
+ *range_end_atom = NULL;
+ }
}
/* Initializes 'datum' as a datum of the given 'type', parsing its contents
while (*p && *p != end_delim) {
union ovsdb_atom key, value;
+ union ovsdb_atom *range_end_key = NULL;
if (ovsdb_token_is_delim(*p)) {
+ char *type_str = ovsdb_type_to_english(type);
error = xasprintf("%s: unexpected \"%c\" parsing %s",
- s, *p, ovsdb_type_to_english(type));
+ s, *p, type_str);
+ free(type_str);
goto error;
}
/* Add to datum. */
- error = parse_key_value(&p, type, &key, &value, symtab);
+ error = parse_key_value(&p, type, &key, &value,
+ symtab, &range_end_key);
if (error) {
goto error;
}
- ovsdb_datum_add_unsafe(datum, &key, &value, type);
- free_key_value(type, &key, &value);
+ ovsdb_datum_add_unsafe(datum, &key, &value, type, range_end_key);
+ free_key_value_range(type, &key, &value, &range_end_key);
/* Skip optional white space and comma. */
p = skip_spaces(p);
}
}
+/* Appends to 'out' the 'datum' (with the given 'type') in a bare string format
+ * that cannot be parsed uniformly back into a datum but is easier for shell
+ * scripts, etc., to deal with. */
+void
+ovsdb_datum_to_bare(const struct ovsdb_datum *datum,
+ const struct ovsdb_type *type, struct ds *out)
+{
+ bool is_map = ovsdb_type_is_map(type);
+ size_t i;
+
+ for (i = 0; i < datum->n; i++) {
+ if (i > 0) {
+ ds_put_cstr(out, " ");
+ }
+
+ ovsdb_atom_to_bare(&datum->keys[i], type->key.type, out);
+ if (is_map) {
+ ds_put_char(out, '=');
+ ovsdb_atom_to_bare(&datum->values[i], type->value.type, out);
+ }
+ }
+}
+
+/* Initializes 'datum' as a string-to-string map whose contents are copied from
+ * 'smap', which is not modified. */
+void
+ovsdb_datum_from_smap(struct ovsdb_datum *datum, const struct smap *smap)
+{
+ datum->n = smap_count(smap);
+ datum->keys = xmalloc(datum->n * sizeof *datum->keys);
+ datum->values = xmalloc(datum->n * sizeof *datum->values);
+
+ struct smap_node *node;
+ size_t i = 0;
+ SMAP_FOR_EACH (node, smap) {
+ datum->keys[i].string = xstrdup(node->key);
+ datum->values[i].string = xstrdup(node->value);
+ i++;
+ }
+ ovs_assert(i == datum->n);
+
+ ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING);
+}
+
+struct ovsdb_error * OVS_WARN_UNUSED_RESULT
+ovsdb_datum_convert(struct ovsdb_datum *dst,
+ const struct ovsdb_type *dst_type,
+ const struct ovsdb_datum *src,
+ const struct ovsdb_type *src_type)
+{
+ struct json *json = ovsdb_datum_to_json(src, src_type);
+ struct ovsdb_error *error = ovsdb_datum_from_json(dst, dst_type, json,
+ NULL);
+ json_destroy(json);
+ return error;
+}
+
static uint32_t
hash_atoms(enum ovsdb_atomic_type type, const union ovsdb_atom *atoms,
unsigned int n, uint32_t basis)
{
size_t i;
+ if (a->n > b->n) {
+ return false;
+ }
for (i = 0; i < a->n; i++) {
if (ovsdb_datum_find(a, i, b, type) == UINT_MAX) {
return false;
}
/* Adds the element with the given 'key' and 'value' to 'datum', which must
- * have the specified 'type'.
+ * have the specified 'type'. Optionally if 'range_end_atom' is not
+ * a null pointer, adds a set of integers to 'datum' from inclusive
+ * range ['key', 'range_end_atom'].
*
* This function always allocates memory, so it is not an efficient way to add
* a number of elements to a datum.
*
+ * When adding a range of integers, this function allocates the memory once
+ * for the whole range.
+ *
* This function does not maintain ovsdb_datum invariants. Use
* ovsdb_datum_sort() to check and restore these invariants. (But a datum with
* 0 or 1 elements cannot violate the invariants anyhow.) */
ovsdb_datum_add_unsafe(struct ovsdb_datum *datum,
const union ovsdb_atom *key,
const union ovsdb_atom *value,
- const struct ovsdb_type *type)
+ const struct ovsdb_type *type,
+ const union ovsdb_atom *range_end_atom)
{
- size_t idx = datum->n++;
+ size_t idx = datum->n;
+ datum->n += range_end_atom ?
+ (range_end_atom->integer - key->integer + 1) : 1;
datum->keys = xrealloc(datum->keys, datum->n * sizeof *datum->keys);
- ovsdb_atom_clone(&datum->keys[idx], key, type->key.type);
- if (type->value.type != OVSDB_TYPE_VOID) {
- datum->values = xrealloc(datum->values,
- datum->n * sizeof *datum->values);
- ovsdb_atom_clone(&datum->values[idx], value, type->value.type);
+ if (range_end_atom && key->integer <= range_end_atom->integer) {
+ for (int64_t i = key->integer; i <= range_end_atom->integer; i++) {
+ datum->keys[idx++].integer = i;
+ }
+ } else {
+ ovsdb_atom_clone(&datum->keys[idx], key, type->key.type);
+ if (type->value.type != OVSDB_TYPE_VOID) {
+ datum->values = xrealloc(datum->values,
+ datum->n * sizeof *datum->values);
+ ovsdb_atom_clone(&datum->values[idx], value, type->value.type);
+ }
}
}
}
}
if (n != a->n) {
- struct ovsdb_error *error;
a->n = n;
- error = ovsdb_datum_sort(a, type->key.type);
- assert(!error);
+ ovs_assert(!ovsdb_datum_sort(a, type->key.type));
}
}
bool changed = false;
size_t i;
- assert(a_type->key.type == b_type->key.type);
- assert(a_type->value.type == b_type->value.type
- || b_type->value.type == OVSDB_TYPE_VOID);
+ ovs_assert(a_type->key.type == b_type->key.type);
+ ovs_assert(a_type->value.type == b_type->value.type
+ || b_type->value.type == OVSDB_TYPE_VOID);
/* XXX The big-O of this could easily be improved. */
for (i = 0; i < a->n; ) {
}
}
\f
-struct ovsdb_symbol_table {
- struct shash sh;
-};
-
struct ovsdb_symbol_table *
ovsdb_symbol_table_create(void)
{
struct ovsdb_symbol *
ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name,
- const struct uuid *uuid, bool used)
+ const struct uuid *uuid, bool created)
{
struct ovsdb_symbol *symbol;
- assert(!ovsdb_symbol_table_get(symtab, name));
+ ovs_assert(!ovsdb_symbol_table_get(symtab, name));
symbol = xmalloc(sizeof *symbol);
symbol->uuid = *uuid;
- symbol->used = used;
+ symbol->created = created;
+ symbol->strong_ref = false;
+ symbol->weak_ref = false;
shash_add(&symtab->sh, name, symbol);
return symbol;
}
}
return symbol;
}
+\f
+/* APIs for Generating and apply diffs. */
+
+/* Generate a difference ovsdb_dataum between 'old' and 'new'.
+ * 'new' can be regenerated by applying the difference to the 'old'.
+ *
+ * The diff operation is reversible. Given 'old',
+ * 'new' can be recreated by applying diff to 'old'.
+ *
+ * Thus
+ * Let d = 'old' diff 'new'
+ * then 'new' = 'old' diff d
+ *
+ * The 'diff' datum is always safe; the orders of keys are maintained
+ * since they are added in order. */
+void
+ovsdb_datum_diff(struct ovsdb_datum *diff,
+ const struct ovsdb_datum *old,
+ const struct ovsdb_datum *new,
+ const struct ovsdb_type *type)
+{
+ size_t oi, ni;
+
+ ovsdb_datum_init_empty(diff);
+ if (!ovsdb_type_is_composite(type)) {
+ ovsdb_datum_clone(diff, new, type);
+ return;
+ }
+
+ /* Generate the diff in O(n) time. */
+ for (oi = ni = 0; oi < old->n && ni < new->n; ) {
+ int c = ovsdb_atom_compare_3way(&old->keys[oi], &new->keys[ni],
+ type->key.type);
+ if (c < 0) {
+ ovsdb_datum_add_unsafe(diff, &old->keys[oi], &old->values[oi],
+ type, NULL);
+ oi++;
+ } else if (c > 0) {
+ ovsdb_datum_add_unsafe(diff, &new->keys[ni], &new->values[ni],
+ type, NULL);
+ ni++;
+ } else {
+ if (type->value.type != OVSDB_TYPE_VOID &&
+ ovsdb_atom_compare_3way(&old->values[oi], &new->values[ni],
+ type->value.type)) {
+ ovsdb_datum_add_unsafe(diff, &new->keys[ni], &new->values[ni],
+ type, NULL);
+ }
+ oi++; ni++;
+ }
+ }
+
+ for (; oi < old->n; oi++) {
+ ovsdb_datum_add_unsafe(diff, &old->keys[oi], &old->values[oi],
+ type, NULL);
+ }
+
+ for (; ni < new->n; ni++) {
+ ovsdb_datum_add_unsafe(diff, &new->keys[ni], &new->values[ni],
+ type, NULL);
+ }
+}
-const char *
-ovsdb_symbol_table_find_unused(const struct ovsdb_symbol_table *symtab)
+/* Apply 'diff' to 'old' to regenerate 'new'.
+ *
+ * Return NULL if the 'new' is successfully generated, otherwise, return
+ * ovsdb_error and the stat of 'new' is indeterministic. */
+struct ovsdb_error *
+ovsdb_datum_apply_diff(struct ovsdb_datum *new,
+ const struct ovsdb_datum *old,
+ const struct ovsdb_datum *diff,
+ const struct ovsdb_type *type)
{
- struct shash_node *node;
+ ovsdb_datum_init_empty(new);
+ ovsdb_datum_diff(new, old, diff, type);
- SHASH_FOR_EACH (node, &symtab->sh) {
- struct ovsdb_symbol *symbol = node->data;
- if (!symbol->used) {
- return node->name;
- }
+ /* Make sure member size of 'new' conforms to type. */
+ if (new->n < type->n_min || new->n > type->n_max) {
+ ovsdb_datum_destroy(new, type);
+ return ovsdb_error(NULL, "Datum crated by diff has size error");
}
return NULL;
}
+
\f
/* Extracts a token from the beginning of 's' and returns a pointer just after
* the token. Stores the token itself into '*outp', which the caller is
{
return strchr(":=, []{}!<>", c) != NULL;
}
+
+struct ovsdb_error *
+ovsdb_atom_range_check_size(int64_t range_start, int64_t range_end)
+{
+ if ((uint64_t) range_end - (uint64_t) range_start
+ >= MAX_OVSDB_ATOM_RANGE_SIZE) {
+ return ovsdb_error("constraint violation",
+ "Range \"%"PRId64"-%"PRId64"\" is too big. "
+ "Maximum allowed size is %d.",
+ range_start, range_end, MAX_OVSDB_ATOM_RANGE_SIZE);
+ }
+ return NULL;
+}
+\f
+char *
+ovsdb_data_row_name(const struct uuid *uuid)
+{
+ char *name;
+ char *p;
+
+ name = xasprintf("row"UUID_FMT, UUID_ARGS(uuid));
+ for (p = name; *p != '\0'; p++) {
+ if (*p == '-') {
+ *p = '_';
+ }
+ }
+
+ return name;
+}