odp-util: Format and scan multiple MPLS labels.
[cascardo/ovs.git] / lib / learn.c
index e1c73cb..66201d5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2012, 2013, 2014 Nicira, Inc.
+ * Copyright (c) 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -101,6 +101,7 @@ learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
     fm->command = OFPFC_MODIFY_STRICT;
     fm->idle_timeout = learn->idle_timeout;
     fm->hard_timeout = learn->hard_timeout;
+    fm->importance = 0;
     fm->buffer_id = UINT32_MAX;
     fm->out_port = OFPP_NONE;
     fm->flags = 0;
@@ -120,8 +121,8 @@ learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
     }
 
     for (spec = learn->specs; spec < &learn->specs[learn->n_specs]; spec++) {
+        struct ofpact_set_field *sf;
         union mf_subvalue value;
-        int chunk, ofs;
 
         if (spec->src_type == NX_LEARN_SRC_FIELD) {
             mf_read_subfield(&spec->src, flow, &value);
@@ -135,25 +136,19 @@ learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
             break;
 
         case NX_LEARN_DST_LOAD:
-            for (ofs = 0; ofs < spec->n_bits; ofs += chunk) {
-                struct ofpact_reg_load *load;
-
-                chunk = MIN(spec->n_bits - ofs, 64);
-
-                load = ofpact_put_REG_LOAD(ofpacts);
-                load->dst.field = spec->dst.field;
-                load->dst.ofs = spec->dst.ofs + ofs;
-                load->dst.n_bits = chunk;
-                bitwise_copy(&value, sizeof value, ofs,
-                             &load->subvalue, sizeof load->subvalue, 0,
-                             chunk);
-            }
+            sf = ofpact_put_reg_load(ofpacts);
+            sf->field = spec->dst.field;
+            bitwise_copy(&value, sizeof value, 0,
+                         &sf->value, spec->dst.field->n_bytes, spec->dst.ofs,
+                         spec->n_bits);
+            bitwise_one(&sf->mask, spec->dst.field->n_bytes, spec->dst.ofs,
+                        spec->n_bits);
             break;
 
         case NX_LEARN_DST_OUTPUT:
             if (spec->n_bits <= 16
                 || is_all_zeros(value.u8, sizeof value - 2)) {
-                ofp_port_t port = u16_to_ofp(ntohs(value.be16[7]));
+                ofp_port_t port = u16_to_ofp(ntohll(value.integer));
 
                 if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
                     || port == OFPP_IN_PORT
@@ -166,10 +161,9 @@ learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
             break;
         }
     }
-    ofpact_pad(ofpacts);
 
-    fm->ofpacts = ofpbuf_data(ofpacts);
-    fm->ofpacts_len = ofpbuf_size(ofpacts);
+    fm->ofpacts = ofpacts->data;
+    fm->ofpacts_len = ofpacts->size;
 }
 
 /* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
@@ -190,32 +184,18 @@ learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
 
 /* 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
+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 {
-        imm.be64[1] = htonll(strtoull(s, (char **) &s, 0));
+    err = parse_int_string(s, imm.u8, sizeof imm.u8, (char **) &s);
+    if (err) {
+        return xasprintf("%s: too many bits in immediate value", full_s);
     }
 
     if (strncmp(s, "->", 2)) {
@@ -227,6 +207,10 @@ learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
     if (error) {
         return error;
     }
+    if (!mf_nxm_header(dst.field->id)) {
+        return xasprintf("%s: experimenter OXM field '%s' not supported",
+                         full_s, s);
+    }
 
     if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
                               (8 * sizeof imm) - dst.n_bits)) {
@@ -244,7 +228,7 @@ learn_parse_load_immediate(const char *s, struct ofpact_learn_spec *spec)
 
 /* 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
+static char * OVS_WARN_UNUSED_RESULT
 learn_parse_spec(const char *orig, char *name, char *value,
                  struct ofpact_learn_spec *spec)
 {
@@ -275,6 +259,10 @@ learn_parse_spec(const char *orig, char *name, char *value,
         if (error) {
             return error;
         }
+        if (!mf_nxm_header(spec->dst.field->id)) {
+            return xasprintf("%s: experimenter OXM field '%s' not supported",
+                             orig, name);
+        }
 
         /* Parse source and check prerequisites. */
         if (value[0] != '\0') {
@@ -333,7 +321,7 @@ learn_parse_spec(const char *orig, char *name, char *value,
 
 /* 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
+static char * OVS_WARN_UNUSED_RESULT
 learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
 {
     struct ofpact_learn *learn;
@@ -375,7 +363,7 @@ learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
             char *error;
 
             spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
-            learn = ofpacts->frame;
+            learn = ofpacts->header;
             learn->n_specs++;
 
             error = learn_parse_spec(orig, name, value, spec);
@@ -391,7 +379,7 @@ learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
             }
         }
     }
-    ofpact_update_len(ofpacts, &learn->ofpact);
+    ofpact_finish(ofpacts, &learn->ofpact);
 
     return NULL;
 }
@@ -408,7 +396,7 @@ learn_parse__(char *orig, char *arg, struct ofpbuf *ofpacts)
  * the action's arguments.
  *
  * Modifies 'arg'. */
-char * WARN_UNUSED_RESULT
+char * OVS_WARN_UNUSED_RESULT
 learn_parse(char *arg, struct ofpbuf *ofpacts)
 {
     char *orig = xstrdup(arg);