X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Flearn.c;h=7f30f6ee0653c0e11586e03fc8ab6d2ed0d1ba7d;hb=b5a36e3878937a489458f7a0ff42769e826ae88b;hp=d2db792e3be3571e618f642917b246c5f84db18a;hpb=337b9cec45b813ef467b39a8d55741030f7404ff;p=openvswitch diff --git a/lib/learn.c b/lib/learn.c index d2db792e..7f30f6ee 100644 --- a/lib/learn.c +++ b/lib/learn.c @@ -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 == 10); } } } @@ -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);