From 0bc9407dbbe18734c25e9dc470592def5cb27077 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 2 Dec 2010 14:53:12 -0800 Subject: [PATCH] ofpbuf: New function ofpbuf_use_const(). This is a code cleanup. Suggested-by: Justin Pettit Acked-by: Jesse Gross --- 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 0345efe1..62ede06e 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 cbd24cbf..5f429c12 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 ba32ca3a..f4af2527 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 eba194e4..e199ac9f 100644 --- a/lib/ofp-print.c +++ b/lib/ofp-print.c @@ -64,8 +64,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) { @@ -136,8 +135,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 3f18d29d..77595e03 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 7d106d88..a7b5ded9 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 87abef97..801614d2 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 64789656..1ac5983d 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); @@ -3515,8 +3514,7 @@ handle_nxst_flow(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. */ nfsr = ofpbuf_try_pull(&b, sizeof *nfsr); @@ -3651,8 +3649,7 @@ handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh) struct ofpbuf *buf; int error; - b.data = (void *) oh; - b.size = ntohs(oh->length); + ofpbuf_use_const(&b, oh, ntohs(oh->length)); /* Dissect the message. */ request = ofpbuf_try_pull(&b, sizeof *request); @@ -4288,8 +4285,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; @@ -4754,8 +4750,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); } -- 2.30.2