Fix "make dist" by adding forgotten files to sources lists.
[sliver-openvswitch.git] / switch / datapath.c
index 5d4590f..452d471 100644 (file)
@@ -44,7 +44,7 @@
 #include "list.h"
 #include "netdev.h"
 #include "ofpbuf.h"
-#include "openflow.h"
+#include "openflow/openflow.h"
 #include "packets.h"
 #include "poll-loop.h"
 #include "rconn.h"
@@ -54,6 +54,7 @@
 #include "timeval.h"
 #include "vconn.h"
 #include "xtoxll.h"
+#include "dp_act.h"
 
 #define THIS_MODULE VLM_datapath
 #include "vlog.h"
@@ -71,7 +72,9 @@ extern char serial_num;
 
 /* Actions supported by this implementation. */
 #define OFP_SUPPORTED_ACTIONS ( (1 << OFPAT_OUTPUT)         \
-                                | (1 << OFPAT_SET_DL_VLAN)  \
+                                | (1 << OFPAT_SET_VLAN_VID) \
+                                | (1 << OFPAT_SET_VLAN_PCP) \
+                                | (1 << OFPAT_STRIP_VLAN)   \
                                 | (1 << OFPAT_SET_DL_SRC)   \
                                 | (1 << OFPAT_SET_DL_DST)   \
                                 | (1 << OFPAT_SET_NW_SRC)   \
@@ -79,12 +82,9 @@ 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;             /* Some subset of PORT_FLAG_BITS. */
-    uint32_t status;            /* Some subset of PORT_STATUS_BITS. */
+    uint32_t config;            /* Some subset of OFPPC_* flags. */
+    uint32_t state;             /* Some subset of OFPPS_* flags. */
     struct datapath *dp;
     struct netdev *netdev;
     struct list node; /* Element in datapath.ports. */
@@ -116,6 +116,9 @@ struct remote {
     void *cb_aux;
 };
 
+#define DP_MAX_PORTS 255
+BUILD_ASSERT_DECL(DP_MAX_PORTS <= OFPP_MAX);
+
 struct datapath {
     /* Remote connections. */
     struct remote *controller;  /* Connection to controller. */
@@ -134,7 +137,7 @@ struct datapath {
     uint16_t miss_send_len;
 
     /* Switch ports. */
-    struct sw_port ports[OFPP_MAX];
+    struct sw_port ports[DP_MAX_PORTS];
     struct list port_list; /* List of ports, for flooding. */
 };
 
@@ -145,26 +148,12 @@ 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 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 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 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 ofpbuf *buffer, const struct sw_flow_key *key,
-                        const struct ofp_action *a);
-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 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
  * into a buffer number (low bits) and a cookie (high bits).  The buffer number
@@ -300,7 +289,7 @@ dp_run(struct datapath *dp)
 
         LIST_FOR_EACH (p, struct sw_port, node, &dp->port_list) {
             if (update_port_status(p)) {
-                send_port_status(p, OFPPR_MOD);
+                send_port_status(p, OFPPR_MODIFY);
             }
         }
 
