2
0
mirror of https://github.com/openvswitch/ovs synced 2025-09-01 06:45:17 +00:00

ofpbuf: Simplify ofpbuf API.

ofpbuf was complicated due to its wide usage across all
layers of OVS, Now we have introduced independent dp_packet
which can be used for datapath packet, we can simplify ofpbuf.
Following patch removes DPDK mbuf and access API of ofpbuf
members.

Signed-off-by: Pravin B Shelar <pshelar@nicira.com>
Acked-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
This commit is contained in:
Pravin B Shelar
2015-03-02 17:29:44 -08:00
parent cf62fa4c70
commit 6fd6ed71cb
42 changed files with 705 additions and 1058 deletions

View File

@@ -781,7 +781,7 @@ decode_NXAST_RAW_OUTPUT_REG2(const struct nx_action_output_reg2 *naor,
if (error) {
return error;
}
if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
if (!is_all_zeros(b.data, b.size)) {
return OFPERR_NXBRC_MUST_BE_ZERO;
}
@@ -799,15 +799,15 @@ encode_OUTPUT_REG(const struct ofpact_output_reg *output_reg,
if (output_reg->ofpact.raw == NXAST_RAW_OUTPUT_REG2
|| !mf_nxm_header(output_reg->src.field->id)) {
struct nx_action_output_reg2 *naor = put_NXAST_OUTPUT_REG2(out);
size_t size = ofpbuf_size(out);
size_t size = out->size;
naor->ofs_nbits = nxm_encode_ofs_nbits(output_reg->src.ofs,
output_reg->src.n_bits);
naor->max_len = htons(output_reg->max_len);
ofpbuf_set_size(out, size - sizeof naor->pad);
out->size = size - sizeof naor->pad;
nx_put_header(out, output_reg->src.field->id, 0, false);
ofpbuf_set_size(out, size);
out->size = size;
} else {
struct nx_action_output_reg *naor = put_NXAST_OUTPUT_REG(out);
@@ -969,7 +969,7 @@ decode_bundle(bool load, const struct nx_action_bundle *nab,
ofpbuf_put(ofpacts, &ofp_port, sizeof ofp_port);
}
bundle = ofpacts->frame;
bundle = ofpacts->header;
ofpact_update_len(ofpacts, &bundle->ofpact);
if (!error) {
@@ -1879,7 +1879,7 @@ decode_copy_field__(ovs_be16 src_offset, ovs_be16 dst_offset, ovs_be16 n_bits,
return error;
}
if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
if (!is_all_zeros(b.data, b.size)) {
return OFPERR_NXBRC_MUST_BE_ZERO;
}
@@ -1929,7 +1929,7 @@ decode_NXAST_RAW_REG_MOVE(const struct nx_action_reg_move *narm,
if (error) {
return error;
}
if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
if (!is_all_zeros(b.data, b.size)) {
return OFPERR_NXBRC_MUST_BE_ZERO;
}
@@ -1946,13 +1946,13 @@ encode_REG_MOVE(const struct ofpact_reg_move *move,
* probe for support. Until we have that ability, we currently prefer
* NXAST_RAW_REG_MOVE for backward compatibility with older Open vSwitch
* versions. */
size_t start_ofs = ofpbuf_size(out);
size_t start_ofs = out->size;
if (ofp_version >= OFP15_VERSION) {
struct ofp15_action_copy_field *copy = put_OFPAT15_COPY_FIELD(out);
copy->n_bits = htons(move->dst.n_bits);
copy->src_offset = htons(move->src.ofs);
copy->dst_offset = htons(move->dst.ofs);
ofpbuf_set_size(out, ofpbuf_size(out) - sizeof copy->pad2);
out->size = out->size - sizeof copy->pad2;
nx_put_header(out, move->src.field->id, ofp_version, false);
nx_put_header(out, move->dst.field->id, ofp_version, false);
} else if (ofp_version == OFP13_VERSION
@@ -1961,7 +1961,7 @@ encode_REG_MOVE(const struct ofpact_reg_move *move,
copy->n_bits = htons(move->dst.n_bits);
copy->src_offset = htons(move->src.ofs);
copy->dst_offset = htons(move->dst.ofs);
ofpbuf_set_size(out, ofpbuf_size(out) - sizeof copy->pad3);
out->size = out->size - sizeof copy->pad3;
nx_put_header(out, move->src.field->id, ofp_version, false);
nx_put_header(out, move->dst.field->id, ofp_version, false);
} else {
@@ -2104,7 +2104,7 @@ decode_ofpat_set_field(const struct ofp12_action_set_field *oasf,
memset(&sf->mask, 0xff, sf->field->n_bytes);
}
if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
if (!is_all_zeros(b.data, b.size)) {
return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
}
@@ -2205,7 +2205,7 @@ decode_NXAST_RAW_REG_LOAD2(const struct nx_action_reg_load2 *narl,
if (error) {
return error;
}
if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
if (!is_all_zeros(b.data, b.size)) {
return OFPERR_OFPBAC_BAD_SET_ARGUMENT;
}
@@ -2223,13 +2223,13 @@ ofpact_put_set_field(struct ofpbuf *openflow, enum ofp_version ofp_version,
{
struct ofp12_action_set_field *oasf OVS_UNUSED;
int n_bytes = mf_from_id(field)->n_bytes;
size_t start_ofs = ofpbuf_size(openflow);
size_t start_ofs = openflow->size;
union mf_value value;
value.be64 = htonll(value_ << (8 * (8 - n_bytes)));
oasf = put_OFPAT12_SET_FIELD(openflow);
ofpbuf_set_size(openflow, ofpbuf_size(openflow) - sizeof oasf->pad);
openflow->size = openflow->size - sizeof oasf->pad;
nx_put_entry(openflow, field, ofp_version, &value, NULL);
pad_ofpat(openflow, start_ofs);
}
@@ -2266,10 +2266,10 @@ set_field_to_nxast(const struct ofpact_set_field *sf, struct ofpbuf *openflow)
if (sf->ofpact.raw == NXAST_RAW_REG_LOAD2
|| !mf_nxm_header(sf->field->id)) {
struct nx_action_reg_load2 *narl OVS_UNUSED;
size_t start_ofs = ofpbuf_size(openflow);
size_t start_ofs = openflow->size;
narl = put_NXAST_REG_LOAD2(openflow);
ofpbuf_set_size(openflow, ofpbuf_size(openflow) - sizeof narl->pad);
openflow->size = openflow->size - sizeof narl->pad;
nx_put_entry(openflow, sf->field->id, 0, &sf->value, &sf->mask);
pad_ofpat(openflow, start_ofs);
} else {
@@ -2412,10 +2412,10 @@ set_field_to_set_field(const struct ofpact_set_field *sf,
enum ofp_version ofp_version, struct ofpbuf *out)
{
struct ofp12_action_set_field *oasf OVS_UNUSED;
size_t start_ofs = ofpbuf_size(out);
size_t start_ofs = out->size;
oasf = put_OFPAT12_SET_FIELD(out);
ofpbuf_set_size(out, ofpbuf_size(out) - sizeof oasf->pad);
out->size = out->size - sizeof oasf->pad;
nx_put_entry(out, sf->field->id, ofp_version, &sf->value, &sf->mask);
pad_ofpat(out, start_ofs);
}
@@ -2609,9 +2609,9 @@ decode_stack_action(const struct nx_action_stack *nasp,
if (error) {
return error;
}
stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) ofpbuf_data(&b));
stack_action->subfield.n_bits = ntohs(*(const ovs_be16 *) b.data);
ofpbuf_pull(&b, 2);
if (!is_all_zeros(ofpbuf_data(&b), ofpbuf_size(&b))) {
if (!is_all_zeros(b.data, b.size)) {
return OFPERR_NXBRC_MUST_BE_ZERO;
}
@@ -2729,7 +2729,7 @@ decode_OFPAT_RAW_DEC_NW_TTL(struct ofpbuf *out)
ids = ofpact_put_DEC_TTL(out);
ids->n_controllers = 1;
ofpbuf_put(out, &id, sizeof id);
ids = out->frame;
ids = out->header;
ofpact_update_len(out, &ids->ofpact);
return error;
}
@@ -2763,7 +2763,7 @@ decode_NXAST_RAW_DEC_TTL_CNT_IDS(const struct nx_action_cnt_ids *nac_ids,
for (i = 0; i < ids->n_controllers; i++) {
uint16_t id = ntohs(((ovs_be16 *)(nac_ids + 1))[i]);
ofpbuf_put(out, &id, sizeof id);
ids = out->frame;
ids = out->header;
}
ofpact_update_len(out, &ids->ofpact);
@@ -2803,7 +2803,7 @@ parse_noargs_dec_ttl(struct ofpbuf *ofpacts)
ofpact_put_DEC_TTL(ofpacts);
ofpbuf_put(ofpacts, &id, sizeof id);
ids = ofpacts->frame;
ids = ofpacts->header;
ids->n_controllers++;
ofpact_update_len(ofpacts, &ids->ofpact);
}
@@ -2825,7 +2825,7 @@ parse_DEC_TTL(char *arg, struct ofpbuf *ofpacts,
uint16_t id = atoi(cntr);
ofpbuf_put(ofpacts, &id, sizeof id);
ids = ofpacts->frame;
ids = ofpacts->header;
ids->n_controllers++;
}
if (!ids->n_controllers) {
@@ -3772,7 +3772,7 @@ decode_NXAST_RAW_LEARN(const struct nx_action_learn *nal,
}
spec = ofpbuf_put_zeros(ofpacts, sizeof *spec);
learn = ofpacts->frame;
learn = ofpacts->header;
learn->n_specs++;
spec->src_type = header & NX_LEARN_SRC_MASK;
@@ -3853,7 +3853,7 @@ encode_LEARN(const struct ofpact_learn *learn,
struct nx_action_learn *nal;
size_t start_ofs;
start_ofs = ofpbuf_size(out);
start_ofs = out->size;
nal = put_NXAST_LEARN(out);
nal->idle_timeout = htons(learn->idle_timeout);
nal->hard_timeout = htons(learn->hard_timeout);
@@ -4141,23 +4141,23 @@ static void
encode_NOTE(const struct ofpact_note *note,
enum ofp_version ofp_version OVS_UNUSED, struct ofpbuf *out)
{
size_t start_ofs = ofpbuf_size(out);
size_t start_ofs = out->size;
struct nx_action_note *nan;
unsigned int remainder;
unsigned int len;
put_NXAST_NOTE(out);
ofpbuf_set_size(out, ofpbuf_size(out) - sizeof nan->note);
out->size = out->size - sizeof nan->note;
ofpbuf_put(out, note->data, note->length);
len = ofpbuf_size(out) - start_ofs;
len = out->size - start_ofs;
remainder = len % OFP_ACTION_ALIGN;
if (remainder) {
ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
}
nan = ofpbuf_at(out, start_ofs, sizeof *nan);
nan->len = htons(ofpbuf_size(out) - start_ofs);
nan->len = htons(out->size - start_ofs);
}
static char * OVS_WARN_UNUSED_RESULT
@@ -4184,7 +4184,7 @@ parse_NOTE(const char *arg, struct ofpbuf *ofpacts,
}
ofpbuf_put(ofpacts, &byte, 1);
note = ofpacts->frame;
note = ofpacts->header;
note->length++;
arg += 2;
@@ -4400,7 +4400,7 @@ encode_WRITE_ACTIONS(const struct ofpact_nest *actions,
enum ofp_version ofp_version, struct ofpbuf *out)
{
if (ofp_version > OFP10_VERSION) {
const size_t ofs = ofpbuf_size(out);
const size_t ofs = out->size;
instruction_put_OFPIT11_WRITE_ACTIONS(out);
ofpacts_put_openflow_actions(actions->actions,
@@ -4420,7 +4420,7 @@ parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
/* Pull off existing actions or instructions. */
ofpact_pad(ofpacts);
ofs = ofpbuf_size(ofpacts);
ofs = ofpacts->size;
ofpbuf_pull(ofpacts, ofs);
/* Add a Write-Actions instruction and then pull it off. */
@@ -4438,7 +4438,7 @@ parse_WRITE_ACTIONS(char *arg, struct ofpbuf *ofpacts,
/* Put the Write-Actions back on and update its length. */
on = ofpbuf_push_uninit(ofpacts, sizeof *on);
on->ofpact.len = ofpbuf_size(ofpacts);
on->ofpact.len = ofpacts->size;
/* Put any previous actions or instructions back on. */
ofpbuf_push_uninit(ofpacts, ofs);
@@ -4600,8 +4600,8 @@ ofpacts_decode(const void *actions, size_t actions_len,
struct ofpbuf openflow;
ofpbuf_use_const(&openflow, actions, actions_len);
while (ofpbuf_size(&openflow)) {
const struct ofp_action_header *action = ofpbuf_data(&openflow);
while (openflow.size) {
const struct ofp_action_header *action = openflow.data;
enum ofp_raw_action_type raw;
enum ofperr error;
uint64_t arg;
@@ -4643,7 +4643,7 @@ ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
if (actions == NULL) {
VLOG_WARN_RL(&rl, "OpenFlow message actions length %u exceeds "
"remaining message length (%"PRIu32")",
actions_len, ofpbuf_size(openflow));
actions_len, openflow->size);
return OFPERR_OFPBRC_BAD_LEN;
}
@@ -4653,7 +4653,7 @@ ofpacts_pull_openflow_actions__(struct ofpbuf *openflow,
return error;
}
error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
error = ofpacts_verify(ofpacts->data, ofpacts->size,
allowed_ovsinsts);
if (error) {
ofpbuf_clear(ofpacts);
@@ -4836,7 +4836,7 @@ ofpacts_copy_last(struct ofpbuf *out, const struct ofpbuf *in,
const struct ofpact *a;
target = NULL;
OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
OFPACT_FOR_EACH (a, in->data, in->size) {
if (a->type == filter) {
target = a;
}
@@ -4855,7 +4855,7 @@ ofpacts_copy_all(struct ofpbuf *out, const struct ofpbuf *in,
{
const struct ofpact *a;
OFPACT_FOR_EACH (a, ofpbuf_data(in), ofpbuf_size(in)) {
OFPACT_FOR_EACH (a, in->data, in->size) {
if (filter(a)) {
ofpact_copy(out, a);
}
@@ -4914,7 +4914,7 @@ ofpacts_decode_for_action_set(const struct ofp_action_header *in,
{
enum ofperr error;
struct ofpact *a;
size_t start = ofpbuf_size(out);
size_t start = out->size;
error = ofpacts_decode(in, n_in, version, out);
@@ -4922,7 +4922,7 @@ ofpacts_decode_for_action_set(const struct ofp_action_header *in,
return error;
}
OFPACT_FOR_EACH (a, ofpact_end(ofpbuf_data(out), start), ofpbuf_size(out) - start) {
OFPACT_FOR_EACH (a, ofpact_end(out->data, start), out->size - start) {
if (!ofpact_is_allowed_in_actions_set(a)) {
VLOG_WARN_RL(&rl, "disallowed action in action set");
return OFPERR_OFPBAC_BAD_TYPE;
@@ -5234,7 +5234,7 @@ ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
if (instructions == NULL) {
VLOG_WARN_RL(&rl, "OpenFlow message instructions length %u exceeds "
"remaining message length (%"PRIu32")",
instructions_len, ofpbuf_size(openflow));
instructions_len, openflow->size);
error = OFPERR_OFPBIC_BAD_LEN;
goto exit;
}
@@ -5279,7 +5279,7 @@ ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
size_t start;
ofpact_pad(ofpacts);
start = ofpbuf_size(ofpacts);
start = ofpacts->size;
on = ofpact_put(ofpacts, OFPACT_WRITE_ACTIONS,
offsetof(struct ofpact_nest, actions));
get_actions_from_instruction(insts[OVSINST_OFPIT11_WRITE_ACTIONS],
@@ -5290,7 +5290,7 @@ ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
goto exit;
}
on = ofpbuf_at_assert(ofpacts, start, sizeof *on);
on->ofpact.len = ofpbuf_size(ofpacts) - start;
on->ofpact.len = ofpacts->size - start;
}
if (insts[OVSINST_OFPIT11_WRITE_METADATA]) {
const struct ofp11_instruction_write_metadata *oiwm;
@@ -5313,7 +5313,7 @@ ofpacts_pull_openflow_instructions(struct ofpbuf *openflow,
ogt->table_id = oigt->table_id;
}
error = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
error = ofpacts_verify(ofpacts->data, ofpacts->size,
(1u << N_OVS_INSTRUCTIONS) - 1);
exit:
if (error) {
@@ -5331,10 +5331,10 @@ ofpacts_update_instruction_actions(struct ofpbuf *openflow, size_t ofs)
struct ofp11_instruction_actions *oia;
oia = ofpbuf_at_assert(openflow, ofs, sizeof *oia);
if (ofpbuf_size(openflow) > ofs + sizeof *oia) {
oia->len = htons(ofpbuf_size(openflow) - ofs);
if (openflow->size > ofs + sizeof *oia) {
oia->len = htons(openflow->size - ofs);
} else {
ofpbuf_set_size(openflow, ofs);
openflow->size = ofs;
}
}
@@ -5754,12 +5754,12 @@ ofpacts_put_openflow_actions(const struct ofpact ofpacts[], size_t ofpacts_len,
enum ofp_version ofp_version)
{
const struct ofpact *a;
size_t start_size = ofpbuf_size(openflow);
size_t start_size = openflow->size;
OFPACT_FOR_EACH (a, ofpacts, ofpacts_len) {
encode_ofpact(a, ofp_version, openflow);
}
return ofpbuf_size(openflow) - start_size;
return openflow->size - start_size;
}
static enum ovs_instruction_type
@@ -5787,7 +5787,7 @@ ofpacts_put_openflow_instructions(const struct ofpact ofpacts[],
a = ofpacts;
while (a < end) {
if (ofpact_is_apply_actions(a)) {
size_t ofs = ofpbuf_size(openflow);
size_t ofs = openflow->size;
instruction_put_OFPIT11_APPLY_ACTIONS(openflow);
do {
@@ -6124,7 +6124,8 @@ ofpact_put(struct ofpbuf *ofpacts, enum ofpact_type type, size_t len)
struct ofpact *ofpact;
ofpact_pad(ofpacts);
ofpact = ofpacts->frame = ofpbuf_put_uninit(ofpacts, len);
ofpacts->header = ofpbuf_put_uninit(ofpacts, len);
ofpact = ofpacts->header;
ofpact_init(ofpact, type, len);
return ofpact;
}
@@ -6147,7 +6148,7 @@ ofpact_init(struct ofpact *ofpact, enum ofpact_type type, size_t len)
void
ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
{
ovs_assert(ofpact == ofpacts->frame);
ovs_assert(ofpact == ofpacts->header);
ofpact->len = (char *) ofpbuf_tail(ofpacts) - (char *) ofpact;
}
@@ -6164,7 +6165,7 @@ ofpact_update_len(struct ofpbuf *ofpacts, struct ofpact *ofpact)
void
ofpact_pad(struct ofpbuf *ofpacts)
{
unsigned int pad = PAD_SIZE(ofpbuf_size(ofpacts), OFPACT_ALIGNTO);
unsigned int pad = PAD_SIZE(ofpacts->size, OFPACT_ALIGNTO);
if (pad) {
ofpbuf_put_zeros(ofpacts, pad);
}
@@ -6275,12 +6276,12 @@ ofpacts_parse__(char *str, struct ofpbuf *ofpacts,
}
ofpact_pad(ofpacts);
if (drop && ofpbuf_size(ofpacts)) {
if (drop && ofpacts->size) {
return xstrdup("\"drop\" must not be accompanied by any other action "
"or instruction");
}
retval = ofpacts_verify(ofpbuf_data(ofpacts), ofpbuf_size(ofpacts),
retval = ofpacts_verify(ofpacts->data, ofpacts->size,
(allow_instructions
? (1u << N_OVS_INSTRUCTIONS) - 1
: 1u << OVSINST_OFPIT11_APPLY_ACTIONS));
@@ -6295,11 +6296,11 @@ static char * OVS_WARN_UNUSED_RESULT
ofpacts_parse(char *str, struct ofpbuf *ofpacts,
enum ofputil_protocol *usable_protocols, bool allow_instructions)
{
uint32_t orig_size = ofpbuf_size(ofpacts);
uint32_t orig_size = ofpacts->size;
char *error = ofpacts_parse__(str, ofpacts, usable_protocols,
allow_instructions);
if (error) {
ofpbuf_set_size(ofpacts, orig_size);
ofpacts->size = orig_size;
}
return error;
}
@@ -6547,13 +6548,13 @@ static enum ofperr
ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
enum ofp_raw_action_type *raw, uint64_t *arg)
{
const struct ofp_action_header *oah = ofpbuf_data(buf);
const struct ofp_action_header *oah = buf->data;
const struct ofpact_raw_instance *action;
unsigned int length;
enum ofperr error;
*raw = *arg = 0;
error = ofpact_decode_raw(ofp_version, oah, ofpbuf_size(buf), &action);
error = ofpact_decode_raw(ofp_version, oah, buf->size, &action);
if (error) {
return error;
}
@@ -6565,9 +6566,9 @@ ofpact_pull_raw(struct ofpbuf *buf, enum ofp_version ofp_version,
}
length = ntohs(oah->len);
if (length > ofpbuf_size(buf)) {
if (length > buf->size) {
VLOG_WARN_RL(&rl, "OpenFlow action %s length %u exceeds action buffer "
"length %"PRIu32, action->name, length, ofpbuf_size(buf));
"length %"PRIu32, action->name, length, buf->size);
return OFPERR_OFPBAC_BAD_LEN;
}
if (length < action->min_length || length > action->max_length) {
@@ -6664,9 +6665,9 @@ pad_ofpat(struct ofpbuf *openflow, size_t start_ofs)
{
struct ofp_action_header *oah;
ofpbuf_put_zeros(openflow, PAD_SIZE(ofpbuf_size(openflow) - start_ofs, 8));
ofpbuf_put_zeros(openflow, PAD_SIZE(openflow->size - start_ofs, 8));
oah = ofpbuf_at_assert(openflow, start_ofs, sizeof *oah);
oah->len = htons(ofpbuf_size(openflow) - start_ofs);
oah->len = htons(openflow->size - start_ofs);
}