/*
- * Copyright (c) 2011 Nicira Networks.
+ * Copyright (c) 2011, 2012 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
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)
{
cls_rule_init_catchall(&rule, 0);
if (learn->flags & ~htons(OFPFF_SEND_FLOW_REM)
- || !is_all_zeros(learn->pad, sizeof learn->pad)
+ || learn->pad
|| learn->table_id == 0xff) {
return OFPERR_OFPBAC_BAD_ARGUMENT;
}
/* 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;
}
/* 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);
+ if (n_bits <= 64) {
+ mf_set_subfield(&dst, value, &rule);
+ } else {
+ /* We're only setting subfields to allow us to check
+ * 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 == 11);
+ }
}
}
}
struct ofpbuf actions;
cls_rule_init_catchall(&fm->cr, ntohs(learn->priority));
- fm->cookie = learn->cookie;
+ fm->cookie = htonll(0);
+ fm->cookie_mask = htonll(0);
+ fm->new_cookie = learn->cookie;
fm->table_id = learn->table_id;
fm->command = OFPFC_MODIFY_STRICT;
fm->idle_timeout = ntohs(learn->idle_timeout);
ofpbuf_init(&actions, 64);
+ if (learn->fin_idle_timeout || learn->fin_hard_timeout) {
+ struct nx_action_fin_timeout *naft;
+
+ naft = ofputil_put_NXAST_FIN_TIMEOUT(&actions);
+ naft->fin_idle_timeout = learn->fin_idle_timeout;
+ naft->fin_hard_timeout = learn->fin_hard_timeout;
+ }
+
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;
+ union mf_subvalue value;
- struct nx_action_reg_load *load;
- ovs_be32 dst_field;
- int dst_ofs;
+ struct mf_subfield dst;
+ int chunk, 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));
+ 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);
+ mf_read_subfield(&src, flow, &value);
} else {
- value = get_bits(n_bits, &p);
+ int p_bytes = 2 * DIV_ROUND_UP(n_bits, 16);
+
+ memset(&value, 0, sizeof value);
+ bitwise_copy(p, p_bytes, 0,
+ &value, sizeof value, 0,
+ n_bits);
+ p = (const uint8_t *) p + p_bytes;
}
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_write_subfield(&dst, &value, &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);
+ get_subfield(n_bits, &p, &dst);
+ for (ofs = 0; ofs < n_bits; ofs += chunk) {
+ struct nx_action_reg_load *load;
+
+ chunk = MIN(n_bits - ofs, 64);
+
+ load = ofputil_put_NXAST_REG_LOAD(&actions);
+ load->ofs_nbits = nxm_encode_ofs_nbits(dst.ofs + ofs, chunk);
+ load->dst = htonl(dst.field->nxm_header);
+ bitwise_copy(&value, sizeof value, ofs,
+ &load->value, sizeof load->value, 0,
+ chunk);
+ }
break;
case NX_LEARN_DST_OUTPUT:
- ofputil_put_OFPAT_OUTPUT(&actions)->port = htons(value);
+ if (n_bits <= 16 || is_all_zeros(value.u8, sizeof value - 2)) {
+ ofputil_put_OFPAT10_OUTPUT(&actions)->port = value.be16[7];
+ }
break;
}
}
int n_bits;
int src_type;
- const struct mf_field *src;
- int src_ofs;
- uint8_t src_imm[sizeof(union mf_value)];
+ struct mf_subfield src;
+ union mf_subvalue src_imm;
int dst_type;
- const struct mf_field *dst;
- int dst_ofs;
+ 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)
{
+ memset(spec, 0, sizeof *spec);
if (mf_from_name(name)) {
const struct mf_field *dst = mf_from_name(name);
union mf_value imm;
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);
+ 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 = 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;
- 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 = mf_from_nxm_header(ntohl(load.dst));
- spec->dst_ofs = nxm_decode_ofs(load.ofs_nbits);
+ learn_parse_load_immediate(value, spec);
} else {
struct nx_action_reg_move move;
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);
}
}
+/* Parses 'arg' as a set of arguments to the "learn" action and appends a
+ * matching NXAST_LEARN action to 'b'. The format parsed is described in
+ * ovs-ofctl(8).
+ *
+ * Prints an error on stderr and aborts the program if 'arg' syntax is invalid.
+ *
+ * If 'flow' is nonnull, then it should be the flow from a cls_rule that is
+ * the matching rule for the learning action. This helps to better validate
+ * the action's arguments.
+ *
+ * Modifies 'arg'. */
void
learn_parse(struct ofpbuf *b, char *arg, const struct flow *flow)
{
learn->idle_timeout = htons(atoi(value));
} else if (!strcmp(name, "hard_timeout")) {
learn->hard_timeout = htons(atoi(value));
+ } else if (!strcmp(name, "fin_idle_timeout")) {
+ learn->fin_idle_timeout = htons(atoi(value));
+ } else if (!strcmp(name, "fin_hard_timeout")) {
+ learn->fin_hard_timeout = htons(atoi(value));
} else if (!strcmp(name, "cookie")) {
learn->cookie = htonll(strtoull(value, NULL, 0));
} else {
/* Check prerequisites. */
if (spec.src_type == NX_LEARN_SRC_FIELD
- && !mf_are_prereqs_ok(spec.src, flow)) {
+ && 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) {
- union mf_value imm;
-
- memcpy(&imm, spec.src_imm, spec.dst->n_bytes);
- mf_set_value(spec.dst, &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->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);
}
learn->len = htons(b->size - learn_ofs);
/* 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));
+ if (flow) {
+ error = learn_check(learn, flow);
+ if (error) {
+ ovs_fatal(0, "%s: %s", orig, ofperr_to_string(error));
+ }
}
free(orig);
}
if (learn->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
ds_put_format(s, ",hard_timeout=%"PRIu16, ntohs(learn->hard_timeout));
}
+ if (learn->fin_idle_timeout) {
+ ds_put_format(s, ",fin_idle_timeout=%"PRIu16,
+ ntohs(learn->fin_idle_timeout));
+ }
+ if (learn->fin_hard_timeout) {
+ ds_put_format(s, ",fin_hard_timeout=%"PRIu16,
+ ntohs(learn->fin_hard_timeout));
+ }
if (learn->priority != htons(OFP_DEFAULT_PRIORITY)) {
ds_put_format(s, ",priority=%"PRIu16, ntohs(learn->priority));
}
if (learn->cookie != htonll(0)) {
ds_put_format(s, ",cookie=0x%"PRIx64, ntohll(learn->cookie));
}
- if (!is_all_zeros(learn->pad, sizeof learn->pad)) {
+ if (learn->pad != 0) {
ds_put_cstr(s, ",***nonzero pad***");
}
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;
/* 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);
/* 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]);
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;
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;
}
}