X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fovsdb-data.c;h=0706dd05fa6c8b715a678e5ee8e459b9bda641a4;hb=28c5588e8e1a8d091c5d2275232c35f2968a97fa;hp=54ce72cf719c03baf3b3303df5f685f9fee8087c;hpb=be44585c21cc8dbe097a460a930696f0350dc12d;p=sliver-openvswitch.git diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c index 54ce72cf7..0706dd05f 100644 --- a/lib/ovsdb-data.c +++ b/lib/ovsdb-data.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2009, 2010, 2011 Nicira Networks +/* Copyright (c) 2009, 2010, 2011, 2012, 2014 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,6 @@ #include "ovsdb-data.h" -#include #include #include #include @@ -25,10 +24,12 @@ #include "dynamic-string.h" #include "hash.h" +#include "ovs-thread.h" #include "ovsdb-error.h" #include "ovsdb-parser.h" #include "json.h" #include "shash.h" +#include "smap.h" #include "sort.h" #include "unicode.h" @@ -40,7 +41,7 @@ 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 * @@ -57,7 +58,7 @@ ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type) { switch (type) { case OVSDB_TYPE_VOID: - NOT_REACHED(); + OVS_NOT_REACHED(); case OVSDB_TYPE_INTEGER: atom->integer = 0; @@ -81,7 +82,7 @@ ovsdb_atom_init_default(union ovsdb_atom *atom, enum ovsdb_atomic_type type) case OVSDB_N_TYPES: default: - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -94,9 +95,9 @@ const union ovsdb_atom * 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++) { @@ -104,10 +105,10 @@ ovsdb_atom_default(enum ovsdb_atomic_type type) 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]; } @@ -122,7 +123,7 @@ ovsdb_atom_is_default(const union ovsdb_atom *atom, { switch (type) { case OVSDB_TYPE_VOID: - NOT_REACHED(); + OVS_NOT_REACHED(); case OVSDB_TYPE_INTEGER: return atom->integer == 0; @@ -141,7 +142,7 @@ ovsdb_atom_is_default(const union ovsdb_atom *atom, case OVSDB_N_TYPES: default: - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -155,7 +156,7 @@ ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old, { switch (type) { case OVSDB_TYPE_VOID: - NOT_REACHED(); + OVS_NOT_REACHED(); case OVSDB_TYPE_INTEGER: new->integer = old->integer; @@ -179,7 +180,7 @@ ovsdb_atom_clone(union ovsdb_atom *new, const union ovsdb_atom *old, case OVSDB_N_TYPES: default: - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -200,7 +201,7 @@ ovsdb_atom_hash(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 hash_int(atom->integer, basis); @@ -219,7 +220,7 @@ ovsdb_atom_hash(const union ovsdb_atom *atom, enum ovsdb_atomic_type type, case OVSDB_N_TYPES: default: - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -232,7 +233,7 @@ ovsdb_atom_compare_3way(const union ovsdb_atom *a, { 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; @@ -251,7 +252,7 @@ ovsdb_atom_compare_3way(const union ovsdb_atom *a, case OVSDB_N_TYPES: default: - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -289,7 +290,7 @@ static void ovsdb_symbol_referenced(struct ovsdb_symbol *symbol, const struct ovsdb_base_type *base) { - assert(base->type == OVSDB_TYPE_UUID); + ovs_assert(base->type == OVSDB_TYPE_UUID); if (base->u.uuid.refTableName) { switch (base->u.uuid.refType) { @@ -352,7 +353,7 @@ ovsdb_atom_from_json__(union ovsdb_atom *atom, switch (type) { case OVSDB_TYPE_VOID: - NOT_REACHED(); + OVS_NOT_REACHED(); case OVSDB_TYPE_INTEGER: if (json->type == JSON_INTEGER) { @@ -393,7 +394,7 @@ ovsdb_atom_from_json__(union ovsdb_atom *atom, case OVSDB_N_TYPES: default: - NOT_REACHED(); + OVS_NOT_REACHED(); } return ovsdb_syntax_error(json, NULL, "expected %s", @@ -408,10 +409,9 @@ ovsdb_atom_from_json__(union ovsdb_atom *atom, * 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. 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. */ + * 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, @@ -435,14 +435,13 @@ ovsdb_atom_from_json(union ovsdb_atom *atom, /* 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); @@ -462,7 +461,7 @@ ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type) case OVSDB_N_TYPES: default: - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -475,7 +474,7 @@ ovsdb_atom_from_string__(union ovsdb_atom *atom, switch (type) { case OVSDB_TYPE_VOID: - NOT_REACHED(); + OVS_NOT_REACHED(); case OVSDB_TYPE_INTEGER: { long long int integer; @@ -543,7 +542,7 @@ ovsdb_atom_from_string__(union ovsdb_atom *atom, case OVSDB_N_TYPES: default: - NOT_REACHED(); + OVS_NOT_REACHED(); } return NULL; @@ -590,6 +589,7 @@ ovsdb_atom_from_string(union ovsdb_atom *atom, error = ovsdb_atom_check_constraints(atom, base); if (error) { + ovsdb_atom_destroy(atom, base->type); msg = ovsdb_error_to_string(error); ovsdb_error_destroy(error); } @@ -628,7 +628,7 @@ ovsdb_atom_to_string(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: ds_put_format(out, "%"PRId64, atom->integer); @@ -660,7 +660,7 @@ ovsdb_atom_to_string(const union ovsdb_atom *atom, enum ovsdb_atomic_type type, case OVSDB_N_TYPES: default: - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -698,12 +698,12 @@ check_string_constraints(const char *s, 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); } @@ -741,7 +741,7 @@ ovsdb_atom_check_constraints(const union ovsdb_atom *atom, switch (base->type) { case OVSDB_TYPE_VOID: - NOT_REACHED(); + OVS_NOT_REACHED(); case OVSDB_TYPE_INTEGER: if (atom->integer >= base->u.integer.min @@ -766,7 +766,7 @@ ovsdb_atom_check_constraints(const union ovsdb_atom *atom, "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) { @@ -793,7 +793,7 @@ ovsdb_atom_check_constraints(const union ovsdb_atom *atom, DBL_DIG, atom->real, DBL_DIG, base->u.real.max); } - NOT_REACHED(); + OVS_NOT_REACHED(); case OVSDB_TYPE_BOOLEAN: return NULL; @@ -806,7 +806,7 @@ ovsdb_atom_check_constraints(const union ovsdb_atom *atom, case OVSDB_N_TYPES: default: - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -841,7 +841,7 @@ ovsdb_datum_init_empty(struct ovsdb_datum *datum) /* 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). * @@ -877,19 +877,20 @@ ovsdb_datum_default(const struct ovsdb_type *type) 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(); } } @@ -1075,7 +1076,7 @@ ovsdb_datum_sort_assert(struct ovsdb_datum *datum, { struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type); if (error) { - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -1180,7 +1181,7 @@ ovsdb_datum_from_json__(struct ovsdb_datum *datum, 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); } @@ -1245,8 +1246,8 @@ ovsdb_datum_from_json__(struct ovsdb_datum *datum, * 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. */ + * RFC 7047 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, @@ -1272,36 +1273,35 @@ ovsdb_datum_from_json(struct ovsdb_datum *datum, * * 'type' constraints on datum->n are ignored. * - * 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_datum_to_json(const struct ovsdb_datum *datum, const struct ovsdb_type *type) { - 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_atom_to_json(&datum->keys[i], type->key.type), + ovsdb_atom_to_json(&datum->values[i], type->value.type)); } - 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_atom_to_json(&datum->keys[0], type->key.type); } 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_atom_to_json(&datum->keys[i], type->key.type); } - return wrap_json("map", json_array_create(elems, datum->n)); + return wrap_json("set", json_array_create(elems, datum->n)); } } @@ -1523,27 +1523,26 @@ ovsdb_datum_to_bare(const struct ovsdb_datum *datum, } /* Initializes 'datum' as a string-to-string map whose contents are taken from - * 'sh'. Destroys 'sh'. */ + * 'smap'. Destroys 'smap'. */ void -ovsdb_datum_from_shash(struct ovsdb_datum *datum, struct shash *sh) +ovsdb_datum_from_smap(struct ovsdb_datum *datum, struct smap *smap) { - struct shash_node *node, *next; + struct smap_node *node, *next; size_t i; - datum->n = shash_count(sh); + datum->n = smap_count(smap); datum->keys = xmalloc(datum->n * sizeof *datum->keys); datum->values = xmalloc(datum->n * sizeof *datum->values); i = 0; - SHASH_FOR_EACH_SAFE (node, next, sh) { - datum->keys[i].string = node->name; - datum->values[i].string = node->data; - shash_steal(sh, node); + SMAP_FOR_EACH_SAFE (node, next, smap) { + smap_steal(smap, node, + &datum->keys[i].string, &datum->values[i].string); i++; } - assert(i == datum->n); + ovs_assert(i == datum->n); - shash_destroy(sh); + smap_destroy(smap); ovsdb_datum_sort_unique(datum, OVSDB_TYPE_STRING, OVSDB_TYPE_STRING); } @@ -1687,6 +1686,9 @@ ovsdb_datum_includes_all(const struct ovsdb_datum *a, { 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; @@ -1797,7 +1799,7 @@ ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b, struct ovsdb_error *error; a->n = n; error = ovsdb_datum_sort(a, type->key.type); - assert(!error); + ovs_assert(!error); } } @@ -1809,9 +1811,9 @@ ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_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; ) { @@ -1858,7 +1860,7 @@ ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name, { 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->created = created;