Merge branch "partner", to simplify partner integration.
[sliver-openvswitch.git] / switch / datapath.c
index 7ed4f2d..0e715f1 100644 (file)
 #include <inttypes.h>
 #include <stdlib.h>
 #include <string.h>
-#include "buffer.h"
 #include "chain.h"
 #include "csum.h"
 #include "flow.h"
+#include "list.h"
 #include "netdev.h"
+#include "ofpbuf.h"
+#include "openflow.h"
 #include "packets.h"
 #include "poll-loop.h"
 #include "rconn.h"
 #include "stp.h"
-#include "vconn.h"
+#include "switch-flow.h"
 #include "table.h"
 #include "timeval.h"
+#include "vconn.h"
 #include "xtoxll.h"
 
 #define THIS_MODULE VLM_datapath
@@ -142,25 +145,25 @@ static void remote_run(struct datapath *, struct remote *);
 static void remote_wait(struct remote *);
 static void remote_destroy(struct remote *);
 
-void dp_output_port(struct datapath *, struct buffer *,
+void dp_output_port(struct datapath *, struct ofpbuf *,
                     int in_port, int out_port, bool ignore_no_fwd);
 void dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm);
-void dp_output_control(struct datapath *, struct buffer *, int in_port,
+void dp_output_control(struct datapath *, struct ofpbuf *, int in_port,
                        size_t max_len, int reason);
 static void send_flow_expired(struct datapath *, struct sw_flow *,
                               enum ofp_flow_expired_reason);
 static int update_port_status(struct sw_port *p);
 static void send_port_status(struct sw_port *p, uint8_t status);
 static void del_switch_port(struct sw_port *p);
-static void execute_actions(struct datapath *, struct buffer *,
+static void execute_actions(struct datapath *, struct ofpbuf *,
                             int in_port, const struct sw_flow_key *,
                             const struct ofp_action *, int n_actions,
                             bool ignore_no_fwd);
-static void modify_vlan(struct buffer *buffer, const struct sw_flow_key *key,
+static void modify_vlan(struct ofpbuf *buffer, const struct sw_flow_key *key,
                         const struct ofp_action *a);
-static void modify_nh(struct buffer *buffer, uint16_t eth_proto,
+static void modify_nh(struct ofpbuf *buffer, uint16_t eth_proto,
                       uint8_t nw_proto, const struct ofp_action *a);
-static void modify_th(struct buffer *buffer, uint16_t eth_proto,
+static void modify_th(struct ofpbuf *buffer, uint16_t eth_proto,
                           uint8_t nw_proto, const struct ofp_action *a);
 
 /* Buffers are identified to userspace by a 31-bit opaque ID.  We divide the ID
@@ -174,14 +177,14 @@ static void modify_th(struct buffer *buffer, uint16_t eth_proto,
 
 #define PKT_COOKIE_BITS (32 - PKT_BUFFER_BITS)
 
-int run_flow_through_tables(struct datapath *, struct buffer *,
+int run_flow_through_tables(struct datapath *, struct ofpbuf *,
                             struct sw_port *);
-void fwd_port_input(struct datapath *, struct buffer *, struct sw_port *);
+void fwd_port_input(struct datapath *, struct ofpbuf *, struct sw_port *);
 int fwd_control_input(struct datapath *, const struct sender *,
                       const void *, size_t);
 
-uint32_t save_buffer(struct buffer *);
-static struct buffer *retrieve_buffer(uint32_t id);
+uint32_t save_buffer(struct ofpbuf *);
+static struct ofpbuf *retrieve_buffer(uint32_t id);
 static void discard_buffer(uint32_t id);
 
 static int port_no(struct datapath *dp, struct sw_port *p) 
@@ -289,7 +292,7 @@ dp_run(struct datapath *dp)
     time_t now = time_now();
     struct sw_port *p, *pn;
     struct remote *r, *rn;
-    struct buffer *buffer = NULL;
+    struct ofpbuf *buffer = NULL;
 
     if (now != dp->last_timeout) {
         struct list deleted = LIST_INITIALIZER(&deleted);
@@ -321,8 +324,8 @@ dp_run(struct datapath *dp)
             const int headroom = 128 + 2;
             const int hard_header = VLAN_ETH_HEADER_LEN;
             const int mtu = netdev_get_mtu(p->netdev);
-            buffer = buffer_new(headroom + hard_header + mtu);
-            buffer->data += headroom;
+            buffer = ofpbuf_new(headroom + hard_header + mtu);
+            buffer->data = (char*)buffer->data + headroom;
         }
         error = netdev_recv(p->netdev, buffer);
         if (!error) {
@@ -335,7 +338,7 @@ dp_run(struct datapath *dp)
                         netdev_get_name(p->netdev), strerror(error));
         }
     }
-    buffer_delete(buffer);
+    ofpbuf_delete(buffer);
 
     /* Talk to remotes. */
     LIST_FOR_EACH_SAFE (r, rn, struct remote, node, &dp->remotes) {
@@ -369,7 +372,7 @@ remote_run(struct datapath *dp, struct remote *r)
      * other processing doesn't starve. */
     for (i = 0; i < 50; i++) {
         if (!r->cb_dump) {
-            struct buffer *buffer;
+            struct ofpbuf *buffer;
             struct ofp_header *oh;
 
             buffer = rconn_recv(r->rconn);
@@ -387,7 +390,7 @@ remote_run(struct datapath *dp, struct remote *r)
             } else {
                 VLOG_WARN_RL(&rl, "received too-short OpenFlow message");
             }
-            buffer_delete(buffer); 
+            ofpbuf_delete(buffer); 
         } else {
             if (r->n_txq < TXQ_LIMIT) {
                 int error = r->cb_dump(dp, r->cb_aux);
@@ -514,7 +517,7 @@ dp_destroy(struct datapath *dp)
  * "flood" argument is set, don't send out ports with flooding disabled.
  */
 static int
-output_all(struct datapath *dp, struct buffer *buffer, int in_port, int flood)
+output_all(struct datapath *dp, struct ofpbuf *buffer, int in_port, int flood)
 {
     struct sw_port *p;
     int prev_port;
@@ -528,7 +531,7 @@ output_all(struct datapath *dp, struct buffer *buffer, int in_port, int flood)
             continue;
         }
         if (prev_port != -1) {
-            dp_output_port(dp, buffer_clone(buffer), in_port, prev_port,
+            dp_output_port(dp, ofpbuf_clone(buffer), in_port, prev_port,
                            false);
         }
         prev_port = port_no(dp, p);
@@ -536,13 +539,13 @@ output_all(struct datapath *dp, struct buffer *buffer, int in_port, int flood)
     if (prev_port != -1)
         dp_output_port(dp, buffer, in_port, prev_port, false);
     else
-        buffer_delete(buffer);
+        ofpbuf_delete(buffer);
 
     return 0;
 }
 
 void
-output_packet(struct datapath *dp, struct buffer *buffer, int out_port) 
+output_packet(struct datapath *dp, struct ofpbuf *buffer, int out_port) 
 {
     if (out_port >= 0 && out_port < OFPP_MAX) { 
         struct sw_port *p = &dp->ports[out_port];
@@ -557,14 +560,14 @@ output_packet(struct datapath *dp, struct buffer *buffer, int out_port)
         }
     }
 
-    buffer_delete(buffer);
+    ofpbuf_delete(buffer);
     VLOG_DBG_RL(&rl, "can't forward to bad port %d\n", out_port);
 }
 
 /* Takes ownership of 'buffer' and transmits it to 'out_port' on 'dp'.
  */
 void
-dp_output_port(struct datapath *dp, struct buffer *buffer,
+dp_output_port(struct datapath *dp, struct ofpbuf *buffer,
                int in_port, int out_port, bool ignore_no_fwd)
 {
 
@@ -580,7 +583,7 @@ dp_output_port(struct datapath *dp, struct buffer *buffer,
     } else if (out_port == OFPP_TABLE) {
         struct sw_port *p = in_port < OFPP_MAX ? &dp->ports[in_port] : 0;
                if (run_flow_through_tables(dp, buffer, p)) {
-                       buffer_delete(buffer);
+                       ofpbuf_delete(buffer);
         }
     } else {
         if (in_port == out_port) {
@@ -593,14 +596,14 @@ dp_output_port(struct datapath *dp, struct buffer *buffer,
 
 static void *
 make_openflow_reply(size_t openflow_len, uint8_t type,
-                    const struct sender *sender, struct buffer **bufferp)
+                    const struct sender *sender, struct ofpbuf **bufferp)
 {
     return make_openflow_xid(openflow_len, type, sender ? sender->xid : 0,
                              bufferp);
 }
 
 static int
-send_openflow_buffer(struct datapath *dp, struct buffer *buffer,
+send_openflow_buffer(struct datapath *dp, struct ofpbuf *buffer,
                      const struct sender *sender)
 {
     struct remote *remote = sender ? sender->remote : dp->controller;
@@ -623,7 +626,7 @@ send_openflow_buffer(struct datapath *dp, struct buffer *buffer,
  * the caller wants to be sent; a value of 0 indicates the entire packet should
  * be sent. */
 void
-dp_output_control(struct datapath *dp, struct buffer *buffer, int in_port,
+dp_output_control(struct datapath *dp, struct ofpbuf *buffer, int in_port,
                   size_t max_len, int reason)
 {
     struct ofp_packet_in *opi;
@@ -632,11 +635,11 @@ dp_output_control(struct datapath *dp, struct buffer *buffer, int in_port,
 
     buffer_id = save_buffer(buffer);
     total_len = buffer->size;
-    if (buffer_id != UINT32_MAX && buffer->size > max_len) {
+    if (buffer_id != UINT32_MAX && max_len && buffer->size > max_len) {
         buffer->size = max_len;
     }
 
-    opi = buffer_push_uninit(buffer, offsetof(struct ofp_packet_in, data));
+    opi = ofpbuf_push_uninit(buffer, offsetof(struct ofp_packet_in, data));
     opi->header.version = OFP_VERSION;
     opi->header.type    = OFPT_PACKET_IN;
     opi->header.length  = htons(buffer->size);
@@ -666,7 +669,7 @@ static void fill_port_desc(struct datapath *dp, struct sw_port *p,
 static void
 dp_send_features_reply(struct datapath *dp, const struct sender *sender)
 {
-    struct buffer *buffer;
+    struct ofpbuf *buffer;
     struct ofp_switch_features *ofr;
     struct sw_port *p;
 
@@ -678,7 +681,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 = buffer_put_uninit(buffer, sizeof *opp);
+        struct ofp_phy_port *opp = ofpbuf_put_uninit(buffer, sizeof *opp);
         memset(opp, 0, sizeof *opp);
         fill_port_desc(dp, p, opp);
     }
@@ -761,7 +764,7 @@ update_port_status(struct sw_port *p)
 static void
 send_port_status(struct sw_port *p, uint8_t status) 
 {
-    struct buffer *buffer;
+    struct ofpbuf *buffer;
     struct ofp_port_status *ops;
     ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &buffer);
     ops->reason = status;
@@ -775,7 +778,7 @@ void
 send_flow_expired(struct datapath *dp, struct sw_flow *flow,
                   enum ofp_flow_expired_reason reason)
 {
-    struct buffer *buffer;
+    struct ofpbuf *buffer;
     struct ofp_flow_expired *ofe;
     ofe = make_openflow_xid(sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &buffer);
     flow_fill_match(&ofe->match, &flow->key);
@@ -795,7 +798,7 @@ void
 dp_send_error_msg(struct datapath *dp, const struct sender *sender,
                   uint16_t type, uint16_t code, const void *data, size_t len)
 {
-    struct buffer *buffer;
+    struct ofpbuf *buffer;
     struct ofp_error_msg *oem;
     oem = make_openflow_reply(sizeof(*oem)+len, OFPT_ERROR, sender, &buffer);
     oem->type = htons(type);
@@ -805,12 +808,12 @@ dp_send_error_msg(struct datapath *dp, const struct sender *sender,
 }
 
 static void
-fill_flow_stats(struct buffer *buffer, struct sw_flow *flow,
+fill_flow_stats(struct ofpbuf *buffer, struct sw_flow *flow,
                 int table_idx, time_t now)
 {
     struct ofp_flow_stats *ofs;
     int length = sizeof *ofs + sizeof *ofs->actions * flow->n_actions;
-    ofs = buffer_put_uninit(buffer, length);
+    ofs = ofpbuf_put_uninit(buffer, length);
     ofs->length          = htons(length);
     ofs->table_id        = table_idx;
     ofs->pad             = 0;
@@ -842,7 +845,7 @@ fill_flow_stats(struct buffer *buffer, struct sw_flow *flow,
  * null pointer.  Process it according to 'dp''s flow table.  Returns 0 if
  * successful, in which case 'buffer' is destroyed, or -ESRCH if there is no
  * matching flow, in which case 'buffer' still belongs to the caller. */
-int run_flow_through_tables(struct datapath *dp, struct buffer *buffer,
+int run_flow_through_tables(struct datapath *dp, struct ofpbuf *buffer,
                             struct sw_port *p)
 {
     struct sw_flow_key key;
@@ -852,13 +855,13 @@ int run_flow_through_tables(struct datapath *dp, struct buffer *buffer,
     if (flow_extract(buffer, p ? port_no(dp, p) : OFPP_NONE, &key.flow)
         && (dp->flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP) {
         /* Drop fragment. */
-        buffer_delete(buffer);
+        ofpbuf_delete(buffer);
         return 0;
     }
        if (p && p->flags & (OFPPFL_NO_RECV | OFPPFL_NO_RECV_STP)
         && p->flags & (!eth_addr_equals(key.flow.dl_dst, stp_eth_addr)
                        ? OFPPFL_NO_RECV : OFPPFL_NO_RECV_STP)) {
-               buffer_delete(buffer);
+               ofpbuf_delete(buffer);
                return 0;
        }
 
@@ -876,7 +879,7 @@ int run_flow_through_tables(struct datapath *dp, struct buffer *buffer,
 /* 'buffer' was received on 'p', which may be a a physical switch port or a
  * null pointer.  Process it according to 'dp''s flow table, sending it up to
  * the controller if no flow matches.  Takes ownership of 'buffer'. */
-void fwd_port_input(struct datapath *dp, struct buffer *buffer,
+void fwd_port_input(struct datapath *dp, struct ofpbuf *buffer,
                     struct sw_port *p)
 {
     if (run_flow_through_tables(dp, buffer, p)) {
@@ -886,7 +889,7 @@ void fwd_port_input(struct datapath *dp, struct buffer *buffer,
 }
 
 static void
-do_output(struct datapath *dp, struct buffer *buffer, int in_port,
+do_output(struct datapath *dp, struct ofpbuf *buffer, int in_port,
           size_t max_len, int out_port, bool ignore_no_fwd)
 {
     if (out_port != OFPP_CONTROLLER) {
@@ -897,7 +900,7 @@ do_output(struct datapath *dp, struct buffer *buffer, int in_port,
 }
 
 static void
-execute_actions(struct datapath *dp, struct buffer *buffer,
+execute_actions(struct datapath *dp, struct ofpbuf *buffer,
                 int in_port, const struct sw_flow_key *key,
                 const struct ofp_action *actions, int n_actions,
                 bool ignore_no_fwd)
@@ -919,7 +922,7 @@ execute_actions(struct datapath *dp, struct buffer *buffer,
         struct eth_header *eh = buffer->l2;
 
         if (prev_port != -1) {
-            do_output(dp, buffer_clone(buffer), in_port, max_len, prev_port,
+            do_output(dp, ofpbuf_clone(buffer), in_port, max_len, prev_port,
                       ignore_no_fwd);
             prev_port = -1;
         }
@@ -959,10 +962,10 @@ execute_actions(struct datapath *dp, struct buffer *buffer,
     if (prev_port != -1)
         do_output(dp, buffer, in_port, max_len, prev_port, ignore_no_fwd);
     else
-        buffer_delete(buffer);
+        ofpbuf_delete(buffer);
 }
 
-static void modify_nh(struct buffer *buffer, uint16_t eth_proto,
+static void modify_nh(struct ofpbuf *buffer, uint16_t eth_proto,
                       uint8_t nw_proto, const struct ofp_action *a)
 {
     if (eth_proto == ETH_TYPE_IP) {
@@ -988,7 +991,7 @@ static void modify_nh(struct buffer *buffer, uint16_t eth_proto,
     }
 }
 
-static void modify_th(struct buffer *buffer, uint16_t eth_proto,
+static void modify_th(struct ofpbuf *buffer, uint16_t eth_proto,
                       uint8_t nw_proto, const struct ofp_action *a)
 {
     if (eth_proto == ETH_TYPE_IP) {
@@ -1011,7 +1014,7 @@ static void modify_th(struct buffer *buffer, uint16_t eth_proto,
 }
 
 static void
-modify_vlan(struct buffer *buffer,
+modify_vlan(struct ofpbuf *buffer,
             const struct sw_flow_key *key, const struct ofp_action *a)
 {
     uint16_t new_id = a->arg.vlan_id;
@@ -1033,9 +1036,9 @@ modify_vlan(struct buffer *buffer,
             tmp.veth_tci = new_id;
             tmp.veth_next_type = eh->eth_type;
             
-            veh = buffer_push_uninit(buffer, VLAN_HEADER_LEN);
+            veh = ofpbuf_push_uninit(buffer, VLAN_HEADER_LEN);
             memcpy(veh, &tmp, sizeof tmp);
-            buffer->l2 -= VLAN_HEADER_LEN;
+            buffer->l2 = (char*)buffer->l2 - VLAN_HEADER_LEN;
         }
     } else  {
         /* Remove an existing vlan header if it exists */
@@ -1048,8 +1051,8 @@ modify_vlan(struct buffer *buffer,
             tmp.eth_type = veh->veth_next_type;
             
             buffer->size -= VLAN_HEADER_LEN;
-            buffer->data += VLAN_HEADER_LEN;
-            buffer->l2 += VLAN_HEADER_LEN;
+            buffer->data = (char*)buffer->data + VLAN_HEADER_LEN;
+            buffer->l2 = (char*)buffer->l2 + VLAN_HEADER_LEN;
             memcpy(buffer->data, &tmp, sizeof tmp);
         }
     }
@@ -1067,7 +1070,7 @@ static int
 recv_get_config_request(struct datapath *dp, const struct sender *sender,
                         const void *msg) 
 {
-    struct buffer *buffer;
+    struct ofpbuf *buffer;
     struct ofp_switch_config *osc;
 
     osc = make_openflow_reply(sizeof *osc, OFPT_GET_CONFIG_REPLY,
@@ -1102,7 +1105,7 @@ recv_packet_out(struct datapath *dp, const struct sender *sender UNUSED,
 {
     const struct ofp_packet_out *opo = msg;
     struct sw_flow_key key;
-    struct buffer *buffer;
+    struct ofpbuf *buffer;
     int n_actions = ntohs(opo->n_actions);
     int act_len = n_actions * sizeof opo->actions[0];
 
@@ -1114,8 +1117,8 @@ recv_packet_out(struct datapath *dp, const struct sender *sender UNUSED,
     if (ntohl(opo->buffer_id) == (uint32_t) -1) {
         /* FIXME: can we avoid copying data here? */
         int data_len = ntohs(opo->header.length) - sizeof *opo - act_len;
-        buffer = buffer_new(data_len);
-        buffer_put(buffer, &opo->actions[n_actions], data_len);
+        buffer = ofpbuf_new(data_len);
+        ofpbuf_put(buffer, &opo->actions[n_actions], data_len);
     } else {
         buffer = retrieve_buffer(ntohl(opo->buffer_id));
         if (!buffer) {
@@ -1190,7 +1193,7 @@ add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
     }
     error = 0;
     if (ntohl(ofm->buffer_id) != UINT32_MAX) {
-        struct buffer *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
+        struct ofpbuf *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
         if (buffer) {
             struct sw_flow_key key;
             uint16_t in_port = ntohs(ofm->match.in_port);
@@ -1237,9 +1240,9 @@ recv_flow(struct datapath *dp, const struct sender *sender UNUSED,
 }
 
 static int desc_stats_dump(struct datapath *dp, void *state,
-                              struct buffer *buffer)
+                              struct ofpbuf *buffer)
 {
-    struct ofp_desc_stats *ods = buffer_put_uninit(buffer, sizeof *ods);
+    struct ofp_desc_stats *ods = ofpbuf_put_uninit(buffer, sizeof *ods);
 
     strncpy(ods->mfr_desc, &mfr_desc, sizeof ods->mfr_desc);
     strncpy(ods->hw_desc, &hw_desc, sizeof ods->hw_desc);
@@ -1255,7 +1258,7 @@ struct flow_stats_state {
     struct ofp_flow_stats_request rq;
     time_t now;
 
-    struct buffer *buffer;
+    struct ofpbuf *buffer;
 };
 
 #define MAX_FLOW_STATS_BYTES 4096
@@ -1280,7 +1283,7 @@ static int flow_stats_dump_callback(struct sw_flow *flow, void *private)
 }
 
 static int flow_stats_dump(struct datapath *dp, void *state,
-                           struct buffer *buffer)
+                           struct ofpbuf *buffer)
 {
     struct flow_stats_state *s = state;
     struct sw_flow_key match_key;
@@ -1333,7 +1336,7 @@ static int aggregate_stats_dump_callback(struct sw_flow *flow, void *private)
 }
 
 static int aggregate_stats_dump(struct datapath *dp, void *state,
-                                struct buffer *buffer)
+                                struct ofpbuf *buffer)
 {
     struct aggregate_stats_state *s = state;
     struct ofp_aggregate_stats_request *rq = &s->rq;
@@ -1342,7 +1345,7 @@ static int aggregate_stats_dump(struct datapath *dp, void *state,
     struct sw_flow_key match_key;
     int table_idx;
 
-    rpy = buffer_put_uninit(buffer, sizeof *rpy);
+    rpy = ofpbuf_put_uninit(buffer, sizeof *rpy);
     memset(rpy, 0, sizeof *rpy);
 
     flow_extract_match(&match_key, &rq->match);
@@ -1375,11 +1378,11 @@ static void aggregate_stats_done(void *state)
 }
 
 static int table_stats_dump(struct datapath *dp, void *state,
-                            struct buffer *buffer)
+                            struct ofpbuf *buffer)
 {
     int i;
     for (i = 0; i < dp->chain->n_tables; i++) {
-        struct ofp_table_stats *ots = buffer_put_uninit(buffer, sizeof *ots);
+        struct ofp_table_stats *ots = ofpbuf_put_uninit(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);
@@ -1407,7 +1410,7 @@ static int port_stats_init(struct datapath *dp, const void *body, int body_len,
 }
 
 static int port_stats_dump(struct datapath *dp, void *state,
-                           struct buffer *buffer)
+                           struct ofpbuf *buffer)
 {
     struct port_stats_state *s = state;
     int i;
@@ -1418,7 +1421,7 @@ static int port_stats_dump(struct datapath *dp, void *state,
         if (!p->netdev) {
             continue;
         }
-        ops = buffer_put_uninit(buffer, sizeof *ops);
+        ops = ofpbuf_put_uninit(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);
@@ -1445,6 +1448,9 @@ static void port_stats_done(void *state)
 }
 
 struct stats_type {
+    /* Value for 'type' member of struct ofp_stats_request. */
+    int type;
+
     /* Minimum and maximum acceptable number of bytes in body member of
      * struct ofp_stats_request. */
     size_t min_body, max_body;
@@ -1461,7 +1467,7 @@ struct stats_type {
      * struct ofp_stats_reply.  On success, it should return 1 if it should be
      * called again later with another buffer, 0 if it is done, or a negative
      * errno value on failure. */
-    int (*dump)(struct datapath *dp, void *state, struct buffer *buffer);
+    int (*dump)(struct datapath *dp, void *state, struct ofpbuf *buffer);
 
     /* Cleans any state created by the init or dump functions.  May be null
      * if no cleanup is required. */
@@ -1469,35 +1475,40 @@ struct stats_type {
 };
 
 static const struct stats_type stats[] = {
-    [OFPST_DESC] = {
+    {
+        OFPST_DESC,
         0,
         0,
         NULL,
         desc_stats_dump,
         NULL
     },
-    [OFPST_FLOW] = {
+    {
+        OFPST_FLOW,
         sizeof(struct ofp_flow_stats_request),
         sizeof(struct ofp_flow_stats_request),
         flow_stats_init,
         flow_stats_dump,
         flow_stats_done
     },
-    [OFPST_AGGREGATE] = {
+    {
+        OFPST_AGGREGATE,
         sizeof(struct ofp_aggregate_stats_request),
         sizeof(struct ofp_aggregate_stats_request),
         aggregate_stats_init,
         aggregate_stats_dump,
         aggregate_stats_done
     },
-    [OFPST_TABLE] = {
+    {
+        OFPST_TABLE,
         0,
         0,
         NULL,
         table_stats_dump,
         NULL
     },
-    [OFPST_PORT] = {
+    {
+        OFPST_PORT,
         0,
         0,
         port_stats_init,
@@ -1519,7 +1530,7 @@ stats_dump(struct datapath *dp, void *cb_)
 {
     struct stats_dump_cb *cb = cb_;
     struct ofp_stats_reply *osr;
-    struct buffer *buffer;
+    struct ofpbuf *buffer;
     int err;
 
     if (cb->done) {
@@ -1528,7 +1539,7 @@ stats_dump(struct datapath *dp, void *cb_)
 
     osr = make_openflow_reply(sizeof *osr, OFPT_STATS_REPLY, &cb->sender,
                               &buffer);
-    osr->type = htons(cb->s - stats);
+    osr->type = htons(cb->s->type);
     osr->flags = 0;
 
     err = cb->s->dump(dp, cb->state, buffer);
@@ -1538,7 +1549,7 @@ stats_dump(struct datapath *dp, void *cb_)
             cb->done = true;
         } else {
             /* Buffer might have been reallocated, so find our data again. */
-            osr = buffer_at_assert(buffer, 0, sizeof *osr);
+            osr = ofpbuf_at_assert(buffer, 0, sizeof *osr);
             osr->flags = ntohs(OFPSF_REPLY_MORE);
         }
         err2 = send_openflow_buffer(dp, buffer, &cb->sender);
@@ -1568,23 +1579,27 @@ recv_stats_request(struct datapath *dp, const struct sender *sender,
 {
     const struct ofp_stats_request *rq = oh;
     size_t rq_len = ntohs(rq->header.length);
+    const struct stats_type *st;
     struct stats_dump_cb *cb;
     int type, body_len;
     int err;
 
     type = ntohs(rq->type);
-    if (type >= ARRAY_SIZE(stats) || !stats[type].dump) {
-        dp_send_error_msg(dp, sender, OFPET_BAD_REQUEST, OFPBRC_BAD_STAT,
-                          rq, rq_len);
-        VLOG_WARN_RL(&rl, "received stats request of unknown type %d", type);
-        return -EINVAL;
+    for (st = stats; ; st++) {
+        if (st >= &stats[ARRAY_SIZE(stats)]) {
+            VLOG_WARN_RL(&rl, "received stats request of unknown type %d",
+                         type);
+            return -EINVAL;
+        } else if (type == st->type) {
+            break;
+        }
     }
 
     cb = xmalloc(sizeof *cb);
     cb->done = false;
     cb->rq = xmemdup(rq, rq_len);
     cb->sender = *sender;
-    cb->s = &stats[type];
+    cb->s = st;
     cb->state = NULL;
     
     body_len = rq_len - offsetof(struct ofp_stats_request, body);
@@ -1634,67 +1649,65 @@ int
 fwd_control_input(struct datapath *dp, const struct sender *sender,
                   const void *msg, size_t length)
 {
-    struct openflow_packet {
-        size_t min_size;
-        int (*handler)(struct datapath *, const struct sender *, const void *);
-    };
-
-    static const struct openflow_packet packets[] = {
-        [OFPT_FEATURES_REQUEST] = {
-            sizeof (struct ofp_header),
-            recv_features_request,
-        },
-        [OFPT_GET_CONFIG_REQUEST] = {
-            sizeof (struct ofp_header),
-            recv_get_config_request,
-        },
-        [OFPT_SET_CONFIG] = {
-            sizeof (struct ofp_switch_config),
-            recv_set_config,
-        },
-        [OFPT_PACKET_OUT] = {
-            sizeof (struct ofp_packet_out),
-            recv_packet_out,
-        },
-        [OFPT_FLOW_MOD] = {
-            sizeof (struct ofp_flow_mod),
-            recv_flow,
-        },
-        [OFPT_PORT_MOD] = {
-            sizeof (struct ofp_port_mod),
-            recv_port_mod,
-        },
-        [OFPT_STATS_REQUEST] = {
-            sizeof (struct ofp_stats_request),
-            recv_stats_request,
-        },
-        [OFPT_ECHO_REQUEST] = {
-            sizeof (struct ofp_header),
-            recv_echo_request,
-        },
-        [OFPT_ECHO_REPLY] = {
-            sizeof (struct ofp_header),
-            recv_echo_reply,
-        },
-    };
-
+    int (*handler)(struct datapath *, const struct sender *, const void *);
     struct ofp_header *oh;
+    size_t min_size;
 
+    /* Check encapsulated length. */
     oh = (struct ofp_header *) msg;
-    if (ntohs(oh->length) > length)
+    if (ntohs(oh->length) > length) {
+        return -EINVAL;
+    }
+    assert(oh->version == OFP_VERSION);
+
+    /* Figure out how to handle it. */
+    switch (oh->type) {
+    case OFPT_FEATURES_REQUEST:
+        min_size = sizeof(struct ofp_header);
+        handler = recv_features_request;
+        break;
+    case OFPT_GET_CONFIG_REQUEST:
+        min_size = sizeof(struct ofp_header);
+        handler = recv_get_config_request;
+        break;
+    case OFPT_SET_CONFIG:
+        min_size = sizeof(struct ofp_switch_config);
+        handler = recv_set_config;
+        break;
+    case OFPT_PACKET_OUT:
+        min_size = sizeof(struct ofp_packet_out);
+        handler = recv_packet_out;
+        break;
+    case OFPT_FLOW_MOD:
+        min_size = sizeof(struct ofp_flow_mod);
+        handler = recv_flow;
+        break;
+    case OFPT_PORT_MOD:
+        min_size = sizeof(struct ofp_port_mod);
+        handler = recv_port_mod;
+        break;
+    case OFPT_STATS_REQUEST:
+        min_size = sizeof(struct ofp_stats_request);
+        handler = recv_stats_request;
+        break;
+    case OFPT_ECHO_REQUEST:
+        min_size = sizeof(struct ofp_header);
+        handler = recv_echo_request;
+        break;
+    case OFPT_ECHO_REPLY:
+        min_size = sizeof(struct ofp_header);
+        handler = recv_echo_reply;
+        break;
+    default:
+        dp_send_error_msg(dp, sender, OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE,
+                          msg, length);
         return -EINVAL;
-
-    if (oh->type < ARRAY_SIZE(packets)) {
-        const struct openflow_packet *pkt = &packets[oh->type];
-        if (pkt->handler) {
-            if (length < pkt->min_size)
-                return -EFAULT;
-            return pkt->handler(dp, sender, msg);
-        }
     }
-    dp_send_error_msg(dp, sender, OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE,
-                      msg, length);
-    return -EINVAL;
+
+    /* Handle it. */
+    if (length < min_size)
+        return -EFAULT;
+    return handler(dp, sender, msg);
 }
 \f
 /* Packet buffering. */
@@ -1702,7 +1715,7 @@ fwd_control_input(struct datapath *dp, const struct sender *sender,
 #define OVERWRITE_SECS  1
 
 struct packet_buffer {
-    struct buffer *buffer;
+    struct ofpbuf *buffer;
     uint32_t cookie;
     time_t timeout;
 };
@@ -1710,7 +1723,7 @@ struct packet_buffer {
 static struct packet_buffer buffers[N_PKT_BUFFERS];
 static unsigned int buffer_idx;
 
-uint32_t save_buffer(struct buffer *buffer)
+uint32_t save_buffer(struct ofpbuf *buffer)
 {
     struct packet_buffer *p;
     uint32_t id;
@@ -1723,23 +1736,23 @@ uint32_t save_buffer(struct buffer *buffer)
         if (time_now() < p->timeout) { /* FIXME */
             return -1;
         } else {
-            buffer_delete(p->buffer); 
+            ofpbuf_delete(p->buffer); 
         }
     }
     /* Don't use maximum cookie value since the all-bits-1 id is
      * special. */
     if (++p->cookie >= (1u << PKT_COOKIE_BITS) - 1)
         p->cookie = 0;
-    p->buffer = buffer_clone(buffer);      /* FIXME */
+    p->buffer = ofpbuf_clone(buffer);      /* FIXME */
     p->timeout = time_now() + OVERWRITE_SECS; /* FIXME */
     id = buffer_idx | (p->cookie << PKT_BUFFER_BITS);
 
     return id;
 }
 
-static struct buffer *retrieve_buffer(uint32_t id)
+static struct ofpbuf *retrieve_buffer(uint32_t id)
 {
-    struct buffer *buffer = NULL;
+    struct ofpbuf *buffer = NULL;
     struct packet_buffer *p;
 
     p = &buffers[id & PKT_BUFFER_MASK];
@@ -1760,7 +1773,7 @@ static void discard_buffer(uint32_t id)
 
     p = &buffers[id & PKT_BUFFER_MASK];
     if (p->cookie == id >> PKT_BUFFER_BITS) {
-        buffer_delete(p->buffer);
+        ofpbuf_delete(p->buffer);
         p->buffer = NULL;
     }
 }