[ovs-dev] [PATCH 3/4] util: Library routines for printing and scanning large hex integers.

Jesse Gross jesse at nicira.com
Thu May 28 04:59:40 UTC 2015


On Wed, May 27, 2015 at 3:25 PM, Ben Pfaff <blp at nicira.com> wrote:
> On Wed, May 27, 2015 at 10:48:40AM -0700, Jesse Gross wrote:
>> Geneve options are variable length and up to 124 bytes long, which means
>> that they can't be easily manipulated by the integer string functions
>> like we do for other fields. This adds a few helper routines to make
>> these operations easier.
>>
>> Signed-off-by: Jesse Gross <jesse at nicira.com>
>
> Can learn_parse_load_immediate() and mf_format_subvalue() use these
> new functions?

Yes, I replaced the existing code with the following:

diff --git a/lib/learn.c b/lib/learn.c
index 99d56e6..8ff1e0a 100644
--- a/lib/learn.c
+++ b/lib/learn.c
@@ -190,29 +190,14 @@ static char * OVS_WARN_UNUSED_RESULT
 learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
 {
     const char *full_s = s;
-    const char *arrow = strstr(s, "->");
     struct mf_subfield dst;
     union mf_subvalue imm;
     char *error;
+    int err;

-    memset(&imm, 0, sizeof imm);
-    if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X') && arrow) {
-        const char *in = arrow - 1;
-        uint8_t *out = imm.u8 + sizeof imm.u8 - 1;
-        int n = arrow - (s + 2);
-        int i;
-
-        for (i = 0; i < n; i++) {
-            int hexit = hexit_value(in[-i]);
-            if (hexit < 0) {
-                return xasprintf("%s: bad hex digit in value", full_s);
-            }
-            out[-(i / 2)] |= i % 2 ? hexit << 4 : hexit;
-        }
-        s = arrow;
-    } else {
-        ovs_be64 *last_be64 = &imm.be64[ARRAY_SIZE(imm.be64) - 1];
-        *last_be64 = htonll(strtoull(s, (char **) &s, 0));
+    err = parse_int_string(s, imm.u8, sizeof imm.u8, (char **) &s);
+    if (err) {
+        return xasprintf("%s: bad hex digit in value", full_s);
     }

     if (strncmp(s, "->", 2)) {
diff --git a/lib/meta-flow.c b/lib/meta-flow.c
index 124b525..e3250e9 100644
--- a/lib/meta-flow.c
+++ b/lib/meta-flow.c
@@ -2300,18 +2300,7 @@ mf_get_subfield(const struct mf_subfield *sf,
const struct flow *flow)
 void
 mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s)
 {
-    int i;
-
-    for (i = 0; i < ARRAY_SIZE(subvalue->u8); i++) {
-        if (subvalue->u8[i]) {
-            ds_put_format(s, "0x%"PRIx8, subvalue->u8[i]);
-            for (i++; i < ARRAY_SIZE(subvalue->u8); i++) {
-                ds_put_format(s, "%02"PRIx8, subvalue->u8[i]);
-            }
-            return;
-        }
-    }
-    ds_put_char(s, '0');
+    ds_put_hex(s, subvalue->u8, ARRAY_SIZE(subvalue->u8));
 }

 void



More information about the dev mailing list