@@ -527,7 +516,7 @@ 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 & OFPPFL_NO_FLOOD) {
+        if (flood && p->config & OFPPC_NO_FLOOD) {
             continue;
         }
         if (prev_port != -1) {
@@ -547,9 +536,9 @@ output_all(struct datapath *dp, struct ofpbuf *buffer, int in_port, int flood)
 void
 output_packet(struct datapath *dp, struct ofpbuf *buffer, int out_port) 
 {
-    if (out_port >= 0 && out_port < OFPP_MAX) { 
+    if (out_port >= 0 && out_port < DP_MAX_PORTS) { 
         struct sw_port *p = &dp->ports[out_port];
-        if (p->netdev != NULL && !(p->status & OFPPFL_PORT_DOWN)) {
+        if (p->netdev != NULL && !(p->config & OFPPC_PORT_DOWN)) {
             if (!netdev_send(p->netdev, buffer)) {
                 p->tx_packets++;
                 p->tx_bytes += buffer->size;
@@ -581,7 +570,7 @@ 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) {
-        struct sw_port *p = in_port < OFPP_MAX ? &dp->ports[in_port] : 0;
+        struct sw_port *p = in_port < DP_MAX_PORTS ? &dp->ports[in_port] : 0;
                if (run_flow_through_tables(dp, buffer, p)) {
                        ofpbuf_delete(buffer);
         }
@@ -660,10 +649,14 @@ static void fill_port_desc(struct datapath *dp, struct sw_port *p,
             sizeof desc->name);
     desc->name[sizeof desc->name - 1] = '\0';
     memcpy(desc->hw_addr, netdev_get_etheraddr(p->netdev), ETH_ADDR_LEN);
-    desc->flags = 0;
-    desc->features = htonl(netdev_get_features(p->netdev));
-    desc->speed = htonl(netdev_get_speed(p->netdev));
-    desc->flags = htonl(p->flags | p->status);
+    desc->config = htonl(p->config);
+    desc->state = htonl(p->state);
+    desc->curr = htonl(netdev_get_features(p->netdev, NETDEV_FEAT_CURRENT));
+    desc->supported = htonl(netdev_get_features(p->netdev, 
+                NETDEV_FEAT_SUPPORTED));
+    desc->advertised = htonl(netdev_get_features(p->netdev, 
+                NETDEV_FEAT_ADVERTISED));
+    desc->peer = htonl(netdev_get_features(p->netdev, NETDEV_FEAT_PEER));
 }
 
 static void
@@ -681,8 +674,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 = ofpbuf_put_uninit(buffer, sizeof *opp);
-        memset(opp, 0, sizeof *opp);
+        struct ofp_phy_port *opp = ofpbuf_put_zeros(buffer, sizeof *opp);
         fill_port_desc(dp, p, opp);
     }
     send_openflow_buffer(dp, buffer, sender);
@@ -691,33 +683,31 @@ dp_send_features_reply(struct datapath *dp, const struct sender *sender)
 void
 dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
 {
-    const struct ofp_phy_port *opp = &opm->desc;
-    int port_no = ntohs(opp->port_no);
-    if (port_no < OFPP_MAX) {
+    int port_no = ntohs(opm->port_no);
+    if (port_no < DP_MAX_PORTS) {
         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),
+        if (memcmp(opm->hw_addr, netdev_get_etheraddr(p->netdev),
                          ETH_ADDR_LEN) != 0) {
             return;
         }
 
 
-        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) {
+            uint32_t config_mask = ntohl(opm->mask);
+            p->config &= ~config_mask;
+            p->config |= ntohl(opm->config) & config_mask;
         }
 
-        if (opm->mask & htonl(OFPPFL_PORT_DOWN)) {
-            if ((opp->flags & htonl(OFPPFL_PORT_DOWN))
-                && (p->status & OFPPFL_PORT_DOWN) == 0) {
-                p->status |= OFPPFL_PORT_DOWN;
+        if (opm->mask & htonl(OFPPC_PORT_DOWN)) {
+            if ((opm->config & htonl(OFPPC_PORT_DOWN))
+                && (p->config & OFPPC_PORT_DOWN) == 0) {
+                p->config |= OFPPC_PORT_DOWN;
                 netdev_turn_flags_off(p->netdev, NETDEV_UP, true);
-            } else if ((opp->flags & htonl(OFPPFL_PORT_DOWN)) == 0
-                       && (p->status & OFPPFL_PORT_DOWN)) {
-                p->status &= ~OFPPFL_PORT_DOWN;
+            } else if ((opm->config & htonl(OFPPC_PORT_DOWN)) == 0
+                       && (p->config & OFPPC_PORT_DOWN)) {
+                p->config &= ~OFPPC_PORT_DOWN;
                 netdev_turn_flags_on(p->netdev, NETDEV_UP, true);
             }
         }
@@ -735,7 +725,8 @@ update_port_status(struct sw_port *p)
 {
     int retval;
     enum netdev_flags flags;
-    uint32_t orig_status = p->status;
+    uint32_t orig_config = p->config;
+    uint32_t orig_state = p->state;
 
     if (netdev_get_flags(p->netdev, &flags) < 0) {
         VLOG_WARN_RL(&rl, "could not get netdev flags for %s", 
@@ -743,9 +734,9 @@ update_port_status(struct sw_port *p)
         return 0;
     } else {
         if (flags & NETDEV_UP) {
-            p->status &= ~OFPPFL_PORT_DOWN;
+            p->config &= ~OFPPC_PORT_DOWN;
         } else {
-            p->status |= OFPPFL_PORT_DOWN;
+            p->config |= OFPPC_PORT_DOWN;
         } 
     }
 
@@ -753,12 +744,12 @@ update_port_status(struct sw_port *p)
      * error. */
     retval = netdev_get_link_status(p->netdev);
     if (retval == 1) {
-        p->status &= ~OFPPFL_LINK_DOWN;
+        p->state &= ~OFPPS_LINK_DOWN;
     } else if (retval == 0) {
-        p->status |= OFPPFL_LINK_DOWN;
+        p->state |= OFPPS_LINK_DOWN;
     } 
 
-    return (orig_status != p->status);
+    return ((orig_config != p->config) || (orig_state != p->state));
 }
 
 static void
@@ -812,11 +803,10 @@ 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->sf_acts->n_actions;
-    ofs = ofpbuf_put_uninit(buffer, length);
+    int length = sizeof *ofs + flow->sf_acts->actions_len;
+    ofs = ofpbuf_put_zeros(buffer, length);
     ofs->length          = htons(length);
     ofs->table_id        = table_idx;
-    ofs->pad             = 0;
     ofs->match.wildcards = htonl(flow->key.wildcards);
     ofs->match.in_port   = flow->key.flow.in_port;
     memcpy(ofs->match.dl_src, flow->key.flow.dl_src, ETH_ADDR_LEN);
@@ -826,18 +816,15 @@ fill_flow_stats(struct ofpbuf *buffer, struct sw_flow *flow,
     ofs->match.nw_src    = flow->key.flow.nw_src;
     ofs->match.nw_dst    = flow->key.flow.nw_dst;
     ofs->match.nw_proto  = flow->key.flow.nw_proto;
-    ofs->match.pad       = 0;
     ofs->match.tp_src    = flow->key.flow.tp_src;
     ofs->match.tp_dst    = flow->key.flow.tp_dst;
     ofs->duration        = htonl(now - flow->created);
     ofs->priority        = htons(flow->priority);
     ofs->idle_timeout    = htons(flow->idle_timeout);
     ofs->hard_timeout    = htons(flow->hard_timeout);
-    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->sf_acts->actions,
-           sizeof *ofs->actions * flow->sf_acts->n_actions);
+    memcpy(ofs->actions, flow->sf_acts->actions, flow->sf_acts->actions_len);
 }
 
 \f
@@ -858,9 +845,9 @@ int run_flow_through_tables(struct datapath *dp, struct ofpbuf *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)) {
+       if (p && p->config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP)
+        && p->config & (!eth_addr_equals(key.flow.dl_dst, stp_eth_addr)
+                       ? OFPPC_NO_RECV : OFPPC_NO_RECV_STP)) {
                ofpbuf_delete(buffer);
                return 0;
        }
@@ -868,9 +855,8 @@ int run_flow_through_tables(struct datapath *dp, struct ofpbuf *buffer,
     flow = chain_lookup(dp->chain, &key);
     if (flow != NULL) {
         flow_used(flow, buffer);
-        execute_actions(dp, buffer, port_no(dp, p),
-                        &key, flow->sf_acts->actions, 
-                        flow->sf_acts->n_actions, false);
+        execute_actions(dp, buffer, &key, flow->sf_acts->actions, 
+                        flow->sf_acts->actions_len, false);
         return 0;
     } else {
         return -ESRCH;
@@ -889,176 +875,6 @@ void fwd_port_input(struct datapath *dp, struct ofpbuf *buffer,
     }
 }
 
-static void
-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) {
-        dp_output_port(dp, buffer, in_port, out_port, ignore_no_fwd);
-    } else {
-        dp_output_control(dp, buffer, in_port, max_len, OFPR_ACTION);
-    }
-}
-
-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,
-                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
-     * freeing the original buffer is wasteful.  So the following code is
-     * slightly obscure just to avoid that. */
-    int prev_port;
-    size_t max_len=0;        /* Initialze to make compiler happy */
-    uint16_t eth_proto;
-    int i;
-
-    prev_port = -1;
-    eth_proto = ntohs(key->flow.dl_type);
-
-    for (i = 0; i < n_actions; i++) {
-        const struct ofp_action *a = &actions[i];
-        struct eth_header *eh = buffer->l2;
-
-        if (prev_port != -1) {
-            do_output(dp, ofpbuf_clone(buffer), in_port, max_len, prev_port,
-                      ignore_no_fwd);
-            prev_port = -1;
-        }
-
-        switch (ntohs(a->type)) {
-        case OFPAT_OUTPUT:
-            prev_port = ntohs(a->arg.output.port);
-            max_len = ntohs(a->arg.output.max_len);
-            break;
-
-        case OFPAT_SET_DL_VLAN:
-            modify_vlan(buffer, key, a);
-            break;
-
-        case OFPAT_SET_DL_SRC:
-            memcpy(eh->eth_src, a->arg.dl_addr, sizeof eh->eth_src);
-            break;
-
-        case OFPAT_SET_DL_DST:
-            memcpy(eh->eth_dst, a->arg.dl_addr, sizeof eh->eth_dst);
-            break;
-
-        case OFPAT_SET_NW_SRC:
-        case OFPAT_SET_NW_DST:
-            modify_nh(buffer, eth_proto, key->flow.nw_proto, a);
-            break;
-
-        case OFPAT_SET_TP_SRC:
-        case OFPAT_SET_TP_DST:
-            modify_th(buffer, eth_proto, key->flow.nw_proto, a);
-            break;
-
-        default:
-            NOT_REACHED();
-        }
-    }
-    if (prev_port != -1)
-        do_output(dp, buffer, in_port, max_len, prev_port, ignore_no_fwd);
-    else
-        ofpbuf_delete(buffer);
-}
-
-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) {
-        struct ip_header *nh = buffer->l3;
-        uint32_t new, *field;
-
-        new = a->arg.nw_addr;
-        field = a->type == OFPAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
-        if (nw_proto == IP_TYPE_TCP) {
-            struct tcp_header *th = buffer->l4;
-            th->tcp_csum = recalc_csum32(th->tcp_csum, *field, new);
-        } else if (nw_proto == IP_TYPE_UDP) {
-            struct udp_header *th = buffer->l4;
-            if (th->udp_csum) {
-                th->udp_csum = recalc_csum32(th->udp_csum, *field, new);
-                if (!th->udp_csum) {
-                    th->udp_csum = 0xffff;
-                }
-            }
-        }
-        nh->ip_csum = recalc_csum32(nh->ip_csum, *field, new);
-        *field = new;
-    }
-}
-
-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) {
-        uint16_t new, *field;
-
-        new = a->arg.tp;
-
-        if (nw_proto == IP_TYPE_TCP) {
-            struct tcp_header *th = buffer->l4;
-            field = a->type == OFPAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
-            th->tcp_csum = recalc_csum16(th->tcp_csum, *field, new);
-            *field = new;
-        } else if (nw_proto == IP_TYPE_UDP) {
-            struct udp_header *th = buffer->l4;
-            field = a->type == OFPAT_SET_TP_SRC ? &th->udp_src : &th->udp_dst;
-            th->udp_csum = recalc_csum16(th->udp_csum, *field, new);
-            *field = new;
-        }
-    }
-}
-
-static void
-modify_vlan(struct ofpbuf *buffer,
-            const struct sw_flow_key *key, const struct ofp_action *a)
-{
-    uint16_t new_id = a->arg.vlan_id;
-    struct vlan_eth_header *veh;
-
-    if (new_id != htons(OFP_VLAN_NONE)) {
-        if (key->flow.dl_vlan != htons(OFP_VLAN_NONE)) {
-            /* Modify vlan id, but maintain other TCI values */
-            veh = buffer->l2;
-            veh->veth_tci &= ~htons(VLAN_VID);
-            veh->veth_tci |= new_id;
-        } else {
-            /* Insert new vlan id. */
-            struct eth_header *eh = buffer->l2;
-            struct vlan_eth_header tmp;
-            memcpy(tmp.veth_dst, eh->eth_dst, ETH_ADDR_LEN);
-            memcpy(tmp.veth_src, eh->eth_src, ETH_ADDR_LEN);
-            tmp.veth_type = htons(ETH_TYPE_VLAN);
-            tmp.veth_tci = new_id;
-            tmp.veth_next_type = eh->eth_type;
-            
-            veh = ofpbuf_push_uninit(buffer, VLAN_HEADER_LEN);
-            memcpy(veh, &tmp, sizeof tmp);
-            buffer->l2 = (char*)buffer->l2 - VLAN_HEADER_LEN;
-        }
-    } else  {
-        /* Remove an existing vlan header if it exists */
-        veh = buffer->l2;
-        if (veh->veth_type == htons(ETH_TYPE_VLAN)) {
-            struct eth_header tmp;
-            
-            memcpy(tmp.eth_dst, veh->veth_dst, ETH_ADDR_LEN);
-            memcpy(tmp.eth_src, veh->veth_src, ETH_ADDR_LEN);
-            tmp.eth_type = veh->veth_next_type;
-            
-            buffer->size -= 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);
-        }
-    }
-}
-
 static int
 recv_features_request(struct datapath *dp, const struct sender *sender,
                       const void *msg) 
@@ -1101,25 +917,25 @@ recv_set_config(struct datapath *dp, const struct sender *sender UNUSED,
 }
 
 static int
-recv_packet_out(struct datapath *dp, const struct sender *sender UNUSED,
+recv_packet_out(struct datapath *dp, const struct sender *sender,
                 const void *msg)
 {
     const struct ofp_packet_out *opo = msg;
     struct sw_flow_key key;
+    uint16_t v_code;
     struct ofpbuf *buffer;
-    int n_actions = ntohs(opo->n_actions);
-    int act_len = n_actions * sizeof opo->actions[0];
+    size_t actions_len = ntohs(opo->actions_len);
 
-    if (act_len > (ntohs(opo->header.length) - sizeof *opo)) {
+    if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
         VLOG_DBG_RL(&rl, "message too short for number of actions");
         return -EINVAL;
     }
 
     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;
+        int data_len = ntohs(opo->header.length) - sizeof *opo - actions_len;
         buffer = ofpbuf_new(data_len);
-        ofpbuf_put(buffer, &opo->actions[n_actions], data_len);
+        ofpbuf_put(buffer, (uint8_t *)opo->actions + actions_len, data_len);
     } else {
         buffer = retrieve_buffer(ntohl(opo->buffer_id));
         if (!buffer) {
@@ -1128,10 +944,21 @@ 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, true);
 
-   return 0;
+    v_code = validate_actions(dp, &key, opo->actions, actions_len);
+    if (v_code != ACT_VALIDATION_OK) {
+        dp_send_error_msg(dp, sender, OFPET_BAD_ACTION, v_code,
+                  msg, ntohs(opo->header.length));
+        goto error;
+    }
+
+    execute_actions(dp, buffer, &key, opo->actions, actions_len, true);
+
+    return 0;
+
+error:
+    ofpbuf_delete(buffer);
+    return -EINVAL;
 }
 
 static int
@@ -1146,51 +973,45 @@ recv_port_mod(struct datapath *dp, const struct sender *sender UNUSED,
 }
 
 static int
-add_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
+add_flow(struct datapath *dp, const struct sender *sender,
+        const struct ofp_flow_mod *ofm)
 {
     int error = -ENOMEM;
-    int n_actions;
-    int i;
-    struct sw_flow *flow;
-
-
-    /* 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;
-        }
-    }
+    uint16_t v_code;
+    struct sw_flow *flow; 
+    size_t actions_len = ntohs(ofm->header.length) - sizeof *ofm;
 
     /* Allocate memory. */
-    flow = flow_alloc(n_actions);
+    flow = flow_alloc(actions_len);
     if (flow == NULL)
         goto error;
 
-    /* Fill out flow. */
     flow_extract_match(&flow->key, &ofm->match);
+
+    v_code = validate_actions(dp, &flow->key, ofm->actions, actions_len);
+    if (v_code != ACT_VALIDATION_OK) {
+        dp_send_error_msg(dp, sender, OFPET_BAD_ACTION, v_code,
+                  ofm, ntohs(ofm->header.length));
+        goto error_free_flow;
+    }
+
+    /* Fill out flow. */
     flow->priority = flow->key.wildcards ? ntohs(ofm->priority) : -1;
     flow->idle_timeout = ntohs(ofm->idle_timeout);
     flow->hard_timeout = ntohs(ofm->hard_timeout);
     flow->used = flow->created = time_now();
-    flow->sf_acts->n_actions = n_actions;
+    flow->sf_acts->actions_len = actions_len;
     flow->byte_count = 0;
     flow->packet_count = 0;
-    memcpy(flow->sf_acts->actions, ofm->actions, 
-                n_actions * sizeof *flow->sf_acts->actions);
+    memcpy(flow->sf_acts->actions, ofm->actions, actions_len);
 
     /* Act. */
     error = chain_insert(dp->chain, flow);
-    if (error) {
+    if (error == -ENOBUFS) {
+        dp_send_error_msg(dp, sender, OFPET_FLOW_MOD_FAILED, 
+                OFPFMFC_ALL_TABLES_FULL, ofm, ntohs(ofm->header.length));
+        goto error_free_flow; 
+    } else if (error) {
         goto error_free_flow; 
     }
     error = 0;
@@ -1201,8 +1022,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, false);
+            execute_actions(dp, buffer, &key, 
+                    ofm->actions, actions_len, false);
         } else {
             error = -ESRCH; 
         }
@@ -1218,37 +1039,30 @@ error:
 }
 
 static int
-mod_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
+mod_flow(struct datapath *dp, const struct sender *sender,
+        const struct ofp_flow_mod *ofm)
 {
     int error = -ENOMEM;
-    int n_actions;
-    int i;
+    uint16_t v_code;
+    size_t actions_len;
     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);
+    actions_len = ntohs(ofm->header.length) - sizeof *ofm;
+    v_code = validate_actions(dp, &key, ofm->actions, actions_len);
+    if (v_code != ACT_VALIDATION_OK) {
+        dp_send_error_msg(dp, sender, OFPET_BAD_ACTION, v_code,
+                  ofm, ntohs(ofm->header.length));
+        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);
+    chain_modify(dp->chain, &key, priority, strict, ofm->actions, actions_len);
 
     if (ntohl(ofm->buffer_id) != UINT32_MAX) {
         struct ofpbuf *buffer = retrieve_buffer(ntohl(ofm->buffer_id));
@@ -1256,8 +1070,8 @@ mod_flow(struct datapath *dp, const struct ofp_flow_mod *ofm)
             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);
+            execute_actions(dp, buffer, &skb_key,
+                            ofm->actions, actions_len, false);
         } else {
             error = -ESRCH; 
         }
