From a948b96315f5744a21ca7a83959ef8e5dc824378 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 14 Oct 2008 12:50:03 -0700 Subject: [PATCH] New routine ofpbuf_put_zeros() to simplify a common code sequence. --- include/ofpbuf.h | 1 + lib/netdev.c | 3 +-- lib/ofpbuf.c | 11 +++++++++++ lib/vconn.c | 12 ++++-------- switch/datapath.c | 19 ++++++------------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/include/ofpbuf.h b/include/ofpbuf.h index a9cb6185..0e94e35b 100644 --- a/include/ofpbuf.h +++ b/include/ofpbuf.h @@ -70,6 +70,7 @@ void *ofpbuf_tail(const struct ofpbuf *); void *ofpbuf_end(const struct ofpbuf *); void *ofpbuf_put_uninit(struct ofpbuf *, size_t); +void *ofpbuf_put_zeros(struct ofpbuf *, size_t); void *ofpbuf_put(struct ofpbuf *, const void *, size_t); void ofpbuf_reserve(struct ofpbuf *, size_t); void *ofpbuf_push_uninit(struct ofpbuf *b, size_t); diff --git a/lib/netdev.c b/lib/netdev.c index 92c1a349..9bedb694 100644 --- a/lib/netdev.c +++ b/lib/netdev.c @@ -507,8 +507,7 @@ static void pad_to_minimum_length(struct ofpbuf *buffer) { if (buffer->size < ETH_TOTAL_MIN) { - size_t shortage = ETH_TOTAL_MIN - buffer->size; - memset(ofpbuf_put_uninit(buffer, shortage), 0, shortage); + ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size); } } diff --git a/lib/ofpbuf.c b/lib/ofpbuf.c index 8c92fb74..77631e1d 100644 --- a/lib/ofpbuf.c +++ b/lib/ofpbuf.c @@ -176,6 +176,17 @@ ofpbuf_put_uninit(struct ofpbuf *b, size_t size) return p; } +/* Appends 'size' zeroed bytes to the tail end of 'b'. Data in 'b' is + * reallocated and copied if necessary. Returns a pointer to the first byte of + * the data's location in the ofpbuf. */ +void * +ofpbuf_put_zeros(struct ofpbuf *b, size_t size) +{ + void *dst = ofpbuf_put_uninit(b, size); + memset(dst, 0, size); + return dst; +} + /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'. Data in 'b' * is reallocated and copied if necessary. Returns a pointer to the first * byte of the data's location in the ofpbuf. */ diff --git a/lib/vconn.c b/lib/vconn.c index 434fa46e..13e096a9 100644 --- a/lib/vconn.c +++ b/lib/vconn.c @@ -739,8 +739,7 @@ make_openflow_xid(size_t openflow_len, uint8_t type, uint32_t xid, assert(openflow_len >= sizeof *oh); assert(openflow_len <= UINT16_MAX); buffer = *bufferp = ofpbuf_new(openflow_len); - oh = ofpbuf_put_uninit(buffer, openflow_len); - memset(oh, 0, openflow_len); + oh = ofpbuf_put_zeros(buffer, openflow_len); oh->version = OFP_VERSION; oh->type = type; oh->length = htons(openflow_len); @@ -764,8 +763,7 @@ make_add_flow(const struct flow *flow, uint32_t buffer_id, struct ofp_flow_mod *ofm; size_t size = sizeof *ofm + actions_len; struct ofpbuf *out = ofpbuf_new(size); - ofm = ofpbuf_put_uninit(out, size); - memset(ofm, 0, size); + ofm = ofpbuf_put_zeros(out, size); ofm->header.version = OFP_VERSION; ofm->header.type = OFPT_FLOW_MOD; ofm->header.length = htons(size); @@ -812,8 +810,7 @@ make_unbuffered_packet_out(const struct ofpbuf *packet, size_t size = sizeof *opo + sizeof *oao; struct ofpbuf *out = ofpbuf_new(size + packet->size); - opo = ofpbuf_put_uninit(out, size); - memset(opo, 0, size); + opo = ofpbuf_put_zeros(out, size); opo->header.version = OFP_VERSION; opo->header.type = OFPT_PACKET_OUT; opo->buffer_id = htonl(UINT32_MAX); @@ -839,8 +836,7 @@ make_buffered_packet_out(uint32_t buffer_id, struct ofp_action_output *oao; size_t size = sizeof *opo + sizeof *oao; struct ofpbuf *out = ofpbuf_new(size); - opo = ofpbuf_put_uninit(out, size); - memset(opo, 0, size); + opo = ofpbuf_put_zeros(out, size); opo->header.version = OFP_VERSION; opo->header.type = OFPT_PACKET_OUT; opo->header.length = htons(size); diff --git a/switch/datapath.c b/switch/datapath.c index 98c8efcf..fb61d0ae 100644 --- a/switch/datapath.c +++ b/switch/datapath.c @@ -674,8 +674,7 @@ dp_send_features_reply(struct datapath *dp, const struct sender *sender) ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES); ofr->actions = htonl(OFP_SUPPORTED_ACTIONS); LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) { - struct ofp_phy_port *opp = ofpbuf_put_uninit(buffer, sizeof *opp); - memset(opp, 0, sizeof *opp); + struct ofp_phy_port *opp = ofpbuf_put_zeros(buffer, sizeof *opp); fill_port_desc(dp, p, opp); } send_openflow_buffer(dp, buffer, sender); @@ -805,10 +804,9 @@ fill_flow_stats(struct ofpbuf *buffer, struct sw_flow *flow, { struct ofp_flow_stats *ofs; int length = sizeof *ofs + flow->sf_acts->actions_len; - ofs = ofpbuf_put_uninit(buffer, length); + ofs = ofpbuf_put_zeros(buffer, length); ofs->length = htons(length); ofs->table_id = table_idx; - ofs->pad = 0; ofs->match.wildcards = htonl(flow->key.wildcards); ofs->match.in_port = flow->key.flow.in_port; memcpy(ofs->match.dl_src, flow->key.flow.dl_src, ETH_ADDR_LEN); @@ -818,14 +816,12 @@ fill_flow_stats(struct ofpbuf *buffer, struct sw_flow *flow, ofs->match.nw_src = flow->key.flow.nw_src; ofs->match.nw_dst = flow->key.flow.nw_dst; ofs->match.nw_proto = flow->key.flow.nw_proto; - ofs->match.pad = 0; ofs->match.tp_src = flow->key.flow.tp_src; ofs->match.tp_dst = flow->key.flow.tp_dst; ofs->duration = htonl(now - flow->created); ofs->priority = htons(flow->priority); ofs->idle_timeout = htons(flow->idle_timeout); ofs->hard_timeout = htons(flow->hard_timeout); - memset(ofs->pad2, 0, sizeof ofs->pad2); ofs->packet_count = htonll(flow->packet_count); ofs->byte_count = htonll(flow->byte_count); memcpy(ofs->actions, flow->sf_acts->actions, flow->sf_acts->actions_len); @@ -1117,7 +1113,7 @@ recv_flow(struct datapath *dp, const struct sender *sender, static int desc_stats_dump(struct datapath *dp, void *state, struct ofpbuf *buffer) { - struct ofp_desc_stats *ods = ofpbuf_put_uninit(buffer, sizeof *ods); + struct ofp_desc_stats *ods = ofpbuf_put_zeros(buffer, sizeof *ods); strncpy(ods->mfr_desc, &mfr_desc, sizeof ods->mfr_desc); strncpy(ods->hw_desc, &hw_desc, sizeof ods->hw_desc); @@ -1220,8 +1216,7 @@ static int aggregate_stats_dump(struct datapath *dp, void *state, struct sw_flow_key match_key; int table_idx; - rpy = ofpbuf_put_uninit(buffer, sizeof *rpy); - memset(rpy, 0, sizeof *rpy); + rpy = ofpbuf_put_zeros(buffer, sizeof *rpy); flow_extract_match(&match_key, &rq->match); table_idx = rq->table_id == 0xff ? 0 : rq->table_id; @@ -1257,13 +1252,12 @@ static int table_stats_dump(struct datapath *dp, void *state, { int i; for (i = 0; i < dp->chain->n_tables; i++) { - struct ofp_table_stats *ots = ofpbuf_put_uninit(buffer, sizeof *ots); + struct ofp_table_stats *ots = ofpbuf_put_zeros(buffer, sizeof *ots); struct sw_table_stats stats; dp->chain->tables[i]->stats(dp->chain->tables[i], &stats); strncpy(ots->name, stats.name, sizeof ots->name); ots->table_id = i; ots->wildcards = htonl(stats.wildcards); - memset(ots->pad, 0, sizeof ots->pad); ots->max_entries = htonl(stats.max_flows); ots->active_count = htonl(stats.n_flows); ots->lookup_count = htonll(stats.n_lookup); @@ -1297,9 +1291,8 @@ static int port_stats_dump(struct datapath *dp, void *state, if (!p->netdev) { continue; } - ops = ofpbuf_put_uninit(buffer, sizeof *ops); + ops = ofpbuf_put_zeros(buffer, sizeof *ops); ops->port_no = htons(port_no(dp, p)); - memset(ops->pad, 0, sizeof ops->pad); ops->rx_packets = htonll(p->rx_packets); ops->tx_packets = htonll(p->tx_packets); ops->rx_bytes = htonll(p->rx_bytes); -- 2.30.2