ofpbuf: New function ofpbuf_use_const().
authorBen Pfaff <blp@nicira.com>
Thu, 2 Dec 2010 22:53:12 +0000 (14:53 -0800)
committerBen Pfaff <blp@nicira.com>
Tue, 7 Dec 2010 21:44:17 +0000 (13:44 -0800)
This is a code cleanup.

Suggested-by: Justin Pettit <jpettit@nicira.com>
Acked-by: Jesse Gross <jesse@nicira.com>
lib/dhcp.c
lib/learning-switch.c
lib/netlink.c
lib/ofp-print.c
lib/ofpbuf.c
lib/ofpbuf.h
ofproto/ofproto-sflow.c
ofproto/ofproto.c

index 0345efe..62ede06 100644 (file)
@@ -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;
index cbd24cb..5f429c1 100644 (file)
@@ -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. */
index ba32ca3..f4af252 100644 (file)
@@ -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);
index eba194e..e199ac9 100644 (file)
@@ -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');
index 3f18d29..77595e0 100644 (file)
@@ -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
index 7d106d8..a7b5ded 100644 (file)
@@ -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 *);
index 87abef9..801614d 100644 (file)
@@ -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 */
index 6478965..1ac5983 100644 (file)
@@ -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);
     }