nxm_read_field() simplifies reading of NXM fields with an ofs_nbits
parameter.  This patch updates nxm_execute_reg_move() to use the
new function.  A user outside of the nx-match module will be added
in future patches.
---
 lib/nx-match.c |   38 ++++++++++++++++++++++++++------------
 lib/nx-match.h |    3 +++
 2 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/lib/nx-match.c b/lib/nx-match.c
index e94be95..1c66d2a 100644
--- a/lib/nx-match.c
+++ b/lib/nx-match.c
@@ -1280,7 +1280,7 @@ nxm_check_reg_load(const struct nx_action_reg_load 
*action,
 /* nxm_execute_reg_move(), nxm_execute_reg_load(). */
 
 static uint64_t
-nxm_read_field(const struct nxm_field *src, const struct flow *flow)
+nxm_read_field__(const struct nxm_field *src, const struct flow *flow)
 {
     switch (src->index) {
     case NFI_NXM_OF_IN_PORT:
@@ -1379,6 +1379,22 @@ nxm_read_field(const struct nxm_field *src, const struct 
flow *flow)
     NOT_REACHED();
 }
 
+/* Returns the value of the NXM field corresponding to 'header' at 'ofs_nbits'
+ * in 'flow'. */
+uint64_t
+nxm_read_field(ovs_be32 header, ovs_be16 ofs_nbits, const struct flow *flow)
+{
+    int n_bits = nxm_decode_n_bits(ofs_nbits);
+    int ofs = nxm_decode_ofs(ofs_nbits);
+    uint64_t mask, data;
+
+    mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
+    data = nxm_read_field__(nxm_field_lookup(ntohl(header)), flow);
+    data &= mask << ofs;
+
+    return data;
+}
+
 static void
 nxm_write_field(const struct nxm_field *dst, struct flow *flow,
                 uint64_t new_value)
@@ -1478,18 +1494,16 @@ void
 nxm_execute_reg_move(const struct nx_action_reg_move *action,
                      struct flow *flow)
 {
-    /* Preparation. */
-    int n_bits = ntohs(action->n_bits);
-    uint64_t mask = n_bits == 64 ? UINT64_MAX : (UINT64_C(1) << n_bits) - 1;
+    ovs_be16 src_ofs_nbits, dst_ofs_nbits;
+    uint64_t src_data;
+    int n_bits;
 
-    /* Get the interesting bits of the source field. */
-    const struct nxm_field *src = nxm_field_lookup(ntohl(action->src));
-    int src_ofs = ntohs(action->src_ofs);
-    uint64_t src_data = nxm_read_field(src, flow) & (mask << src_ofs);
+    n_bits = ntohs(action->n_bits);
+    src_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->src_ofs), n_bits);
+    dst_ofs_nbits = nxm_encode_ofs_nbits(ntohs(action->dst_ofs), n_bits);
 
-    nxm_reg_load(action->dst,
-                 nxm_encode_ofs_nbits(ntohs(action->dst_ofs), n_bits),
-                 src_data, flow);
+    src_data = nxm_read_field(action->src, src_ofs_nbits, flow);
+    nxm_reg_load(action->dst, dst_ofs_nbits, src_data, flow);
 }
 
 void
@@ -1511,7 +1525,7 @@ nxm_reg_load(ovs_be32 dst_header, ovs_be16 ofs_nbits, 
uint64_t src_data,
 
     /* Get remaining bits of the destination field. */
     const struct nxm_field *dst = nxm_field_lookup(ntohl(dst_header));
-    uint64_t dst_data = nxm_read_field(dst, flow) & ~(mask << dst_ofs);
+    uint64_t dst_data = nxm_read_field__(dst, flow) & ~(mask << dst_ofs);
 
     /* Get the final value. */
     uint64_t new_data = dst_data | (src_data << dst_ofs);
diff --git a/lib/nx-match.h b/lib/nx-match.h
index 264a65a..289f86e 100644
--- a/lib/nx-match.h
+++ b/lib/nx-match.h
@@ -41,6 +41,9 @@ int nx_put_match(struct ofpbuf *, const struct cls_rule *);
 char *nx_match_to_string(const uint8_t *, unsigned int match_len);
 int nx_match_from_string(const char *, struct ofpbuf *);
 
+uint64_t nxm_read_field(ovs_be32 header, ovs_be16 ofs_nbits,
+                        const struct flow *);
+
 void nxm_parse_reg_move(struct nx_action_reg_move *, const char *);
 void nxm_parse_reg_load(struct nx_action_reg_load *, const char *);
 
-- 
1.7.6

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

Reply via email to