summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2011-12-28 12:42:14 -0800
committerBen Pfaff <blp@nicira.com>2012-02-01 14:15:11 -0800
commit816fd533f85923c03cf8d9d6450bd9a0845d5160 (patch)
tree2e74c39e3bbe7e80388f76d7d1ef68f91c3141fa /lib
parent6cc7ea5ea05a71f01526577439ebedcc133f99cd (diff)
downloadopenvswitch-816fd533f85923c03cf8d9d6450bd9a0845d5160.tar.gz
meta-flow: New "subfield" data structure.
Until now, parts of a field have been dealt with in a fairly ad-hoc way. struct mf_subfield and the supporting functions added by this commit make their use more systematic. Signed-off-by: Ben Pfaff <blp@nicira.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/autopath.c35
-rw-r--r--lib/bundle.c39
-rw-r--r--lib/learn.c194
-rw-r--r--lib/meta-flow.c335
-rw-r--r--lib/meta-flow.h26
-rw-r--r--lib/multipath.c40
-rw-r--r--lib/nx-match.c293
-rw-r--r--lib/nx-match.h24
-rw-r--r--lib/ofp-parse.c9
-rw-r--r--lib/ofp-print.c12
-rw-r--r--lib/ofp-util.c6
11 files changed, 549 insertions, 464 deletions
diff --git a/lib/autopath.c b/lib/autopath.c
index 321b106d6..b8ba3c173 100644
--- a/lib/autopath.c
+++ b/lib/autopath.c
@@ -22,6 +22,7 @@
#include <stdlib.h>
#include "flow.h"
+#include "meta-flow.h"
#include "nx-match.h"
#include "ofp-errors.h"
#include "ofp-util.h"
@@ -36,23 +37,26 @@ void
autopath_execute(const struct nx_action_autopath *ap, struct flow *flow,
uint16_t ofp_port)
{
- nxm_reg_load(ap->dst, ap->ofs_nbits, ofp_port, flow);
+ struct mf_subfield dst;
+
+ nxm_decode(&dst, ap->dst, ap->ofs_nbits);
+ mf_set_subfield_value(&dst, ofp_port, flow);
}
void
autopath_parse(struct nx_action_autopath *ap, const char *s_)
{
char *s;
- uint32_t reg;
- int id_int, ofs, n_bits;
- char *id_str, *dst, *save_ptr;
+ char *id_str, *dst_s, *save_ptr;
+ struct mf_subfield dst;
+ int id_int;
s = xstrdup(s_);
save_ptr = NULL;
id_str = strtok_r(s, ", ", &save_ptr);
- dst = strtok_r(NULL, ", ", &save_ptr);
+ dst_s = strtok_r(NULL, ", ", &save_ptr);
- if (!dst) {
+ if (!dst_s) {
ovs_fatal(0, "%s: not enough arguments to autopath action", s_);
}
@@ -62,16 +66,17 @@ autopath_parse(struct nx_action_autopath *ap, const char *s_)
"1 to %"PRIu32, s_, id_int, UINT32_MAX);
}
- nxm_parse_field_bits(dst, &reg, &ofs, &n_bits);
- if (n_bits < 16) {
+ mf_parse_subfield(&dst, dst_s);
+ if (dst.n_bits < 16) {
ovs_fatal(0, "%s: %d-bit destination field has %u possible values, "
- "less than required 65536", s_, n_bits, 1u << n_bits);
+ "less than required 65536",
+ s_, dst.n_bits, 1u << dst.n_bits);
}
ofputil_init_NXAST_AUTOPATH(ap);
ap->id = htonl(id_int);
- ap->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
- ap->dst = htonl(reg);
+ ap->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
+ ap->dst = htonl(dst.field->nxm_header);
free(s);
}
@@ -79,14 +84,14 @@ autopath_parse(struct nx_action_autopath *ap, const char *s_)
enum ofperr
autopath_check(const struct nx_action_autopath *ap, const struct flow *flow)
{
- int n_bits = nxm_decode_n_bits(ap->ofs_nbits);
- int ofs = nxm_decode_ofs(ap->ofs_nbits);
+ struct mf_subfield dst;
- if (n_bits < 16) {
+ nxm_decode(&dst, ap->dst, ap->ofs_nbits);
+ if (dst.n_bits < 16) {
VLOG_WARN("at least 16 bit destination is required for autopath "
"action.");
return OFPERR_OFPBAC_BAD_ARGUMENT;
}
- return nxm_dst_check(ap->dst, ofs, n_bits, flow);
+ return mf_check_dst(&dst, flow);
}
diff --git a/lib/bundle.c b/lib/bundle.c
index 8e98fb57c..733d79a98 100644
--- a/lib/bundle.c
+++ b/lib/bundle.c
@@ -22,6 +22,7 @@
#include "dynamic-string.h"
#include "multipath.h"
+#include "meta-flow.h"
#include "nx-match.h"
#include "ofpbuf.h"
#include "ofp-errors.h"
@@ -94,8 +95,11 @@ bundle_execute_load(const struct nx_action_bundle *nab, struct flow *flow,
bool (*slave_enabled)(uint16_t ofp_port, void *aux),
void *aux)
{
- nxm_reg_load(nab->dst, nab->ofs_nbits,
- bundle_execute(nab, flow, slave_enabled, aux), flow);
+ struct mf_subfield dst;
+
+ nxm_decode(&dst, nab->dst, nab->ofs_nbits);
+ mf_set_subfield_value(&dst, bundle_execute(nab, flow, slave_enabled, aux),
+ flow);
}
/* Checks that 'nab' specifies a bundle action which is supported by this
@@ -146,15 +150,15 @@ bundle_check(const struct nx_action_bundle *nab, int max_ports,
}
if (subtype == NXAST_BUNDLE_LOAD) {
- int ofs = nxm_decode_ofs(nab->ofs_nbits);
- int n_bits = nxm_decode_n_bits(nab->ofs_nbits);
+ struct mf_subfield dst;
- if (n_bits < 16) {
+ nxm_decode(&dst, nab->dst, nab->ofs_nbits);
+ if (dst.n_bits < 16) {
VLOG_WARN_RL(&rl, "bundle_load action requires at least 16 bit "
"destination.");
error = OFPERR_OFPBAC_BAD_ARGUMENT;
} else if (!error) {
- error = nxm_dst_check(nab->dst, ofs, n_bits, flow);
+ error = mf_check_dst(&dst, flow);
}
}
@@ -192,7 +196,7 @@ bundle_check(const struct nx_action_bundle *nab, int max_ports,
static void
bundle_parse__(struct ofpbuf *b, const char *s, char **save_ptr,
const char *fields, const char *basis, const char *algorithm,
- const char *slave_type, const char *dst,
+ const char *slave_type, const char *dst_s,
const char *slave_delim)
{
enum ofputil_action_code code;
@@ -208,7 +212,7 @@ bundle_parse__(struct ofpbuf *b, const char *s, char **save_ptr,
s, slave_delim);
}
- code = dst ? OFPUTIL_NXAST_BUNDLE_LOAD : OFPUTIL_NXAST_BUNDLE;
+ code = dst_s ? OFPUTIL_NXAST_BUNDLE_LOAD : OFPUTIL_NXAST_BUNDLE;
b->l2 = ofputil_put_action(code, b);
n_slaves = 0;
@@ -259,14 +263,12 @@ bundle_parse__(struct ofpbuf *b, const char *s, char **save_ptr,
ovs_fatal(0, "%s: unknown slave_type `%s'", s, slave_type);
}
- if (dst) {
- uint32_t reg;
- int ofs, n_bits;
-
- nxm_parse_field_bits(dst, &reg, &ofs, &n_bits);
+ if (dst_s) {
+ struct mf_subfield dst;
- nab->dst = htonl(reg);
- nab->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
+ mf_parse_subfield(&dst, dst_s);
+ nab->dst = htonl(dst.field->nxm_header);
+ nab->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
}
b->l2 = NULL;
@@ -359,9 +361,10 @@ bundle_format(const struct nx_action_bundle *nab, struct ds *s)
ntohs(nab->basis), algorithm, slave_type);
if (nab->subtype == htons(NXAST_BUNDLE_LOAD)) {
- nxm_format_field_bits(s, ntohl(nab->dst),
- nxm_decode_ofs(nab->ofs_nbits),
- nxm_decode_n_bits(nab->ofs_nbits));
+ struct mf_subfield dst;
+
+ nxm_decode(&dst, nab->dst, nab->ofs_nbits);
+ mf_format_subfield(&dst, s);
ds_put_cstr(s, ",");
}
diff --git a/lib/learn.c b/lib/learn.c
index 241f3d1fb..167646217 100644
--- a/lib/learn.c
+++ b/lib/learn.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2011 Nicira Networks.
+ * Copyright (c) 2011, 2012 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -59,6 +59,14 @@ get_bits(int n_bits, const void **p)
return value;
}
+static void
+get_subfield(int n_bits, const void **p, struct mf_subfield *sf)
+{
+ sf->field = mf_from_nxm_header(ntohl(get_be32(p)));
+ sf->ofs = ntohs(get_be16(p));
+ sf->n_bits = n_bits;
+}
+
static unsigned int
learn_min_len(uint16_t header)
{
@@ -143,10 +151,10 @@ learn_check(const struct nx_action_learn *learn, const struct flow *flow)
/* Check the source. */
if (src_type == NX_LEARN_SRC_FIELD) {
- ovs_be32 src_field = get_be32(&p);
- int src_ofs = ntohs(get_be16(&p));
+ struct mf_subfield src;
- error = nxm_src_check(src_field, src_ofs, n_bits, flow);
+ get_subfield(n_bits, &p, &src);
+ error = mf_check_src(&src, flow);
if (error) {
return error;
}
@@ -157,20 +165,19 @@ learn_check(const struct nx_action_learn *learn, const struct flow *flow)
/* Check the destination. */
if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) {
- ovs_be32 dst_field = get_be32(&p);
- int dst_ofs = ntohs(get_be16(&p));
+ struct mf_subfield dst;
+ get_subfield(n_bits, &p, &dst);
error = (dst_type == NX_LEARN_DST_LOAD
- ? nxm_dst_check(dst_field, dst_ofs, n_bits, &rule.flow)
- : nxm_src_check(dst_field, dst_ofs, n_bits, &rule.flow));
+ ? mf_check_dst(&dst, &rule.flow)
+ : mf_check_src(&dst, &rule.flow));
if (error) {
return error;
}
if (dst_type == NX_LEARN_DST_MATCH
&& src_type == NX_LEARN_SRC_IMMEDIATE) {
- mf_set_subfield(mf_from_nxm_header(ntohl(dst_field)), value,
- dst_ofs, n_bits, &rule);
+ mf_set_subfield(&dst, value, &rule);
}
}
}
@@ -210,38 +217,32 @@ learn_execute(const struct nx_action_learn *learn, const struct flow *flow,
uint64_t value;
struct nx_action_reg_load *load;
- ovs_be32 dst_field;
- int dst_ofs;
+ struct mf_subfield dst;
if (!header) {
break;
}
if (src_type == NX_LEARN_SRC_FIELD) {
- ovs_be32 src_field = get_be32(&p);
- int src_ofs = ntohs(get_be16(&p));
+ struct mf_subfield src;
- value = nxm_read_field_bits(src_field,
- nxm_encode_ofs_nbits(src_ofs, n_bits),
- flow);
+ get_subfield(n_bits, &p, &src);
+ value = mf_get_subfield(&src, flow);
} else {
value = get_bits(n_bits, &p);
}
switch (dst_type) {
case NX_LEARN_DST_MATCH:
- dst_field = get_be32(&p);
- dst_ofs = ntohs(get_be16(&p));
- mf_set_subfield(mf_from_nxm_header(ntohl(dst_field)), value,
- dst_ofs, n_bits, &fm->cr);
+ get_subfield(n_bits, &p, &dst);
+ mf_set_subfield(&dst, value, &fm->cr);
break;
case NX_LEARN_DST_LOAD:
- dst_field = get_be32(&p);
- dst_ofs = ntohs(get_be16(&p));
+ get_subfield(n_bits, &p, &dst);
load = ofputil_put_NXAST_REG_LOAD(&actions);
- load->ofs_nbits = nxm_encode_ofs_nbits(dst_ofs, n_bits);
- load->dst = dst_field;
+ load->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
+ load->dst = htonl(dst.field->nxm_header);
load->value = htonll(value);
break;
@@ -283,19 +284,18 @@ struct learn_spec {
int n_bits;
int src_type;
- const struct mf_field *src;
- int src_ofs;
+ struct mf_subfield src;
uint8_t src_imm[sizeof(union mf_value)];
int dst_type;
- const struct mf_field *dst;
- int dst_ofs;
+ struct mf_subfield dst;
};
static void
learn_parse_spec(const char *orig, char *name, char *value,
struct learn_spec *spec)
{
+ memset(spec, 0, sizeof *spec);
if (mf_from_name(name)) {
const struct mf_field *dst = mf_from_name(name);
union mf_value imm;
@@ -308,49 +308,36 @@ learn_parse_spec(const char *orig, char *name, char *value,
spec->n_bits = dst->n_bits;
spec->src_type = NX_LEARN_SRC_IMMEDIATE;
- spec->src = NULL;
- spec->src_ofs = 0;
memcpy(spec->src_imm, &imm, dst->n_bytes);
spec->dst_type = NX_LEARN_DST_MATCH;
- spec->dst = dst;
- spec->dst_ofs = 0;
+ spec->dst.field = dst;
+ spec->dst.ofs = 0;
+ spec->dst.n_bits = dst->n_bits;
} else if (strchr(name, '[')) {
- uint32_t src_header, dst_header;
- int src_ofs, dst_ofs;
- int n_bits;
-
/* Parse destination and check prerequisites. */
- if (nxm_parse_field_bits(name, &dst_header, &dst_ofs,
- &n_bits)[0] != '\0') {
+ if (mf_parse_subfield(&spec->dst, name)[0] != '\0') {
ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
orig, name);
}
/* Parse source and check prerequisites. */
if (value[0] != '\0') {
- int src_nbits;
-
- if (nxm_parse_field_bits(value, &src_header, &src_ofs,
- &src_nbits)[0] != '\0') {
+ if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
orig, value);
}
- if (src_nbits != n_bits) {
- ovs_fatal(0, "%s: bit widths of %s (%d) and %s (%d) differ",
- orig, name, dst_header, value, dst_header);
+ if (spec->src.n_bits != spec->dst.n_bits) {
+ ovs_fatal(0, "%s: bit widths of %s (%u) and %s (%u) differ",
+ orig, name, spec->src.n_bits, value,
+ spec->dst.n_bits);
}
} else {
- src_header = dst_header;
- src_ofs = dst_ofs;
+ spec->src = spec->dst;
}
- spec->n_bits = n_bits;
+ spec->n_bits = spec->src.n_bits;
spec->src_type = NX_LEARN_SRC_FIELD;
- spec->src = mf_from_nxm_header(src_header);
- spec->src_ofs = src_ofs;
spec->dst_type = NX_LEARN_DST_MATCH;
- spec->dst = mf_from_nxm_header(dst_header);
- spec->dst_ofs = 0;
} else if (!strcmp(name, "load")) {
if (value[strcspn(value, "[-")] == '-') {
struct nx_action_reg_load load;
@@ -365,14 +352,11 @@ learn_parse_spec(const char *orig, char *name, char *value,
spec->n_bits = nbits;
spec->src_type = NX_LEARN_SRC_IMMEDIATE;
- spec->src = NULL;
- spec->src_ofs = 0;
for (i = 0; i < imm_bytes; i++) {
spec->src_imm[i] = imm >> ((imm_bytes - i - 1) * 8);
}
spec->dst_type = NX_LEARN_DST_LOAD;
- spec->dst = mf_from_nxm_header(ntohl(load.dst));
- spec->dst_ofs = nxm_decode_ofs(load.ofs_nbits);
+ nxm_decode(&spec->dst, load.dst, load.ofs_nbits);
} else {
struct nx_action_reg_move move;
@@ -380,28 +364,21 @@ learn_parse_spec(const char *orig, char *name, char *value,
spec->n_bits = ntohs(move.n_bits);
spec->src_type = NX_LEARN_SRC_FIELD;
- spec->src = mf_from_nxm_header(ntohl(move.src));
- spec->src_ofs = ntohs(move.src_ofs);
+ nxm_decode_discrete(&spec->src,
+ move.src, move.src_ofs, move.n_bits);
spec->dst_type = NX_LEARN_DST_LOAD;
- spec->dst = mf_from_nxm_header(ntohl(move.dst));
- spec->dst_ofs = ntohs(move.dst_ofs);
+ nxm_decode_discrete(&spec->dst,
+ move.dst, move.dst_ofs, move.n_bits);
}
} else if (!strcmp(name, "output")) {
- uint32_t header;
- int ofs, n_bits;
-
- if (nxm_parse_field_bits(value, &header, &ofs, &n_bits)[0] != '\0') {
+ if (mf_parse_subfield(&spec->src, value)[0] != '\0') {
ovs_fatal(0, "%s: syntax error after NXM field name `%s'",
orig, name);
}
- spec->n_bits = n_bits;
+ spec->n_bits = spec->src.n_bits;
spec->src_type = NX_LEARN_SRC_FIELD;
- spec->src = mf_from_nxm_header(header);
- spec->src_ofs = ofs;
spec->dst_type = NX_LEARN_DST_OUTPUT;
- spec->dst = NULL;
- spec->dst_ofs = 0;
} else {
ovs_fatal(0, "%s: unknown keyword %s", orig, name);
}
@@ -452,29 +429,29 @@ learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow)
/* Check prerequisites. */
if (spec.src_type == NX_LEARN_SRC_FIELD
- && !mf_are_prereqs_ok(spec.src, flow)) {
+ && !mf_are_prereqs_ok(spec.src.field, flow)) {
ovs_fatal(0, "%s: cannot specify source field %s because "
"prerequisites are not satisfied",
- orig, spec.src->name);
+ orig, spec.src.field->name);
}
if ((spec.dst_type == NX_LEARN_DST_MATCH
|| spec.dst_type == NX_LEARN_DST_LOAD)
- && !mf_are_prereqs_ok(spec.dst, &rule.flow)) {
+ && !mf_are_prereqs_ok(spec.dst.field, &rule.flow)) {
ovs_fatal(0, "%s: cannot specify destination field %s because "
"prerequisites are not satisfied",
- orig, spec.dst->name);
+ orig, spec.dst.field->name);
}
/* 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->n_bytes * 8) {
+ && spec.dst.ofs == 0
+ && spec.n_bits == spec.dst.field->n_bytes * 8) {
union mf_value imm;
- memcpy(&imm, spec.src_imm, spec.dst->n_bytes);
- mf_set_value(spec.dst, &imm, &rule);
+ memcpy(&imm, spec.src_imm, spec.dst.field->n_bytes);
+ mf_set_value(spec.dst.field, &imm, &rule);
}
/* Output the flow_mod_spec. */
@@ -486,13 +463,13 @@ learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow)
}
ofpbuf_put(b, spec.src_imm, n_bytes);
} else {
- put_u32(b, spec.src->nxm_header);
- put_u16(b, spec.src_ofs);
+ put_u32(b, spec.src.field->nxm_header);
+ put_u16(b, spec.src.ofs);
}
if (spec.dst_type == NX_LEARN_DST_MATCH ||
spec.dst_type == NX_LEARN_DST_LOAD) {
- put_u32(b, spec.dst->nxm_header);
- put_u16(b, spec.dst_ofs);
+ put_u32(b, spec.dst.field->nxm_header);
+ put_u16(b, spec.dst.ofs);
} else {
assert(spec.dst_type == NX_LEARN_DST_OUTPUT);
}
@@ -555,15 +532,12 @@ learn_format(const struct nx_action_learn *learn, struct ds *s)
int n_bits = header & NX_LEARN_N_BITS_MASK;
int src_type = header & NX_LEARN_SRC_MASK;
- uint32_t src_header;
- int src_ofs;
+ struct mf_subfield src;
const uint8_t *src_value;
int src_value_bytes;
int dst_type = header & NX_LEARN_DST_MASK;
- uint32_t dst_header;
- int dst_ofs;
- const struct mf_field *dst_field;
+ struct mf_subfield dst;
enum ofperr error;
int i;
@@ -589,13 +563,13 @@ learn_format(const struct nx_action_learn *learn, struct ds *s)
/* Get the source. */
if (src_type == NX_LEARN_SRC_FIELD) {
- src_header = ntohl(get_be32(&p));
- src_ofs = ntohs(get_be16(&p));
+ get_subfield(n_bits, &p, &src);
src_value_bytes = 0;
src_value = NULL;
} else {
- src_header = 0;
- src_ofs = 0;
+ src.field = NULL;
+ src.ofs = 0;
+ src.n_bits = 0;
src_value_bytes = 2 * DIV_ROUND_UP(n_bits, 16);
src_value = p;
p = (const void *) ((const uint8_t *) p + src_value_bytes);
@@ -603,41 +577,39 @@ learn_format(const struct nx_action_learn *learn, struct ds *s)
/* Get the destination. */
if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) {
- dst_header = ntohl(get_be32(&p));
- dst_field = mf_from_nxm_header(dst_header);
- dst_ofs = ntohs(get_be16(&p));
+ get_subfield(n_bits, &p, &dst);
} else {
- dst_header = 0;
- dst_field = NULL;
- dst_ofs = 0;
+ dst.field = NULL;
+ dst.ofs = 0;
+ dst.n_bits = 0;
}
ds_put_char(s, ',');
switch (src_type | dst_type) {
case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH:
- if (dst_field && dst_ofs == 0 && n_bits == dst_field->n_bits) {
+ if (dst.field && dst.ofs == 0 && n_bits == dst.field->n_bits) {
union mf_value value;
uint8_t *bytes = (uint8_t *) &value;
- if (src_value_bytes > dst_field->n_bytes) {
+ if (src_value_bytes > dst.field->n_bytes) {
/* The destination field is an odd number of bytes, which
* got rounded up to a multiple of 2 to be put into the
* learning action. Skip over the leading byte, which
* should be zero anyway. Otherwise the memcpy() below
* will overrun the start of 'value'. */
- int diff = src_value_bytes - dst_field->n_bytes;
+ int diff = src_value_bytes - dst.field->n_bytes;
src_value += diff;
src_value_bytes -= diff;
}
memset(&value, 0, sizeof value);
- memcpy(&bytes[dst_field->n_bytes - src_value_bytes],
+ memcpy(&bytes[dst.field->n_bytes - src_value_bytes],
src_value, src_value_bytes);
- ds_put_format(s, "%s=", dst_field->name);
- mf_format(dst_field, &value, NULL, s);
+ ds_put_format(s, "%s=", dst.field->name);
+ mf_format(dst.field, &value, NULL, s);
} else {
- nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
+ mf_format_subfield(&dst, s);
ds_put_cstr(s, "=0x");
for (i = 0; i < src_value_bytes; i++) {
ds_put_format(s, "%02"PRIx8, src_value[i]);
@@ -646,10 +618,10 @@ learn_format(const struct nx_action_learn *learn, struct ds *s)
break;
case NX_LEARN_SRC_FIELD | NX_LEARN_DST_MATCH:
- nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
- if (src_header != dst_header || src_ofs != dst_ofs) {
+ mf_format_subfield(&dst, s);
+ if (src.field != dst.field || src.ofs != dst.ofs) {
ds_put_char(s, '=');
- nxm_format_field_bits(s, src_header, src_ofs, n_bits);
+ mf_format_subfield(&src, s);
}
break;
@@ -659,19 +631,19 @@ learn_format(const struct nx_action_learn *learn, struct ds *s)
ds_put_format(s, "%02"PRIx8, src_value[i]);
}
ds_put_cstr(s, "->");
- nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
+ mf_format_subfield(&dst, s);
break;
case NX_LEARN_SRC_FIELD | NX_LEARN_DST_LOAD:
ds_put_cstr(s, "load:");
- nxm_format_field_bits(s, src_header, src_ofs, n_bits);
+ mf_format_subfield(&src, s);
ds_put_cstr(s, "->");
- nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
+ mf_format_subfield(&dst, s);
break;
case NX_LEARN_SRC_FIELD | NX_LEARN_DST_OUTPUT:
ds_put_cstr(s, "output:");
- nxm_format_field_bits(s, src_header, src_ofs, n_bits);
+ mf_format_subfield(&src, s);
break;
}
}
diff --git a/lib/meta-flow.c b/lib/meta-flow.c
index 41cbf4944..a0e2aa82b 100644
--- a/lib/meta-flow.c
+++ b/lib/meta-flow.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2011 Nicira Networks.
+ * Copyright (c) 2011, 2012 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,12 +26,16 @@
#include "classifier.h"
#include "dynamic-string.h"
+#include "ofp-errors.h"
#include "ofp-util.h"
#include "packets.h"
#include "random.h"
#include "shash.h"
#include "socket-util.h"
#include "unaligned.h"
+#include "vlog.h"
+
+VLOG_DEFINE_THIS_MODULE(meta_flow);
#define MF_FIELD_SIZES(MEMBER) \
sizeof ((union mf_value *)0)->MEMBER, \
@@ -394,6 +398,10 @@ struct nxm_field {
static struct hmap all_nxm_fields = HMAP_INITIALIZER(&all_nxm_fields);
+/* Rate limit for parse errors. These always indicate a bug in an OpenFlow
+ * controller and so there's not much point in showing a lot of them. */
+static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
+
/* Returns the field with the given 'id'. */
const struct mf_field *
mf_from_id(enum mf_field_id id)
@@ -1621,77 +1629,53 @@ mf_set(const struct mf_field *mf,
}
}
-/* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
- * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
- * 'x'.
- *
- * Example: suppose that 'mf' is originally the following 2-byte field in
- * 'rule':
- *
- * value == 0xe00a == 2#1110000000001010
- * mask == 0xfc3f == 2#1111110000111111
- *
- * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
- * effect (note that 0x55 is 2#1010101):
- *
- * value == 0xd50a == 2#1101010100001010
- * mask == 0xff3f == 2#1111111100111111
- *
- * The caller is responsible for ensuring that the result will be a valid
- * wildcard pattern for 'mf'. The caller is responsible for ensuring that
- * 'rule' meets 'mf''s prerequisites. */
-void
-mf_set_subfield(const struct mf_field *mf, uint64_t x, unsigned int ofs,
- unsigned int n_bits, struct cls_rule *rule)
+static enum ofperr
+mf_check__(const struct mf_subfield *sf, const struct flow *flow,
+ const char *type)
{
- if (ofs == 0 && mf->n_bytes * 8 == n_bits) {
- union mf_value value;
- int i;
-
- for (i = mf->n_bytes - 1; i >= 0; i--) {
- ((uint8_t *) &value)[i] = x;
- x >>= 8;
- }
- mf_set_value(mf, &value, rule);
+ if (!sf->field) {
+ VLOG_WARN_RL(&rl, "unknown %s field", type);
+ } else if (!sf->n_bits) {
+ VLOG_WARN_RL(&rl, "zero bit %s field %s", type, sf->field->name);
+ } else if (sf->ofs >= sf->field->n_bits) {
+ VLOG_WARN_RL(&rl, "bit offset %d exceeds %d-bit width of %s field %s",
+ sf->ofs, sf->field->n_bits, type, sf->field->name);
+ } else if (sf->ofs + sf->n_bits > sf->field->n_bits) {
+ VLOG_WARN_RL(&rl, "bit offset %d and width %d exceeds %d-bit width "
+ "of %s field %s", sf->ofs, sf->n_bits,
+ sf->field->n_bits, type, sf->field->name);
+ } else if (flow && !mf_are_prereqs_ok(sf->field, flow)) {
+ VLOG_WARN_RL(&rl, "%s field %s lacks correct prerequisites",
+ type, sf->field->name);
} else {
- union mf_value value, mask;
- uint8_t *vp, *mp;
- unsigned int byte_ofs;
-
- mf_get(mf, rule, &value, &mask);
-
- byte_ofs = mf->n_bytes - ofs / 8;
- vp = &((uint8_t *) &value)[byte_ofs];
- mp = &((uint8_t *) &mask)[byte_ofs];
- if (ofs % 8) {
- unsigned int chunk = MIN(8 - ofs % 8, n_bits);
- uint8_t chunk_mask = ((1 << chunk) - 1) << (ofs % 8);
-
- *--vp &= ~chunk_mask;
- *vp |= chunk_mask & (x << (ofs % 8));
- *--mp |= chunk_mask;
+ return 0;
+ }
- x >>= chunk;
- n_bits -= chunk;
- ofs += chunk;
- }
- while (n_bits >= 8) {
- *--vp = x;
- *--mp = 0xff;
- x >>= 8;
- n_bits -= 8;
- ofs += 8;
- }
- if (n_bits) {
- uint8_t chunk_mask = (1 << n_bits) - 1;
+ return OFPERR_OFPBAC_BAD_ARGUMENT;
+}
- *--vp &= ~chunk_mask;
- *vp |= chunk_mask & x;
- *--mp |= chunk_mask;
- }
+/* Checks whether 'sf' is valid for reading a subfield out of 'flow'. Returns
+ * 0 if so, otherwise an OpenFlow error code (e.g. as returned by
+ * ofp_mkerr()). */
+enum ofperr
+mf_check_src(const struct mf_subfield *sf, const struct flow *flow)
+{
+ return mf_check__(sf, flow, "source");
+}
- mf_set(mf, &value, &mask, rule);
+/* Checks whether 'sf' is valid for writing a subfield into 'flow'. Returns 0
+ * if so, otherwise an OpenFlow error code (e.g. as returned by
+ * ofp_mkerr()). */
+enum ofperr
+mf_check_dst(const struct mf_subfield *sf, const struct flow *flow)
+{
+ int error = mf_check__(sf, flow, "destination");
+ if (!error && !sf->field->writable) {
+ VLOG_WARN_RL(&rl, "destination field %s is not writable",
+ sf->field->name);
+ return OFPERR_OFPBAC_BAD_ARGUMENT;
}
+ return error;
}
/* Copies the value and wildcard bit pattern for 'mf' from 'rule' into the
@@ -2141,3 +2125,222 @@ mf_format(const struct mf_field *mf,
NOT_REACHED();
}
}
+
+/* Makes a subfield starting at bit offset 'ofs' and continuing for 'n_bits' in
+ * 'rule''s field 'mf' exactly match the 'n_bits' least-significant bits of
+ * 'x'.
+ *
+ * Example: suppose that 'mf' is originally the following 2-byte field in
+ * 'rule':
+ *
+ * value == 0xe00a == 2#1110000000001010
+ * mask == 0xfc3f == 2#1111110000111111
+ *
+ * The call mf_set_subfield(mf, 0x55, 8, 7, rule) would have the following
+ * effect (note that 0x55 is 2#1010101):
+ *
+ * value == 0xd50a == 2#1101010100001010
+ * mask == 0xff3f == 2#1111111100111111
+ *
+ * The caller is responsible for ensuring that the result will be a valid
+ * wildcard pattern for 'mf'. The caller is responsible for ensuring that
+ * 'rule' meets 'mf''s prerequisites. */
+void
+mf_set_subfield(const struct mf_subfield *sf, uint64_t x,
+ struct cls_rule *rule)
+{
+ const struct mf_field *field = sf->field;
+ unsigned int n_bits = sf->n_bits;
+ unsigned int ofs = sf->ofs;
+
+ if (ofs == 0 && field->n_bytes * 8 == n_bits) {
+ union mf_value value;
+ int i;
+
+ for (i = field->n_bytes - 1; i >= 0; i--) {
+ ((uint8_t *) &value)[i] = x;
+ x >>= 8;
+ }
+ mf_set_value(field, &value, rule);
+ } else {
+ union mf_value value, mask;
+ uint8_t *vp = (uint8_t *) &value;
+ uint8_t *mp = (uint8_t *) &mask;
+
+ mf_get(field, rule, &value, &mask);
+ bitwise_put(x, vp, field->n_bytes, ofs, n_bits);
+ bitwise_put(UINT64_MAX, mp, field->n_bytes, ofs, n_bits);
+ mf_set(field, &value, &mask, rule);
+ }
+}
+
+/* Similar to mf_set_subfield() but modifies only a flow, not a cls_rule. */
+void
+mf_set_subfield_value(const struct mf_subfield *sf, uint64_t x,
+ struct flow *flow)
+{
+ const struct mf_field *field = sf->field;
+ unsigned int n_bits = sf->n_bits;
+ unsigned int ofs = sf->ofs;
+ union mf_value value;
+
+ if (ofs == 0 && field->n_bytes * 8 == n_bits) {
+ int i;
+
+ for (i = field->n_bytes - 1; i >= 0; i--) {
+ ((uint8_t *) &value)[i] = x;
+ x >>= 8;
+ }
+ mf_set_flow_value(field, &value, flow);
+ } else {
+ mf_get_value(field, flow, &value);
+ bitwise_put(x, &value, field->n_bytes, ofs, n_bits);
+ mf_set_flow_value(field, &value, flow);
+ }
+}
+
+/* Returns the value of 'sf' within 'flow'. 'sf' must be valid for reading
+ * 'flow', e.g. as checked by mf_check_src() and sf->n_bits must be 64 or
+ * less. */
+uint64_t
+mf_get_subfield(const struct mf_subfield *sf, const struct flow *flow)
+{
+ union mf_value value;
+
+ mf_get_value(sf->field, flow, &value);
+ return bitwise_get(&value, sf->field->n_bytes, sf->ofs, sf->n_bits);
+}
+
+/* Formats 'sf' into 's' in a format normally acceptable to
+ * mf_parse_subfield(). (It won't be acceptable if sf->field is NULL or if
+ * sf->field has no NXM name.) */
+void
+mf_format_subfield(const struct mf_subfield *sf, struct ds *s)
+{
+ if (!sf->field) {
+ ds_put_cstr(s, "<unknown>");
+ } else if (sf->field->nxm_name) {
+ ds_put_cstr(s, sf->field->nxm_name);
+ } else if (sf->field->nxm_header) {
+ uint32_t header = sf->field->nxm_header;
+ ds_put_format(s, "%d:%d", NXM_VENDOR(header), NXM_FIELD(header));
+ } else {
+ ds_put_cstr(s, sf->field->name);
+ }
+
+ if (sf->ofs == 0 && sf->n_bits == sf->field->n_bits) {
+ ds_put_cstr(s, "[]");
+ } else if (sf->n_bits == 1) {
+ ds_put_format(s, "[%d]", sf->ofs);
+ } else {
+ ds_put_format(s, "[%d..%d]", sf->ofs, sf->ofs + sf->n_bits - 1);
+ }
+}
+
+static const struct mf_field *
+mf_parse_subfield_name(const char *name, int name_len, bool *wild)
+{
+ int i;
+
+ *wild = name_len > 2 && !memcmp(&name[name_len - 2], "_W", 2);
+ if (*wild) {
+ name_len -= 2;
+ }
+
+ for (i = 0; i < MFF_N_IDS; i++) {
+ const struct mf_field *mf = mf_from_id(i);
+
+ if (mf->nxm_name
+ && !strncmp(mf->nxm_name, name, name_len)
+ && mf->nxm_name[name_len] == '\0') {
+ return mf;
+ }
+ }
+
+ return NULL;
+}
+
+/* Parses a subfield from the beginning of '*sp' into 'sf'. If successful,
+ * returns NULL and advances '*sp' to the first byte following the parsed
+ * string. On failure, returns a malloc()'d error message, does not modify
+ * '*sp', and does not properly initialize 'sf'.
+ *
+ * The syntax parsed from '*sp' takes the form "header[start..end]" where
+ * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
+ * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
+ * may both be omitted (the [] are still required) to indicate an entire
+ * field. */
+char *
+mf_parse_subfield__(struct mf_subfield *sf, const char **sp)
+{
+ const struct mf_field *field;
+ const char *name;
+ int start, end;
+ const char *s;
+ int name_len;
+ bool wild;
+
+ s = *sp;
+ name = s;
+ name_len = strcspn(s, "[");
+ if (s[name_len] != '[') {
+ return xasprintf("%s: missing [ looking for field name", *sp);
+ }
+
+ field = mf_parse_subfield_name(name, name_len, &wild);
+ if (!field) {
+ return xasprintf("%s: unknown field `%.*s'", *sp, name_len, s);
+ }
+
+ s += name_len;
+ if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
+ /* Nothing to do. */
+ } else if (sscanf(s, "[%d]", &start) == 1) {
+ end = start;
+ } else if (!strncmp(s, "[]", 2)) {
+ start = 0;
+ end = field->n_bits - 1;
+ } else {
+ return xasprintf("%s: syntax error expecting [] or [<bit>] or "
+ "[<start>..<end>]", *sp);
+ }
+ s = strchr(s, ']') + 1;
+
+ if (start > end) {
+ return xasprintf("%s: starting bit %d is after ending bit %d",
+ *sp, start, end);
+ } else if (start >= field->n_bits) {
+ return xasprintf("%s: starting bit %d is not valid because field is "
+ "only %d bits wide", *sp, start, field->n_bits);
+ } else if (end >= field->n_bits){
+ return xasprintf("%s: ending bit %d is not valid because field is "
+ "only %d bits wide", *sp, end, field->n_bits);
+ }
+
+ sf->field = field;
+ sf->ofs = start;
+ sf->n_bits = end - start + 1;
+
+ *sp = s;
+ return NULL;
+}
+
+/* Parses a subfield from the beginning of 's' into 'sf'. Returns the first
+ * byte in 's' following the parsed string.
+ *
+ * Exits with an error message if 's' has incorrect syntax.
+ *
+ * The syntax parsed from 's' takes the form "header[start..end]" where
+ * 'header' is the name of an NXM field and 'start' and 'end' are (inclusive)
+ * bit indexes. "..end" may be omitted to indicate a single bit. "start..end"
+ * may both be omitted (the [] are still required) to indicate an entire
+ * field. */
+const char *
+mf_parse_subfield(struct mf_subfield *sf, const char *s)
+{
+ char *msg = mf_parse_subfield__(sf, &s);
+ if (msg) {
+ ovs_fatal(0, "%s", msg);
+ }
+ return s;
+}
diff --git a/lib/meta-flow.h b/lib/meta-flow.h
index a552e3ce8..491783476 100644
--- a/lib/meta-flow.h
+++ b/lib/meta-flow.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2011 Nicira Networks.
+ * Copyright (c) 2011, 2012 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
#include <netinet/in.h>
#include <netinet/ip6.h>
#include "flow.h"
+#include "ofp-errors.h"
#include "packets.h"
struct cls_rule;
@@ -198,6 +199,13 @@ union mf_value {
struct in6_addr ipv6;
};
+/* Part of a field. */
+struct mf_subfield {
+ const struct mf_field *field;
+ unsigned int ofs; /* Bit offset. */
+ unsigned int n_bits; /* Number of bits. */
+};
+
/* Finding mf_fields. */
const struct mf_field *mf_from_id(enum mf_field_id);
const struct mf_field *mf_from_name(const char *name);
@@ -230,13 +238,25 @@ void mf_get(const struct mf_field *, const struct cls_rule *,
void mf_set(const struct mf_field *,
const union mf_value *value, const union mf_value *mask,
struct cls_rule *);
-void mf_set_subfield(const struct mf_field *, uint64_t value, unsigned int ofs,
- unsigned int n_bits, struct cls_rule *);
void mf_set_wild(const struct mf_field *, struct cls_rule *);
void mf_random_value(const struct mf_field *, union mf_value *value);
+/* Subfields. */
+void mf_set_subfield(const struct mf_subfield *, uint64_t value,
+ struct cls_rule *);
+void mf_set_subfield_value(const struct mf_subfield *, uint64_t value,
+ struct flow *);
+uint64_t mf_get_subfield(const struct mf_subfield *, const struct flow *);
+
+void mf_format_subfield(const struct mf_subfield *, struct ds *);
+char *mf_parse_subfield__(struct mf_subfield *sf, const char **s);
+const char *mf_parse_subfield(struct mf_subfield *, const char *);
+
+enum ofperr mf_check_src(const struct mf_subfield *, const struct flow *);
+enum ofperr mf_check_dst(const struct mf_subfield *, const struct flow *);
+
/* Parsing and formatting. */
char *mf_parse(const struct mf_field *, const char *,
union mf_value *value, union mf_value *mask);
diff --git a/lib/multipath.c b/lib/multipath.c
index 51758033d..46976fb2a 100644
--- a/lib/multipath.c
+++ b/lib/multipath.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010, 2011 Nicira Networks.
+ * Copyright (c) 2010, 2011, 2012 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
#include <sys/types.h>
#include <netinet/in.h>
#include "dynamic-string.h"
+#include "meta-flow.h"
#include "nx-match.h"
#include "ofp-errors.h"
#include "ofp-util.h"
@@ -39,11 +40,11 @@ multipath_check(const struct nx_action_multipath *mp, const struct flow *flow)
{
uint32_t n_links = ntohs(mp->max_link) + 1;
size_t min_n_bits = log_2_ceil(n_links);
- int ofs = nxm_decode_ofs(mp->ofs_nbits);
- int n_bits = nxm_decode_n_bits(mp->ofs_nbits);
+ struct mf_subfield dst;
enum ofperr error;
- error = nxm_dst_check(mp->dst, ofs, n_bits, flow);
+ nxm_decode(&dst, mp->dst, mp->ofs_nbits);
+ error = mf_check_dst(&dst, flow);
if (error) {
return error;
}
@@ -56,7 +57,7 @@ multipath_check(const struct nx_action_multipath *mp, const struct flow *flow)
&& mp->algorithm != htons(NX_MP_ALG_ITER_HASH)) {
VLOG_WARN_RL(&rl, "unsupported algorithm %"PRIu16,
ntohs(mp->algorithm));
- } else if (n_bits < min_n_bits) {
+ } else if (dst.n_bits < min_n_bits) {
VLOG_WARN_RL(&rl, "multipath action requires at least %zu bits for "
"%"PRIu32" links", min_n_bits, n_links);
} else {
@@ -80,8 +81,10 @@ multipath_execute(const struct nx_action_multipath *mp, struct flow *flow)
uint16_t link = multipath_algorithm(hash, ntohs(mp->algorithm),
ntohs(mp->max_link) + 1,
ntohl(mp->arg));
+ struct mf_subfield dst;
- nxm_reg_load(mp->dst, mp->ofs_nbits, link, flow);
+ nxm_decode(&dst, mp->dst, mp->ofs_nbits);
+ mf_set_subfield_value(&dst, link, flow);
}
static uint16_t
@@ -166,9 +169,8 @@ multipath_parse(struct nx_action_multipath *mp, const char *s_)
{
char *s = xstrdup(s_);
char *save_ptr = NULL;
- char *fields, *basis, *algorithm, *n_links_str, *arg, *dst;
- uint32_t header;
- int ofs, n_bits;
+ char *fields, *basis, *algorithm, *n_links_str, *arg, *dst_s;
+ struct mf_subfield dst;
int n_links;
fields = strtok_r(s, ", ", &save_ptr);
@@ -176,8 +178,8 @@ multipath_parse(struct nx_action_multipath *mp, const char *s_)
algorithm = strtok_r(NULL, ", ", &save_ptr);
n_links_str = strtok_r(NULL, ", ", &save_ptr);
arg = strtok_r(NULL, ", ", &save_ptr);
- dst = strtok_r(NULL, ", ", &save_ptr);
- if (!dst) {
+ dst_s = strtok_r(NULL, ", ", &save_ptr);
+ if (!dst_s) {
ovs_fatal(0, "%s: not enough arguments to multipath action", s_);
}
@@ -209,14 +211,14 @@ multipath_parse(struct nx_action_multipath *mp, const char *s_)
mp->max_link = htons(n_links - 1);
mp->arg = htonl(atoi(arg));
- nxm_parse_field_bits(dst, &header, &ofs, &n_bits);
- if (n_bits < 16 && n_links > (1u << n_bits)) {
+ mf_parse_subfield(&dst, dst_s);
+ if (dst.n_bits < 16 && n_links > (1u << dst.n_bits)) {
ovs_fatal(0, "%s: %d-bit destination field has %u possible values, "
"less than specified n_links %d",
- s_, n_bits, 1u << n_bits, n_links);
+ s_, dst.n_bits, 1u << dst.n_bits, n_links);
}
- mp->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
- mp->dst = htonl(header);
+ mp->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
+ mp->dst = htonl(dst.field->nxm_header);
free(s);
}
@@ -229,6 +231,8 @@ multipath_format(const struct nx_action_multipath *mp, struct ds *s)
uint16_t mp_fields = ntohs(mp->fields);
uint16_t mp_algorithm = ntohs(mp->algorithm);
+ struct mf_subfield dst;
+
fields = flow_hash_fields_to_str(mp_fields);
switch ((enum nx_mp_algorithm) mp_algorithm) {
@@ -251,7 +255,7 @@ multipath_format(const struct nx_action_multipath *mp, struct ds *s)
ds_put_format(s, "multipath(%s,%"PRIu16",%s,%d,%"PRIu16",",
fields, ntohs(mp->basis), algorithm, ntohs(mp->max_link) + 1,
ntohl(mp->arg));
- nxm_format_field_bits(s, ntohl(mp->dst), nxm_decode_ofs(mp->ofs_nbits),
- nxm_decode_n_bits(mp->ofs_nbits));
+ nxm_decode(&dst, mp->dst, mp->ofs_nbits);
+ mf_format_subfield(&dst, s);
ds_put_char(s, ')');
}
diff --git a/lib/nx-match.c b/lib/nx-match.c
index ef166c792..9d3b2be1b 100644
--- a/lib/nx-match.c
+++ b/lib/nx-match.c
@@ -794,97 +794,40 @@ nx_match_from_string(const char *s, struct ofpbuf *b)
return match_len;
}
-const char *
-nxm_parse_field_bits(const char *s, uint32_t *headerp, int *ofsp, int *n_bitsp)
-{
- const char *full_s = s;
- const char *name;
- uint32_t header;
- int start, end;
- int name_len;
- int width;
-
- name = s;
- name_len = strcspn(s, "[");
- if (s[name_len] != '[') {
- ovs_fatal(0, "%s: missing [ looking for field name", full_s);
- }
-
- header = parse_nxm_field_name(name, name_len);
- if (!header) {
- ovs_fatal(0, "%s: unknown field `%.*s'", full_s, name_len, s);
- }
- width = nxm_field_bits(header);
-
- s += name_len;
- if (sscanf(s, "[%d..%d]", &start, &end) == 2) {
- /* Nothing to do. */
- } else if (sscanf(s, "[%d]", &start) == 1) {
- end = start;
- } else if (!strncmp(s, "[]", 2)) {
- start = 0;
- end = width - 1;
- } else {
- ovs_fatal(0, "%s: syntax error expecting [] or [<bit>] or "
- "[<start>..<end>]", full_s);
- }
- s = strchr(s, ']') + 1;
-
- if (start > end) {
- ovs_fatal(0, "%s: starting bit %d is after ending bit %d",
- full_s, start, end);
- } else if (start >= width) {
- ovs_fatal(0, "%s: starting bit %d is not valid because field is only "
- "%d bits wide", full_s, start, width);
- } else if (end >= width){
- ovs_fatal(0, "%s: ending bit %d is not valid because field is only "
- "%d bits wide", full_s, end, width);
- }
-
- *headerp = header;
- *ofsp = start;
- *n_bitsp = end - start + 1;
-
- return s;
-}
-
void
nxm_parse_reg_move(struct nx_action_reg_move *move, const char *s)
{
const char *full_s = s;
- uint32_t src, dst;
- int src_ofs, dst_ofs;
- int src_n_bits, dst_n_bits;
+ struct mf_subfield src, dst;
- s = nxm_parse_field_bits(s, &src, &src_ofs, &src_n_bits);
+ s = mf_parse_subfield(&src, s);
if (strncmp(s, "->", 2)) {
ovs_fatal(0, "%s: missing `->' following source", full_s);
}
s += 2;
- s = nxm_parse_field_bits(s, &dst, &dst_ofs, &dst_n_bits);
+ s = mf_parse_subfield(&dst, s);
if (*s != '\0') {
ovs_fatal(0, "%s: trailing garbage following destination", full_s);
}
- if (src_n_bits != dst_n_bits) {
+ if (src.n_bits != dst.n_bits) {
ovs_fatal(0, "%s: source field is %d bits wide but destination is "
- "%d bits wide", full_s, src_n_bits, dst_n_bits);
+ "%d bits wide", full_s, src.n_bits, dst.n_bits);
}
ofputil_init_NXAST_REG_MOVE(move);
- move->n_bits = htons(src_n_bits);
- move->src_ofs = htons(src_ofs);
- move->dst_ofs = htons(dst_ofs);
- move->src = htonl(src);
- move->dst = htonl(dst);
+ move->n_bits = htons(src.n_bits);
+ move->src_ofs = htons(src.ofs);
+ move->dst_ofs = htons(dst.ofs);
+ move->src = htonl(src.field->nxm_header);
+ move->dst = htonl(dst.field->nxm_header);
}
void
nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
{
const char *full_s = s;
- uint32_t dst;
- int ofs, n_bits;
+ struct mf_subfield dst;
uint64_t value;
value = strtoull(s, (char **) &s, 0);
@@ -892,151 +835,85 @@ nxm_parse_reg_load(struct nx_action_reg_load *load, const char *s)
ovs_fatal(0, "%s: missing `->' following value", full_s);
}
s += 2;
- s = nxm_parse_field_bits(s, &dst, &ofs, &n_bits);
+ s = mf_parse_subfield(&dst, s);
if (*s != '\0') {
ovs_fatal(0, "%s: trailing garbage following destination", full_s);
}
- if (n_bits < 64 && (value >> n_bits) != 0) {
- ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits",
- full_s, value, n_bits);
+ if (dst.n_bits < 64 && (value >> dst.n_bits) != 0) {
+ ovs_fatal(0, "%s: value %"PRIu64" does not fit into %u bits",
+ full_s, value, dst.n_bits);
}
ofputil_init_NXAST_REG_LOAD(load);
- load->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
- load->dst = htonl(dst);
+ load->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs, dst.n_bits);
+ load->dst = htonl(dst.field->nxm_header);
load->value = htonll(value);
}
/* nxm_format_reg_move(), nxm_format_reg_load(). */
void
-nxm_format_field_bits(struct ds *s, uint32_t header, int ofs, int n_bits)
-{
- format_nxm_field_name(s, header);
- if (ofs == 0 && n_bits == nxm_field_bits(header)) {
- ds_put_cstr(s, "[]");
- } else if (n_bits == 1) {
- ds_put_format(s, "[%d]", ofs);
- } else {
- ds_put_format(s, "[%d..%d]", ofs, ofs + n_bits - 1);
- }
-}
-
-void
nxm_format_reg_move(const struct nx_action_reg_move *move, struct ds *s)
{
- int n_bits = ntohs(move->n_bits);
- int src_ofs = ntohs(move->src_ofs);
- int dst_ofs = ntohs(move->dst_ofs);
- uint32_t src = ntohl(move->src);
- uint32_t dst = ntohl(move->dst);
+ struct mf_subfield src, dst;
+
+ nxm_decode_discrete(&src, move->src, move->src_ofs, move->n_bits);
+ nxm_decode_discrete(&dst, move->dst, move->dst_ofs, move->n_bits);
ds_put_format(s, "move:");
- nxm_format_field_bits(s, src, src_ofs, n_bits);
+ mf_format_subfield(&src, s);
ds_put_cstr(s, "->");
- nxm_format_field_bits(s, dst, dst_ofs, n_bits);
+ mf_format_subfield(&dst, s);
}
void
nxm_format_reg_load(const struct nx_action_reg_load *load, struct ds *s)
{
- int ofs = nxm_decode_ofs(load->ofs_nbits);
- int n_bits = nxm_decode_n_bits(load->ofs_nbits);
- uint32_t dst = ntohl(load->dst);
- uint64_t value = ntohll(load->value);
+ struct mf_subfield dst;
- ds_put_format(s, "load:%#"PRIx64"->", value);
- nxm_format_field_bits(s, dst, ofs, n_bits);
+ ds_put_format(s, "load:%#"PRIx64"->", ntohll(load->value));
+
+ nxm_decode(&dst, load->dst, load->ofs_nbits);
+ mf_format_subfield(&dst, s);
}
/* nxm_check_reg_move(), nxm_check_reg_load(). */
-static bool
-field_ok(const struct mf_field *mf, const struct flow *flow, int size)
-{
- return (mf
- && mf_are_prereqs_ok(mf, flow)
- && size <= nxm_field_bits(mf->nxm_header));
-}
-
-int
+enum ofperr
nxm_check_reg_move(const struct nx_action_reg_move *action,
const struct flow *flow)
{
- int src_ofs, dst_ofs, n_bits;
+ struct mf_subfield src;
+ struct mf_subfield dst;
int error;
- n_bits = ntohs(action->n_bits);
- src_ofs = ntohs(action->src_ofs);
- dst_ofs = ntohs(action->dst_ofs);
-
- error = nxm_src_check(action->src, src_ofs, n_bits, flow);
+ nxm_decode_discrete(&src, action->src, action->src_ofs, action->n_bits);
+ error = mf_check_src(&src, flow);
if (error) {
return error;
}
- return nxm_dst_check(action->dst, dst_ofs, n_bits, flow);
-}
-
-/* Given a flow, checks that the source field represented by 'src_header'
- * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
-enum ofperr
-nxm_src_check(ovs_be32 src_header_, unsigned int ofs, unsigned int n_bits,
- const struct flow *flow)
-{
- uint32_t src_header = ntohl(src_header_);
- const struct mf_field *src = mf_from_nxm_header(src_header);
-
- if (!n_bits) {
- VLOG_WARN_RL(&rl, "zero bit source field");
- } else if (NXM_HASMASK(src_header) || !field_ok(src, flow, ofs + n_bits)) {
- VLOG_WARN_RL(&rl, "invalid source field");
- } else {
- return 0;
- }
-
- return OFPERR_OFPBAC_BAD_ARGUMENT;
-}
-
-/* Given a flow, checks that the destination field represented by 'dst_header'
- * in the range ['ofs', 'ofs' + 'n_bits') is valid. */
-enum ofperr
-nxm_dst_check(ovs_be32 dst_header_, unsigned int ofs, unsigned int n_bits,
- const struct flow *flow)
-{
- uint32_t dst_header = ntohl(dst_header_);
- const struct mf_field *dst = mf_from_nxm_header(dst_header);
-
- if (!n_bits) {
- VLOG_WARN_RL(&rl, "zero bit destination field");
- } else if (NXM_HASMASK(dst_header) || !field_ok(dst, flow, ofs + n_bits)) {
- VLOG_WARN_RL(&rl, "invalid destination field");
- } else if (!dst->writable) {
- VLOG_WARN_RL(&rl, "destination field is not writable");
- } else {
- return 0;
- }
-
- return OFPERR_OFPBAC_BAD_ARGUMENT;
+ nxm_decode_discrete(&dst, action->dst, action->dst_ofs, action->n_bits);
+ return mf_check_dst(&dst, flow);
}
enum ofperr
nxm_check_reg_load(const struct nx_action_reg_load *action,
const struct flow *flow)
{
- unsigned int ofs = nxm_decode_ofs(action->ofs_nbits);
- unsigned int n_bits = nxm_decode_n_bits(action->ofs_nbits);
+ struct mf_subfield dst;
enum ofperr error;
- error = nxm_dst_check(action->dst, ofs, n_bits, flow);
+ nxm_decode(&dst, action->dst, action->ofs_nbits);
+ error = mf_check_dst(&dst, flow);
if (error) {
return error;
}
/* Reject 'action' if a bit numbered 'n_bits' or higher is set to 1 in
* action->value. */
- if (n_bits < 64 && ntohll(action->value) >> n_bits) {
+ if (dst.n_bits < 64 && ntohll(action->value) >> dst.n_bits) {
return OFPERR_OFPBAC_BAD_ARGUMENT;
}
@@ -1045,64 +922,72 @@ nxm_check_reg_load(const struct nx_action_reg_load *action,
/* nxm_execute_reg_move(), nxm_execute_reg_load(). */
-/* Returns the value of the NXM field corresponding to 'header' at 'ofs_nbits'
- * in 'flow'. */
-uint64_t
-nxm_read_field_bits(ovs_be32 header, ovs_be16 ofs_nbits,
- const struct flow *flow)
-{
- const struct mf_field *field = mf_from_nxm_header(ntohl(header));
- union mf_value value;
- union mf_value bits;
-
- mf_get_value(field, flow, &value);
- bits.be64 = htonll(0);
- bitwise_copy(&value, field->n_bytes, nxm_decode_ofs(ofs_nbits),
- &bits, sizeof bits.be64, 0,
- nxm_decode_n_bits(ofs_nbits));
- return ntohll(bits.be64);
-}
-
void
nxm_execute_reg_move(const struct nx_action_reg_move *action,
struct flow *flow)
{
- const struct mf_field *src = mf_from_nxm_header(ntohl(action->src));
- const struct mf_field *dst = mf_from_nxm_header(ntohl(action->dst));
+ struct mf_subfield src, dst;
union mf_value src_value;
union mf_value dst_value;
- mf_get_value(dst, flow, &dst_value);
- mf_get_value(src, flow, &src_value);
- bitwise_copy(&src_value, src->n_bytes, ntohs(action->src_ofs),
- &dst_value, dst->n_bytes, ntohs(action->dst_ofs),
- ntohs(action->n_bits));
- mf_set_flow_value(dst, &dst_value, flow);
+ nxm_decode_discrete(&src, action->src, action->src_ofs, action->n_bits);
+ nxm_decode_discrete(&dst, action->dst, action->dst_ofs, action->n_bits);
+
+ mf_get_value(dst.field, flow, &dst_value);
+ mf_get_value(src.field, flow, &src_value);
+ bitwise_copy(&src_value, src.field->n_bytes, src.ofs,
+ &dst_value, dst.field->n_bytes, dst.ofs,
+ src.n_bits);
+ mf_set_flow_value(dst.field, &dst_value, flow);
}
void
nxm_execute_reg_load(const struct nx_action_reg_load *action,
struct flow *flow)
{
- nxm_reg_load(action->dst, action->ofs_nbits, ntohll(action->value), flow);
+ struct mf_subfield dst;
+
+ nxm_decode(&dst, action->dst, action->ofs_nbits);
+ mf_set_subfield_value(&dst, ntohll(action->value), flow);
}
-/* Calculates ofs and n_bits from the given 'ofs_nbits' parameter, and copies
- * 'src_data'[0:n_bits] to 'dst_header'[ofs:ofs+n_bits] in the given 'flow'. */
+/* Initializes 'sf->field' with the field corresponding to the given NXM
+ * 'header' and 'sf->ofs' and 'sf->n_bits' decoded from 'ofs_nbits' with
+ * nxm_decode_ofs() and nxm_decode_n_bits(), respectively.
+ *
+ * Afterward, 'sf' might be invalid in a few different ways:
+ *
+ * - 'sf->field' will be NULL if 'header' is unknown.
+ *
+ * - 'sf->ofs' and 'sf->n_bits' might exceed the width of sf->field.
+ *
+ * The caller should call mf_check_src() or mf_check_dst() to check for these
+ * problems. */
void
-nxm_reg_load(ovs_be32 dst_header, ovs_be16 ofs_nbits, uint64_t src_data,
- struct flow *flow)
+nxm_decode(struct mf_subfield *sf, ovs_be32 header, ovs_be16 ofs_nbits)
{
- const struct mf_field *dst = mf_from_nxm_header(ntohl(dst_header));
- int n_bits = nxm_decode_n_bits(ofs_nbits);
- int dst_ofs = nxm_decode_ofs(ofs_nbits);
- union mf_value dst_value;
- union mf_value src_value;
+ sf->field = mf_from_nxm_header(ntohl(header));
+ sf->ofs = nxm_decode_ofs(ofs_nbits);
+ sf->n_bits = nxm_decode_n_bits(ofs_nbits);
+}
- mf_get_value(dst, flow, &dst_value);
- src_value.be64 = htonll(src_data);
- bitwise_copy(&src_value, sizeof src_value.be64, 0,
- &dst_value, dst->n_bytes, dst_ofs,
- n_bits);
- mf_set_flow_value(dst, &dst_value, flow);
+/* Initializes 'sf->field' with the field corresponding to the given NXM
+ * 'header' and 'sf->ofs' and 'sf->n_bits' from 'ofs' and 'n_bits',
+ * respectively.
+ *
+ * Afterward, 'sf' might be invalid in a few different ways:
+ *
+ * - 'sf->field' will be NULL if 'header' is unknown.
+ *
+ * - 'sf->ofs' and 'sf->n_bits' might exceed the width of sf->field.
+ *
+ * The caller should call mf_check_src() or mf_check_dst() to check for these
+ * problems. */
+void
+nxm_decode_discrete(struct mf_subfield *sf, ovs_be32 header,
+ ovs_be16 ofs, ovs_be16 n_bits)
+{
+ sf->field = mf_from_nxm_header(ntohl(header));
+ sf->ofs = ntohs(ofs);
+ sf->n_bits = ntohs(n_bits);
}
diff --git a/lib/nx-match.h b/lib/nx-match.h
index c1892873b..3406e0465 100644
--- a/lib/nx-match.h
+++ b/lib/nx-match.h
@@ -26,6 +26,7 @@
struct cls_rule;
struct ds;
struct flow;
+struct mf_subfield;
struct ofpbuf;
struct nx_action_reg_load;
struct nx_action_reg_move;
@@ -47,37 +48,24 @@ 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_bits(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 *);
void nxm_format_reg_move(const struct nx_action_reg_move *, struct ds *);
void nxm_format_reg_load(const struct nx_action_reg_load *, struct ds *);
-int nxm_check_reg_move(const struct nx_action_reg_move *, const struct flow *);
+enum ofperr nxm_check_reg_move(const struct nx_action_reg_move *,
+ const struct flow *);
enum ofperr nxm_check_reg_load(const struct nx_action_reg_load *,
const struct flow *);
-enum ofperr nxm_src_check(ovs_be32 src, unsigned int ofs, unsigned int n_bits,
- const struct flow *);
-enum ofperr nxm_dst_check(ovs_be32 dst, unsigned int ofs, unsigned int n_bits,
- const struct flow *);
void nxm_execute_reg_move(const struct nx_action_reg_move *, struct flow *);
void nxm_execute_reg_load(const struct nx_action_reg_load *, struct flow *);
-void nxm_reg_load(ovs_be32 dst, ovs_be16 ofs_nbits, uint64_t src_data,
- struct flow *);
int nxm_field_bytes(uint32_t header);
int nxm_field_bits(uint32_t header);
-const char *nxm_parse_field_bits(const char *s,
- uint32_t *headerp, int *ofsp, int *n_bitsp);
-void nxm_format_field_bits(struct ds *, uint32_t header, int ofs, int n_bits);
-
-/* Dealing with the 'ofs_nbits' members of struct nx_action_reg_load and struct
- * nx_action_multipath. */
+/* Dealing with the 'ofs_nbits' members in several Nicira extensions. */
static inline ovs_be16
nxm_encode_ofs_nbits(int ofs, int n_bits)
@@ -96,6 +84,10 @@ nxm_decode_n_bits(ovs_be16 ofs_nbits)
{
return (ntohs(ofs_nbits) & 0x3f) + 1;
}
+
+void nxm_decode(struct mf_subfield *, ovs_be32 header, ovs_be16 ofs_nbits);
+void nxm_decode_discrete(struct mf_subfield *, ovs_be32 header,
+ ovs_be16 ofs, ovs_be16 n_bits);
/* Upper bound on the length of an nx_match. The longest nx_match (an
* IPV6 neighbor discovery message using 5 registers) would be:
diff --git a/lib/ofp-parse.c b/lib/ofp-parse.c
index 5321364b1..3a1fcb7df 100644
--- a/lib/ofp-parse.c
+++ b/lib/ofp-parse.c
@@ -151,14 +151,13 @@ parse_output(struct ofpbuf *b, char *arg)
{
if (strchr(arg, '[')) {
struct nx_action_output_reg *naor;
- int ofs, n_bits;
- uint32_t src;
+ struct mf_subfield src;
- nxm_parse_field_bits(arg, &src, &ofs, &n_bits);
+ mf_parse_subfield(&src, arg);
naor = ofputil_put_NXAST_OUTPUT_REG(b);
- naor->ofs_nbits = nxm_encode_ofs_nbits(ofs, n_bits);
- naor->src = htonl(src);
+ naor->ofs_nbits = nxm_encode_ofs_nbits(src.ofs, src.n_bits);
+ naor->src = htonl(src.field->nxm_header);
naor->max_len = htons(UINT16_MAX);
} else {
put_output_action(b, str_to_u32(arg));
diff --git a/lib/ofp-print.c b/lib/ofp-print.c
index 7bc26c9dd..3c5c34a32 100644
--- a/lib/ofp-print.c
+++ b/lib/ofp-print.c
@@ -33,6 +33,7 @@
#include "flow.h"
#include "learn.h"
#include "multipath.h"
+#include "meta-flow.h"
#include "nx-match.h"
#include "ofp-errors.h"
#include "ofp-util.h"
@@ -187,6 +188,7 @@ ofp_print_action(struct ds *s, const union ofp_action *a,
const struct nx_action_multipath *nam;
const struct nx_action_autopath *naa;
const struct nx_action_output_reg *naor;
+ struct mf_subfield subfield;
uint16_t port;
switch (code) {
@@ -319,9 +321,8 @@ ofp_print_action(struct ds *s, const union ofp_action *a,
case OFPUTIL_NXAST_AUTOPATH:
naa = (const struct nx_action_autopath *)a;
ds_put_format(s, "autopath(%u,", ntohl(naa->id));
- nxm_format_field_bits(s, ntohl(naa->dst),
- nxm_decode_ofs(naa->ofs_nbits),
- nxm_decode_n_bits(naa->ofs_nbits));
+ nxm_decode(&subfield, naa->dst, naa->ofs_nbits);
+ mf_format_subfield(&subfield, s);
ds_put_char(s, ')');
break;
@@ -333,9 +334,8 @@ ofp_print_action(struct ds *s, const union ofp_action *a,
case OFPUTIL_NXAST_OUTPUT_REG:
naor = (const struct nx_action_output_reg *) a;
ds_put_cstr(s, "output:");
- nxm_format_field_bits(s, ntohl(naor->src),
- nxm_decode_ofs(naor->ofs_nbits),
- nxm_decode_n_bits(naor->ofs_nbits));
+ nxm_decode(&subfield, naor->src, naor->ofs_nbits);
+ mf_format_subfield(&subfield, s);
break;
case OFPUTIL_NXAST_LEARN:
diff --git a/lib/ofp-util.c b/lib/ofp-util.c
index 6fe1611e2..2565ec7ff 100644
--- a/lib/ofp-util.c
+++ b/lib/ofp-util.c
@@ -29,6 +29,7 @@
#include "dynamic-string.h"
#include "learn.h"
#include "multipath.h"
+#include "meta-flow.h"
#include "nx-match.h"
#include "ofp-errors.h"
#include "ofp-util.h"
@@ -2320,6 +2321,7 @@ static enum ofperr
check_output_reg(const struct nx_action_output_reg *naor,
const struct flow *flow)
{
+ struct mf_subfield src;
size_t i;
for (i = 0; i < sizeof naor->zero; i++) {
@@ -2328,8 +2330,8 @@ check_output_reg(const struct nx_action_output_reg *naor,
}
}
- return nxm_src_check(naor->src, nxm_decode_ofs(naor->ofs_nbits),
- nxm_decode_n_bits(naor->ofs_nbits), flow);
+ nxm_decode(&src, naor->src, naor->ofs_nbits);
+ return mf_check_src(&src, flow);
}
enum ofperr