/*
- * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
+ * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
{
size_t ofs;
- ofpact_pad(ofpacts);
ofs = ofpacts->size;
ofpbuf_pull(ofpacts, ofs);
}
bundle = ofpacts->header;
- ofpact_update_len(ofpacts, &bundle->ofpact);
+ ofpact_finish(ofpacts, &bundle->ofpact);
if (!error) {
error = bundle_check(bundle, OFPP_MAX, NULL);
ids->n_controllers = 1;
ofpbuf_put(out, &id, sizeof id);
ids = out->header;
- ofpact_update_len(out, &ids->ofpact);
+ ofpact_finish(out, &ids->ofpact);
return error;
}
ids = out->header;
}
- ofpact_update_len(out, &ids->ofpact);
+ ofpact_finish(out, &ids->ofpact);
return 0;
}
ofpbuf_put(ofpacts, &id, sizeof id);
ids = ofpacts->header;
ids->n_controllers++;
- ofpact_update_len(ofpacts, &ids->ofpact);
+ ofpact_finish(ofpacts, &ids->ofpact);
}
static char * OVS_WARN_UNUSED_RESULT
return xstrdup("dec_ttl_cnt_ids: expected at least one controller "
"id.");
}
- ofpact_update_len(ofpacts, &ids->ofpact);
+ ofpact_finish(ofpacts, &ids->ofpact);
}
return NULL;
}
get_subfield(spec->n_bits, &p, &spec->dst);
}
}
- ofpact_update_len(ofpacts, &learn->ofpact);
+ ofpact_finish(ofpacts, &learn->ofpact);
if (!is_all_zeros(p, (char *) end - (char *) p)) {
return OFPERR_OFPBAC_BAD_ARGUMENT;
unsigned int length;
length = ntohs(nan->len) - offsetof(struct nx_action_note, note);
- note = ofpact_put(out, OFPACT_NOTE,
- offsetof(struct ofpact_note, data) + length);
+ note = ofpact_put_NOTE(out);
note->length = length;
- memcpy(note->data, nan->note, length);
+ ofpbuf_put(out, nan->note, length);
+ ofpact_finish(out, out->header);
return 0;
}
parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
enum ofputil_protocol *usable_protocols OVS_UNUSED)
{
- struct ofpact_note *note;
-
- note = ofpact_put_NOTE(ofpacts);
- while (*arg != '\0') {
- uint8_t byte;
- bool ok;
-
- if (*arg == '.') {
- arg++;
- }
- if (*arg == '\0') {
- break;
- }
-
- byte = hexits_value(arg, 2, &ok);
- if (!ok) {
- return xstrdup("bad hex digit in `note' argument");
- }
- ofpbuf_put(ofpacts, &byte, 1);
-
- note = ofpacts->header;
- note->length++;
-
- arg += 2;
+ size_t start_ofs = ofpacts->size;
+ ofpact_put_NOTE(ofpacts);
+ arg = ofpbuf_put_hex(ofpacts, arg, NULL);
+ if (arg[0]) {
+ return xstrdup("bad hex digit in `note' argument");
}
- ofpact_update_len(ofpacts, ¬e->ofpact);
+ struct ofpact_note *note = ofpbuf_at_assert(ofpacts, start_ofs,
+ sizeof *note);
+ note->length = ofpacts->size - (start_ofs + sizeof *note);
+ ofpact_finish(ofpacts, ¬e->ofpact);
return NULL;
}
}
static void
-format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a OVS_UNUSED,
- struct ds *s)
+format_UNROLL_XLATE(const struct ofpact_unroll_xlate *a, struct ds *s)
{
- ds_put_cstr(s, "unroll_xlate");
+ ds_put_format(s, "unroll_xlate(table=%"PRIu8", cookie=%"PRIu64")",
+ a->rule_table_id, ntohll(a->rule_cookie));
}
\f
/* Action structure for NXAST_SAMPLE.
conntrack = ofpbuf_push_uninit(out, sizeof(*conntrack));
out->header = &conntrack->ofpact;
- ofpact_update_len(out, &conntrack->ofpact);
+ ofpact_finish(out, &conntrack->ofpact);
if (conntrack->ofpact.len > sizeof(*conntrack)
&& !(conntrack->flags & NX_CT_F_COMMIT)) {
const size_t nat_offset = ofpacts_pull(ofpacts);
error = parse_NAT(value, ofpacts, usable_protocols);
- ofpact_pad(ofpacts);
/* Update CT action pointer and length. */
ofpacts->header = ofpbuf_push_uninit(ofpacts, nat_offset);
oc = ofpacts->header;
error = ofpacts_parse_copy(value, ofpacts, &usable_protocols2,
false, OFPACT_CT);
*usable_protocols &= usable_protocols2;
- ofpact_pad(ofpacts);
ofpacts->header = ofpbuf_push_uninit(ofpacts, exec_offset);
oc = ofpacts->header;
} else {
}
}
- ofpact_update_len(ofpacts, &oc->ofpact);
+ ofpact_finish(ofpacts, &oc->ofpact);
ofpbuf_push_uninit(ofpacts, ct_offset);
return error;
}
return error;
}
}
-
- ofpact_pad(ofpacts);
return 0;
}
enum ofpact_type outer_action)
{
const struct ofp_action_header *actions;
+ size_t orig_size = ofpacts->size;
enum ofperr error;
- if (!outer_action) {
- ofpbuf_clear(ofpacts);
- }
-
if (actions_len % OFP_ACTION_ALIGN != 0) {
VLOG_WARN_RL(&rl, "OpenFlow message actions length %u is not a "
"multiple of %d", actions_len, OFP_ACTION_ALIGN);
error = ofpacts_decode(actions, actions_len, version, ofpacts);
if (error) {
- ofpbuf_clear(ofpacts);
+ ofpacts->size = orig_size;
return error;
}
error = ofpacts_verify(ofpacts->data, ofpacts->size, allowed_ovsinsts,
outer_action);
if (error) {
- ofpbuf_clear(ofpacts);
+ ofpacts->size = orig_size;
}
return error;
}
-/* Attempts to convert 'actions_len' bytes of OpenFlow actions from the
- * front of 'openflow' into ofpacts. On success, replaces any existing content
- * in 'ofpacts' by the converted ofpacts; on failure, clears 'ofpacts'.
+/* Attempts to convert 'actions_len' bytes of OpenFlow actions from the front
+ * of 'openflow' into ofpacts. On success, appends the converted actions to
+ * 'ofpacts'; on failure, 'ofpacts' is unchanged (but might be reallocated) .
* Returns 0 if successful, otherwise an OpenFlow error.
*
* Actions are processed according to their OpenFlow version which
const struct ofp11_instruction *insts[N_OVS_INSTRUCTIONS];
enum ofperr error;
+ ofpbuf_clear(ofpacts);
if (version == OFP10_VERSION) {
return ofpacts_pull_openflow_actions__(openflow, instructions_len,
version,
ofpacts, 0);
}
- ofpbuf_clear(ofpacts);
-
if (instructions_len % OFP11_INSTRUCTION_ALIGN != 0) {
VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u is not a "
"multiple of %d",
struct ofpact_nest *on;
const struct ofp_action_header *actions;
size_t actions_len;
- size_t start;
-
- ofpact_pad(ofpacts);
- start = ofpacts->size;
+ size_t start = ofpacts->size;
ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
offsetof(struct ofpact_nest, actions));
get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
: 0);
}
-static const struct mf_field *
-ofpact_get_mf_field(enum ofpact_type type, const void *ofpact)
+/* Returns the destination field that 'ofpact' would write to, or NULL
+ * if the action would not write to an mf_field. */
+const struct mf_field *
+ofpact_get_mf_dst(const struct ofpact *ofpact)
{
- if (type == OFPACT_SET_FIELD) {
- const struct ofpact_set_field *orl = ofpact;
+ if (ofpact->type == OFPACT_SET_FIELD) {
+ const struct ofpact_set_field *orl;
+ orl = CONTAINER_OF(ofpact, struct ofpact_set_field, ofpact);
return orl->field;
- } else if (type == OFPACT_REG_MOVE) {
- const struct ofpact_reg_move *orm = ofpact;
+ } else if (ofpact->type == OFPACT_REG_MOVE) {
+ const struct ofpact_reg_move *orm;
+ orm = CONTAINER_OF(ofpact, struct ofpact_reg_move, ofpact);
return orm->dst.field;
}
static enum ofperr
ofpacts_verify_nested(const struct ofpact *a, enum ofpact_type outer_action)
{
- const struct mf_field *field = ofpact_get_mf_field(a->type, a);
+ const struct mf_field *field = ofpact_get_mf_dst(a);
if (field && field_requires_ct(field->id) && outer_action != OFPACT_CT) {
VLOG_WARN("cannot set CT fields outside of ct action");
{
struct ofpact *ofpact;
- ofpact_pad(ofpacts);
ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
ofpact = ofpacts->header;
ofpact_init(ofpact, type, len);
ofpact->len = len;
}
\f
-/* Updates 'ofpact->len' to the number of bytes in the tail of 'ofpacts'
- * starting at 'ofpact'.
- *
- * This is the correct way to update a variable-length ofpact's length after
- * adding the variable-length part of the payload. (See the large comment
- * near the end of ofp-actions.h for more information.) */
+/* Finishes composing a variable-length action (begun using
+ * ofpact_put_<NAME>()), by padding the action to a multiple of OFPACT_ALIGNTO
+ * bytes and updating its embedded length field. See the large comment near
+ * the end of ofp-actions.h for more information. */
void
-ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
+ofpact_finish(struct ofpbuf *ofpacts, struct ofpact *ofpact)
{
ovs_assert(ofpact == ofpacts->header);
ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
-}
-
-/* Pads out 'ofpacts' to a multiple of OFPACT_ALIGNTO bytes in length. Each
- * ofpact_put_<ENUM>() calls this function automatically beforehand, but the
- * client must call this itself after adding the final ofpact to an array of
- * them.
- *
- * (The consequences of failing to call this function are probably not dire.
- * OFPACT_FOR_EACH will calculate a pointer beyond the end of the ofpacts, but
- * not dereference it. That's undefined behavior, technically, but it will not
- * cause a real problem on common systems. Still, it seems better to call
- * it.) */
-void
-ofpact_pad(struct ofpbuf *ofpacts)
-{
- unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
- if (pad) {
- ofpbuf_put_zeros(ofpacts, pad);
- }
+ ofpbuf_padto(ofpacts, OFPACT_ALIGN(ofpacts->size));
}
\f
-
-
-
static char * OVS_WARN_UNUSED_RESULT
ofpact_parse(enum ofpact_type type, char *value, struct ofpbuf *ofpacts,
enum ofputil_protocol *usable_protocols)
}
prev_inst = inst;
}
- ofpact_pad(ofpacts);
if (drop && ofpacts->size) {
return xstrdup("\"drop\" must not be accompanied by any other action "