learn: Make it possible to parse "load" actions wider than 64 bits.
authorBen Pfaff <blp@nicira.com>
Sat, 14 Apr 2012 04:49:12 +0000 (21:49 -0700)
committerBen Pfaff <blp@nicira.com>
Sat, 14 Apr 2012 04:49:12 +0000 (21:49 -0700)
The implementation of the "learn" action now properly implements
specifications such as 0x20010db885a308d313198a2e03707348->NXM_NX_IPV6_DST
but the parser used in ovs-ofctl and elsewhere could not generate such
specifications.  This commit adds that support.

Signed-off-by: Ben Pfaff <blp@nicira.com>
lib/learn.c
tests/learn.at

index a402c5f..2c75720 100644 (file)
@@ -184,6 +184,7 @@ learn_check(const struct nx_action_learn *learn, const struct flow *flow)
                      * prerequisites.  No prerequisite depends on the value of
                      * a field that is wider than 64 bits.  So just skip
                      * setting it entirely. */
+                    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 8);
                 }
             }
         }
@@ -317,12 +318,62 @@ struct learn_spec {
 
     int src_type;
     struct mf_subfield src;
-    uint8_t src_imm[sizeof(union mf_value)];
+    union mf_subvalue src_imm;
 
     int dst_type;
     struct mf_subfield dst;
 };
 
+static void
+learn_parse_load_immediate(const char *s, struct learn_spec *spec)
+{
+    const char *full_s = s;
+    const char *arrow = strstr(s, "->");
+    struct mf_subfield dst;
+    union mf_subvalue imm;
+
+    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) {
+                ovs_fatal(0, "%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));
+    }
+
+    if (strncmp(s, "->", 2)) {
+        ovs_fatal(0, "%s: missing `->' following value", full_s);
+    }
+    s += 2;
+
+    s = mf_parse_subfield(&dst, s);
+    if (*s != '\0') {
+        ovs_fatal(0, "%s: trailing garbage following destination", full_s);
+    }
+
+    if (!bitwise_is_all_zeros(&imm, sizeof imm, dst.n_bits,
+                              (8 * sizeof imm) - dst.n_bits)) {
+        ovs_fatal(0, "%s: value does not fit into %u bits",
+                  full_s, dst.n_bits);
+    }
+
+    spec->n_bits = dst.n_bits;
+    spec->src_type = NX_LEARN_SRC_IMMEDIATE;
+    spec->src_imm = imm;
+    spec->dst_type = NX_LEARN_DST_LOAD;
+    spec->dst = dst;
+}
+
 static void
 learn_parse_spec(const char *orig, char *name, char *value,
                  struct learn_spec *spec)
