Added lookup count to ofp_table_stats.
[sliver-openvswitch.git] / switch / datapath.c
index 607347d..5d4590f 100644 (file)
@@ -48,6 +48,7 @@
 #include "packets.h"
 #include "poll-loop.h"
 #include "rconn.h"
+#include "stp.h"
 #include "switch-flow.h"
 #include "table.h"
 #include "timeval.h"
 #define THIS_MODULE VLM_datapath
 #include "vlog.h"
 
-enum br_port_flags {
-    BRPF_NO_FLOOD = 1 << 0,
-};
-
-enum br_port_status {
-    BRPS_PORT_DOWN = 1 << 0,
-    BRPS_LINK_DOWN = 1 << 1,
-};
-
 extern char mfr_desc;
 extern char hw_desc;
 extern char sw_desc;
@@ -87,9 +79,12 @@ extern char serial_num;
                                 | (1 << OFPAT_SET_TP_SRC)   \
                                 | (1 << OFPAT_SET_TP_DST) )
 
+#define PORT_STATUS_BITS (OFPPFL_PORT_DOWN | OFPPFL_LINK_DOWN)
+#define PORT_FLAG_BITS (~PORT_STATUS_BITS)
+
 struct sw_port {
-    uint32_t flags;                    /* BRPF_* flags */
-    uint32_t status;                   /* BRPS_* flags */
+    uint32_t flags;             /* Some subset of PORT_FLAG_BITS. */
+    uint32_t status;            /* Some subset of PORT_STATUS_BITS. */
     struct datapath *dp;
     struct netdev *netdev;
     struct list node; /* Element in datapath.ports. */
@@ -125,7 +120,7 @@ struct datapath {
     /* Remote connections. */
     struct remote *controller;  /* Connection to controller. */
     struct list remotes;        /* All connections (including controller). */
-    struct vconn *listen_vconn;
+    struct pvconn *listen_pvconn;
 
     time_t last_timeout;
 
@@ -151,7 +146,7 @@ static void remote_wait(struct remote *);
 static void remote_destroy(struct remote *);
 
 void dp_output_port(struct datapath *, struct ofpbuf *,
-                    int in_port, int out_port);
+                    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 ofpbuf *, int in_port,
                        size_t max_len, int reason);
@@ -162,7 +157,8 @@ 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 ofpbuf *,
                             int in_port, const struct sw_flow_key *,
-                            const struct ofp_action *, int n_actions);
+                            const struct ofp_action *, int n_actions,
+                            bool ignore_no_fwd);
 static void modify_vlan(struct ofpbuf *buffer, const struct sw_flow_key *key,
                         const struct ofp_action *a);
 static void modify_nh(struct ofpbuf *buffer, uint16_t eth_proto,
@@ -181,8 +177,9 @@ static void modify_th(struct ofpbuf *buffer, uint16_t eth_proto,
 
 #define PKT_COOKIE_BITS (32 - PKT_BUFFER_BITS)
 
-int run_flow_through_tables(struct datapath *, struct ofpbuf *, int in_port);
-void fwd_port_input(struct datapath *, struct ofpbuf *, int in_port);
+int run_flow_through_tables(struct datapath *, struct ofpbuf *,
+                            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);
 
@@ -218,7 +215,7 @@ dp_new(struct datapath **dp_, uint64_t dpid, struct rconn *rconn)
     dp->last_timeout = time_now();
     list_init(&dp->remotes);
     dp->controller = remote_create(dp, rconn);
-    dp->listen_vconn = NULL;
+    dp->listen_pvconn = NULL;
     dp->id = dpid <= UINT64_C(0xffffffffffff) ? dpid : gen_datapath_id();
     dp->chain = chain_create();
     if (!dp->chain) {
@@ -283,10 +280,10 @@ dp_add_port(struct datapath *dp, const char *name)
 }
 
 void
-dp_add_listen_vconn(struct datapath *dp, struct vconn *listen_vconn)
+dp_add_listen_pvconn(struct datapath *dp, struct pvconn *listen_pvconn)
 {
-    assert(!dp->listen_vconn);
-    dp->listen_vconn = listen_vconn;
+    assert(!dp->listen_pvconn);
+    dp->listen_pvconn = listen_pvconn;
 }
 
 void
@@ -328,13 +325,13 @@ dp_run(struct datapath *dp)
             const int hard_header = VLAN_ETH_HEADER_LEN;
             const int mtu = netdev_get_mtu(p->netdev);
             buffer = ofpbuf_new(headroom + hard_header + mtu);
-            buffer->data += headroom;
+            buffer->data = (char*)buffer->data + headroom;
         }
         error = netdev_recv(p->netdev, buffer);
         if (!error) {
             p->rx_packets++;
             p->rx_bytes += buffer->size;
-            fwd_port_input(dp, buffer, port_no(dp, p));
+            fwd_port_input(dp, buffer, p);
             buffer = NULL;
         } else if (error != EAGAIN) {
             VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
@@ -347,12 +344,12 @@ dp_run(struct datapath *dp)
     LIST_FOR_EACH_SAFE (r, rn, struct remote, node, &dp->remotes) {
         remote_run(dp, r);
     }
-    if (dp->listen_vconn) {
+    if (dp->listen_pvconn) {
         for (;;) {
             struct vconn *new_vconn;
             int retval;
 
-            retval = vconn_accept(dp->listen_vconn, &new_vconn);
+            retval = pvconn_accept(dp->listen_pvconn, OFP_VERSION, &new_vconn);
             if (retval) {
                 if (retval != EAGAIN) {
                     VLOG_WARN_RL(&rl, "accept failed (%s)", strerror(retval));
@@ -485,8 +482,8 @@ dp_wait(struct datapath *dp)
     LIST_FOR_EACH (r, struct remote, node, &dp->remotes) {
         remote_wait(r);
     }
-    if (dp->listen_vconn) {
-        vconn_accept_wait(dp->listen_vconn);
+    if (dp->listen_pvconn) {
+        pvconn_wait(dp->listen_pvconn);
     }
 }
 
@@ -530,16 +527,17 @@ output_all(struct datapath *dp, struct ofpbuf *buffer, int in_port, int flood)
         if (port_no(dp, p) == in_port) {
             continue;
         }
-        if (flood && p->flags & BRPF_NO_FLOOD) {
+        if (flood && p->flags & OFPPFL_NO_FLOOD) {
             continue;
         }
         if (prev_port != -1) {
-            dp_output_port(dp, ofpbuf_clone(buffer), in_port, prev_port);
+            dp_output_port(dp, ofpbuf_clone(buffer), in_port, prev_port,
+                           false);
         }
         prev_port = port_no(dp, p);
     }
     if (prev_port != -1)
-        dp_output_port(dp, buffer, in_port, prev_port);
+        dp_output_port(dp, buffer, in_port, prev_port, false);
     else
         ofpbuf_delete(buffer);
 
@@ -551,7 +549,7 @@ 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];
-        if (p->netdev != NULL && !(p->status & BRPS_PORT_DOWN)) {
+        if (p->netdev != NULL && !(p->status & OFPPFL_PORT_DOWN)) {
             if (!netdev_send(p->netdev, buffer)) {
                 p->tx_packets++;
                 p->tx_bytes += buffer->size;
@@ -570,7 +568,7 @@ output_packet(struct datapath *dp, struct ofpbuf *buffer, int out_port)
  */
 void
 dp_output_port(struct datapath *dp, struct ofpbuf *buffer,
-               int in_port, int out_port)
+               int in_port, int out_port, bool ignore_no_fwd)
 {
 
     assert(buffer);
@@ -583,7 +581,8 @@ dp_output_port(struct datapath *dp, struct ofpbuf *buffer,
     } else if (out_port == OFPP_IN_PORT) {
         output_packet(dp, buffer, in_port);
     } else if (out_port == OFPP_TABLE) {
-               if (run_flow_through_tables(dp, buffer, in_port)) {
+        struct sw_port *p = in_port < OFPP_MAX ? &dp->ports[in_port] : 0;
+               if (run_flow_through_tables(dp, buffer, p)) {
                        ofpbuf_delete(buffer);
         }
     } else {
@@ -636,7 +635,7 @@ dp_output_control(struct datapath *dp, struct ofpbuf *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;
     }
 
@@ -664,14 +663,7 @@ static void fill_port_desc(struct datapath *dp, struct sw_port *p,
     desc->flags = 0;
     desc->features = htonl(netdev_get_features(p->netdev));
     desc->speed = htonl(netdev_get_speed(p->netdev));
-
-    if (p->flags & BRPF_NO_FLOOD) {
-        desc->flags |= htonl(OFPPFL_NO_FLOOD);
-    } else if (p->status & BRPS_PORT_DOWN) {
-        desc->flags |= htonl(OFPPFL_PORT_DOWN);
-    } else if (p->status & BRPS_LINK_DOWN) { 
-        desc->flags |= htonl(OFPPFL_LINK_DOWN);
-    }
+    desc->flags = htonl(p->flags | p->status);
 }
 
 static void
@@ -683,14 +675,11 @@ dp_send_features_reply(struct datapath *dp, const struct sender *sender)
 
     ofr = make_openflow_reply(sizeof *ofr, OFPT_FEATURES_REPLY,
                                sender, &buffer);
-    ofr->datapath_id    = htonll(dp->id); 
-    ofr->n_exact        = htonl(2 * TABLE_HASH_MAX_FLOWS);
-    ofr->n_compression  = 0;         /* Not supported */
-    ofr->n_general      = htonl(TABLE_LINEAR_MAX_FLOWS);
-    ofr->buffer_mb      = htonl(UINT32_MAX);
-    ofr->n_buffers      = htonl(N_PKT_BUFFERS);
-    ofr->capabilities   = htonl(OFP_SUPPORTED_CAPABILITIES);
-    ofr->actions        = htonl(OFP_SUPPORTED_ACTIONS);
+    ofr->datapath_id  = htonll(dp->id); 
+    ofr->n_tables     = dp->chain->n_tables;
+    ofr->n_buffers    = htonl(N_PKT_BUFFERS);
+    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 = ofpbuf_put_uninit(buffer, sizeof *opp);
         memset(opp, 0, sizeof *opp);
@@ -706,6 +695,7 @@ dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
     int port_no = ntohs(opp->port_no);
     if (port_no < OFPP_MAX) {
         struct sw_port *p = &dp->ports[port_no];
+        uint32_t flag_mask;
 
         /* Make sure the port id hasn't changed since this was sent */
         if (!p || memcmp(opp->hw_addr, netdev_get_etheraddr(p->netdev),
@@ -714,21 +704,20 @@ dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
         }
 
 
-        if (opm->mask & htonl(OFPPFL_NO_FLOOD)) {
-            if (opp->flags & htonl(OFPPFL_NO_FLOOD))
-                p->flags |= BRPF_NO_FLOOD;
-            else 
-                p->flags &= ~BRPF_NO_FLOOD;
+        flag_mask = ntohl(opm->mask) & PORT_FLAG_BITS;
+        if (flag_mask) {
+            p->flags &= ~flag_mask;
+            p->flags |= ntohl(opp->flags) & flag_mask;
         }
 
         if (opm->mask & htonl(OFPPFL_PORT_DOWN)) {
             if ((opp->flags & htonl(OFPPFL_PORT_DOWN))
-                            && (p->status & BRPS_PORT_DOWN) == 0) {
-                p->status |= BRPS_PORT_DOWN;
+                && (p->status & OFPPFL_PORT_DOWN) == 0) {
+                p->status |= OFPPFL_PORT_DOWN;
                 netdev_turn_flags_off(p->netdev, NETDEV_UP, true);
             } else if ((opp->flags & htonl(OFPPFL_PORT_DOWN)) == 0
-                            && (p->status & BRPS_PORT_DOWN)) {
-                p->status &= ~BRPS_PORT_DOWN;
+                       && (p->status & OFPPFL_PORT_DOWN)) {
+                p->status &= ~OFPPFL_PORT_DOWN;
                 netdev_turn_flags_on(p->netdev, NETDEV_UP, true);
             }
         }
@@ -754,9 +743,9 @@ update_port_status(struct sw_port *p)
         return 0;
     } else {
         if (flags & NETDEV_UP) {
-            p->status &= ~BRPS_PORT_DOWN;
+            p->status &= ~OFPPFL_PORT_DOWN;
         } else {
-            p->status |= BRPS_PORT_DOWN;
+            p->status |= OFPPFL_PORT_DOWN;
         } 
     }
 
@@ -764,9 +753,9 @@ update_port_status(struct sw_port *p)
      * error. */
     retval = netdev_get_link_status(p->netdev);
     if (retval == 1) {
-        p->status &= ~BRPS_LINK_DOWN;
+        p->status &= ~OFPPFL_LINK_DOWN;
     } else if (retval == 0) {
-        p->status |= BRPS_LINK_DOWN;
+        p->status |= OFPPFL_LINK_DOWN;
     } 
 
     return (orig_status != p->status);
@@ -807,12 +796,11 @@ send_flow_expired(struct datapath *dp, struct sw_flow *flow,
 
 void
 dp_send_error_msg(struct datapath *dp, const struct sender *sender,
-        uint16_t type, uint16_t code, const uint8_t *data, size_t len)
+                  uint16_t type, uint16_t code, const void *data, size_t len)
 {
     struct ofpbuf *buffer;
     struct ofp_error_msg *oem;
-    oem = make_openflow_reply(sizeof(*oem)+len, OFPT_ERROR_MSG, 
-                              sender, &buffer);
+    oem = make_openflow_reply(sizeof(*oem)+len, OFPT_ERROR, sender, &buffer);
     oem->type = htons(type);
     oem->code = htons(code);
     memcpy(oem->data, data, len);
@@ -824,7 +812,7 @@ 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;
+    int length = sizeof *ofs + sizeof *ofs->actions * flow->sf_acts->n_actions;
     ofs = ofpbuf_put_uninit(buffer, length);
     ofs->length          = htons(length);
     ofs->table_id        = table_idx;
@@ -848,57 +836,65 @@ fill_flow_stats(struct ofpbuf *buffer, struct sw_flow *flow,
     memset(ofs->pad2, 0, sizeof ofs->pad2);
     ofs->packet_count    = htonll(flow->packet_count);
     ofs->byte_count      = htonll(flow->byte_count);
-    memcpy(ofs->actions, flow->actions,
-           sizeof *ofs->actions * flow->n_actions);
+    memcpy(ofs->actions, flow->sf_acts->actions,
+           sizeof *ofs->actions * flow->sf_acts->n_actions);
 }
 
 \f
-/* 'buffer' was received on 'in_port', a physical switch port between 0 and
- * OFPP_MAX.  Process it according to 'dp''s flow table.  Returns 0 if
+/* '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.  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 ofpbuf *buffer,
-                            int in_port)
+                            struct sw_port *p)
 {
     struct sw_flow_key key;
     struct sw_flow *flow;
 
     key.wildcards = 0;
-    if (flow_extract(buffer, in_port, &key.flow)
+    if (flow_extract(buffer, p ? port_no(dp, p) : OFPP_NONE, &key.flow)
         && (dp->flags & OFPC_FRAG_MASK) == OFPC_FRAG_DROP) {
         /* Drop fragment. */
         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)) {
+               ofpbuf_delete(buffer);
+               return 0;
+       }
 
     flow = chain_lookup(dp->chain, &key);
     if (flow != NULL) {
         flow_used(flow, buffer);
-        execute_actions(dp, buffer, in_port, &key,
-                        flow->actions, flow->n_actions);
+        execute_actions(dp, buffer, port_no(dp, p),
+                        &key, flow->sf_acts->actions, 
+                        flow->sf_acts->n_actions, false);
         return 0;
     } else {
         return -ESRCH;
     }
 }
 
-/* 'buffer' was received on 'in_port', a physical switch port between 0 and
- * OFPP_MAX.  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 ofpbuf *buffer, int in_port) 
+/* '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 ofpbuf *buffer,
+                    struct sw_port *p)
 {
-    if (run_flow_through_tables(dp, buffer, in_port)) {
-        dp_output_control(dp, buffer, in_port, dp->miss_send_len,
-                          OFPR_NO_MATCH);
+    if (run_flow_through_tables(dp, buffer, p)) {
+        dp_output_control(dp, buffer, port_no(dp, p),
+                          dp->miss_send_len, OFPR_NO_MATCH);
     }
 }
 
 static void
 do_output(struct datapath *dp, struct ofpbuf *buffer, int in_port,
-          size_t max_len, int out_port)
+          size_t max_len, int out_port, bool ignore_no_fwd)
 {
     if (out_port != OFPP_CONTROLLER) {
-        dp_output_port(dp, buffer, in_port, out_port);
+        dp_output_port(dp, buffer, in_port, out_port, ignore_no_fwd);
     } else {
         dp_output_control(dp, buffer, in_port, max_len, OFPR_ACTION);
     }
@@ -907,7 +903,8 @@ do_output(struct datapath *dp, struct ofpbuf *buffer, int in_port,
 static void
 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)
+                const struct ofp_action *actions, int n_actions,
+                bool ignore_no_fwd)
 {
     /* Every output action needs a separate clone of 'buffer', but the common
      * case is just a single output action, so that doing a clone and then
@@ -926,7 +923,8 @@ execute_actions(struct datapath *dp, struct ofpbuf *buffer,
         struct eth_header *eh = buffer->l2;
 
         if (prev_port != -1) {
-            do_output(dp, ofpbuf_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;
         }
 
@@ -963,7 +961,7 @@ execute_actions(struct datapath *dp, struct ofpbuf *buffer,
         }
     }
     if (prev_port != -1)
-        do_output(dp, buffer, in_port, max_len, prev_port);
+        do_output(dp, buffer, in_port, max_len, prev_port, ignore_no_fwd);
     else
         ofpbuf_delete(buffer);
 }
@@ -1041,7 +1039,7 @@ modify_vlan(struct ofpbuf *buffer,
             
             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 */
@@ -1054,8 +1052,8 @@ modify_vlan(struct ofpbuf *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);
         }
     }
@@ -1131,7 +1129,7 @@ recv_packet_out(struct datapath *dp, const struct sender *sender UNUSED,
  
     flow_extract(buffer, ntohs(opo->in_port), &key.flow);
     execute_actions(dp, buffer, ntohs(opo->in_port),
-                    &key, opo->actions, n_actions);
+                    &key, opo->actions, n_actions, true);
 
    return 0;
 }
@@ -1184,10 +1182,11 @@ add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
     flow->idle_timeout = ntohs(ofm->idle_timeout);
     flow->hard_timeout = ntohs(ofm->hard_timeout);
     flow->used = flow->created = time_now();
-    flow->n_actions = n_actions;
+    flow->sf_acts->n_actions = n_actions;
     flow->byte_count = 0;
     flow->packet_count = 0;
-    memcpy(flow->actions, ofm->actions, n_actions * sizeof *flow->actions);
+    memcpy(flow->sf_acts->actions, ofm->actions, 
+                n_actions * sizeof *flow->sf_acts->actions);
 
     /* Act. */
     error = chain_insert(dp->chain, flow);
@@ -1202,7 +1201,8 @@ add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
             uint16_t in_port = ntohs(ofm->match.in_port);
             flow_used(flow, buffer);
             flow_extract(buffer, in_port, &key.flow);
-            execute_actions(dp, buffer, in_port, &key, ofm->actions, n_actions);
+            execute_actions(dp, buffer, in_port, &key,
+                            ofm->actions, n_actions, false);
         } else {
             error = -ESRCH; 
         }
@@ -1217,6 +1217,59 @@ error:
     return error;
 }
 
+static int
+mod_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
+{
+    int error = -ENOMEM;
+    int n_actions;
+    int i;
+    struct sw_flow_key key;
+    uint16_t priority;
+    int strict;
+
+
+    /* To prevent loops, make sure there's no action to send to the
+     * OFP_TABLE virtual port.
+     */
+    n_actions = (ntohs(ofm->header.length) - sizeof *ofm) 
+            / sizeof *ofm->actions;
+    for (i=0; i<n_actions; i++) {
+        const struct ofp_action *a = &ofm->actions[i];
+
+        if (a->type == htons(OFPAT_OUTPUT)
+                    && (a->arg.output.port == htons(OFPP_TABLE)
+                        || a->arg.output.port == htons(OFPP_NONE)
+                        || a->arg.output.port == ofm->match.in_port)) {
+            /* xxx Send fancy new error message? */
+            goto error;
+        }
+    }
+
+    flow_extract_match(&key, &ofm->match);
+    priority = key.wildcards ? ntohs(ofm->priority) : -1;
+    strict = (ofm->command == htons(OFPFC_MODIFY_STRICT)) ? 1 : 0;
+    chain_modify(dp->chain, &key, priority, strict, ofm->actions, n_actions);
+
+    if (ntohl(ofm->buffer_id) != UINT32_MAX) {
+        struct ofpbuf *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
+        if (buffer) {
+            struct sw_flow_key skb_key;
+            uint16_t in_port = ntohs(ofm->match.in_port);
+            flow_extract(buffer, in_port, &skb_key.flow);
+            execute_actions(dp, buffer, in_port, &skb_key,
+                            ofm->actions, n_actions, false);
+        } else {
+            error = -ESRCH; 
+        }
+    }
+    return error;
+
+error:
+    if (ntohl(ofm->buffer_id) != (uint32_t) -1)
+        discard_buffer(ntohl(ofm->buffer_id));
+    return error;
+}
+
 static int
 recv_flow(struct datapath *dp, const struct sender *sender UNUSED,
           const void *msg)
@@ -1226,6 +1279,8 @@ recv_flow(struct datapath *dp, const struct sender *sender UNUSED,
 
     if (command == OFPFC_ADD) {
         return add_flow(dp, ofm);
+    } else if ((command == OFPFC_MODIFY) || (command == OFPFC_MODIFY_STRICT)) {
+        return mod_flow(dp, ofm);
     }  else if (command == OFPFC_DELETE) {
         struct sw_flow_key key;
         flow_extract_match(&key, &ofm->match);
@@ -1389,9 +1444,11 @@ static int table_stats_dump(struct datapath *dp, void *state,
         dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
         strncpy(ots->name, stats.name, sizeof ots->name);
         ots->table_id = i;
+        ots->wildcards = htonl(stats.wildcards);
         memset(ots->pad, 0, sizeof ots->pad);
         ots->max_entries = htonl(stats.max_flows);
         ots->active_count = htonl(stats.n_flows);
+        ots->lookup_count = htonll(stats.n_lookup);
         ots->matched_count = htonll(stats.n_matched);
     }
     return 0;
@@ -1449,6 +1506,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;
@@ -1473,35 +1533,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,
@@ -1532,7 +1597,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);
@@ -1572,21 +1637,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) {
-        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);
@@ -1636,65 +1707,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,
-        },
-    };
-
-    const struct openflow_packet *pkt;
+    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) {
+        return -EINVAL;
+    }
     assert(oh->version == OFP_VERSION);
-    if (oh->type >= ARRAY_SIZE(packets) || ntohs(oh->length) > length)
+
+    /* 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;
+    }
 
-    pkt = &packets[oh->type];
-    if (!pkt->handler)
-        return -ENOSYS;
-    if (length < pkt->min_size)
+    /* Handle it. */
+    if (length < min_size)
         return -EFAULT;
-
-    return pkt->handler(dp, sender, msg);
+    return handler(dp, sender, msg);
 }
 \f
 /* Packet buffering. */