/*
- * Copyright (c) 2011 Nicira Networks.
+ * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "learn.h"
#include "byte-order.h"
-#include "dynamic-string.h"
-#include "meta-flow.h"
+#include "colors.h"
#include "nx-match.h"
-#include "ofp-errors.h"
-#include "ofp-util.h"
-#include "ofpbuf.h"
#include "openflow/openflow.h"
+#include "openvswitch/dynamic-string.h"
+#include "openvswitch/match.h"
+#include "openvswitch/meta-flow.h"
+#include "openvswitch/ofp-actions.h"
+#include "openvswitch/ofp-errors.h"
+#include "openvswitch/ofp-util.h"
+#include "openvswitch/ofpbuf.h"
+#include "vl-mff-map.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 enum ofperr
-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 OFPERR_OFPBAC_BAD_ARGUMENT;
- }
- /* Check that the arguments don't overrun the end of the action. */
- if (len < learn_min_len(header)) {
- return OFPERR_OFPBAC_BAD_LEN;
- }
-
- return 0;
-}
-
-/* Checks that 'learn' (which must be at least 'sizeof *learn' bytes long) is a
- * valid action on 'flow'. */
+/* Checks that 'learn' is a valid action on 'flow'. Returns 0 if it is valid,
+ * otherwise an OFPERR_*. */
enum ofperr
-learn_check(const struct nx_action_learn *learn, const struct flow *flow)
+learn_check(const struct ofpact_learn *learn, const struct match *src_match)
{
- 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 OFPERR_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;
+ const struct ofpact_learn_spec *spec;
+ struct match dst_match;
+ match_init_catchall(&dst_match);
+ OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
enum ofperr error;
- uint64_t value;
-
- 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 (spec->src_type == NX_LEARN_SRC_FIELD) {
+ error = mf_check_src(&spec->src, src_match);
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));
-
- 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));
+ switch (spec->dst_type) {
+ case NX_LEARN_DST_MATCH:
+ error = mf_check_src(&spec->dst, &dst_match);
if (error) {
return error;
}
+ if (spec->src_type & NX_LEARN_SRC_IMMEDIATE) {
+ mf_write_subfield_value(&spec->dst,
+ ofpact_learn_spec_imm(spec),
+ &dst_match);
+ }
+ break;
- 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);
+ case NX_LEARN_DST_LOAD:
+ error = mf_check_dst(&spec->dst, &dst_match);
+ if (error) {
+ return error;
}
+ break;
+
+ case NX_LEARN_DST_OUTPUT:
+ /* Nothing to do. */
+ break;
}
}
- if (!is_all_zeros(p, (char *) end - (char *) p)) {
- return OFPERR_OFPBAC_BAD_ARGUMENT;
- }
-
return 0;
}
+/* Composes 'fm' so that executing it will implement 'learn' given that the
+ * packet being processed has 'flow' as its flow.
+ *
+ * Uses 'ofpacts' to store the flow mod's actions. The caller must initialize
+ * 'ofpacts' and retains ownership of it. 'fm->ofpacts' will point into the
+ * 'ofpacts' buffer.
+ *
+ * The caller has to actually execute 'fm'. */
void
-learn_execute(const struct nx_action_learn *learn, const struct flow *flow,
- struct ofputil_flow_mod *fm)
+learn_execute(const struct ofpact_learn *learn, const struct flow *flow,
+ struct ofputil_flow_mod *fm, struct ofpbuf *ofpacts)
{
- const void *p, *end;
- struct ofpbuf actions;
-
- cls_rule_init_catchall(&fm->cr, ntohs(learn->priority));
- fm->cookie = learn->cookie;
+ const struct ofpact_learn_spec *spec;
+
+ match_init_catchall(&fm->match);
+ fm->priority = learn->priority;
+ fm->cookie = htonll(0);
+ fm->cookie_mask = htonll(0);
+ fm->new_cookie = learn->cookie;
+ fm->modify_cookie = fm->new_cookie != OVS_BE64_MAX;
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->idle_timeout = learn->idle_timeout;
+ fm->hard_timeout = learn->hard_timeout;
+ fm->importance = 0;
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;
+ fm->ofpacts_tlv_bitmap = 0;
+ fm->flags = 0;
+ if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
+ fm->flags |= OFPUTIL_FF_SEND_FLOW_REM;
+ }
+ fm->ofpacts = NULL;
+ fm->ofpacts_len = 0;
- struct nx_action_reg_load *load;
- ovs_be32 dst_field;
- int dst_ofs;
+ if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
+ struct ofpact_fin_timeout *oft;
- if (!header) {
- break;
- }
+ oft = ofpact_put_FIN_TIMEOUT(ofpacts);
+ oft->fin_idle_timeout = learn->fin_idle_timeout;
+ oft->fin_hard_timeout = learn->fin_hard_timeout;
+ }
- if (src_type == NX_LEARN_SRC_FIELD) {
- ovs_be32 src_field = get_be32(&p);
- int src_ofs = ntohs(get_be16(&p));
+ OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
+ struct ofpact_set_field *sf;
+ union mf_subvalue value;
- value = nxm_read_field_bits(src_field,
- nxm_encode_ofs_nbits(src_ofs, n_bits),
- flow);
+ if (spec->src_type == NX_LEARN_SRC_FIELD) {
+ mf_read_subfield(&spec->src, flow, &value);
} else {
- value = get_bits(n_bits, &p);
+ mf_subvalue_from_value(&spec->dst, &value,
+ ofpact_learn_spec_imm(spec));
}
- switch (dst_type) {
+ switch (spec->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);
+ mf_write_subfield(&spec->dst, &value, &fm->match);
+ match_add_ethernet_prereq(&fm->match, spec->dst.field);
+ mf_vl_mff_set_tlv_bitmap(
+ spec->dst.field, &fm->match.flow.tunnel.metadata.present.map);
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);
+ sf = ofpact_put_reg_load(ofpacts, spec->dst.field, NULL, NULL);
+ bitwise_copy(&value, sizeof value, 0,
+ sf->value, spec->dst.field->n_bytes, spec->dst.ofs,
+ spec->n_bits);
+ bitwise_one(ofpact_set_field_mask(sf), spec->dst.field->n_bytes,
+ spec->dst.ofs, spec->n_bits);
+ mf_vl_mff_set_tlv_bitmap(spec->dst.field, &fm->ofpacts_tlv_bitmap);
break;
case NX_LEARN_DST_OUTPUT:
- ofputil_put_OFPAT_OUTPUT(&actions)->port = htons(value);
+ if (spec->n_bits <= 16
+ || is_all_zeros(value.u8, sizeof value - 2)) {
+ ofp_port_t port = u16_to_ofp(ntohll(value.integer));
+
+ if (ofp_to_u16(port) < ofp_to_u16(OFPP_MAX)
+ || port == OFPP_IN_PORT
+ || port == OFPP_FLOOD
+ || port == OFPP_LOCAL
+ || port == OFPP_ALL) {
+ ofpact_put_OUTPUT(ofpacts)->port = port;
+ }
+ }
break;
}
}
- fm->actions = ofpbuf_steal_data(&actions);
- fm->n_actions = actions.size / sizeof(struct ofp_action_header);
+ fm->ofpacts = ofpacts->data;
+ fm->ofpacts_len = ofpacts->size;
}
-static void
-put_be16(struct ofpbuf *b, ovs_be16 x)
+/* Perform a bitwise-OR on 'wc''s fields that are relevant as sources in
+ * the learn action 'learn'. */
+void
+learn_mask(const struct ofpact_learn *learn, struct flow_wildcards *wc)
{
- ofpbuf_put(b, &x, sizeof x);
-}
+ const struct ofpact_learn_spec *spec;
+ union mf_subvalue value;
-static void
-put_be32(struct ofpbuf *b, ovs_be32 x)
-{
- ofpbuf_put(b, &x, sizeof x);
+ memset(&value, 0xff, sizeof value);
+ OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
+ if (spec->src_type == NX_LEARN_SRC_FIELD) {
+ mf_write_subfield_flow(&spec->src, &value, &wc->masks);
+ }
+ }
}
-static void
-put_u16(struct ofpbuf *b, uint16_t x)
+/* Returns NULL if successful, otherwise a malloc()'d string describing the
+ * error. The caller is responsible for freeing the returned string. */
+static char * OVS_WARN_UNUSED_RESULT
+learn_parse_load_immediate(union mf_subvalue *imm, const char *s,
+ const char *full_s, struct ofpact_learn_spec *spec,
+ struct ofpbuf *ofpacts)
{
- put_be16(b, htons(x));
-}
+ struct mf_subfield dst;
+ char *error;
-static void
-put_u32(struct ofpbuf *b, uint32_t x)
-{
- put_be32(b, htonl(x));
-}
+ error = mf_parse_subfield(&dst, s);
+ if (error) {
+ return error;
+ }
+ if (!mf_nxm_header(dst.field->id)) {
+ return xasprintf("%s: experimenter OXM field '%s' not supported",
+ full_s, s);
+ }
+
+ if (!bitwise_is_all_zeros(imm, sizeof *imm, dst.n_bits,
+ (8 * sizeof *imm) - dst.n_bits)) {
+ return xasprintf("%s: value does not fit into %u bits",
+ full_s, dst.n_bits);
+ }
-struct learn_spec {
- int n_bits;
+ spec->n_bits = dst.n_bits;
+ spec->src_type = NX_LEARN_SRC_IMMEDIATE;
+ spec->dst_type = NX_LEARN_DST_LOAD;
+ spec->dst = dst;
- int src_type;
- const struct mf_field *src;
- int src_ofs;
- uint8_t src_imm[sizeof(union mf_value)];
+ /* Push value last, as this may reallocate 'spec'! */
+ unsigned int n_bytes = DIV_ROUND_UP(dst.n_bits, 8);
+ uint8_t *src_imm = ofpbuf_put_zeros(ofpacts, OFPACT_ALIGN(n_bytes));
+ memcpy(src_imm, &imm->u8[sizeof imm->u8 - n_bytes], n_bytes);
- int dst_type;
- const struct mf_field *dst;
- int dst_ofs;
-};
+ return NULL;
+}
-static void
+/* Returns NULL if successful, otherwise a malloc()'d string describing the
+ * error. The caller is responsible for freeing the returned string. */
+static char * OVS_WARN_UNUSED_RESULT
learn_parse_spec(const char *orig, char *name, char *value,
- struct learn_spec *spec)
+ const struct ofputil_port_map *port_map,
+ struct ofpact_learn_spec *spec,
+ struct ofpbuf *ofpacts, struct match *match)
{
- if (mf_from_name(name)) {
- const struct mf_field *dst = mf_from_name(name);
- union mf_value imm;
- char *error;
+ /* Parse destination and check prerequisites. */
+ struct mf_subfield dst;
- error = mf_parse_value(dst, value, &imm);
- if (error) {
- ovs_fatal(0, "%s", error);
- }
+ char *error = mf_parse_subfield(&dst, name);
+ bool parse_error = error != NULL;
+ free(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);
+ if (!parse_error) {
+ if (!mf_nxm_header(dst.field->id)) {
+ return xasprintf("%s: experimenter OXM field '%s' not supported",
+ orig, name);
}
+ spec->dst = dst;
+ spec->n_bits = dst.n_bits;
+ spec->dst_type = NX_LEARN_DST_MATCH;
/* Parse source and check prerequisites. */
if (value[0] != '\0') {
- int src_nbits;
+ struct mf_subfield src;
+ error = mf_parse_subfield(&src, value);
+ if (error) {
+ union mf_value imm;
+ char *imm_error = NULL;
+
+ /* Try an immediate value. */
+ if (dst.ofs == 0 && dst.n_bits == dst.field->n_bits) {
+ /* Full field value. */
+ imm_error = mf_parse_value(dst.field, value, port_map,
+ &imm);
+ } else {
+ char *tail;
+ /* Partial field value. */
+ if (parse_int_string(value, (uint8_t *)&imm,
+ dst.field->n_bytes, &tail)
+ || *tail != 0) {
+ imm_error = xasprintf("%s: cannot parse integer value", orig);
+ }
+
+ if (!imm_error &&
+ !bitwise_is_all_zeros(&imm, dst.field->n_bytes,
+ dst.n_bits,
+ dst.field->n_bytes * 8 - dst.n_bits)) {
+ struct ds ds;
+
+ ds_init(&ds);
+ mf_format(dst.field, &imm, NULL, NULL, &ds);
+ imm_error = xasprintf("%s: value %s does not fit into %d bits",
+ orig, ds_cstr(&ds), dst.n_bits);
+ ds_destroy(&ds);
+ }
+ }
+ if (imm_error) {
+ char *err = xasprintf("%s: %s value %s cannot be parsed as a subfield (%s) or an immediate value (%s)",
+ orig, name, value, error, imm_error);
+ free(error);
+ free(imm_error);
+ return err;
+ }
+
+ spec->src_type = NX_LEARN_SRC_IMMEDIATE;
+
+ /* Update 'match' to allow for satisfying destination
+ * prerequisites. */
+ mf_write_subfield_value(&dst, &imm, match);
- 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);
+ /* Push value last, as this may reallocate 'spec'! */
+ unsigned int imm_bytes = DIV_ROUND_UP(dst.n_bits, 8);
+ uint8_t *src_imm = ofpbuf_put_zeros(ofpacts,
+ OFPACT_ALIGN(imm_bytes));
+ memcpy(src_imm, &imm, imm_bytes);
+
+ free(error);
+ return NULL;
}
- 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);
+ spec->src = src;
+ if (spec->src.n_bits != spec->dst.n_bits) {
+ return xasprintf("%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->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;
- 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);
+ union mf_subvalue imm;
+ char *tail;
+ char *dst_value = strstr(value, "->");
+
+ if (dst_value == value) {
+ return xasprintf("%s: missing source before `->' in `%s'", name,
+ value);
+ }
+ if (!dst_value) {
+ return xasprintf("%s: missing `->' in `%s'", name, value);
+ }
+
+ if (!parse_int_string(value, imm.u8, sizeof imm.u8, (char **) &tail)
+ && tail != value) {
+ if (tail != dst_value) {
+ return xasprintf("%s: garbage before `->' in `%s'",
+ name, value);
+ }
+
+ error = learn_parse_load_immediate(&imm, dst_value + 2, value, spec,
+ ofpacts);
+ if (error) {
+ return error;
}
- 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);
} else {
- struct nx_action_reg_move move;
+ struct ofpact_reg_move move;
- nxm_parse_reg_move(&move, value);
+ error = nxm_parse_reg_move(&move, value);
+ if (error) {
+ return error;
+ }
- spec->n_bits = ntohs(move.n_bits);
+ spec->n_bits = move.src.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);
+ spec->src = move.src;
spec->dst_type = NX_LEARN_DST_LOAD;
- spec->dst = mf_from_nxm_header(ntohl(move.dst));
- spec->dst_ofs = ntohs(move.dst_ofs);
+ spec->dst = move.dst;
}
} 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);
+ error = mf_parse_subfield(&spec->src, value);
+ if (error) {
+ return error;
}
- 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);
+ return xasprintf("%s: unknown keyword %s", orig, name);
}
+
+ return NULL;
}
-void
-learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow)
+/* Returns NULL if successful, otherwise a malloc()'d string describing the
+ * error. The caller is responsible for freeing the returned string. */
+static char * OVS_WARN_UNUSED_RESULT
+learn_parse__(char *orig, char *arg, const struct ofputil_port_map *port_map,
+ struct ofpbuf *ofpacts)
{
- char *orig = xstrdup(arg);
+ struct ofpact_learn *learn;
+ struct match match;
char *name, *value;
- enum ofperr error;
- 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 = ofpact_put_LEARN(ofpacts);
+ learn->idle_timeout = OFP_FLOW_PERMANENT;
+ learn->hard_timeout = OFP_FLOW_PERMANENT;
+ learn->priority = OFP_DEFAULT_PRIORITY;
learn->table_id = 1;
- cls_rule_init_catchall(&rule, 0);
+ match_init_catchall(&match);
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);
+ return xasprintf("%s: table id 255 not valid for `learn' "
+ "action", orig);
}
} else if (!strcmp(name, "priority")) {
- learn->priority = htons(atoi(value));
+ learn->priority = atoi(value);
} else if (!strcmp(name, "idle_timeout")) {
- learn->idle_timeout = htons(atoi(value));
+ learn->idle_timeout = atoi(value);
} else if (!strcmp(name, "hard_timeout")) {
- learn->hard_timeout = htons(atoi(value));
+ learn->hard_timeout = atoi(value);
+ } else if (!strcmp(name, "fin_idle_timeout")) {
+ learn->fin_idle_timeout = atoi(value);
+ } else if (!strcmp(name, "fin_hard_timeout")) {
+ learn->fin_hard_timeout = 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);
+ } else if (!strcmp(name, "send_flow_rem")) {
+ learn->flags |= NX_LEARN_F_SEND_FLOW_REM;
+ } else if (!strcmp(name, "delete_learned")) {
+ learn->flags |= NX_LEARN_F_DELETE_LEARNED;
+ } else if (!strcmp(name, "limit")) {
+ learn->limit = atoi(value);
+ } else if (!strcmp(name, "result_dst")) {
+ char *error;
+ learn->flags |= NX_LEARN_F_WRITE_RESULT;
+ error = mf_parse_subfield(&learn->result_dst, value);
+ if (error) {
+ return error;
}
- 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);
+ if (!learn->result_dst.field->writable) {
+ return xasprintf("%s is read-only", value);
}
-
- /* 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);
+ if (learn->result_dst.n_bits != 1) {
+ return xasprintf("result_dst in 'learn' action must be a "
+ "single bit");
}
+ } else {
+ struct ofpact_learn_spec *spec;
+ char *error;
- /* 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);
+ spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
+ error = learn_parse_spec(orig, name, value, port_map,
+ spec, ofpacts, &match);
+ if (error) {
+ return error;
}
+ learn = ofpacts->header;
}
}
+ ofpact_finish_LEARN(ofpacts, &learn);
- 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);
+ return NULL;
+}
- /* In theory the above should have caught any errors, but... */
- error = learn_check(learn, flow);
- if (error) {
- ovs_fatal(0, "%s: %s", orig, ofperr_to_string(error));
- }
+/* Parses 'arg' as a set of arguments to the "learn" action and appends a
+ * matching OFPACT_LEARN action to 'ofpacts'. ovs-ofctl(8) describes the
+ * format parsed.
+ *
+ * Returns NULL if successful, otherwise a malloc()'d string describing the
+ * error. The caller is responsible for freeing the returned string.
+ *
+ * If 'flow' is nonnull, then it should be the flow from a struct match that is
+ * the matching rule for the learning action. This helps to better validate
+ * the action's arguments.
+ *
+ * Modifies 'arg'. */
+char * OVS_WARN_UNUSED_RESULT
+learn_parse(char *arg, const struct ofputil_port_map *port_map,
+ struct ofpbuf *ofpacts)
+{
+ char *orig = xstrdup(arg);
+ char *error = learn_parse__(orig, arg, port_map, ofpacts);
free(orig);
+ return error;
}
+/* Appends a description of 'learn' to 's', in the format that ovs-ofctl(8)
+ * describes. */
void
-learn_format(const struct nx_action_learn *learn, struct ds *s)
+learn_format(const struct ofpact_learn *learn,
+ const struct ofputil_port_map *port_map, struct ds *s)
{
- struct cls_rule rule;
- const void *p, *end;
+ const struct ofpact_learn_spec *spec;
+ struct match match;
- cls_rule_init_catchall(&rule, 0);
+ match_init_catchall(&match);
- 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));
+ ds_put_format(s, "%slearn(%s%stable=%s%"PRIu8,
+ colors.learn, colors.end, colors.special, colors.end,
+ learn->table_id);
+ if (learn->idle_timeout != OFP_FLOW_PERMANENT) {
+ ds_put_format(s, ",%sidle_timeout=%s%"PRIu16,
+ colors.param, colors.end, learn->idle_timeout);
}
- if (learn->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
- ds_put_format(s, ",hard_timeout=%"PRIu16, ntohs(learn->hard_timeout));
+ if (learn->hard_timeout != OFP_FLOW_PERMANENT) {
+ ds_put_format(s, ",%shard_timeout=%s%"PRIu16,
+ colors.param, colors.end, learn->hard_timeout);
}
- if (learn->priority != htons(OFP_DEFAULT_PRIORITY)) {
- ds_put_format(s, ",priority=%"PRIu16, ntohs(learn->priority));
+ if (learn->fin_idle_timeout) {
+ ds_put_format(s, ",%sfin_idle_timeout=%s%"PRIu16,
+ colors.param, colors.end, learn->fin_idle_timeout);
}
- if (learn->flags & htons(OFPFF_SEND_FLOW_REM)) {
- ds_put_cstr(s, ",OFPFF_SEND_FLOW_REM");
+ if (learn->fin_hard_timeout) {
+ ds_put_format(s, "%s,fin_hard_timeout=%s%"PRIu16,
+ colors.param, colors.end, learn->fin_hard_timeout);
}
- if (learn->flags & htons(~OFPFF_SEND_FLOW_REM)) {
- ds_put_format(s, ",***flags=%"PRIu16"***",
- ntohs(learn->flags) & ~OFPFF_SEND_FLOW_REM);
+ if (learn->priority != OFP_DEFAULT_PRIORITY) {
+ ds_put_format(s, "%s,priority=%s%"PRIu16,
+ colors.special, colors.end, learn->priority);
}
- if (learn->cookie != htonll(0)) {
- ds_put_format(s, ",cookie=0x%"PRIx64, ntohll(learn->cookie));
+ if (learn->flags & NX_LEARN_F_SEND_FLOW_REM) {
+ ds_put_format(s, ",%ssend_flow_rem%s", colors.value, colors.end);
}
- if (!is_all_zeros(learn->pad, sizeof learn->pad)) {
- ds_put_cstr(s, ",***nonzero pad***");
+ if (learn->flags & NX_LEARN_F_DELETE_LEARNED) {
+ ds_put_format(s, ",%sdelete_learned%s", colors.value, colors.end);
+ }
+ if (learn->cookie != 0) {
+ ds_put_format(s, ",%scookie=%s%#"PRIx64,
+ colors.param, colors.end, ntohll(learn->cookie));
+ }
+ if (learn->limit != 0) {
+ ds_put_format(s, ",%slimit=%s%"PRIu32,
+ colors.param, colors.end, learn->limit);
+ }
+ if (learn->flags & NX_LEARN_F_WRITE_RESULT) {
+ ds_put_format(s, ",%sresult_dst=%s", colors.param, colors.end);
+ mf_format_subfield(&learn->result_dst, s);
}
- 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;
-
- enum ofperr error;
- int i;
-
- if (!header) {
- break;
- }
-
- error = learn_check_header(header, (char *) end - (char *) p);
- if (error == OFPERR_OFPBAC_BAD_ARGUMENT) {
- ds_put_format(s, ",***bad flow_mod_spec header %"PRIx16"***)",
- header);
- return;
- } else if (error == OFPERR_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 = mf_from_nxm_header(dst_header);
- dst_ofs = ntohs(get_be16(&p));
- } else {
- dst_header = 0;
- dst_field = NULL;
- dst_ofs = 0;
- }
-
+ OFPACT_LEARN_SPEC_FOR_EACH (spec, learn) {
+ unsigned int n_bytes = DIV_ROUND_UP(spec->n_bits, 8);
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) {
+ switch (spec->src_type | spec->dst_type) {
+ case NX_LEARN_SRC_IMMEDIATE | NX_LEARN_DST_MATCH: {
+ if (spec->dst.ofs == 0
+ && spec->dst.n_bits == spec->dst.field->n_bits) {
union mf_value value;
- uint8_t *bytes = (uint8_t *) &value;
-
- 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;
- src_value += diff;
- src_value_bytes -= diff;
- }
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);
+ memcpy(&value.b[spec->dst.field->n_bytes - n_bytes],
+ ofpact_learn_spec_imm(spec), n_bytes);
+ ds_put_format(s, "%s%s=%s", colors.param,
+ spec->dst.field->name, colors.end);
+ mf_format(spec->dst.field, &value, NULL, port_map, 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]);
- }
+ ds_put_format(s, "%s", colors.param);
+ mf_format_subfield(&spec->dst, s);
+ ds_put_format(s, "=%s", colors.end);
+ ds_put_hex(s, ofpact_learn_spec_imm(spec), n_bytes);
}
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);
+ ds_put_format(s, "%s", colors.param);
+ mf_format_subfield(&spec->dst, s);
+ ds_put_format(s, "%s", colors.end);
+ if (spec->src.field != spec->dst.field ||
+ spec->src.ofs != spec->dst.ofs) {
+ ds_put_format(s, "%s=%s", colors.param, colors.end);
+ mf_format_subfield(&spec->src, s);
}
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);
+ ds_put_format(s, "%sload:%s", colors.special, colors.end);
+ ds_put_hex(s, ofpact_learn_spec_imm(spec), n_bytes);
+ ds_put_format(s, "%s->%s", colors.special, colors.end);
+ mf_format_subfield(&spec->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);
- ds_put_cstr(s, "->");
- nxm_format_field_bits(s, dst_header, dst_ofs, n_bits);
+ ds_put_format(s, "%sload:%s", colors.special, colors.end);
+ mf_format_subfield(&spec->src, s);
+ ds_put_format(s, "%s->%s", colors.special, colors.end);
+ mf_format_subfield(&spec->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);
+ ds_put_format(s, "%soutput:%s", colors.special, colors.end);
+ mf_format_subfield(&spec->src, s);
break;
}
}
- if (!is_all_zeros(p, (char *) end - (char *) p)) {
- ds_put_cstr(s, ",***nonzero trailer***");
- }
- ds_put_char(s, ')');
+ ds_put_format(s, "%s)%s", colors.learn, colors.end);
}