@@ -340,7 +391,9 @@ learn_parse_spec(const char *orig, char *name, char *value,
 
         spec->n_bits = dst->n_bits;
         spec->src_type = NX_LEARN_SRC_IMMEDIATE;
-        memcpy(spec->src_imm, &imm, dst->n_bytes);
+        memset(&spec->src_imm, 0, sizeof spec->src_imm);
+        memcpy(&spec->src_imm.u8[sizeof spec->src_imm - dst->n_bytes],
+               &imm, dst->n_bytes);
         spec->dst_type = NX_LEARN_DST_MATCH;
         spec->dst.field = dst;
         spec->dst.ofs = 0;
@@ -372,23 +425,7 @@ learn_parse_spec(const char *orig, char *name, char *value,
         spec->dst_type = NX_LEARN_DST_MATCH;
     } else if (!strcmp(name, "load")) {
         if (value[strcspn(value, "[-")] == '-') {
-            struct nx_action_reg_load load;
-            int nbits, imm_bytes;
-            uint64_t imm;
-            int i;
-
-            nxm_parse_reg_load(&load, value);
-            nbits = nxm_decode_n_bits(load.ofs_nbits);
-            imm_bytes = DIV_ROUND_UP(nbits, 8);
-            imm = ntohll(load.value);
-
-            spec->n_bits = nbits;
-            spec->src_type = NX_LEARN_SRC_IMMEDIATE;
-            for (i = 0; i < imm_bytes; i++) {
-                spec->src_imm[i] = imm >> ((imm_bytes - i - 1) * 8);
-            }
-            spec->dst_type = NX_LEARN_DST_LOAD;
-            nxm_decode(&spec->dst, load.dst, load.ofs_nbits);
+            learn_parse_load_immediate(value, spec);
         } else {
             struct nx_action_reg_move move;
 
@@ -492,23 +529,16 @@ learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow)
             /* Update 'rule' to allow for satisfying destination
              * prerequisites. */
             if (spec.src_type == NX_LEARN_SRC_IMMEDIATE
-                && spec.dst_type == NX_LEARN_DST_MATCH
-                && spec.dst.ofs == 0
-                && spec.n_bits == spec.dst.field->n_bytes * 8) {
-                union mf_value imm;
-
-                memcpy(&imm, spec.src_imm, spec.dst.field->n_bytes);
-                mf_set_value(spec.dst.field, &imm, &rule);
+                && spec.dst_type == NX_LEARN_DST_MATCH) {
+                mf_write_subfield(&spec.dst, &spec.src_imm, &rule);
             }
 
             /* Output the flow_mod_spec. */
             put_u16(b, spec.n_bits | spec.src_type | spec.dst_type);
             if (spec.src_type == NX_LEARN_SRC_IMMEDIATE) {
-                int n_bytes = DIV_ROUND_UP(spec.n_bits, 8);
-                if (n_bytes % 2) {
-                    ofpbuf_put_zeros(b, 1);
-                }
-                ofpbuf_put(b, spec.src_imm, n_bytes);
+                int n_bytes = DIV_ROUND_UP(spec.n_bits, 16) * 2;
+                int ofs = sizeof spec.src_imm - n_bytes;
+                ofpbuf_put(b, &spec.src_imm.u8[ofs], n_bytes);
             } else {
                 put_u32(b, spec.src.field->nxm_header);
                 put_u16(b, spec.src.ofs);
index 88fcf26..c2d00e3 100644 (file)
@@ -134,7 +134,9 @@ OVS_VSWITCHD_START(
    add-port br0 eth1 -- set Interface eth1 type=dummy -- \
    add-port br0 eth2 -- set Interface eth2 type=dummy])
 # Set up flow table for TCPv6 port learning.
-AT_CHECK([[ovs-ofctl add-flow br0 'table=0 tcp6 actions=learn(table=1, hard_timeout=60, eth_type=0x86dd, nw_proto=6, NXM_NX_IPV6_SRC[]=NXM_NX_IPV6_DST[], NXM_NX_IPV6_DST[]=NXM_NX_IPV6_SRC[], NXM_OF_TCP_SRC[]=NXM_OF_TCP_DST[], NXM_OF_TCP_DST[]=NXM_OF_TCP_SRC[]), flood']])
+# Also add a 128-bit-wide "load" action and a 128-bit literal match to check
+# that they work.
+AT_CHECK([[ovs-ofctl add-flow br0 'table=0 tcp6 actions=learn(table=1, hard_timeout=60, eth_type=0x86dd, nw_proto=6, NXM_NX_IPV6_SRC[]=NXM_NX_IPV6_DST[], ipv6_dst=2001:0db8:85a3:0000:0000:8a2e:0370:7334, NXM_OF_TCP_SRC[]=NXM_OF_TCP_DST[], NXM_OF_TCP_DST[]=NXM_OF_TCP_SRC[], load(0x20010db885a308d313198a2e03707348->NXM_NX_IPV6_DST[])), flood']])
 
 # Trace a TCPv6 packet arriving on port 3.
 AT_CHECK([ovs-appctl ofproto/trace br0 'in_port(3),eth(src=50:54:00:00:00:05,dst=50:54:00:00:00:06),eth_type(0x86dd),ipv6(src=fec0::2,dst=fec0::1,label=0,proto=6,tclass=0,hlimit=255,frag=no),tcp(src=40000,dst=80)' -generate], [0], [stdout])
@@ -142,8 +144,9 @@ AT_CHECK([tail -1 stdout], [0], [Datapath actions: 2,0,1
 ])
 
 # Check for the learning entry.
-AT_CHECK([ovs-ofctl dump-flows br0 table=1 | ofctl_strip | sort], [0], [dnl
- table=1, hard_timeout=60,tcp6,ipv6_src=fec0::1,ipv6_dst=fec0::2,tp_src=80,tp_dst=40000 actions=drop
+AT_CHECK([ovs-ofctl dump-flows br0 | ofctl_strip | sort], [0], [dnl
+ table=1, hard_timeout=60,tcp6,ipv6_src=fec0::1,ipv6_dst=2001:db8:85a3::8a2e:370:7334,tp_src=80,tp_dst=40000 actions=load:0x13198a2e03707348->NXM_NX_IPV6_DST[[0..63]],load:0x20010db885a308d3->NXM_NX_IPV6_DST[[64..127]]
+ tcp6 actions=learn(table=1,hard_timeout=60,eth_type=0x86dd,nw_proto=6,NXM_NX_IPV6_SRC[[]]=NXM_NX_IPV6_DST[[]],ipv6_dst=2001:db8:85a3::8a2e:370:7334,NXM_OF_TCP_SRC[[]]=NXM_OF_TCP_DST[[]],NXM_OF_TCP_DST[[]]=NXM_OF_TCP_SRC[[]],load:0x20010db885a308d313198a2e03707348->NXM_NX_IPV6_DST[[]]),FLOOD
 NXST_FLOW reply:
 ])
 OVS_VSWITCHD_STOP