[ovs-dev] [PATCH 06/12] ofpbuf: New function ofpbuf_use_const().
Ben Pfaff
blp at nicira.com
Tue Dec 7 19:00:28 UTC 2010
This is a code cleanup.
Suggested-by: Justin Pettit <jpettit at nicira.com>
---
lib/dhcp.c | 3 +--
lib/learning-switch.c | 3 +--
lib/netlink.c | 12 ++++--------
lib/ofp-print.c | 6 ++----
lib/ofpbuf.c | 11 +++++++++++
lib/ofpbuf.h | 1 +
ofproto/ofproto-sflow.c | 3 +--
ofproto/ofproto.c | 17 +++++++----------
8 files changed, 28 insertions(+), 28 deletions(-)
diff --git a/lib/dhcp.c b/lib/dhcp.c
index 0345efe..62ede06 100644
--- a/lib/dhcp.c
+++ b/lib/dhcp.c
@@ -561,8 +561,7 @@ parse_options(struct dhcp_msg *msg, const char *name, void *data, size_t size,
{
struct ofpbuf b;
- b.data = data;
- b.size = size;
+ ofpbuf_use_const(&b, data, size);
for (;;) {
uint8_t *code, *len;
void *payload;
diff --git a/lib/learning-switch.c b/lib/learning-switch.c
index cbd24cb..5f429c1 100644
--- a/lib/learning-switch.c
+++ b/lib/learning-switch.c
@@ -400,8 +400,7 @@ process_packet_in(struct lswitch *sw, struct rconn *rconn,
/* Extract flow data from 'opi' into 'flow'. */
pkt_ofs = offsetof(struct ofp_packet_in, data);
pkt_len = ntohs(opi->header.length) - pkt_ofs;
- pkt.data = (void *) opi->data;
- pkt.size = pkt_len;
+ ofpbuf_use_const(&pkt, opi->data, pkt_len);
flow_extract(&pkt, 0, in_port, &flow);
/* Choose output port. */
diff --git a/lib/netlink.c b/lib/netlink.c
index ba32ca3..f4af252 100644
--- a/lib/netlink.c
+++ b/lib/netlink.c
@@ -882,8 +882,7 @@ nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg)
struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(buffer);
size_t len = nlmsghdr->nlmsg_len;
if (len >= sizeof *nlmsghdr && len <= buffer->size) {
- msg->data = nlmsghdr;
- msg->size = len;
+ ofpbuf_use_const(msg, nlmsghdr, len);
ofpbuf_pull(buffer, len);
return nlmsghdr;
}
@@ -979,13 +978,11 @@ nl_attr_get_string(const struct nlattr *nla)
return nl_attr_get(nla);
}
-/* Initializes 'nested' to the payload of 'nla'. Doesn't initialize every
- * field in 'nested', but enough to poke around with it in a read-only way. */
+/* Initializes 'nested' to the payload of 'nla'. */
void
nl_attr_get_nested(const struct nlattr *nla, struct ofpbuf *nested)
{
- nested->data = (void *) nl_attr_get(nla);
- nested->size = nl_attr_get_size(nla);
+ ofpbuf_use_const(nested, nl_attr_get(nla), nl_attr_get_size(nla));
}
/* Default minimum and maximum payload sizes for each type of attribute. */
@@ -1324,8 +1321,7 @@ log_nlmsg(const char *function, int error,
return;
}
- buffer.data = (void *) message;
- buffer.size = size;
+ ofpbuf_use_const(&buffer, message, size);
nlmsg = nlmsg_to_string(&buffer);
VLOG_DBG_RL(&rl, "%s (%s): %s", function, strerror(error), nlmsg);
free(nlmsg);
diff --git a/lib/ofp-print.c b/lib/ofp-print.c
index 6ac0959..d801236 100644
--- a/lib/ofp-print.c
+++ b/lib/ofp-print.c
@@ -61,8 +61,7 @@ ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
int status;
int c;
- buf.data = (void *) data;
- buf.size = len;
+ ofpbuf_use_const(&buf, data, len);
pcap = tmpfile();
if (!pcap) {
@@ -133,8 +132,7 @@ ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op,
struct flow flow;
struct ofpbuf packet;
- packet.data = (void *) op->data;
- packet.size = data_len;
+ ofpbuf_use_const(&packet, op->data, data_len);
flow_extract(&packet, 0, ntohs(op->in_port), &flow);
flow_format(string, &flow);
ds_put_char(string, '\n');
diff --git a/lib/ofpbuf.c b/lib/ofpbuf.c
index 3f18d29..77595e0 100644
--- a/lib/ofpbuf.c
+++ b/lib/ofpbuf.c
@@ -40,6 +40,17 @@ ofpbuf_use(struct ofpbuf *b, void *base, size_t allocated)
b->private_p = NULL;
}
+/* Initializes 'b' as an ofpbuf whose data starts at 'data' and continues for
+ * 'size' bytes. This is appropriate for an ofpbuf that will be used to
+ * inspect existing data, without moving it around or reallocating it, and
+ * generally without modifying it at all. */
+void
+ofpbuf_use_const(struct ofpbuf *b, const void *data, size_t size)
+{
+ ofpbuf_use(b, (void *) data, size);
+ b->size = size;
+}
+
/* Initializes 'b' as an empty ofpbuf with an initial capacity of 'size'
* bytes. */
void
diff --git a/lib/ofpbuf.h b/lib/ofpbuf.h
index 7d106d8..a7b5ded 100644
--- a/lib/ofpbuf.h
+++ b/lib/ofpbuf.h
@@ -43,6 +43,7 @@ struct ofpbuf {
};
void ofpbuf_use(struct ofpbuf *, void *, size_t);
+void ofpbuf_use_const(struct ofpbuf *, const void *, size_t);
void ofpbuf_init(struct ofpbuf *, size_t);
void ofpbuf_uninit(struct ofpbuf *);
diff --git a/ofproto/ofproto-sflow.c b/ofproto/ofproto-sflow.c
index 87abef9..801614d 100644
--- a/ofproto/ofproto-sflow.c
+++ b/ofproto/ofproto-sflow.c
@@ -517,8 +517,7 @@ ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg)
actions = (const union odp_action *) (hdr + 1);
/* Get packet payload and extract flow. */
- payload.data = (union odp_action *) (actions + n_actions);
- payload.size = msg->length - min_size;
+ ofpbuf_use_const(&payload, actions + n_actions, msg->length - min_size);
flow_extract(&payload, 0, msg->port, &flow);
/* Build a flow sample */
diff --git a/ofproto/ofproto.c b/ofproto/ofproto.c
index 07f7b5c..7a93b10 100644
--- a/ofproto/ofproto.c
+++ b/ofproto/ofproto.c
@@ -3072,8 +3072,7 @@ handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
}
/* Get ofp_packet_out. */
- request.data = (void *) oh;
- request.size = ntohs(oh->length);
+ ofpbuf_use_const(&request, oh, ntohs(oh->length));
opo = ofpbuf_try_pull(&request, offsetof(struct ofp_packet_out, actions));
if (!opo) {
return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
@@ -4083,8 +4082,7 @@ handle_ofpt_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
struct ofpbuf b;
int error;
- b.data = (void *) oh;
- b.size = ntohs(oh->length);
+ ofpbuf_use_const(&b, oh, ntohs(oh->length));
/* Dissect the message. */
ofm = ofpbuf_try_pull(&b, sizeof *ofm);
@@ -4138,8 +4136,7 @@ handle_nxt_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
struct ofpbuf b;
int error;
- b.data = (void *) oh;
- b.size = ntohs(oh->length);
+ ofpbuf_use_const(&b, oh, ntohs(oh->length));
/* Dissect the message. */
nfm = ofpbuf_try_pull(&b, sizeof *nfm);
@@ -4390,8 +4387,7 @@ handle_odp_miss_msg(struct ofproto *p, struct ofpbuf *packet)
struct facet *facet;
struct flow flow;
- payload.data = msg + 1;
- payload.size = msg->length - sizeof *msg;
+ ofpbuf_use_const(&payload, msg + 1, msg->length - sizeof *msg);
flow_extract(&payload, msg->arg, msg->port, &flow);
packet->l2 = payload.l2;
@@ -4856,8 +4852,9 @@ schedule_packet_in(struct ofconn *ofconn, struct ofpbuf *packet, int max_len,
buffer_id = UINT32_MAX;
} else {
struct ofpbuf payload;
- payload.data = opi->data;
- payload.size = packet->size - offsetof(struct ofp_packet_in, data);
+
+ ofpbuf_use_const(&payload, opi->data,
+ packet->size - offsetof(struct ofp_packet_in, data));
buffer_id = pktbuf_save(ofconn->pktbuf, &payload, in_port);
}
--
1.7.1
More information about the dev
mailing list