This is a code cleanup.
Suggested-by: Justin Pettit <jpettit@nicira.com>
Acked-by: Jesse Gross <jesse@nicira.com>
{
struct ofpbuf b;
- b.data = data;
- b.size = size;
+ ofpbuf_use_const(&b, data, size);
for (;;) {
uint8_t *code, *len;
void *payload;
/* 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. */
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;
}
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. */
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);
int status;
int c;
- buf.data = (void *) data;
- buf.size = len;
+ ofpbuf_use_const(&buf, data, len);
pcap = tmpfile();
if (!pcap) {
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');
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
};
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 *);
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 */
}
/* 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);
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);
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);
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;
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);
}