Implement QoS framework.
[sliver-openvswitch.git] / lib / ovsdb-data.c
index 08d623a..ee98c80 100644 (file)
@@ -366,7 +366,7 @@ ovsdb_atom_to_json(const union ovsdb_atom *atom, enum ovsdb_atomic_type type)
 
 static char *
 ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
-                         const char *s)
+                         const char *s, struct ovsdb_symbol_table *symtab)
 {
     switch (type) {
     case OVSDB_TYPE_VOID:
@@ -427,7 +427,9 @@ ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
         break;
 
     case OVSDB_TYPE_UUID:
-        if (!uuid_from_string(&atom->uuid, s)) {
+        if (*s == '@') {
+            atom->uuid = ovsdb_symbol_table_insert(symtab, s)->uuid;
+        } else if (!uuid_from_string(&atom->uuid, s)) {
             return xasprintf("\"%s\" is not a valid UUID", s);
         }
         break;
@@ -454,19 +456,24 @@ ovsdb_atom_from_string__(union ovsdb_atom *atom, enum ovsdb_atomic_type type,
  *      - OVSDB_TYPE_STRING: A JSON string if it begins with a quote, otherwise
  *        an arbitrary string.
  *
- *      - OVSDB_TYPE_UUID: A UUID in RFC 4122 format.
+ *      - OVSDB_TYPE_UUID: A UUID in RFC 4122 format.  If 'symtab' is nonnull,
+ *        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.
  *
  * Returns a null pointer if successful, otherwise an error message describing
  * the problem.  The caller is responsible for freeing the error.
  */
 char *
 ovsdb_atom_from_string(union ovsdb_atom *atom,
-                       const struct ovsdb_base_type *base, const char *s)
+                       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);
+    msg = ovsdb_atom_from_string__(atom, base->type, s, symtab);
     if (msg) {
         return msg;
     }
@@ -577,30 +584,6 @@ check_string_constraints(const char *s,
             "length %u", s, n_chars, c->maxLen);
     }
 
-#if HAVE_PCRE
-    if (c->re) {
-        int retval;
-
-        retval = pcre_exec(c->re, NULL, s, strlen(s), 0,
-                           PCRE_ANCHORED | PCRE_NO_UTF8_CHECK, NULL, 0);
-        if (retval == PCRE_ERROR_NOMATCH) {
-            if (c->reComment) {
-                return ovsdb_error("constraint violation",
-                                   "\"%s\" is not a %s", s, c->reComment);
-            } else {
-                return ovsdb_error("constraint violation",
-                                   "\"%s\" does not match regular expression "
-                                   "/%s/", s, c->reMatch);
-            }
-        } else if (retval < 0) {
-            /* PCRE doesn't have a function to translate an error code to a
-             * description.  Bizarre.  See pcreapi(3) for error details. */
-            return ovsdb_error("internal error", "PCRE returned error %d",
-                               retval);
-        }
-    }
-#endif  /* HAVE_PCRE */
-
     return NULL;
 }
 
@@ -614,6 +597,25 @@ struct ovsdb_error *
 ovsdb_atom_check_constraints(const union ovsdb_atom *atom,
                              const struct ovsdb_base_type *base)
 {
+    if (base->enum_
+        && ovsdb_datum_find_key(base->enum_, atom, base->type) == UINT_MAX) {
+        struct ovsdb_error *error;
+        struct ds actual = DS_EMPTY_INITIALIZER;
+        struct ds valid = DS_EMPTY_INITIALIZER;
+
+        ovsdb_atom_to_string(atom, base->type, &actual);
+        ovsdb_datum_to_string(base->enum_,
+                              ovsdb_base_type_get_enum_type(base->type),
+                              &valid);
+        error = ovsdb_error("constraint violation",
+                            "%s is not one of the allowed values (%s)",
+                            ds_cstr(&actual), ds_cstr(&valid));
+        ds_destroy(&actual);
+        ds_destroy(&valid);
+
+        return error;
+    }
+
     switch (base->type) {
     case OVSDB_TYPE_VOID:
         NOT_REACHED();
@@ -801,7 +803,7 @@ ovsdb_datum_swap(struct ovsdb_datum *a, struct ovsdb_datum *b)
 }
 
 struct ovsdb_datum_sort_cbdata {
-    const struct ovsdb_type *type;
+    enum ovsdb_atomic_type key_type;
     struct ovsdb_datum *datum;
 };
 
@@ -812,7 +814,7 @@ ovsdb_datum_sort_compare_cb(size_t a, size_t b, void *cbdata_)
 
     return ovsdb_atom_compare_3way(&cbdata->datum->keys[a],
                                    &cbdata->datum->keys[b],
-                                   cbdata->type->key.type);
+                                   cbdata->key_type);
 }
 
 static void
@@ -821,13 +823,13 @@ ovsdb_datum_sort_swap_cb(size_t a, size_t b, void *cbdata_)
     struct ovsdb_datum_sort_cbdata *cbdata = cbdata_;
 
     ovsdb_atom_swap(&cbdata->datum->keys[a], &cbdata->datum->keys[b]);
-    if (cbdata->type->value.type != OVSDB_TYPE_VOID) {
+    if (cbdata->datum->values) {
         ovsdb_atom_swap(&cbdata->datum->values[a], &cbdata->datum->values[b]);
     }
 }
 
 struct ovsdb_error *
-ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type)
+ovsdb_datum_sort(struct ovsdb_datum *datum, enum ovsdb_atomic_type key_type)
 {
     if (datum->n < 2) {
         return NULL;
@@ -835,15 +837,15 @@ ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type)
         struct ovsdb_datum_sort_cbdata cbdata;
         size_t i;
 
-        cbdata.type = type;
+        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],
-                                  type->key.type)) {
-                if (ovsdb_type_is_map(type)) {
+                                  key_type)) {
+                if (datum->values) {
                     return ovsdb_error(NULL, "map contains duplicate key");
                 } else {
                     return ovsdb_error(NULL, "set contains duplicate");
@@ -855,6 +857,16 @@ ovsdb_datum_sort(struct ovsdb_datum *datum, const struct ovsdb_type *type)
     }
 }
 
+void
+ovsdb_datum_sort_assert(struct ovsdb_datum *datum,
+                        enum ovsdb_atomic_type key_type)
+{
+    struct ovsdb_error *error = ovsdb_datum_sort(datum, key_type);
+    if (error) {
+        NOT_REACHED();
+    }
+}
+
 /* Checks that each of the atoms in 'datum' conforms to the constraints
  * specified by its 'type'.  Returns an error if a constraint is violated,
  * otherwise a null pointer.
@@ -955,7 +967,7 @@ ovsdb_datum_from_json(struct ovsdb_datum *datum,
             datum->n++;
         }
 
-        error = ovsdb_datum_sort(datum, type);
+        error = ovsdb_datum_sort(datum, type->key.type);
         if (error) {
             goto error;
         }
@@ -1023,13 +1035,13 @@ skip_spaces(const char *p)
 
 static char *
 parse_atom_token(const char **s, const struct ovsdb_base_type *base,
-                 union ovsdb_atom *atom)
+                 union ovsdb_atom *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);
+        error = ovsdb_atom_from_string(atom, base, token, symtab);
         free(token);
     }
     return error;
@@ -1037,18 +1049,19 @@ parse_atom_token(const char **s, const struct ovsdb_base_type *base,
 
 static char *
 parse_key_value(const char **s, const struct ovsdb_type *type,
-                union ovsdb_atom *key, union ovsdb_atom *value)
+                union ovsdb_atom *key, union ovsdb_atom *value,
+                struct ovsdb_symbol_table *symtab)
 {
     const char *start = *s;
     char *error;
 
-    error = parse_atom_token(s, &type->key, key);
+    error = parse_atom_token(s, &type->key, 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);
+            error = parse_atom_token(s, &type->value, value, symtab);
         } else {
             error = xasprintf("%s: syntax error at \"%c\" expecting \"=\"",
                               start, **s);
@@ -1075,10 +1088,14 @@ free_key_value(const struct ovsdb_type *type,
  * or, for a map, '='-delimited pairs of atoms.  Each atom must in a format
  * acceptable to ovsdb_atom_from_string().  Optionally, a set may be enclosed
  * in "[]" or a map in "{}"; for an empty set or map these punctuators are
- * required. */
+ * required.
+ *
+ * Optionally, a symbol table may be supplied as 'symtab'.  It is passed to
+ * ovsdb_atom_to_string(). */
 char *
 ovsdb_datum_from_string(struct ovsdb_datum *datum,
-                        const struct ovsdb_type *type, const char *s)
+                        const struct ovsdb_type *type, const char *s,
+                        struct ovsdb_symbol_table *symtab)
 {
     bool is_map = ovsdb_type_is_map(type);
     struct ovsdb_error *dberror;
@@ -1113,7 +1130,7 @@ ovsdb_datum_from_string(struct ovsdb_datum *datum,
         }
 
         /* Add to datum. */
-        error = parse_key_value(&p, type, &key, &value);
+        error = parse_key_value(&p, type, &key, &value, symtab);
         if (error) {
             goto error;
         }
@@ -1152,7 +1169,7 @@ ovsdb_datum_from_string(struct ovsdb_datum *datum,
         goto error;
     }
 
-    dberror = ovsdb_datum_sort(datum, type);
+    dberror = ovsdb_datum_sort(datum, type->key.type);
     if (dberror) {
         ovsdb_error_destroy(dberror);
         if (ovsdb_type_is_map(type)) {
@@ -1448,7 +1465,7 @@ ovsdb_datum_union(struct ovsdb_datum *a, const struct ovsdb_datum *b,
     if (n != a->n) {
         struct ovsdb_error *error;
         a->n = n;
-        error = ovsdb_datum_sort(a, type);
+        error = ovsdb_datum_sort(a, type->key.type);
         assert(!error);
     }
 }
@@ -1476,8 +1493,7 @@ ovsdb_datum_subtract(struct ovsdb_datum *a, const struct ovsdb_type *a_type,
         }
     }
     if (changed) {
-        struct ovsdb_error *error = ovsdb_datum_sort(a, a_type);
-        assert(!error);
+        ovsdb_datum_sort_assert(a, a_type->key.type);
     }
 }
 \f
@@ -1545,6 +1561,21 @@ ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab,
     }
     return symbol;
 }
+
+const char *
+ovsdb_symbol_table_find_unused(const struct ovsdb_symbol_table *symtab)
+{
+    struct shash_node *node;
+
+    SHASH_FOR_EACH (node, &symtab->sh) {
+        struct ovsdb_symbol *symbol = node->data;
+        if (!symbol->used) {
+            return node->name;
+        }
+    }
+
+    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