Provide and use helper to parse group id.

This in preparation for adding group_out support to the parser
for flow monitor requests. This also requires parsing of a group id.

Signed-off-by: Simon Horman <ho...@verge.net.au>

---
v2
* No change
---
 lib/ofp-parse.c | 48 ++++++++++++++++++++++++++++++------------------
 1 file changed, 30 insertions(+), 18 deletions(-)

diff --git a/lib/ofp-parse.c b/lib/ofp-parse.c
index 5ad1d62..612f9c6 100644
--- a/lib/ofp-parse.c
+++ b/lib/ofp-parse.c
@@ -169,6 +169,34 @@ str_to_ip(const char *str, ovs_be32 *ip)
     return NULL;
 }
 
+/* Parses 'arg' as a group id into '*group_id'.
+ *
+ * Allows the all gropup, expressed as the string "all" or numerically
+ * if 'allow_all' is true.
+ *
+ * 'log_tag' may be included in returned error strings. It must be non-NULL.
+ *
+ * Returns NULL if successful, otherwise a malloc()'d string describing the
+ * error.  The caller is responsible for freeing the returned string. */
+static char * WARN_UNUSED_RESULT
+str_to_group_id(const char *arg, uint32_t *group_id, bool allow_all,
+                const char *log_tag)
+{
+    if(allow_all && !strcmp(arg, "all")) {
+        *group_id = OFPG_ALL;
+    } else {
+        char *error = str_to_u32(arg, group_id);
+        if (error) {
+            return error;
+        }
+        if ((!allow_all || *group_id != OFPG_ALL) && *group_id > OFPG_MAX) {
+            return xasprintf("invalid %s id %s", arg, log_tag);
+        }
+    }
+
+    return NULL;
+}
+
 /* Parses 'arg' as the argument to an "enqueue" action, and appends such an
  * action to 'ofpacts'.
  *
@@ -2117,11 +2145,7 @@ parse_bucket_str(struct ofputil_bucket *bucket, char 
*str_,
                 error = xasprintf("%s: invalid watch_port", arg);
             }
         } else if (!strcasecmp(act, "watch_group")) {
-            error = str_to_u32(arg, &bucket->watch_group);
-            if (!error && bucket->watch_group > OFPG_MAX) {
-                error = xasprintf("invalid watch_group id %"PRIu32,
-                                  bucket->watch_group);
-            }
+            error = str_to_group_id(arg, &bucket->watch_group, false, act);
         } else {
             error = str_to_ofpact__(pos, act, arg, &ofpacts, n_actions,
                                     usable_protocols);
@@ -2231,19 +2255,7 @@ parse_ofp_group_mod_str__(struct ofputil_group_mod *gm, 
uint16_t command,
         }
 
         if (!strcmp(name, "group_id")) {
-            if(!strcmp(value, "all")) {
-                gm->group_id = OFPG_ALL;
-            } else {
-                char *error = str_to_u32(value, &gm->group_id);
-                if (error) {
-                    goto out;
-                }
-                if (gm->group_id != OFPG_ALL && gm->group_id > OFPG_MAX) {
-                    error = xasprintf("invalid group id %"PRIu32,
-                                      gm->group_id);
-                    goto out;
-                }
-            }
+            error = str_to_group_id(value, &gm->group_id, true, "group");
         } else if (!strcmp(name, "type")){
             if (!(fields & F_GROUP_TYPE)) {
                 error = xstrdup("type is not needed");
-- 
2.0.0.rc2

_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to