@@ -1271,26 +1085,27 @@ error:
 }
 
 static int
-recv_flow(struct datapath *dp, const struct sender *sender UNUSED,
+recv_flow(struct datapath *dp, const struct sender *sender,
           const void *msg)
 {
     const struct ofp_flow_mod *ofm = msg;
     uint16_t command = ntohs(ofm->command);
 
     if (command == OFPFC_ADD) {
-        return add_flow(dp, ofm);
+        return add_flow(dp, sender, ofm);
     } else if ((command == OFPFC_MODIFY) || (command == OFPFC_MODIFY_STRICT)) {
-        return mod_flow(dp, ofm);
+        return mod_flow(dp, sender, ofm);
     }  else if (command == OFPFC_DELETE) {
         struct sw_flow_key key;
         flow_extract_match(&key, &ofm->match);
-        return chain_delete(dp->chain, &key, 0, 0) ? 0 : -ESRCH;
+        return chain_delete(dp->chain, &key, ofm->out_port, 0, 0) ? 0 : -ESRCH;
     } else if (command == OFPFC_DELETE_STRICT) {
         struct sw_flow_key key;
         uint16_t priority;
         flow_extract_match(&key, &ofm->match);
         priority = key.wildcards ? ntohs(ofm->priority) : -1;
-        return chain_delete(dp->chain, &key, priority, 1) ? 0 : -ESRCH;
+        return chain_delete(dp->chain, &key, ofm->out_port, 
+                priority, 1) ? 0 : -ESRCH;
     } else {
         return -ENODEV;
     }
@@ -1299,7 +1114,7 @@ recv_flow(struct datapath *dp, const struct sender *sender UNUSED,
 static int desc_stats_dump(struct datapath *dp, void *state,
                               struct ofpbuf *buffer)
 {
-    struct ofp_desc_stats *ods = ofpbuf_put_uninit(buffer, sizeof *ods);
+    struct ofp_desc_stats *ods = ofpbuf_put_zeros(buffer, sizeof *ods);
 
     strncpy(ods->mfr_desc, &mfr_desc, sizeof ods->mfr_desc);
     strncpy(ods->hw_desc, &hw_desc, sizeof ods->hw_desc);
@@ -1353,8 +1168,8 @@ static int flow_stats_dump(struct datapath *dp, void *state,
     {
         struct sw_table *table = dp->chain->tables[s->table_idx];
 
-        if (table->iterate(table, &match_key, &s->position,
-                           flow_stats_dump_callback, s))
+        if (table->iterate(table, &match_key, s->rq.out_port,
+                    &s->position, flow_stats_dump_callback, s))
             break;
 
         s->table_idx++;
@@ -1402,8 +1217,7 @@ static int aggregate_stats_dump(struct datapath *dp, void *state,
     struct sw_flow_key match_key;
     int table_idx;
 
-    rpy = ofpbuf_put_uninit(buffer, sizeof *rpy);
-    memset(rpy, 0, sizeof *rpy);
+    rpy = ofpbuf_put_zeros(buffer, sizeof *rpy);
 
     flow_extract_match(&match_key, &rq->match);
     table_idx = rq->table_id == 0xff ? 0 : rq->table_id;
@@ -1414,7 +1228,7 @@ static int aggregate_stats_dump(struct datapath *dp, void *state,
         struct sw_table *table = dp->chain->tables[table_idx];
         int error;
 
-        error = table->iterate(table, &match_key, &position,
+        error = table->iterate(table, &match_key, rq->out_port, &position,
                                aggregate_stats_dump_callback, rpy);
         if (error)
             return error;
@@ -1439,13 +1253,12 @@ static int table_stats_dump(struct datapath *dp, void *state,
 {
     int i;
     for (i = 0; i < dp->chain->n_tables; i++) {
-        struct ofp_table_stats *ots = ofpbuf_put_uninit(buffer, sizeof *ots);
+        struct ofp_table_stats *ots = ofpbuf_put_zeros(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);
         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);
@@ -1473,15 +1286,14 @@ static int port_stats_dump(struct datapath *dp, void *state,
     struct port_stats_state *s = state;
     int i;
 
-    for (i = s->port; i < OFPP_MAX; i++) {
+    for (i = s->port; i < DP_MAX_PORTS; i++) {
         struct sw_port *p = &dp->ports[i];
         struct ofp_port_stats *ops;
         if (!p->netdev) {
             continue;
         }
-        ops = ofpbuf_put_uninit(buffer, sizeof *ops);
+        ops = ofpbuf_put_zeros(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);
         ops->tx_packets   = htonll(p->tx_packets);
         ops->rx_bytes     = htonll(p->rx_bytes);