tunneling: Update port pools on config change.
[openvswitch] / ofproto / ofproto.c
index bf557cfc3b01544f4876fc6693898f69e94d65a1..00b573110e99ed71091d36911f87cff3e718d820 100644 (file)
@@ -90,6 +90,8 @@ COVERAGE_DEFINE(ofproto_update_port);
 
 #include "sflow_api.h"
 
+struct rule;
+
 struct ofport {
     struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */
     struct netdev *netdev;
@@ -100,11 +102,48 @@ struct ofport {
 static void ofport_free(struct ofport *);
 static void hton_ofp_phy_port(struct ofp_phy_port *);
 
-static int xlate_actions(const union ofp_action *in, size_t n_in,
-                         const struct flow *, struct ofproto *,
-                         const struct ofpbuf *packet,
-                         struct odp_actions *out, tag_type *tags,
-                         bool *may_set_up_flow, uint16_t *nf_output_iface);
+struct action_xlate_ctx {
+/* action_xlate_ctx_init() initializes these members. */
+
+    /* The ofproto. */
+    struct ofproto *ofproto;
+
+    /* Flow to which the OpenFlow actions apply.  xlate_actions() will modify
+     * this flow when actions change header fields. */
+    struct flow flow;
+
+    /* The packet corresponding to 'flow', or a null pointer if we are
+     * revalidating without a packet to refer to. */
+    const struct ofpbuf *packet;
+
+    /* If nonnull, called just before executing a resubmit action.
+     *
+     * This is normally null so the client has to set it manually after
+     * calling action_xlate_ctx_init(). */
+    void (*resubmit_hook)(struct action_xlate_ctx *, const struct rule *);
+
+/* xlate_actions() initializes and uses these members.  The client might want
+ * to look at them after it returns. */
+
+    /* Datapath action set.  This is xlate_actions()'s primary output. */
+    struct odp_actions out;
+
+    tag_type tags;              /* Tags associated with OFPP_NORMAL actions. */
+    bool may_set_up_flow;       /* True ordinarily; false if the actions must
+                                 * be reassessed for every packet. */
+    uint16_t nf_output_iface;   /* Output interface index for NetFlow. */
+
+/* xlate_actions() initializes and uses these members, but the client has no
+ * reason to look at them. */
+
+    int recurse;                /* Recursion level, via xlate_table_action. */
+};
+
+static void action_xlate_ctx_init(struct action_xlate_ctx *,
+                                  struct ofproto *, const struct flow *,
+                                  const struct ofpbuf *);
+static int xlate_actions(struct action_xlate_ctx *ctx,
+                         const union ofp_action *in, size_t n_in);
 
 /* An OpenFlow flow. */
 struct rule {
@@ -255,7 +294,7 @@ struct ofconn {
     struct list node;           /* In struct ofproto's "all_conns" list. */
     struct rconn *rconn;        /* OpenFlow connection. */
     enum ofconn_type type;      /* Type. */
-    int flow_format;            /* One of NXFF_*. */
+    enum nx_flow_format flow_format; /* Currently selected flow format. */
 
     /* OFPT_PACKET_IN related data. */
     struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
@@ -359,6 +398,9 @@ struct ofproto {
     struct mac_learning *ml;
 };
 
+/* Map from dpif name to struct ofproto, for use by unixctl commands. */
+static struct shash all_ofprotos = SHASH_INITIALIZER(&all_ofprotos);
+
 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
 
 static const struct ofhooks default_ofhooks;
@@ -377,6 +419,8 @@ static void update_port(struct ofproto *, const char *devname);
 static int init_ports(struct ofproto *);
 static void reinit_ports(struct ofproto *);
 
+static void ofproto_unixctl_init(void);
+
 int
 ofproto_create(const char *datapath, const char *datapath_type,
                const struct ofhooks *ofhooks, void *aux,
@@ -389,6 +433,8 @@ ofproto_create(const char *datapath, const char *datapath_type,
 
     *ofprotop = NULL;
 
+    ofproto_unixctl_init();
+
     /* Connect to datapath and start listening for messages. */
     error = dpif_open(datapath, datapath_type, &dpif);
     if (error) {
@@ -470,6 +516,8 @@ ofproto_create(const char *datapath, const char *datapath_type,
     p->datapath_id = pick_datapath_id(p);
     VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
 
+    shash_add_once(&all_ofprotos, dpif_name(p->dpif), p);
+
     *ofprotop = p;
     return 0;
 }
@@ -991,6 +1039,8 @@ ofproto_destroy(struct ofproto *p)
         return;
     }
 
+    shash_find_and_delete(&all_ofprotos, dpif_name(p->dpif));
+
     /* Destroy fail-open and in-band early, since they touch the classifier. */
     fail_open_destroy(p->fail_open);
     p->fail_open = NULL;
@@ -1316,7 +1366,7 @@ int
 ofproto_port_del(struct ofproto *ofproto, uint16_t odp_port)
 {
     struct ofport *ofport = get_port(ofproto, odp_port);
-    const char *name = ofport ? (char *) ofport->opp.name : "<unknown>";
+    const char *name = ofport ? ofport->opp.name : "<unknown>";
     int error;
 
     error = dpif_port_del(ofproto->dpif, odp_port);
@@ -1349,18 +1399,18 @@ ofproto_send_packet(struct ofproto *p, const struct flow *flow,
                     const union ofp_action *actions, size_t n_actions,
                     const struct ofpbuf *packet)
 {
-    struct odp_actions odp_actions;
+    struct action_xlate_ctx ctx;
     int error;
 
-    error = xlate_actions(actions, n_actions, flow, p, packet, &odp_actions,
-                          NULL, NULL, NULL);
+    action_xlate_ctx_init(&ctx, p, flow, packet);
+    error = xlate_actions(&ctx, actions, n_actions);
     if (error) {
         return error;
     }
 
     /* XXX Should we translate the dpif_execute() errno value into an OpenFlow
      * error code? */
-    dpif_execute(p->dpif, odp_actions.actions, odp_actions.n_actions, packet);
+    dpif_execute(p->dpif, ctx.out.actions, ctx.out.n_actions, packet);
     return 0;
 }
 
@@ -1439,7 +1489,7 @@ reinit_ports(struct ofproto *p)
 
     svec_init(&devnames);
     HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
-        svec_add (&devnames, (char *) ofport->opp.name);
+        svec_add (&devnames, ofport->opp.name);
     }
     dpif_port_list(p->dpif, &odp_ports, &n_odp_ports);
     for (i = 0; i < n_odp_ports; i++) {
@@ -1465,6 +1515,7 @@ make_ofport(const struct odp_port *odp_port)
 
     memset(&netdev_options, 0, sizeof netdev_options);
     netdev_options.name = odp_port->devname;
+    netdev_options.type = odp_port->type;
     netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
 
     error = netdev_open(&netdev_options, &netdev);
@@ -1521,7 +1572,7 @@ ofport_equal(const struct ofport *a_, const struct ofport *b_)
     BUILD_ASSERT_DECL(sizeof *a == 48); /* Detect ofp_phy_port changes. */
     return (a->port_no == b->port_no
             && !memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr)
-            && !strcmp((char *) a->name, (char *) b->name)
+            && !strcmp(a->name, b->name)
             && a->state == b->state
             && a->config == b->config
             && a->curr == b->curr
@@ -1558,7 +1609,7 @@ send_port_status(struct ofproto *p, const struct ofport *ofport,
 static void
 ofport_install(struct ofproto *p, struct ofport *ofport)
 {
-    const char *netdev_name = (const char *) ofport->opp.name;
+    const char *netdev_name = ofport->opp.name;
 
     netdev_monitor_add(p->netdev_monitor, ofport->netdev);
     hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->odp_port, 0));
@@ -1574,7 +1625,7 @@ ofport_remove(struct ofproto *p, struct ofport *ofport)
     netdev_monitor_remove(p->netdev_monitor, ofport->netdev);
     hmap_remove(&p->ports, &ofport->hmap_node);
     shash_delete(&p->port_by_name,
-                 shash_find(&p->port_by_name, (char *) ofport->opp.name));
+                 shash_find(&p->port_by_name, ofport->opp.name));
     if (p->sflow) {
         ofproto_sflow_del_port(p->sflow, ofport->odp_port);
     }
@@ -2068,8 +2119,8 @@ static void
 rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port,
              struct ofpbuf *packet)
 {
+    struct action_xlate_ctx ctx;
     struct facet *facet;
-    struct odp_actions a;
     struct flow flow;
     size_t size;
 
@@ -2095,14 +2146,15 @@ rule_execute(struct ofproto *ofproto, struct rule *rule, uint16_t in_port,
 
     /* We can't account anything to a facet.  If we were to try, then that
      * facet would have a non-matching rule, busting our invariants. */
-    if (xlate_actions(rule->actions, rule->n_actions, &flow, ofproto,
-                      packet, &a, NULL, 0, NULL)) {
+    action_xlate_ctx_init(&ctx, ofproto, &flow, packet);
+    if (xlate_actions(&ctx, rule->actions, rule->n_actions)) {
         ofpbuf_delete(packet);
         return;
     }
+
     size = packet->size;
     if (execute_odp_actions(ofproto, in_port,
-                            a.actions, a.n_actions, packet)) {
+                            ctx.out.actions, ctx.out.n_actions, packet)) {
         rule->used = time_msec();
         rule->packet_count++;
         rule->byte_count += size;
@@ -2194,19 +2246,21 @@ facet_make_actions(struct ofproto *p, struct facet *facet,
                    const struct ofpbuf *packet)
 {
     const struct rule *rule = facet->rule;
-    struct odp_actions a;
+    struct action_xlate_ctx ctx;
     size_t actions_len;
 
-    xlate_actions(rule->actions, rule->n_actions, &facet->flow, p,
-                  packet, &a, &facet->tags, &facet->may_install,
-                  &facet->nf_flow.output_iface);
+    action_xlate_ctx_init(&ctx, p, &facet->flow, packet);
+    xlate_actions(&ctx, rule->actions, rule->n_actions);
+    facet->tags = ctx.tags;
+    facet->may_install = ctx.may_set_up_flow;
+    facet->nf_flow.output_iface = ctx.nf_output_iface;
 
-    actions_len = a.n_actions * sizeof *a.actions;
-    if (facet->n_actions != a.n_actions
-        || memcmp(facet->actions, a.actions, actions_len)) {
+    actions_len = ctx.out.n_actions * sizeof *ctx.out.actions;
+    if (facet->n_actions != ctx.out.n_actions
+        || memcmp(facet->actions, ctx.out.actions, actions_len)) {
         free(facet->actions);
-        facet->n_actions = a.n_actions;
-        facet->actions = xmemdup(a.actions, actions_len);
+        facet->n_actions = ctx.out.n_actions;
+        facet->actions = xmemdup(ctx.out.actions, actions_len);
     }
 }
 
@@ -2374,10 +2428,9 @@ facet_lookup_valid(struct ofproto *ofproto, const struct flow *flow)
 static bool
 facet_revalidate(struct ofproto *ofproto, struct facet *facet)
 {
+    struct action_xlate_ctx ctx;
     struct rule *new_rule;
-    struct odp_actions a;
     size_t actions_len;
-    uint16_t new_nf_output_iface;
     bool actions_changed;
 
     COVERAGE_INC(facet_revalidate);
@@ -2395,12 +2448,12 @@ facet_revalidate(struct ofproto *ofproto, struct facet *facet)
      * We are very cautious about actually modifying 'facet' state at this
      * point, because we might need to, e.g., emit a NetFlow expiration and, if
      * so, we need to have the old state around to properly compose it. */
-    xlate_actions(new_rule->actions, new_rule->n_actions, &facet->flow,
-                  ofproto, NULL, &a, &facet->tags, &facet->may_install,
-                  &new_nf_output_iface);
-    actions_len = a.n_actions * sizeof *a.actions;
-    actions_changed = (facet->n_actions != a.n_actions
-                       || memcmp(facet->actions, a.actions, actions_len));
+    action_xlate_ctx_init(&ctx, ofproto, &facet->flow, NULL);
+    xlate_actions(&ctx, new_rule->actions, new_rule->n_actions);
+    actions_len = ctx.out.n_actions * sizeof *ctx.out.actions;
+    actions_changed = (facet->n_actions != ctx.out.n_actions
+                       || memcmp(facet->actions, ctx.out.actions,
+                                 actions_len));
 
     /* If the ODP actions changed or the installability changed, then we need
      * to talk to the datapath. */
@@ -2410,8 +2463,8 @@ facet_revalidate(struct ofproto *ofproto, struct facet *facet)
 
             memset(&put.flow.stats, 0, sizeof put.flow.stats);
             odp_flow_key_from_flow(&put.flow.key, &facet->flow);
-            put.flow.actions = a.actions;
-            put.flow.n_actions = a.n_actions;
+            put.flow.actions = ctx.out.actions;
+            put.flow.n_actions = ctx.out.n_actions;
             put.flow.flags = 0;
             put.flags = ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS;
             dpif_flow_put(ofproto->dpif, &put);
@@ -2427,11 +2480,13 @@ facet_revalidate(struct ofproto *ofproto, struct facet *facet)
     }
 
     /* Update 'facet' now that we've taken care of all the old state. */
-    facet->nf_flow.output_iface = new_nf_output_iface;
+    facet->tags = ctx.tags;
+    facet->nf_flow.output_iface = ctx.nf_output_iface;
+    facet->may_install = ctx.may_set_up_flow;
     if (actions_changed) {
         free(facet->actions);
-        facet->n_actions = a.n_actions;
-        facet->actions = xmemdup(a.actions, actions_len);
+        facet->n_actions = ctx.out.n_actions;
+        facet->actions = xmemdup(ctx.out.actions, actions_len);
     }
     if (facet->rule != new_rule) {
         COVERAGE_INC(facet_changed_rule);
@@ -2478,15 +2533,14 @@ hton_ofp_phy_port(struct ofp_phy_port *opp)
 }
 
 static int
-handle_echo_request(struct ofconn *ofconn, struct ofp_header *oh)
+handle_echo_request(struct ofconn *ofconn, const struct ofp_header *oh)
 {
-    struct ofp_header *rq = oh;
-    queue_tx(make_echo_reply(rq), ofconn, ofconn->reply_counter);
+    queue_tx(make_echo_reply(oh), ofconn, ofconn->reply_counter);
     return 0;
 }
 
 static int
-handle_features_request(struct ofconn *ofconn, struct ofp_header *oh)
+handle_features_request(struct ofconn *ofconn, const struct ofp_header *oh)
 {
     struct ofp_switch_features *osf;
     struct ofpbuf *buf;
@@ -2520,7 +2574,7 @@ handle_features_request(struct ofconn *ofconn, struct ofp_header *oh)
 }
 
 static int
-handle_get_config_request(struct ofconn *ofconn, struct ofp_header *oh)
+handle_get_config_request(struct ofconn *ofconn, const struct ofp_header *oh)
 {
     struct ofpbuf *buf;
     struct ofp_switch_config *osc;
@@ -2541,16 +2595,9 @@ handle_get_config_request(struct ofconn *ofconn, struct ofp_header *oh)
 }
 
 static int
-handle_set_config(struct ofconn *ofconn, struct ofp_switch_config *osc)
+handle_set_config(struct ofconn *ofconn, const struct ofp_switch_config *osc)
 {
-    uint16_t flags;
-    int error;
-
-    error = check_ofp_message(&osc->header, OFPT_SET_CONFIG, sizeof *osc);
-    if (error) {
-        return error;
-    }
-    flags = ntohs(osc->flags);
+    uint16_t flags = ntohs(osc->flags);
 
     if (ofconn->type == OFCONN_PRIMARY && ofconn->role != NX_ROLE_SLAVE) {
         switch (flags & OFPC_FRAG_MASK) {
@@ -2579,23 +2626,6 @@ add_controller_action(struct odp_actions *actions, uint16_t max_len)
     a->controller.arg = max_len;
 }
 
-struct action_xlate_ctx {
-    /* Input. */
-    struct flow flow;           /* Flow to which these actions correspond. */
-    int recurse;                /* Recursion level, via xlate_table_action. */
-    struct ofproto *ofproto;
-    const struct ofpbuf *packet; /* The packet corresponding to 'flow', or a
-                                  * null pointer if we are revalidating
-                                  * without a packet to refer to. */
-
-    /* Output. */
-    struct odp_actions *out;    /* Datapath actions. */
-    tag_type tags;              /* Tags associated with OFPP_NORMAL actions. */
-    bool may_set_up_flow;       /* True ordinarily; false if the actions must
-                                 * be reassessed for every packet. */
-    uint16_t nf_output_iface;   /* Output interface index for NetFlow. */
-};
-
 /* Maximum depth of flow table recursion (due to NXAST_RESUBMIT actions) in a
  * flow translation. */
 #define MAX_RESUBMIT_RECURSION 8
@@ -2621,7 +2651,7 @@ add_output_action(struct action_xlate_ctx *ctx, uint16_t port)
          */
     }
 
-    odp_actions_add(ctx->out, ODPAT_OUTPUT)->output.port = port;
+    odp_actions_add(&ctx->out, ODPAT_OUTPUT)->output.port = port;
     ctx->nf_output_iface = port;
 }
 
@@ -2646,6 +2676,10 @@ xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
         rule = rule_lookup(ctx->ofproto, &ctx->flow);
         ctx->flow.in_port = old_in_port;
 
+        if (ctx->resubmit_hook) {
+            ctx->resubmit_hook(ctx, rule);
+        }
+
         if (rule) {
             ctx->recurse++;
             do_xlate_actions(rule->actions, rule->n_actions, ctx);
@@ -2692,7 +2726,7 @@ xlate_output_action__(struct action_xlate_ctx *ctx,
         break;
     case OFPP_NORMAL:
         if (!ctx->ofproto->ofhooks->normal_cb(&ctx->flow, ctx->packet,
-                                              ctx->out, &ctx->tags,
+                                              &ctx->out, &ctx->tags,
                                               &ctx->nf_output_iface,
                                               ctx->ofproto->aux)) {
             COVERAGE_INC(ofproto_uninstallable);
@@ -2701,14 +2735,14 @@ xlate_output_action__(struct action_xlate_ctx *ctx,
         break;
     case OFPP_FLOOD:
         flood_packets(ctx->ofproto, ctx->flow.in_port, OFPPC_NO_FLOOD,
-                      &ctx->nf_output_iface, ctx->out);
+                      &ctx->nf_output_iface, &ctx->out);
         break;
     case OFPP_ALL:
         flood_packets(ctx->ofproto, ctx->flow.in_port, 0,
-                      &ctx->nf_output_iface, ctx->out);
+                      &ctx->nf_output_iface, &ctx->out);
         break;
     case OFPP_CONTROLLER:
-        add_controller_action(ctx->out, max_len);
+        add_controller_action(&ctx->out, max_len);
         break;
     case OFPP_LOCAL:
         add_output_action(ctx, ODPP_LOCAL);
@@ -2745,9 +2779,9 @@ xlate_output_action(struct action_xlate_ctx *ctx,
 static void
 remove_pop_action(struct action_xlate_ctx *ctx)
 {
-    size_t n = ctx->out->n_actions;
-    if (n > 0 && ctx->out->actions[n - 1].type == ODPAT_POP_PRIORITY) {
-        ctx->out->n_actions--;
+    size_t n = ctx->out.n_actions;
+    if (n > 0 && ctx->out.actions[n - 1].type == ODPAT_POP_PRIORITY) {
+        ctx->out.n_actions--;
     }
 }
 
@@ -2777,10 +2811,10 @@ xlate_enqueue_action(struct action_xlate_ctx *ctx,
 
     /* Add ODP actions. */
     remove_pop_action(ctx);
-    odp_actions_add(ctx->out, ODPAT_SET_PRIORITY)->priority.priority
+    odp_actions_add(&ctx->out, ODPAT_SET_PRIORITY)->priority.priority
         = priority;
     add_output_action(ctx, odp_port);
-    odp_actions_add(ctx->out, ODPAT_POP_PRIORITY);
+    odp_actions_add(&ctx->out, ODPAT_POP_PRIORITY);
 
     /* Update NetFlow output port. */
     if (ctx->nf_output_iface == NF_OUT_DROP) {
@@ -2806,7 +2840,7 @@ xlate_set_queue_action(struct action_xlate_ctx *ctx,
     }
 
     remove_pop_action(ctx);
-    odp_actions_add(ctx->out, ODPAT_SET_PRIORITY)->priority.priority
+    odp_actions_add(&ctx->out, ODPAT_SET_PRIORITY)->priority.priority
         = priority;
 }
 
@@ -2815,9 +2849,9 @@ xlate_set_dl_tci(struct action_xlate_ctx *ctx)
 {
     ovs_be16 tci = ctx->flow.vlan_tci;
     if (!(tci & htons(VLAN_CFI))) {
-        odp_actions_add(ctx->out, ODPAT_STRIP_VLAN);
+        odp_actions_add(&ctx->out, ODPAT_STRIP_VLAN);
     } else {
-        union odp_action *oa = odp_actions_add(ctx->out, ODPAT_SET_DL_TCI);
+        union odp_action *oa = odp_actions_add(&ctx->out, ODPAT_SET_DL_TCI);
         oa->dl_tci.tci = tci & ~htons(VLAN_CFI);
     }
 }
@@ -2843,7 +2877,7 @@ xlate_nicira_action(struct action_xlate_ctx *ctx,
     const struct nx_action_set_tunnel *nast;
     const struct nx_action_set_queue *nasq;
     union odp_action *oa;
-    int subtype = ntohs(nah->subtype);
+    enum nx_action_subtype subtype = ntohs(nah->subtype);
 
     assert(nah->vendor == htonl(NX_VENDOR_ID));
     switch (subtype) {
@@ -2854,13 +2888,13 @@ xlate_nicira_action(struct action_xlate_ctx *ctx,
 
     case NXAST_SET_TUNNEL:
         nast = (const struct nx_action_set_tunnel *) nah;
-        oa = odp_actions_add(ctx->out, ODPAT_SET_TUNNEL);
+        oa = odp_actions_add(&ctx->out, ODPAT_SET_TUNNEL);
         ctx->flow.tun_id = oa->tunnel.tun_id = nast->tun_id;
         break;
 
     case NXAST_DROP_SPOOFED_ARP:
         if (ctx->flow.dl_type == htons(ETH_TYPE_ARP)) {
-            odp_actions_add(ctx->out, ODPAT_DROP_SPOOFED_ARP);
+            odp_actions_add(&ctx->out, ODPAT_DROP_SPOOFED_ARP);
         }
         break;
 
@@ -2870,7 +2904,7 @@ xlate_nicira_action(struct action_xlate_ctx *ctx,
         break;
 
     case NXAST_POP_QUEUE:
-        odp_actions_add(ctx->out, ODPAT_POP_PRIORITY);
+        odp_actions_add(&ctx->out, ODPAT_POP_PRIORITY);
         break;
 
     case NXAST_REG_MOVE:
@@ -2888,8 +2922,9 @@ xlate_nicira_action(struct action_xlate_ctx *ctx,
     /* If you add a new action here that modifies flow data, don't forget to
      * update the flow key in ctx->flow at the same time. */
 
+    case NXAST_SNAT__OBSOLETE:
     default:
-        VLOG_DBG_RL(&rl, "unknown Nicira action type %"PRIu16, subtype);
+        VLOG_DBG_RL(&rl, "unknown Nicira action type %d", (int) subtype);
         break;
     }
 }
@@ -2911,7 +2946,7 @@ do_xlate_actions(const union ofp_action *in, size_t n_in,
     }
 
     for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
-        uint16_t type = ntohs(ia->type);
+        enum ofp_action_type type = ntohs(ia->type);
         union odp_action *oa;
 
         switch (type) {
@@ -2938,7 +2973,7 @@ do_xlate_actions(const union ofp_action *in, size_t n_in,
             break;
 
         case OFPAT_SET_DL_SRC:
-            oa = odp_actions_add(ctx->out, ODPAT_SET_DL_SRC);
+            oa = odp_actions_add(&ctx->out, ODPAT_SET_DL_SRC);
             memcpy(oa->dl_addr.dl_addr,
                    ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
             memcpy(ctx->flow.dl_src,
@@ -2946,7 +2981,7 @@ do_xlate_actions(const union ofp_action *in, size_t n_in,
             break;
 
         case OFPAT_SET_DL_DST:
-            oa = odp_actions_add(ctx->out, ODPAT_SET_DL_DST);
+            oa = odp_actions_add(&ctx->out, ODPAT_SET_DL_DST);
             memcpy(oa->dl_addr.dl_addr,
                    ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
             memcpy(ctx->flow.dl_dst,
@@ -2954,27 +2989,27 @@ do_xlate_actions(const union ofp_action *in, size_t n_in,
             break;
 
         case OFPAT_SET_NW_SRC:
-            oa = odp_actions_add(ctx->out, ODPAT_SET_NW_SRC);
+            oa = odp_actions_add(&ctx->out, ODPAT_SET_NW_SRC);
             ctx->flow.nw_src = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
             break;
 
         case OFPAT_SET_NW_DST:
-            oa = odp_actions_add(ctx->out, ODPAT_SET_NW_DST);
+            oa = odp_actions_add(&ctx->out, ODPAT_SET_NW_DST);
             ctx->flow.nw_dst = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
             break;
 
         case OFPAT_SET_NW_TOS:
-            oa = odp_actions_add(ctx->out, ODPAT_SET_NW_TOS);
+            oa = odp_actions_add(&ctx->out, ODPAT_SET_NW_TOS);
             ctx->flow.nw_tos = oa->nw_tos.nw_tos = ia->nw_tos.nw_tos;
             break;
 
         case OFPAT_SET_TP_SRC:
-            oa = odp_actions_add(ctx->out, ODPAT_SET_TP_SRC);
+            oa = odp_actions_add(&ctx->out, ODPAT_SET_TP_SRC);
             ctx->flow.tp_src = oa->tp_port.tp_port = ia->tp_port.tp_port;
             break;
 
         case OFPAT_SET_TP_DST:
-            oa = odp_actions_add(ctx->out, ODPAT_SET_TP_DST);
+            oa = odp_actions_add(&ctx->out, ODPAT_SET_TP_DST);
             ctx->flow.tp_dst = oa->tp_port.tp_port = ia->tp_port.tp_port;
             break;
 
@@ -2987,52 +3022,45 @@ do_xlate_actions(const union ofp_action *in, size_t n_in,
             break;
 
         default:
-            VLOG_DBG_RL(&rl, "unknown action type %"PRIu16, type);
+            VLOG_DBG_RL(&rl, "unknown action type %d", (int) type);
             break;
         }
     }
 }
 
-static int
-xlate_actions(const union ofp_action *in, size_t n_in,
-              const struct flow *flow, struct ofproto *ofproto,
-              const struct ofpbuf *packet,
-              struct odp_actions *out, tag_type *tags, bool *may_set_up_flow,
-              uint16_t *nf_output_iface)
+static void
+action_xlate_ctx_init(struct action_xlate_ctx *ctx,
+                      struct ofproto *ofproto, const struct flow *flow,
+                      const struct ofpbuf *packet)
 {
-    struct action_xlate_ctx ctx;
+    ctx->ofproto = ofproto;
+    ctx->flow = *flow;
+    ctx->packet = packet;
+    ctx->resubmit_hook = NULL;
+}
 
+static int
+xlate_actions(struct action_xlate_ctx *ctx,
+              const union ofp_action *in, size_t n_in)
+{
     COVERAGE_INC(ofproto_ofp2odp);
-    odp_actions_init(out);
-    ctx.flow = *flow;
-    ctx.recurse = 0;
-    ctx.ofproto = ofproto;
-    ctx.packet = packet;
-    ctx.out = out;
-    ctx.tags = 0;
-    ctx.may_set_up_flow = true;
-    ctx.nf_output_iface = NF_OUT_DROP;
-    do_xlate_actions(in, n_in, &ctx);
-    remove_pop_action(&ctx);
+    odp_actions_init(&ctx->out);
+    ctx->tags = 0;
+    ctx->may_set_up_flow = true;
+    ctx->nf_output_iface = NF_OUT_DROP;
+    ctx->recurse = 0;
+    do_xlate_actions(in, n_in, ctx);
+    remove_pop_action(ctx);
 
     /* Check with in-band control to see if we're allowed to set up this
      * flow. */
-    if (!in_band_rule_check(ofproto->in_band, flow, out)) {
-        ctx.may_set_up_flow = false;
+    if (!in_band_rule_check(ctx->ofproto->in_band, &ctx->flow, &ctx->out)) {
+        ctx->may_set_up_flow = false;
     }
 
-    if (tags) {
-        *tags = ctx.tags;
-    }
-    if (may_set_up_flow) {
-        *may_set_up_flow = ctx.may_set_up_flow;
-    }
-    if (nf_output_iface) {
-        *nf_output_iface = ctx.nf_output_iface;
-    }
-    if (odp_actions_overflow(out)) {
+    if (odp_actions_overflow(&ctx->out)) {
         COVERAGE_INC(odp_overflow);
-        odp_actions_init(out);
+        odp_actions_init(&ctx->out);
         return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_TOO_MANY);
     }
     return 0;
@@ -3058,13 +3086,13 @@ reject_slave_controller(struct ofconn *ofconn, const const char *msg_type)
 }
 
 static int
-handle_packet_out(struct ofconn *ofconn, struct ofp_header *oh)
+handle_packet_out(struct ofconn *ofconn, const struct ofp_header *oh)
 {
     struct ofproto *p = ofconn->ofproto;
     struct ofp_packet_out *opo;
     struct ofpbuf payload, *buffer;
     union ofp_action *ofp_actions;
-    struct odp_actions odp_actions;
+    struct action_xlate_ctx ctx;
     struct ofpbuf request;
     struct flow flow;
     size_t n_ofp_actions;
@@ -3079,12 +3107,8 @@ handle_packet_out(struct ofconn *ofconn, struct ofp_header *oh)
     }
 
     /* Get ofp_packet_out. */
-    request.data = oh;
-    request.size = 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);
-    }
+    ofpbuf_use_const(&request, oh, ntohs(oh->length));
+    opo = ofpbuf_pull(&request, offsetof(struct ofp_packet_out, actions));
 
     /* Get actions. */
     error = ofputil_pull_actions(&request, ntohs(opo->actions_len),
@@ -3115,11 +3139,10 @@ handle_packet_out(struct ofconn *ofconn, struct ofp_header *oh)
     }
 
     /* Send. */
-    error = xlate_actions(ofp_actions, n_ofp_actions, &flow, p, &payload,
-                          &odp_actions, NULL, NULL, NULL);
+    action_xlate_ctx_init(&ctx, p, &flow, &payload);
+    error = xlate_actions(&ctx, ofp_actions, n_ofp_actions);
     if (!error) {
-        dpif_execute(p->dpif, odp_actions.actions, odp_actions.n_actions,
-                     &payload);
+        dpif_execute(p->dpif, ctx.out.actions, ctx.out.n_actions, &payload);
     }
 
 exit:
@@ -3153,10 +3176,10 @@ update_port_config(struct ofproto *p, struct ofport *port,
 }
 
 static int
-handle_port_mod(struct ofconn *ofconn, struct ofp_header *oh)
+handle_port_mod(struct ofconn *ofconn, const struct ofp_header *oh)
 {
     struct ofproto *p = ofconn->ofproto;
-    const struct ofp_port_mod *opm;
+    const struct ofp_port_mod *opm = (const struct ofp_port_mod *) oh;
     struct ofport *port;
     int error;
 
@@ -3164,11 +3187,6 @@ handle_port_mod(struct ofconn *ofconn, struct ofp_header *oh)
     if (error) {
         return error;
     }
-    error = check_ofp_message(oh, OFPT_PORT_MOD, sizeof *opm);
-    if (error) {
-        return error;
-    }
-    opm = (struct ofp_port_mod *) oh;
 
     port = get_port(p, ofp_port_to_odp_port(ntohs(opm->port_no)));
     if (!port) {
@@ -3198,9 +3216,11 @@ make_ofp_stats_reply(ovs_be32 xid, ovs_be16 type, size_t body_len)
 }
 
 static struct ofpbuf *
-start_ofp_stats_reply(const struct ofp_stats_request *request, size_t body_len)
+start_ofp_stats_reply(const struct ofp_header *request, size_t body_len)
 {
-    return make_ofp_stats_reply(request->header.xid, request->type, body_len);
+    const struct ofp_stats_request *osr
+        = (const struct ofp_stats_request *) request;
+    return make_ofp_stats_reply(osr->header.xid, osr->type, body_len);
 }
 
 static void *
@@ -3256,7 +3276,7 @@ append_nxstats_reply(size_t nbytes, struct ofconn *ofconn,
 
 static int
 handle_desc_stats_request(struct ofconn *ofconn,
-                          struct ofp_stats_request *request)
+                          const struct ofp_header *request)
 {
     struct ofproto *p = ofconn->ofproto;
     struct ofp_desc_stats *ods;
@@ -3277,7 +3297,7 @@ handle_desc_stats_request(struct ofconn *ofconn,
 
 static int
 handle_table_stats_request(struct ofconn *ofconn,
-                           struct ofp_stats_request *request)
+                           const struct ofp_header *request)
 {
     struct ofproto *p = ofconn->ofproto;
     struct ofp_table_stats *ots;
@@ -3330,21 +3350,15 @@ append_port_stat(struct ofport *port, struct ofconn *ofconn,
 }
 
 static int
-handle_port_stats_request(struct ofconn *ofconn, struct ofp_stats_request *osr,
-                          size_t arg_size)
+handle_port_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
 {
     struct ofproto *p = ofconn->ofproto;
-    struct ofp_port_stats_request *psr;
+    const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
     struct ofp_port_stats *ops;
     struct ofpbuf *msg;
     struct ofport *port;
 
-    if (arg_size != sizeof *psr) {
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-    psr = (struct ofp_port_stats_request *) osr->body;
-
-    msg = start_ofp_stats_reply(osr, sizeof *ops * 16);
+    msg = start_ofp_stats_reply(oh, sizeof *ops * 16);
     if (psr->port_no != htons(OFPP_NONE)) {
         port = get_port(p, ofp_port_to_odp_port(ntohs(psr->port_no)));
         if (port) {
@@ -3459,19 +3473,13 @@ is_valid_table(uint8_t table_id)
 }
 
 static int
-handle_flow_stats_request(struct ofconn *ofconn,
-                          const struct ofp_stats_request *osr, size_t arg_size)
+handle_flow_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
 {
-    struct ofp_flow_stats_request *fsr;
+    const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
     struct ofpbuf *reply;
 
-    if (arg_size != sizeof *fsr) {
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-    fsr = (struct ofp_flow_stats_request *) osr->body;
-
     COVERAGE_INC(ofproto_flows_req);
-    reply = start_ofp_stats_reply(osr, 1024);
+    reply = start_ofp_stats_reply(oh, 1024);
     if (is_valid_table(fsr->table_id)) {
         struct cls_cursor cursor;
         struct cls_rule target;
@@ -3529,22 +3537,25 @@ put_nx_flow_stats(struct ofconn *ofconn, struct rule *rule,
 }
 
 static int
-handle_nxst_flow(struct ofconn *ofconn, struct ofpbuf *b)
+handle_nxst_flow(struct ofconn *ofconn, const struct ofp_header *oh)
 {
     struct nx_flow_stats_request *nfsr;
     struct cls_rule target;
     struct ofpbuf *reply;
+    struct ofpbuf b;
     int error;
 
+    ofpbuf_use_const(&b, oh, ntohs(oh->length));
+
     /* Dissect the message. */
-    nfsr = ofpbuf_try_pull(b, sizeof *nfsr);
-    if (!nfsr) {
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-    error = nx_pull_match(b, ntohs(nfsr->match_len), 0, &target);
+    nfsr = ofpbuf_pull(&b, sizeof *nfsr);
+    error = nx_pull_match(&b, ntohs(nfsr->match_len), 0, &target);
     if (error) {
         return error;
     }
+    if (b.size) {
+        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
+    }
 
     COVERAGE_INC(ofproto_flows_req);
     reply = start_nxstats_reply(&nfsr->nsm, 1024);
@@ -3638,23 +3649,17 @@ query_aggregate_stats(struct ofproto *ofproto, struct cls_rule *target,
 
 static int
 handle_aggregate_stats_request(struct ofconn *ofconn,
-                               const struct ofp_stats_request *osr,
-                               size_t arg_size)
+                               const struct ofp_header *oh)
 {
-    struct ofp_aggregate_stats_request *request;
+    const struct ofp_aggregate_stats_request *request = ofputil_stats_body(oh);
     struct ofp_aggregate_stats_reply *reply;
     struct cls_rule target;
     struct ofpbuf *msg;
 
-    if (arg_size != sizeof *request) {
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-    request = (struct ofp_aggregate_stats_request *) osr->body;
-
     ofputil_cls_rule_from_match(&request->match, 0, NXFF_OPENFLOW10, 0,
                                 &target);
 
-    msg = start_ofp_stats_reply(osr, sizeof *reply);
+    msg = start_ofp_stats_reply(oh, sizeof *reply);
     reply = append_ofp_stats_reply(sizeof *reply, ofconn, &msg);
     query_aggregate_stats(ofconn->ofproto, &target, request->out_port,
                           request->table_id, reply);
@@ -3663,23 +3668,26 @@ handle_aggregate_stats_request(struct ofconn *ofconn,
 }
 
 static int
-handle_nxst_aggregate(struct ofconn *ofconn, struct ofpbuf *b)
+handle_nxst_aggregate(struct ofconn *ofconn, const struct ofp_header *oh)
 {
     struct nx_aggregate_stats_request *request;
     struct ofp_aggregate_stats_reply *reply;
     struct cls_rule target;
+    struct ofpbuf b;
     struct ofpbuf *buf;
     int error;
 
+    ofpbuf_use_const(&b, oh, ntohs(oh->length));
+
     /* Dissect the message. */
-    request = ofpbuf_try_pull(b, sizeof *request);
-    if (!request) {
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-    error = nx_pull_match(b, ntohs(request->match_len), 0, &target);
+    request = ofpbuf_pull(&b, sizeof *request);
+    error = nx_pull_match(&b, ntohs(request->match_len), 0, &target);
     if (error) {
         return error;
     }
+    if (b.size) {
+        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
+    }
 
     /* Reply. */
     COVERAGE_INC(ofproto_flows_req);
@@ -3741,26 +3749,24 @@ handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
 }
 
 static int
-handle_queue_stats_request(struct ofconn *ofconn,
-                           const struct ofp_stats_request *osr,
-                           size_t arg_size)
+handle_queue_stats_request(struct ofconn *ofconn, const struct ofp_header *oh)
 {
     struct ofproto *ofproto = ofconn->ofproto;
-    struct ofp_queue_stats_request *qsr;
+    const struct ofp_queue_stats_request *qsr;
     struct queue_stats_cbdata cbdata;
     struct ofport *port;
     unsigned int port_no;
     uint32_t queue_id;
 
-    if (arg_size != sizeof *qsr) {
+    qsr = ofputil_stats_body(oh);
+    if (!qsr) {
         return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
     }
-    qsr = (struct ofp_queue_stats_request *) osr->body;
 
     COVERAGE_INC(ofproto_queue_req);
 
     cbdata.ofconn = ofconn;
-    cbdata.msg = start_ofp_stats_reply(osr, 128);
+    cbdata.msg = start_ofp_stats_reply(oh, 128);
 
     port_no = ntohs(qsr->port_no);
     queue_id = ntohl(qsr->queue_id);
@@ -3782,85 +3788,6 @@ handle_queue_stats_request(struct ofconn *ofconn,
     return 0;
 }
 
-static int
-handle_vendor_stats_request(struct ofconn *ofconn,
-                            struct ofp_stats_request *osr, size_t arg_size)
-{
-    struct nicira_stats_msg *nsm;
-    struct ofpbuf b;
-    ovs_be32 vendor;
-
-    if (arg_size < 4) {
-        VLOG_WARN_RL(&rl, "truncated vendor stats request body");
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-
-    memcpy(&vendor, osr->body, sizeof vendor);
-    if (vendor != htonl(NX_VENDOR_ID)) {
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
-    }
-
-    if (ntohs(osr->header.length) < sizeof(struct nicira_stats_msg)) {
-        VLOG_WARN_RL(&rl, "truncated Nicira stats request");
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-
-    nsm = (struct nicira_stats_msg *) osr;
-    b.data = nsm;
-    b.size = ntohs(nsm->header.length);
-    switch (ntohl(nsm->subtype)) {
-    case NXST_FLOW:
-        return handle_nxst_flow(ofconn, &b);
-
-    case NXST_AGGREGATE:
-        return handle_nxst_aggregate(ofconn, &b);
-
-    default:
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE);
-    }
-}
-
-static int
-handle_stats_request(struct ofconn *ofconn, struct ofp_header *oh)
-{
-    struct ofp_stats_request *osr;
-    size_t arg_size;
-    int error;
-
-    error = check_ofp_message_array(oh, OFPT_STATS_REQUEST, sizeof *osr,
-                                    1, &arg_size);
-    if (error) {
-        return error;
-    }
-    osr = (struct ofp_stats_request *) oh;
-
-    switch (ntohs(osr->type)) {
-    case OFPST_DESC:
-        return handle_desc_stats_request(ofconn, osr);
-
-    case OFPST_FLOW:
-        return handle_flow_stats_request(ofconn, osr, arg_size);
-
-    case OFPST_AGGREGATE:
-        return handle_aggregate_stats_request(ofconn, osr, arg_size);
-
-    case OFPST_TABLE:
-        return handle_table_stats_request(ofconn, osr);
-
-    case OFPST_PORT:
-        return handle_port_stats_request(ofconn, osr, arg_size);
-
-    case OFPST_QUEUE:
-        return handle_queue_stats_request(ofconn, osr, arg_size);
-
-    case OFPST_VENDOR:
-        return handle_vendor_stats_request(ofconn, osr, arg_size);
-
-    default:
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
-    }
-}
-
 static long long int
 msec_from_nsec(uint64_t sec, uint32_t nsec)
 {
@@ -3899,19 +3826,6 @@ facet_update_stats(struct ofproto *ofproto, struct facet *facet,
     }
 }
 
-struct flow_mod {
-    struct cls_rule cr;
-    ovs_be64 cookie;
-    uint16_t command;
-    uint16_t idle_timeout;
-    uint16_t hard_timeout;
-    uint32_t buffer_id;
-    uint16_t out_port;
-    uint16_t flags;
-    union ofp_action *actions;
-    size_t n_actions;
-};
-
 /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
  * in which no matching flow already exists in the flow table.
  *
@@ -4122,9 +4036,10 @@ delete_flow(struct ofproto *p, struct rule *rule, ovs_be16 out_port)
 }
 \f
 static int
-flow_mod_core(struct ofconn *ofconn, struct flow_mod *fm)
+handle_flow_mod(struct ofconn *ofconn, const struct ofp_header *oh)
 {
     struct ofproto *p = ofconn->ofproto;
+    struct flow_mod fm;
     int error;
 
     error = reject_slave_controller(ofconn, "flow_mod");
@@ -4132,36 +4047,41 @@ flow_mod_core(struct ofconn *ofconn, struct flow_mod *fm)
         return error;
     }
 
-    error = validate_actions(fm->actions, fm->n_actions,
-                             &fm->cr.flow, p->max_ports);
+    error = ofputil_decode_flow_mod(&fm, oh, ofconn->flow_format);
     if (error) {
         return error;
     }
 
-    /* We do not support the emergency flow cache.  It will hopefully
-     * get dropped from OpenFlow in the near future. */
-    if (fm->flags & OFPFF_EMERG) {
+    /* We do not support the emergency flow cache.  It will hopefully get
+     * dropped from OpenFlow in the near future. */
+    if (fm.flags & OFPFF_EMERG) {
         /* There isn't a good fit for an error code, so just state that the
          * flow table is full. */
         return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
     }
 
-    switch (fm->command) {
+    error = validate_actions(fm.actions, fm.n_actions,
+                             &fm.cr.flow, p->max_ports);
+    if (error) {
+        return error;
+    }
+
+    switch (fm.command) {
     case OFPFC_ADD:
-        return add_flow(ofconn, fm);
+        return add_flow(ofconn, &fm);
 
     case OFPFC_MODIFY:
-        return modify_flows_loose(ofconn, fm);
+        return modify_flows_loose(ofconn, &fm);
 
     case OFPFC_MODIFY_STRICT:
-        return modify_flow_strict(ofconn, fm);
+        return modify_flow_strict(ofconn, &fm);
 
     case OFPFC_DELETE:
-        delete_flows_loose(p, fm);
+        delete_flows_loose(p, &fm);
         return 0;
 
     case OFPFC_DELETE_STRICT:
-        delete_flow_strict(p, fm);
+        delete_flow_strict(p, &fm);
         return 0;
 
     default:
@@ -4170,129 +4090,23 @@ flow_mod_core(struct ofconn *ofconn, struct flow_mod *fm)
 }
 
 static int
-handle_ofpt_flow_mod(struct ofconn *ofconn, struct ofp_header *oh)
-{
-    struct ofp_match orig_match;
-    struct ofp_flow_mod *ofm;
-    struct flow_mod fm;
-    struct ofpbuf b;
-    int error;
-
-    b.data = oh;
-    b.size = ntohs(oh->length);
-
-    /* Dissect the message. */
-    ofm = ofpbuf_try_pull(&b, sizeof *ofm);
-    if (!ofm) {
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-    error = ofputil_pull_actions(&b, b.size, &fm.actions, &fm.n_actions);
-    if (error) {
-        return error;
-    }
-
-    /* Normalize ofm->match.  If normalization actually changes anything, then
-     * log the differences. */
-    ofm->match.pad1[0] = ofm->match.pad2[0] = 0;
-    orig_match = ofm->match;
-    normalize_match(&ofm->match);
-    if (memcmp(&ofm->match, &orig_match, sizeof orig_match)) {
-        static struct vlog_rate_limit normal_rl = VLOG_RATE_LIMIT_INIT(1, 1);
-        if (!VLOG_DROP_INFO(&normal_rl)) {
-            char *old = ofp_match_to_literal_string(&orig_match);
-            char *new = ofp_match_to_literal_string(&ofm->match);
-            VLOG_INFO("%s: normalization changed ofp_match, details:",
-                      rconn_get_name(ofconn->rconn));
-            VLOG_INFO(" pre: %s", old);
-            VLOG_INFO("post: %s", new);
-            free(old);
-            free(new);
-        }
-    }
-
-    /* Translate the message. */
-    ofputil_cls_rule_from_match(&ofm->match, ntohs(ofm->priority),
-                                ofconn->flow_format, ofm->cookie, &fm.cr);
-    fm.cookie = ofm->cookie;
-    fm.command = ntohs(ofm->command);
-    fm.idle_timeout = ntohs(ofm->idle_timeout);
-    fm.hard_timeout = ntohs(ofm->hard_timeout);
-    fm.buffer_id = ntohl(ofm->buffer_id);
-    fm.out_port = ntohs(ofm->out_port);
-    fm.flags = ntohs(ofm->flags);
-
-    /* Execute the command. */
-    return flow_mod_core(ofconn, &fm);
-}
-
-static int
-handle_nxt_flow_mod(struct ofconn *ofconn, struct ofp_header *oh)
-{
-    struct nx_flow_mod *nfm;
-    struct flow_mod fm;
-    struct ofpbuf b;
-    int error;
-
-    b.data = oh;
-    b.size = ntohs(oh->length);
-
-    /* Dissect the message. */
-    nfm = ofpbuf_try_pull(&b, sizeof *nfm);
-    if (!nfm) {
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-    error = nx_pull_match(&b, ntohs(nfm->match_len), ntohs(nfm->priority),
-                          &fm.cr);
-    if (error) {
-        return error;
-    }
-    error = ofputil_pull_actions(&b, b.size, &fm.actions, &fm.n_actions);
-    if (error) {
-        return error;
-    }
-
-    /* Translate the message. */
-    fm.cookie = nfm->cookie;
-    fm.command = ntohs(nfm->command);
-    fm.idle_timeout = ntohs(nfm->idle_timeout);
-    fm.hard_timeout = ntohs(nfm->hard_timeout);
-    fm.buffer_id = ntohl(nfm->buffer_id);
-    fm.out_port = ntohs(nfm->out_port);
-    fm.flags = ntohs(nfm->flags);
-
-    /* Execute the command. */
-    return flow_mod_core(ofconn, &fm);
-}
-
-static int
-handle_tun_id_from_cookie(struct ofconn *ofconn, struct nxt_tun_id_cookie *msg)
+handle_tun_id_from_cookie(struct ofconn *ofconn, const struct ofp_header *oh)
 {
-    int error;
-
-    error = check_ofp_message(&msg->header, OFPT_VENDOR, sizeof *msg);
-    if (error) {
-        return error;
-    }
+    const struct nxt_tun_id_cookie *msg
+        = (const struct nxt_tun_id_cookie *) oh;
 
     ofconn->flow_format = msg->set ? NXFF_TUN_ID_FROM_COOKIE : NXFF_OPENFLOW10;
     return 0;
 }
 
 static int
-handle_role_request(struct ofconn *ofconn, struct nicira_header *msg)
+handle_role_request(struct ofconn *ofconn, const struct ofp_header *oh)
 {
-    struct nx_role_request *nrr;
+    struct nx_role_request *nrr = (struct nx_role_request *) oh;
     struct nx_role_request *reply;
     struct ofpbuf *buf;
     uint32_t role;
 
-    if (ntohs(msg->header.length) != sizeof *nrr) {
-        VLOG_WARN_RL(&rl, "received role request of length %u (expected %zu)",
-                     ntohs(msg->header.length), sizeof *nrr);
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-    nrr = (struct nx_role_request *) msg;
-
     if (ofconn->type != OFCONN_PRIMARY) {
         VLOG_WARN_RL(&rl, "ignoring role request on non-controller "
                      "connection");
@@ -4319,8 +4133,7 @@ handle_role_request(struct ofconn *ofconn, struct nicira_header *msg)
     }
     ofconn->role = role;
 
-    reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, msg->header.xid,
-                           &buf);
+    reply = make_nxmsg_xid(sizeof *reply, NXT_ROLE_REPLY, oh->xid, &buf);
     reply->role = htonl(role);
     queue_tx(buf, ofconn, ofconn->reply_counter);
 
@@ -4328,16 +4141,11 @@ handle_role_request(struct ofconn *ofconn, struct nicira_header *msg)
 }
 
 static int
-handle_nxt_set_flow_format(struct ofconn *ofconn,
-                           struct nxt_set_flow_format *msg)
+handle_nxt_set_flow_format(struct ofconn *ofconn, const struct ofp_header *oh)
 {
+    const struct nxt_set_flow_format *msg
+        = (const struct nxt_set_flow_format *) oh;
     uint32_t format;
-    int error;
-
-    error = check_ofp_message(&msg->header, OFPT_VENDOR, sizeof *msg);
-    if (error) {
-        return error;
-    }
 
     format = ntohl(msg->format);
     if (format == NXFF_OPENFLOW10
@@ -4351,52 +4159,7 @@ handle_nxt_set_flow_format(struct ofconn *ofconn,
 }
 
 static int
-handle_vendor(struct ofconn *ofconn, void *msg)
-{
-    struct ofproto *p = ofconn->ofproto;
-    struct ofp_vendor_header *ovh = msg;
-    struct nicira_header *nh;
-
-    if (ntohs(ovh->header.length) < sizeof(struct ofp_vendor_header)) {
-        VLOG_WARN_RL(&rl, "received vendor message of length %u "
-                          "(expected at least %zu)",
-                   ntohs(ovh->header.length), sizeof(struct ofp_vendor_header));
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-    if (ovh->vendor != htonl(NX_VENDOR_ID)) {
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR);
-    }
-    if (ntohs(ovh->header.length) < sizeof(struct nicira_header)) {
-        VLOG_WARN_RL(&rl, "received Nicira vendor message of length %u "
-                          "(expected at least %zu)",
-                     ntohs(ovh->header.length), sizeof(struct nicira_header));
-        return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
-    }
-
-    nh = msg;
-    switch (ntohl(nh->subtype)) {
-    case NXT_STATUS_REQUEST:
-        return switch_status_handle_request(p->switch_status, ofconn->rconn,
-                                            msg);
-
-    case NXT_TUN_ID_FROM_COOKIE:
-        return handle_tun_id_from_cookie(ofconn, msg);
-
-    case NXT_ROLE_REQUEST:
-        return handle_role_request(ofconn, msg);
-
-    case NXT_SET_FLOW_FORMAT:
-        return handle_nxt_set_flow_format(ofconn, msg);
-
-    case NXT_FLOW_MOD:
-        return handle_nxt_flow_mod(ofconn, &ovh->header);
-    }
-
-    return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE);
-}
-
-static int
-handle_barrier_request(struct ofconn *ofconn, struct ofp_header *oh)
+handle_barrier_request(struct ofconn *ofconn, const struct ofp_header *oh)
 {
     struct ofp_header *ob;
     struct ofpbuf *buf;
@@ -4408,71 +4171,135 @@ handle_barrier_request(struct ofconn *ofconn, struct ofp_header *oh)
     return 0;
 }
 
-static void
-handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
+static int
+handle_openflow__(struct ofconn *ofconn, const struct ofpbuf *msg)
 {
-    struct ofp_header *oh = ofp_msg->data;
+    const struct ofp_header *oh = msg->data;
+    const struct ofputil_msg_type *type;
     int error;
 
-    COVERAGE_INC(ofproto_recv_openflow);
-    switch (oh->type) {
-    case OFPT_ECHO_REQUEST:
-        error = handle_echo_request(ofconn, oh);
-        break;
-
-    case OFPT_ECHO_REPLY:
-        error = 0;
-        break;
+    error = ofputil_decode_msg_type(oh, &type);
+    if (error) {
+        return error;
+    }
 
-    case OFPT_FEATURES_REQUEST:
-        error = handle_features_request(ofconn, oh);
-        break;
+    switch (ofputil_msg_type_code(type)) {
+        /* OpenFlow requests. */
+    case OFPUTIL_OFPT_ECHO_REQUEST:
+        return handle_echo_request(ofconn, oh);
 
-    case OFPT_GET_CONFIG_REQUEST:
-        error = handle_get_config_request(ofconn, oh);
-        break;
+    case OFPUTIL_OFPT_FEATURES_REQUEST:
+        return handle_features_request(ofconn, oh);
 
-    case OFPT_SET_CONFIG:
-        error = handle_set_config(ofconn, ofp_msg->data);
-        break;
+    case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
+        return handle_get_config_request(ofconn, oh);
 
-    case OFPT_PACKET_OUT:
-        error = handle_packet_out(ofconn, ofp_msg->data);
-        break;
+    case OFPUTIL_OFPT_SET_CONFIG:
+        return handle_set_config(ofconn, msg->data);
 
-    case OFPT_PORT_MOD:
-        error = handle_port_mod(ofconn, oh);
-        break;
+    case OFPUTIL_OFPT_PACKET_OUT:
+        return handle_packet_out(ofconn, oh);
 
-    case OFPT_FLOW_MOD:
-        error = handle_ofpt_flow_mod(ofconn, ofp_msg->data);
-        break;
+    case OFPUTIL_OFPT_PORT_MOD:
+        return handle_port_mod(ofconn, oh);
 
-    case OFPT_STATS_REQUEST:
-        error = handle_stats_request(ofconn, oh);
-        break;
+    case OFPUTIL_OFPT_FLOW_MOD:
+        return handle_flow_mod(ofconn, oh);
 
-    case OFPT_VENDOR:
-        error = handle_vendor(ofconn, ofp_msg->data);
-        break;
+    case OFPUTIL_OFPT_BARRIER_REQUEST:
+        return handle_barrier_request(ofconn, oh);
 
-    case OFPT_BARRIER_REQUEST:
-        error = handle_barrier_request(ofconn, oh);
-        break;
+        /* OpenFlow replies. */
+    case OFPUTIL_OFPT_ECHO_REPLY:
+        return 0;
 
+        /* Nicira extension requests. */
+    case OFPUTIL_NXT_STATUS_REQUEST:
+        return switch_status_handle_request(
+            ofconn->ofproto->switch_status, ofconn->rconn, oh);
+
+    case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
+        return handle_tun_id_from_cookie(ofconn, oh);
+
+    case OFPUTIL_NXT_ROLE_REQUEST:
+        return handle_role_request(ofconn, oh);
+
+    case OFPUTIL_NXT_SET_FLOW_FORMAT:
+        return handle_nxt_set_flow_format(ofconn, oh);
+
+    case OFPUTIL_NXT_FLOW_MOD:
+        return handle_flow_mod(ofconn, oh);
+
+        /* OpenFlow statistics requests. */
+    case OFPUTIL_OFPST_DESC_REQUEST:
+        return handle_desc_stats_request(ofconn, oh);
+
+    case OFPUTIL_OFPST_FLOW_REQUEST:
+        return handle_flow_stats_request(ofconn, oh);
+
+    case OFPUTIL_OFPST_AGGREGATE_REQUEST:
+        return handle_aggregate_stats_request(ofconn, oh);
+
+    case OFPUTIL_OFPST_TABLE_REQUEST:
+        return handle_table_stats_request(ofconn, oh);
+
+    case OFPUTIL_OFPST_PORT_REQUEST:
+        return handle_port_stats_request(ofconn, oh);
+
+    case OFPUTIL_OFPST_QUEUE_REQUEST:
+        return handle_queue_stats_request(ofconn, oh);
+
+        /* Nicira extension statistics requests. */
+    case OFPUTIL_NXST_FLOW_REQUEST:
+        return handle_nxst_flow(ofconn, oh);
+
+    case OFPUTIL_NXST_AGGREGATE_REQUEST:
+        return handle_nxst_aggregate(ofconn, oh);
+
+    case OFPUTIL_INVALID:
+    case OFPUTIL_OFPT_HELLO:
+    case OFPUTIL_OFPT_ERROR:
+    case OFPUTIL_OFPT_FEATURES_REPLY:
+    case OFPUTIL_OFPT_GET_CONFIG_REPLY:
+    case OFPUTIL_OFPT_PACKET_IN:
+    case OFPUTIL_OFPT_FLOW_REMOVED:
+    case OFPUTIL_OFPT_PORT_STATUS:
+    case OFPUTIL_OFPT_BARRIER_REPLY:
+    case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
+    case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
+    case OFPUTIL_OFPST_DESC_REPLY:
+    case OFPUTIL_OFPST_FLOW_REPLY:
+    case OFPUTIL_OFPST_QUEUE_REPLY:
+    case OFPUTIL_OFPST_PORT_REPLY:
+    case OFPUTIL_OFPST_TABLE_REPLY:
+    case OFPUTIL_OFPST_AGGREGATE_REPLY:
+    case OFPUTIL_NXT_STATUS_REPLY:
+    case OFPUTIL_NXT_ROLE_REPLY:
+    case OFPUTIL_NXT_FLOW_REMOVED:
+    case OFPUTIL_NXST_FLOW_REPLY:
+    case OFPUTIL_NXST_AGGREGATE_REPLY:
     default:
         if (VLOG_IS_WARN_ENABLED()) {
             char *s = ofp_to_string(oh, ntohs(oh->length), 2);
             VLOG_DBG_RL(&rl, "OpenFlow message ignored: %s", s);
             free(s);
         }
-        error = ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
-        break;
+        if (oh->type == OFPT_STATS_REQUEST || oh->type == OFPT_STATS_REPLY) {
+            return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT);
+        } else {
+            return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE);
+        }
     }
+}
 
+static void
+handle_openflow(struct ofconn *ofconn, struct ofpbuf *ofp_msg)
+{
+    int error = handle_openflow__(ofconn, ofp_msg);
     if (error) {
         send_error_oh(ofconn, ofp_msg->data, error);
     }
+    COVERAGE_INC(ofproto_recv_openflow);
 }
 \f
 static void
@@ -4483,8 +4310,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;
@@ -4949,8 +4775,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);
     }
 
@@ -5085,6 +4912,172 @@ pick_fallback_dpid(void)
     return eth_addr_to_uint64(ea);
 }
 \f
+static void
+ofproto_unixctl_list(struct unixctl_conn *conn, const char *arg OVS_UNUSED,
+                     void *aux OVS_UNUSED)
+{
+    const struct shash_node *node;
+    struct ds results;
+
+    ds_init(&results);
+    SHASH_FOR_EACH (node, &all_ofprotos) {
+        ds_put_format(&results, "%s\n", node->name);
+    }
+    unixctl_command_reply(conn, 200, ds_cstr(&results));
+    ds_destroy(&results);
+}
+
+struct ofproto_trace {
+    struct action_xlate_ctx ctx;
+    struct flow flow;
+    struct ds *result;
+};
+
+static void
+trace_format_rule(struct ds *result, int level, const struct rule *rule)
+{
+    ds_put_char_multiple(result, '\t', level);
+    if (!rule) {
+        ds_put_cstr(result, "No match\n");
+        return;
+    }
+
+    ds_put_format(result, "Rule: cookie=%#"PRIx64" ",
+                  ntohll(rule->flow_cookie));
+    cls_rule_format(&rule->cr, result);
+    ds_put_char(result, '\n');
+
+    ds_put_char_multiple(result, '\t', level);
+    ds_put_cstr(result, "OpenFlow ");
+    ofp_print_actions(result, (const struct ofp_action_header *) rule->actions,
+                      rule->n_actions * sizeof *rule->actions);
+    ds_put_char(result, '\n');
+}
+
+static void
+trace_format_flow(struct ds *result, int level, const char *title,
+                 struct ofproto_trace *trace)
+{
+    ds_put_char_multiple(result, '\t', level);
+    ds_put_format(result, "%s: ", title);
+    if (flow_equal(&trace->ctx.flow, &trace->flow)) {
+        ds_put_cstr(result, "unchanged");
+    } else {
+        flow_format(result, &trace->ctx.flow);
+        trace->flow = trace->ctx.flow;
+    }
+    ds_put_char(result, '\n');
+}
+
+static void
+trace_resubmit(struct action_xlate_ctx *ctx, const struct rule *rule)
+{
+    struct ofproto_trace *trace = CONTAINER_OF(ctx, struct ofproto_trace, ctx);
+    struct ds *result = trace->result;
+
+    ds_put_char(result, '\n');
+    trace_format_flow(result, ctx->recurse + 1, "Resubmitted flow", trace);
+    trace_format_rule(result, ctx->recurse + 1, rule);
+}
+
+static void
+ofproto_unixctl_trace(struct unixctl_conn *conn, const char *args_,
+                      void *aux OVS_UNUSED)
+{
+    char *dpname, *in_port_s, *tun_id_s, *packet_s;
+    char *args = xstrdup(args_);
+    char *save_ptr = NULL;
+    struct ofproto *ofproto;
+    struct ofpbuf packet;
+    struct rule *rule;
+    struct ds result;
+    struct flow flow;
+    uint16_t in_port;
+    ovs_be32 tun_id;
+    char *s;
+
+    ofpbuf_init(&packet, strlen(args) / 2);
+    ds_init(&result);
+
+    dpname = strtok_r(args, " ", &save_ptr);
+    tun_id_s = strtok_r(NULL, " ", &save_ptr);
+    in_port_s = strtok_r(NULL, " ", &save_ptr);
+    packet_s = strtok_r(NULL, "", &save_ptr); /* Get entire rest of line. */
+    if (!dpname || !in_port_s || !packet_s) {
+        unixctl_command_reply(conn, 501, "Bad command syntax");
+        goto exit;
+    }
+
+    ofproto = shash_find_data(&all_ofprotos, dpname);
+    if (!ofproto) {
+        unixctl_command_reply(conn, 501, "Unknown ofproto (use ofproto/list "
+                              "for help)");
+        goto exit;
+    }
+
+    tun_id = ntohl(strtoul(tun_id_s, NULL, 10));
+    in_port = ofp_port_to_odp_port(atoi(in_port_s));
+
+    packet_s = ofpbuf_put_hex(&packet, packet_s, NULL);
+    packet_s += strspn(packet_s, " ");
+    if (*packet_s != '\0') {
+        unixctl_command_reply(conn, 501, "Trailing garbage in command");
+        goto exit;
+    }
+    if (packet.size < ETH_HEADER_LEN) {
+        unixctl_command_reply(conn, 501, "Packet data too short for Ethernet");
+        goto exit;
+    }
+
+    ds_put_cstr(&result, "Packet: ");
+    s = ofp_packet_to_string(packet.data, packet.size, packet.size);
+    ds_put_cstr(&result, s);
+    free(s);
+
+    flow_extract(&packet, tun_id, in_port, &flow);
+    ds_put_cstr(&result, "Flow: ");
+    flow_format(&result, &flow);
+    ds_put_char(&result, '\n');
+
+    rule = rule_lookup(ofproto, &flow);
+    trace_format_rule(&result, 0, rule);
+    if (rule) {
+        struct ofproto_trace trace;
+
+        trace.result = &result;
+        trace.flow = flow;
+        action_xlate_ctx_init(&trace.ctx, ofproto, &flow, &packet);
+        trace.ctx.resubmit_hook = trace_resubmit;
+        xlate_actions(&trace.ctx, rule->actions, rule->n_actions);
+
+        ds_put_char(&result, '\n');
+        trace_format_flow(&result, 0, "Final flow", &trace);
+        ds_put_cstr(&result, "Datapath actions: ");
+        format_odp_actions(&result,
+                           trace.ctx.out.actions, trace.ctx.out.n_actions);
+    }
+
+    unixctl_command_reply(conn, 200, ds_cstr(&result));
+
+exit:
+    ds_destroy(&result);
+    ofpbuf_uninit(&packet);
+    free(args);
+}
+
+static void
+ofproto_unixctl_init(void)
+{
+    static bool registered;
+    if (registered) {
+        return;
+    }
+    registered = true;
+
+    unixctl_command_register("ofproto/list", ofproto_unixctl_list, NULL);
+    unixctl_command_register("ofproto/trace", ofproto_unixctl_trace, NULL);
+}
+\f
 static bool
 default_normal_ofhook_cb(const struct flow *flow, const struct ofpbuf *packet,
                          struct odp_actions *actions, tag_type *tags,