From 75a75043564dc9b002fffa6c6ad71e0d4d5c892e Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 12 Sep 2011 16:19:57 -0700 Subject: Implement new "learn" action. There are a few loose ends here. First, learning actions cause too much flow revalidation. Upcoming commits will fix that problem. The following additional issues have not yet been addressed: * Resource limits: nothing yet limits the maximum number of flows that can be learned. It is possible to exhaust all system memory. * Age reporting: there is no way to find out how soon a learned table entry is due to be evicted. To try this action out, here's a recipe for a very simple-minded MAC learning switch. It uses a 10-second MAC expiration time to make it easier to see what's going on: ovs-vsctl del-controller br0 ovs-ofctl del-flows br0 ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, hard_timeout=10, \ NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \ output:NXM_OF_IN_PORT[]), resubmit(,1)" ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood" You can then dump the MAC learning table with: ovs-ofctl dump-flows br0 table=1 --- lib/automake.mk | 2 + lib/learn.c | 662 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/learn.h | 38 ++++ lib/meta-flow.c | 26 --- lib/ofp-parse.c | 35 +-- lib/ofp-print.c | 5 + lib/ofp-util.c | 5 + lib/ofp-util.def | 1 + lib/util.c | 29 +++ lib/util.h | 3 + 10 files changed, 766 insertions(+), 40 deletions(-) create mode 100644 lib/learn.c create mode 100644 lib/learn.h (limited to 'lib') diff --git a/lib/automake.mk b/lib/automake.mk index 7cc0dbe34..df3071129 100644 --- a/lib/automake.mk +++ b/lib/automake.mk @@ -67,6 +67,8 @@ lib_libopenvswitch_a_SOURCES = \ lib/lacp.h \ lib/leak-checker.c \ lib/leak-checker.h \ + lib/learn.c \ + lib/learn.h \ lib/learning-switch.c \ lib/learning-switch.h \ lib/list.c \ diff --git a/lib/learn.c b/lib/learn.c new file mode 100644 index 000000000..8927ff899 --- /dev/null +++ b/lib/learn.c @@ -0,0 +1,662 @@ +/* + * Copyright (c) 2011 Nicira Networks. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "learn.h" + +#include "byte-order.h" +#include "dynamic-string.h" +#include "meta-flow.h" +#include "nx-match.h" +#include "ofp-util.h" +#include "ofpbuf.h" +#include "openflow/openflow.h" +#include "unaligned.h" + +static ovs_be16 +get_be16(const void **pp) +{ + const ovs_be16 *p = *pp; + ovs_be16 value = *p; + *pp = p + 1; + return value; +} + +static ovs_be32 +get_be32(const void **pp) +{ + const ovs_be32 *p = *pp; + ovs_be32 value = get_unaligned_be32(p); + *pp = p + 1; + return value; +} + +static uint64_t +get_bits(int n_bits, const void **p) +{ + int n_segs = DIV_ROUND_UP(n_bits, 16); + uint64_t value; + + value = 0; + while (n_segs-- > 0) { + value = (value << 16) | ntohs(get_be16(p)); + } + return value; +} + +static unsigned int +learn_min_len(uint16_t header) +{ + int n_bits = header & NX_LEARN_N_BITS_MASK; + int src_type = header & NX_LEARN_SRC_MASK; + int dst_type = header & NX_LEARN_DST_MASK; + unsigned int min_len; + + min_len = 0; + if (src_type == NX_LEARN_SRC_FIELD) { + min_len += sizeof(ovs_be32); /* src_field */ + min_len += sizeof(ovs_be16); /* src_ofs */ + } else { + min_len += DIV_ROUND_UP(n_bits, 16); + } + if (dst_type == NX_LEARN_DST_MATCH || + dst_type == NX_LEARN_DST_LOAD) { + min_len += sizeof(ovs_be32); /* dst_field */ + min_len += sizeof(ovs_be16); /* dst_ofs */ + } + return min_len; +} + +static int +learn_check_header(uint16_t header, size_t len) +{ + int src_type = header & NX_LEARN_SRC_MASK; + int dst_type = header & NX_LEARN_DST_MASK; + + /* Check for valid src and dst type combination. */ + if (dst_type == NX_LEARN_DST_MATCH || + dst_type == NX_LEARN_DST_LOAD || + (dst_type == NX_LEARN_DST_OUTPUT && + src_type == NX_LEARN_SRC_FIELD)) { + /* OK. */ + } else { + return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT); + } + + /* Check that the arguments don't overrun the end of the action. */ + if (len < learn_min_len(header)) { + return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN); + } + + return 0; +} + +/* Checks that 'learn' (which must be at least 'sizeof *learn' bytes long) is a + * valid action on 'flow'. */ +int +learn_check(const struct nx_action_learn *learn, const struct flow *flow) +{ + struct cls_rule rule; + const void *p, *end; + + cls_rule_init_catchall(&rule, 0); + + if (learn->flags & ~htons(OFPFF_SEND_FLOW_REM) + || !is_all_zeros(learn->pad, sizeof learn->pad) + || learn->table_id == 0xff) { + return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT); + } + + end = (char *) learn + ntohs(learn->len); + for (p = learn + 1; p != end; ) { + uint16_t header = ntohs(get_be16(&p)); + int n_bits = header & NX_LEARN_N_BITS_MASK; + int src_type = header & NX_LEARN_SRC_MASK; + int dst_type = header & NX_LEARN_DST_MASK; + + uint64_t value; + int error; + + if (!header) { + break; + } + + error = learn_check_header(header, (char *) end - (char *) p); + if (error) { + return error; + } + + /* Check the source. */ + if (src_type == NX_LEARN_SRC_FIELD) { + ovs_be32 src_field = get_be32(&p); + int src_ofs = ntohs(get_be16(&p)); + + error = nxm_src_check(src_field, src_ofs, n_bits, flow); + if (error) { + return error; + } + value = 0; + } else { + value = get_bits(n_bits, &p); + } + + /* 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)); + int error; + + error = nxm_dst_check(dst_field, dst_ofs, n_bits, &rule.flow); + if (error) { + return error; + } + + if (dst_type == NX_LEARN_DST_MATCH + && src_type == NX_LEARN_SRC_IMMEDIATE) { + mf_set_subfield(nxm_field_to_mf_field(ntohl(dst_field)), value, + dst_ofs, n_bits, &rule); + } + } + } + if (!is_all_zeros(p, (char *) end - (char *) p)) { + return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT); + } + + return 0; +} + +void +learn_execute(const struct nx_action_learn *learn, const struct flow *flow, + struct ofputil_flow_mod *fm) +{ + const void *p, *end; + struct ofpbuf actions; + + cls_rule_init_catchall(&fm->cr, ntohs(learn->priority)); + fm->cookie = learn->cookie; + fm->table_id = learn->table_id; + fm->command = OFPFC_MODIFY_STRICT; + fm->idle_timeout = ntohs(learn->idle_timeout); + fm->hard_timeout = ntohs(learn->hard_timeout); + fm->buffer_id = UINT32_MAX; + fm->out_port = OFPP_NONE; + fm->flags = ntohs(learn->flags) & OFPFF_SEND_FLOW_REM; + fm->actions = NULL; + fm->n_actions = 0; + + ofpbuf_init(&actions, 64); + + for (p = learn + 1, end = (char *) learn + ntohs(learn->len); p != end; ) { + uint16_t header = ntohs(get_be16(&p)); + int n_bits = header & NX_LEARN_N_BITS_MASK; + int src_type = header & NX_LEARN_SRC_MASK; + int dst_type = header & NX_LEARN_DST_MASK; + uint64_t value; + + struct nx_action_reg_load *load; + ovs_be32 dst_field; + int dst_ofs; + + if (!header) { + break; + } + + if (src_type == NX_LEARN_SRC_FIELD) { + ovs_be32 src_field = get_be32(&p); + int src_ofs = ntohs(get_be16(&p)); + + value = nxm_read_field_bits(src_field, + nxm_encode_ofs_nbits(src_ofs, n_bits), + 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(nxm_field_to_mf_field(ntohl(dst_field)), value, + dst_ofs, n_bits, &fm->cr); + break; + + case NX_LEARN_DST_LOAD: + dst_field = get_be32(&p); + dst_ofs = ntohs(get_be16(&p)); + load = ofputil_put_NXAST_REG_LOAD(&actions); + load->ofs_nbits = nxm_encode_ofs_nbits(dst_ofs, n_bits); + load->dst = dst_field; + load->value = htonll(value); + break; + + case NX_LEARN_DST_OUTPUT: + ofputil_put_OFPAT_OUTPUT(&actions)->port = htons(value); + break; + } + } + + fm->actions = ofpbuf_steal_data(&actions); + fm->n_actions = actions.size / sizeof(struct ofp_action_header); +} + +static void +put_be16(struct ofpbuf *b, ovs_be16 x) +{ + ofpbuf_put(b, &x, sizeof x); +} + +static void +put_be32(struct ofpbuf *b, ovs_be32 x) +{ + ofpbuf_put(b, &x, sizeof x); +} + +static void +put_u16(struct ofpbuf *b, uint16_t x) +{ + put_be16(b, htons(x)); +} + +static void +put_u32(struct ofpbuf *b, uint32_t x) +{ + put_be32(b, htonl(x)); +} + +struct learn_spec { + int n_bits; + + int src_type; + const struct mf_field *src; + int src_ofs; + uint8_t src_imm[sizeof(union mf_value)]; + + int dst_type; + const struct mf_field *dst; + int dst_ofs; +}; + +static void +learn_parse_spec(const char *orig, char *name, char *value, + struct learn_spec *spec) +{ + if (mf_from_name(name)) { + const struct mf_field *dst = mf_from_name(name); + union mf_value imm; + char *error; + + error = mf_parse_value(dst, value, &imm); + if (error) { + ovs_fatal(0, "%s", error); + } + + 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; + } 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') { + 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') { + 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); + } + } else { + src_header = dst_header; + src_ofs = dst_ofs; + } + + spec->n_bits = n_bits; + spec->src_type = NX_LEARN_SRC_FIELD; + spec->src = nxm_field_to_mf_field(src_header); + spec->src_ofs = src_ofs; + spec->dst_type = NX_LEARN_DST_MATCH; + spec->dst = nxm_field_to_mf_field(dst_header); + spec->dst_ofs = 0; + } 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; + 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 = nxm_field_to_mf_field(ntohl(load.dst)); + spec->dst_ofs = nxm_decode_ofs(load.ofs_nbits); + } else { + struct nx_action_reg_move move; + + nxm_parse_reg_move(&move, value); + + spec->n_bits = ntohs(move.n_bits); + spec->src_type = NX_LEARN_SRC_FIELD; + spec->src = nxm_field_to_mf_field(ntohl(move.src)); + spec->src_ofs = ntohs(move.src_ofs); + spec->dst_type = NX_LEARN_DST_LOAD; + spec->dst = nxm_field_to_mf_field(ntohl(move.dst)); + spec->dst_ofs = ntohs(move.dst_ofs); + } + } else if (!strcmp(name, "output")) { + uint32_t header; + int ofs, n_bits; + + if (nxm_parse_field_bits(value, &header, &ofs, &n_bits)[0] != '\0') { + ovs_fatal(0, "%s: syntax error after NXM field name `%s'", + orig, name); + } + + spec->n_bits = n_bits; + spec->src_type = NX_LEARN_SRC_FIELD; + spec->src = nxm_field_to_mf_field(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); + } +} + +void +learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow) +{ + char *orig = xstrdup(arg); + char *name, *value; + size_t learn_ofs; + size_t len; + + struct nx_action_learn *learn; + struct cls_rule rule; + + learn_ofs = b->size; + learn = ofputil_put_NXAST_LEARN(b); + learn->idle_timeout = htons(OFP_FLOW_PERMANENT); + learn->hard_timeout = htons(OFP_FLOW_PERMANENT); + learn->priority = htons(OFP_DEFAULT_PRIORITY); + learn->cookie = htonll(0); + learn->flags = htons(0); + learn->table_id = 1; + + cls_rule_init_catchall(&rule, 0); + while (ofputil_parse_key_value(&arg, &name, &value)) { + learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn); + if (!strcmp(name, "table")) { + learn->table_id = atoi(value); + if (learn->table_id == 255) { + ovs_fatal(0, "%s: table id 255 not valid for `learn' action", + orig); + } + } else if (!strcmp(name, "priority")) { + learn->priority = htons(atoi(value)); + } else if (!strcmp(name, "idle_timeout")) { + learn->idle_timeout = htons(atoi(value)); + } else if (!strcmp(name, "hard_timeout")) { + learn->hard_timeout = htons(atoi(value)); + } else if (!strcmp(name, "cookie")) { + learn->cookie = htonll(strtoull(value, NULL, 0)); + } else { + struct learn_spec spec; + + learn_parse_spec(orig, name, value, &spec); + + /* Check prerequisites. */ + if (spec.src_type == NX_LEARN_SRC_FIELD + && !mf_are_prereqs_ok(spec.src, flow)) { + ovs_fatal(0, "%s: cannot specify source field %s because " + "prerequisites are not satisfied", + orig, spec.src->name); + } + if ((spec.dst_type == NX_LEARN_DST_MATCH + || spec.dst_type == NX_LEARN_DST_LOAD) + && !mf_are_prereqs_ok(spec.dst, &rule.flow)) { + ovs_fatal(0, "%s: cannot specify destination field %s because " + "prerequisites are not satisfied", + orig, spec.dst->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) { + union mf_value imm; + + memcpy(&imm, spec.src_imm, spec.dst->n_bytes); + mf_set_value(spec.dst, &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); + } else { + put_u32(b, spec.src->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); + } else { + assert(spec.dst_type == NX_LEARN_DST_OUTPUT); + } + } + } + free(orig); + + put_u16(b, 0); + + len = b->size - learn_ofs; + if (len % 8) { + ofpbuf_put_zeros(b, 8 - len % 8); + } + + learn = ofpbuf_at_assert(b, learn_ofs, sizeof *learn); + learn->len = htons(b->size - learn_ofs); +} + +void +learn_format(const struct nx_action_learn *learn, struct ds *s) +{ + struct cls_rule rule; + const void *p, *end; + + cls_rule_init_catchall(&rule, 0); + + ds_put_format(s, "learn(table=%"PRIu8, learn->table_id); + if (learn->idle_timeout != htons(OFP_FLOW_PERMANENT)) { + ds_put_format(s, ",idle_timeout=%"PRIu16, ntohs(learn->idle_timeout)); + } + if (learn->hard_timeout != htons(OFP_FLOW_PERMANENT)) { + ds_put_format(s, ",hard_timeout=%"PRIu16, ntohs(learn->hard_timeout)); + } + if (learn->priority != htons(OFP_DEFAULT_PRIORITY)) { + ds_put_format(s, ",priority=%"PRIu16, ntohs(learn->priority)); + } + if (learn->flags & htons(OFPFF_SEND_FLOW_REM)) { + ds_put_cstr(s, ",OFPFF_SEND_FLOW_REM"); + } + if (learn->flags & htons(~OFPFF_SEND_FLOW_REM)) { + ds_put_format(s, ",***flags=%"PRIu16"***", + ntohs(learn->flags) & ~OFPFF_SEND_FLOW_REM); + } + if (learn->cookie != htonll(0)) { + ds_put_format(s, ",cookie=0x%"PRIx64, ntohll(learn->cookie)); + } + if (!is_all_zeros(learn->pad, sizeof learn->pad)) { + ds_put_cstr(s, ",***nonzero pad***"); + } + + end = (char *) learn + ntohs(learn->len); + for (p = learn + 1; p != end; ) { + uint16_t header = ntohs(get_be16(&p)); + int n_bits = header & NX_LEARN_N_BITS_MASK; + + int src_type = header & NX_LEARN_SRC_MASK; + uint32_t src_header; + int src_ofs; + 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; + + int error; + int i; + + if (!header) { + break; + } + + error = learn_check_header(header, (char *) end - (char *) p); + if (error == ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT)) { + ds_put_format(s, ",***bad flow_mod_spec header %"PRIx16"***)", + header); + return; + } else if (error == ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_LEN)) { + ds_put_format(s, ",***flow_mod_spec at offset %td is %u bytes " + "long but only %td bytes are left***)", + (char *) p - (char *) (learn + 1) - 2, + learn_min_len(header) + 2, + (char *) end - (char *) p + 2); + return; + } + assert(!error); + + /* Get the source. */ + if (src_type == NX_LEARN_SRC_FIELD) { + src_header = ntohl(get_be32(&p)); + src_ofs = ntohs(get_be16(&p)); + src_value_bytes = 0; + src_value = NULL; + } else { + src_header = 0; + src_ofs = 0; + src_value_bytes = 2 * DIV_ROUND_UP(n_bits, 16); + src_value = p; + p = (const void *) ((const uint8_t *) p + src_value_bytes); + } + + /* Get the destination. */ + if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) { + dst_header = ntohl(get_be32(&p)); + dst_field = nxm_field_to_mf_field(dst_header); + dst_ofs = ntohs(get_be16(&p)); + } else { + dst_header = 0; + dst_field = NULL; + dst_ofs = 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) { + union mf_value value; + uint8_t *bytes = (uint8_t *) &value; + + memset(&value, 0, sizeof value); + 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); + } else { + nxm_format_field_bits(s, dst_header, dst_ofs, n_bits); + ds_put_cstr(s, "=0x"); + for (i = 0; i < src_value_bytes; i++) { + ds_put_format(s, "%02"PRIx8, src_value[i]); + } + } + 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) { + ds_put_char(s, '='); + nxm_format_field_bits(s, src_header, src_ofs, n_bits); + } + break; + + case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_LOAD: + ds_put_cstr(s, "load:0x"); + for (i = 0; i < src_value_bytes; i++) { + ds_put_format(s, "%02"PRIx8, src_value[i]); + } + ds_put_cstr(s, "->"); + nxm_format_field_bits(s, dst_header, dst_ofs, n_bits); + 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); + ds_put_cstr(s, "->"); + nxm_format_field_bits(s, dst_header, dst_ofs, n_bits); + 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); + break; + } + } + if (!is_all_zeros(p, (char *) end - (char *) p)) { + ds_put_cstr(s, ",***nonzero trailer***"); + } + ds_put_char(s, ')'); +} diff --git a/lib/learn.h b/lib/learn.h new file mode 100644 index 000000000..19a9089c2 --- /dev/null +++ b/lib/learn.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2011 Nicira Networks. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LEARN_H +#define LEARN_H 1 + +struct ds; +struct flow; +struct ofpbuf; +struct ofputil_flow_mod; +struct nx_action_learn; + +/* NXAST_LEARN helper functions. + * + * See include/openflow/nicira-ext.h for NXAST_LEARN specification. + */ + +int learn_check(const struct nx_action_learn *, const struct flow *); +void learn_execute(const struct nx_action_learn *, const struct flow *, + struct ofputil_flow_mod *); + +void learn_parse(struct ofpbuf *, char *, const struct flow *); +void learn_format(const struct nx_action_learn *, struct ds *); + +#endif /* learn.h */ diff --git a/lib/meta-flow.c b/lib/meta-flow.c index 9727253ef..140f90224 100644 --- a/lib/meta-flow.c +++ b/lib/meta-flow.c @@ -302,32 +302,6 @@ static const struct mf_field mf_fields[MFF_N_IDS] = { } }; -static bool -is_all_zeros(const uint8_t *field, size_t length) -{ - size_t i; - - for (i = 0; i < length; i++) { - if (field[i] != 0x00) { - return false; - } - } - return true; -} - -static bool -is_all_ones(const uint8_t *field, size_t length) -{ - size_t i; - - for (i = 0; i < length; i++) { - if (field[i] != 0xff) { - return false; - } - } - return true; -} - /* Returns the field with the given 'id'. */ const struct mf_field * mf_from_id(enum mf_field_id id) diff --git a/lib/ofp-parse.c b/lib/ofp-parse.c index a80d45fe6..fd0a72e02 100644 --- a/lib/ofp-parse.c +++ b/lib/ofp-parse.c @@ -26,6 +26,7 @@ #include "bundle.h" #include "byte-order.h" #include "dynamic-string.h" +#include "learn.h" #include "meta-flow.h" #include "netdev.h" #include "multipath.h" @@ -228,7 +229,8 @@ parse_note(struct ofpbuf *b, const char *arg) } static void -parse_named_action(enum ofputil_action_code code, struct ofpbuf *b, char *arg) +parse_named_action(enum ofputil_action_code code, const struct flow *flow, + struct ofpbuf *b, char *arg) { struct ofp_action_dl_addr *oada; struct ofp_action_vlan_pcp *oavp; @@ -332,11 +334,15 @@ parse_named_action(enum ofputil_action_code code, struct ofpbuf *b, char *arg) case OFPUTIL_NXAST_RESUBMIT_TABLE: case OFPUTIL_NXAST_OUTPUT_REG: NOT_REACHED(); + + case OFPUTIL_NXAST_LEARN: + learn_parse(b, arg, flow); + break; } } static void -str_to_action(char *str, struct ofpbuf *b) +str_to_action(const struct flow *flow, char *str, struct ofpbuf *b) { char *pos, *act, *arg; int n_actions; @@ -349,7 +355,7 @@ str_to_action(char *str, struct ofpbuf *b) code = ofputil_action_code_from_name(act); if (code >= 0) { - parse_named_action(code, b, arg); + parse_named_action(code, flow, b, arg); } else if (!strcasecmp(act, "drop")) { /* A drop action in OpenFlow occurs by just not setting * an action. */ @@ -462,6 +468,7 @@ parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_, } fields; char *string = xstrdup(str_); char *save_ptr = NULL; + char *act_str = NULL; char *name; switch (command) { @@ -503,9 +510,6 @@ parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_, fm->out_port = OFPP_NONE; fm->flags = 0; if (fields & F_ACTIONS) { - struct ofpbuf actions; - char *act_str; - act_str = strstr(string, "action"); if (!act_str) { ofp_fatal(str_, verbose, "must specify an action"); @@ -518,14 +522,6 @@ parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_, } act_str++; - - ofpbuf_init(&actions, sizeof(union ofp_action)); - str_to_action(act_str, &actions); - fm->actions = ofpbuf_steal_data(&actions); - fm->n_actions = actions.size / sizeof(union ofp_action); - } else { - fm->actions = NULL; - fm->n_actions = 0; } for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name; name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) { @@ -569,6 +565,17 @@ parse_ofp_str(struct ofputil_flow_mod *fm, int command, const char *str_, } } } + if (fields & F_ACTIONS) { + struct ofpbuf actions; + + ofpbuf_init(&actions, sizeof(union ofp_action)); + str_to_action(&fm->cr.flow, act_str, &actions); + fm->actions = ofpbuf_steal_data(&actions); + fm->n_actions = actions.size / sizeof(union ofp_action); + } else { + fm->actions = NULL; + fm->n_actions = 0; + } free(string); } diff --git a/lib/ofp-print.c b/lib/ofp-print.c index 3b9c58263..23110920b 100644 --- a/lib/ofp-print.c +++ b/lib/ofp-print.c @@ -31,6 +31,7 @@ #include "compiler.h" #include "dynamic-string.h" #include "flow.h" +#include "learn.h" #include "multipath.h" #include "nx-match.h" #include "ofp-util.h" @@ -333,6 +334,10 @@ ofp_print_action(struct ds *s, const union ofp_action *a, nxm_decode_n_bits(naor->ofs_nbits)); break; + case OFPUTIL_NXAST_LEARN: + learn_format((const struct nx_action_learn *) a, s); + break; + default: break; } diff --git a/lib/ofp-util.c b/lib/ofp-util.c index 6887217da..00d1af76f 100644 --- a/lib/ofp-util.c +++ b/lib/ofp-util.c @@ -25,6 +25,7 @@ #include "byte-order.h" #include "classifier.h" #include "dynamic-string.h" +#include "learn.h" #include "multipath.h" #include "nx-match.h" #include "ofp-errors.h" @@ -2132,6 +2133,10 @@ validate_actions(const union ofp_action *actions, size_t n_actions, (const struct nx_action_resubmit *) a); break; + case OFPUTIL_NXAST_LEARN: + error = learn_check((const struct nx_action_learn *) a, flow); + break; + case OFPUTIL_OFPAT_STRIP_VLAN: case OFPUTIL_OFPAT_SET_NW_SRC: case OFPUTIL_OFPAT_SET_NW_DST: diff --git a/lib/ofp-util.def b/lib/ofp-util.def index c5d883d67..7868faa4b 100644 --- a/lib/ofp-util.def +++ b/lib/ofp-util.def @@ -34,4 +34,5 @@ NXAST_ACTION(NXAST_BUNDLE, nx_action_bundle, 1, "bundle") NXAST_ACTION(NXAST_BUNDLE_LOAD, nx_action_bundle, 1, "bundle_load") NXAST_ACTION(NXAST_RESUBMIT_TABLE, nx_action_resubmit, 0, NULL) NXAST_ACTION(NXAST_OUTPUT_REG, nx_action_output_reg, 0, NULL) +NXAST_ACTION(NXAST_LEARN, nx_action_learn, 1, "learn") #undef NXAST_ACTION diff --git a/lib/util.c b/lib/util.c index 5e90ecb50..e02f59fb0 100644 --- a/lib/util.c +++ b/lib/util.c @@ -696,3 +696,32 @@ ctz(uint32_t n) #endif } } + +/* Returns true if the 'n' bytes starting at 'p' are zeros. */ +bool +is_all_zeros(const uint8_t *p, size_t n) +{ + size_t i; + + for (i = 0; i < n; i++) { + if (p[i] != 0x00) { + return false; + } + } + return true; +} + +/* Returns true if the 'n' bytes starting at 'p' are 0xff. */ +bool +is_all_ones(const uint8_t *p, size_t n) +{ + size_t i; + + for (i = 0; i < n; i++) { + if (p[i] != 0xff) { + return false; + } + } + return true; +} + diff --git a/lib/util.h b/lib/util.h index 5c8618d33..5ae0775f1 100644 --- a/lib/util.h +++ b/lib/util.h @@ -197,6 +197,9 @@ void ignore(bool x OVS_UNUSED); int log_2_floor(uint32_t); int ctz(uint32_t); +bool is_all_zeros(const uint8_t *, size_t); +bool is_all_ones(const uint8_t *, size_t); + #ifdef __cplusplus } #endif -- cgit v1.2.1