From 9bab681f33e09882e8f652c8e96a2b998b3ef9b3 Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Wed, 12 Sep 2012 17:44:28 +0900 Subject: [PATCH] lib/ofp-actions: use mf_subvalue in struct ofpact_reg_load. Use a uninion mf_subvalue instead of a uint64_t for the value member of struct ofpact_reg_load. set_field action needs to hold values wider than 64 bits. This is preparation for set_field action. Signed-off-by: Isaku Yamahata Signed-off-by: Simon Horman Signed-off-by: Ben Pfaff --- lib/learn.c | 29 +++------------------------ lib/meta-flow.c | 33 ++++++++++++++++++++++++++++++ lib/meta-flow.h | 3 +++ lib/nx-match.c | 51 ++++++++++++++++++++++++++++++++--------------- lib/ofp-actions.h | 4 ++-- 5 files changed, 76 insertions(+), 44 deletions(-) diff --git a/lib/learn.c b/lib/learn.c index cd7532179..7504227ce 100644 --- a/lib/learn.c +++ b/lib/learn.c @@ -339,7 +339,6 @@ learn_execute(const struct ofpact_learn *learn, const struct flow *flow, case NX_LEARN_DST_LOAD: for (ofs = 0; ofs < spec->n_bits; ofs += chunk) { struct ofpact_reg_load *load; - ovs_be64 value_be; chunk = MIN(spec->n_bits - ofs, 64); @@ -347,12 +346,7 @@ learn_execute(const struct ofpact_learn *learn, const struct flow *flow, load->dst.field = spec->dst.field; load->dst.ofs = spec->dst.ofs + ofs; load->dst.n_bits = chunk; - - memset(&value_be, 0, sizeof value_be); - bitwise_copy(&value, sizeof value, ofs, - &value_be, sizeof value_be, 0, - chunk); - load->value = ntohll(value_be); + load->subvalue = value; } break; @@ -595,23 +589,6 @@ learn_parse(char *arg, const struct flow *flow, struct ofpbuf *ofpacts) free(orig); } -static void -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'); -} - /* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8) * describes. */ void @@ -663,7 +640,7 @@ learn_format(const struct ofpact_learn *learn, struct ds *s) } else { mf_format_subfield(&spec->dst, s); ds_put_char(s, '='); - format_subvalue(&spec->src_imm, s); + mf_format_subvalue(&spec->src_imm, s); } break; @@ -678,7 +655,7 @@ learn_format(const struct ofpact_learn *learn, struct ds *s) case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD: ds_put_format(s, "load:"); - format_subvalue(&spec->src_imm, s); + mf_format_subvalue(&spec->src_imm, s); ds_put_cstr(s, "->"); mf_format_subfield(&spec->dst, s); break; diff --git a/lib/meta-flow.c b/lib/meta-flow.c index 0de9b4557..fd33726e7 100644 --- a/lib/meta-flow.c +++ b/lib/meta-flow.c @@ -2153,6 +2153,22 @@ mf_format(const struct mf_field *mf, } } +/* Makes subfield 'sf' within 'flow' exactly match the 'sf->n_bits' + * least-significant bits in 'x'. + */ +void +mf_write_subfield_flow(const struct mf_subfield *sf, + const union mf_subvalue *x, struct flow *flow) +{ + const struct mf_field *field = sf->field; + union mf_value value; + + mf_get_value(field, flow, &value); + bitwise_copy(x, sizeof *x, sf->ofs, &value, field->n_bytes, + sf->ofs, sf->n_bits); + mf_set_flow_value(field, &value, flow); +} + /* Makes subfield 'sf' within 'match' exactly match the 'sf->n_bits' * least-significant bits in 'x'. */ @@ -2335,3 +2351,20 @@ mf_parse_subfield(struct mf_subfield *sf, const char *s) } return s; } + +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'); +} diff --git a/lib/meta-flow.h b/lib/meta-flow.h index 8c8b7be8e..60bfecaa5 100644 --- a/lib/meta-flow.h +++ b/lib/meta-flow.h @@ -318,6 +318,8 @@ void mf_set_wild(const struct mf_field *, struct match *); void mf_random_value(const struct mf_field *, union mf_value *value); /* Subfields. */ +void mf_write_subfield_flow(const struct mf_subfield *, + const union mf_subvalue *, struct flow *); void mf_write_subfield(const struct mf_subfield *, const union mf_subvalue *, struct match *); @@ -340,5 +342,6 @@ char *mf_parse_value(const struct mf_field *, const char *, union mf_value *); void mf_format(const struct mf_field *, const union mf_value *value, const union mf_value *mask, struct ds *); +void mf_format_subvalue(const union mf_subvalue *subvalue, struct ds *s); #endif /* meta-flow.h */ diff --git a/lib/nx-match.c b/lib/nx-match.c index 9f9184aec..1ff58bbff 100644 --- a/lib/nx-match.c +++ b/lib/nx-match.c @@ -988,8 +988,9 @@ void nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s) { const char *full_s = s; + uint64_t value = strtoull(s, (char **) &s, 0); + ovs_be64 value_be = htonll(value); - load->value = strtoull(s, (char **) &s, 0); if (strncmp(s, "->", 2)) { ovs_fatal(0, "%s: missing `->' following value", full_s); } @@ -999,10 +1000,15 @@ nxm_parse_reg_load(struct ofpact_reg_load *load, const char *s) ovs_fatal(0, "%s: trailing garbage following destination", full_s); } - if (load->dst.n_bits < 64 && (load->value >> load->dst.n_bits) != 0) { + if (load->dst.n_bits < 64 && (value >> load->dst.n_bits) != 0) { ovs_fatal(0, "%s: value %"PRIu64" does not fit into %d bits", - full_s, load->value, load->dst.n_bits); + full_s, value, load->dst.n_bits); } + + memset(&load->subvalue, 0, sizeof &load->subvalue); + bitwise_copy(&value_be, sizeof value_be, 0, + &load->subvalue, sizeof load->subvalue, load->dst.ofs, + load->dst.n_bits); } /* nxm_format_reg_move(), nxm_format_reg_load(). */ @@ -1019,7 +1025,16 @@ nxm_format_reg_move(const struct ofpact_reg_move *move, struct ds *s) void nxm_format_reg_load(const struct ofpact_reg_load *load, struct ds *s) { - ds_put_format(s, "load:%#"PRIx64"->", load->value); + union mf_subvalue right_justified; + + memset(&right_justified, 0, sizeof right_justified); + bitwise_copy(&load->subvalue, sizeof load->subvalue, load->dst.ofs, + &right_justified, sizeof right_justified, 0, + load->dst.n_bits); + + ds_put_cstr(s, "load:"); + mf_format_subvalue(&right_justified, s); + ds_put_cstr(s, "->"); mf_format_subfield(&load->dst, s); } @@ -1050,11 +1065,15 @@ nxm_reg_load_from_openflow(const struct nx_action_reg_load *narl, load->dst.field = mf_from_nxm_header(ntohl(narl->dst)); load->dst.ofs = nxm_decode_ofs(narl->ofs_nbits); load->dst.n_bits = nxm_decode_n_bits(narl->ofs_nbits); - load->value = ntohll(narl->value); + memset(&load->subvalue, 0, sizeof &load->subvalue); + bitwise_copy(&narl->value, sizeof narl->value, 0, + &load->subvalue, sizeof load->subvalue, load->dst.ofs, + load->dst.n_bits); /* Reject 'narl' if a bit numbered 'n_bits' or higher is set to 1 in * narl->value. */ - if (load->dst.n_bits < 64 && load->value >> load->dst.n_bits) { + if (load->dst.n_bits < 64 && + ntohll(narl->value) >> load->dst.n_bits) { return OFPERR_OFPBAC_BAD_ARGUMENT; } @@ -1103,7 +1122,9 @@ nxm_reg_load_to_nxast(const struct ofpact_reg_load *load, narl = ofputil_put_NXAST_REG_LOAD(openflow); narl->ofs_nbits = nxm_encode_ofs_nbits(load->dst.ofs, load->dst.n_bits); narl->dst = htonl(load->dst.field->nxm_header); - narl->value = htonll(load->value); + narl->value = htonll(0); + bitwise_copy(&load->subvalue, sizeof load->subvalue, load->dst.ofs, + &narl->value, sizeof narl->value, 0, load->dst.n_bits); } /* nxm_execute_reg_move(), nxm_execute_reg_load(). */ @@ -1126,20 +1147,18 @@ nxm_execute_reg_move(const struct ofpact_reg_move *move, void nxm_execute_reg_load(const struct ofpact_reg_load *load, struct flow *flow) { - nxm_reg_load(&load->dst, load->value, flow); + mf_write_subfield_flow(&load->dst, &load->subvalue, flow); } void nxm_reg_load(const struct mf_subfield *dst, uint64_t src_data, struct flow *flow) { - union mf_value dst_value; - union mf_value src_value; + union mf_subvalue src_subvalue; + ovs_be64 src_data_be = htonll(src_data); - mf_get_value(dst->field, flow, &dst_value); - src_value.be64 = htonll(src_data); - bitwise_copy(&src_value, sizeof src_value.be64, 0, - &dst_value, dst->field->n_bytes, dst->ofs, - dst->n_bits); - mf_set_flow_value(dst->field, &dst_value, flow); + bitwise_copy(&src_data_be, sizeof src_data_be, 0, + &src_subvalue, sizeof src_subvalue, 0, + sizeof src_data_be * 8); + mf_write_subfield_flow(dst, &src_subvalue, flow); } diff --git a/lib/ofp-actions.h b/lib/ofp-actions.h index 4fc9094f1..58383e78f 100644 --- a/lib/ofp-actions.h +++ b/lib/ofp-actions.h @@ -271,11 +271,11 @@ struct ofpact_reg_move { /* OFPACT_REG_LOAD. * - * Used for NXAST_REG_LOAD. */ + * Used for NXAST_REG_LOAD, OFPAT12_SET_FIELD. */ struct ofpact_reg_load { struct ofpact ofpact; struct mf_subfield dst; - uint64_t value; + union mf_subvalue subvalue; }; /* OFPACT_SET_TUNNEL. -- 2.43.0