Merge "master" into "wdp".
authorBen Pfaff <blp@nicira.com>
Wed, 6 Oct 2010 18:37:03 +0000 (11:37 -0700)
committerBen Pfaff <blp@nicira.com>
Wed, 6 Oct 2010 18:37:03 +0000 (11:37 -0700)
38 files changed:
1  2 
datapath/datapath.c
datapath/datapath.h
datapath/flow.c
datapath/flow.h
datapath/tunnel.c
datapath/vport-internal_dev.c
datapath/vport.c
datapath/vport.h
include/openflow/nicira-ext.h
lib/classifier.c
lib/hmap.h
lib/learning-switch.c
lib/list.h
lib/mac-learning.c
lib/netdev-tunnel.c
lib/netdev-vport.c
lib/netdev.c
lib/ofp-parse.c
lib/ofp-parse.h
lib/ofp-print.c
lib/ofp-util.c
lib/ofp-util.h
lib/rtnetlink.c
lib/vlog-modules.def
lib/xfif-linux.c
lib/xfif-netdev.c
lib/xflow-util.c
ofproto/ofproto-sflow.c
ofproto/ofproto.c
ofproto/pktbuf.c
ofproto/wdp-xflow.c
tests/automake.mk
tests/test-classifier.c
utilities/ovs-controller.c
utilities/ovs-ofctl.8.in
utilities/ovs-ofctl.c
utilities/ovs-openflowd.c
vswitchd/bridge.c

diff --combined datapath/datapath.c
@@@ -29,7 -29,6 +29,6 @@@
  #include <linux/udp.h>
  #include <linux/version.h>
  #include <linux/ethtool.h>
- #include <linux/random.h>
  #include <linux/wait.h>
  #include <asm/system.h>
  #include <asm/div64.h>
  #include <linux/inetdevice.h>
  #include <linux/list.h>
  #include <linux/rculist.h>
- #include <linux/workqueue.h>
  #include <linux/dmi.h>
  #include <net/inet_ecn.h>
  #include <linux/compat.h>
  
 -#include "openvswitch/datapath-protocol.h"
 +#include "openvswitch/xflow.h"
  #include "datapath.h"
  #include "actions.h"
  #include "flow.h"
 -#include "odp-compat.h"
 +#include "xflow-compat.h"
  #include "table.h"
  #include "vport-internal_dev.h"
  
@@@ -68,7 -66,7 +66,7 @@@ EXPORT_SYMBOL(dp_ioctl_hook)
   * It is safe to access the datapath and dp_port structures with just
   * dp_mutex.
   */
 -static struct datapath *dps[ODP_MAX];
 +static struct datapath *dps[XFLOW_MAX];
  static DEFINE_MUTEX(dp_mutex);
  
  /* We limit the number of times that we pass into dp_process_received_packet()
@@@ -88,12 -86,12 +86,12 @@@ struct percpu_loop_counters 
  
  static DEFINE_PER_CPU(struct percpu_loop_counters, dp_loop_counters);
  
 -static int new_dp_port(struct datapath *, struct odp_port *, int port_no);
 +static int new_dp_port(struct datapath *, struct xflow_port *, int port_no);
  
  /* Must be called with rcu_read_lock or dp_mutex. */
  struct datapath *get_dp(int dp_idx)
  {
 -      if (dp_idx < 0 || dp_idx >= ODP_MAX)
 +      if (dp_idx < 0 || dp_idx >= XFLOW_MAX)
                return NULL;
        return rcu_dereference(dps[dp_idx]);
  }
@@@ -114,7 -112,7 +112,7 @@@ static struct datapath *get_dp_locked(i
  /* Must be called with rcu_read_lock or RTNL lock. */
  const char *dp_name(const struct datapath *dp)
  {
 -      return vport_get_name(dp->ports[ODPP_LOCAL]->vport);
 +      return vport_get_name(dp->ports[XFLOWP_LOCAL]->vport);
  }
  
  static inline size_t br_nlmsg_size(void)
@@@ -157,7 -155,7 +155,7 @@@ static int dp_fill_ifinfo(struct sk_buf
        hdr->ifi_change = 0;
  
        NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port->vport));
 -      NLA_PUT_U32(skb, IFLA_MASTER, vport_get_ifindex(dp->ports[ODPP_LOCAL]->vport));
 +      NLA_PUT_U32(skb, IFLA_MASTER, vport_get_ifindex(dp->ports[XFLOWP_LOCAL]->vport));
        NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port->vport));
  #ifdef IFLA_OPERSTATE
        NLA_PUT_U8(skb, IFLA_OPERSTATE,
@@@ -214,7 -212,7 +212,7 @@@ static struct kobj_type dp_ktype = 
  
  static int create_dp(int dp_idx, const char __user *devnamep)
  {
 -      struct odp_port internal_dev_port;
 +      struct xflow_port internal_dev_port;
        char devname[IFNAMSIZ];
        struct datapath *dp;
        int err;
        /* Set up our datapath device. */
        BUILD_BUG_ON(sizeof(internal_dev_port.devname) != sizeof(devname));
        strcpy(internal_dev_port.devname, devname);
 -      internal_dev_port.flags = ODP_PORT_INTERNAL;
 -      err = new_dp_port(dp, &internal_dev_port, ODPP_LOCAL);
 +      internal_dev_port.flags = XFLOW_PORT_INTERNAL;
 +      err = new_dp_port(dp, &internal_dev_port, XFLOWP_LOCAL);
        if (err) {
                if (err == -EBUSY)
                        err = -EEXIST;
        return 0;
  
  err_destroy_local_port:
 -      dp_detach_port(dp->ports[ODPP_LOCAL], 1);
 +      dp_detach_port(dp->ports[XFLOWP_LOCAL], 1);
  err_destroy_table:
        tbl_destroy(dp->table, NULL);
  err_free_dp:
@@@ -314,14 -312,14 +312,14 @@@ static void do_destroy_dp(struct datapa
        int i;
  
        list_for_each_entry_safe (p, n, &dp->port_list, node)
 -              if (p->port_no != ODPP_LOCAL)
 +              if (p->port_no != XFLOWP_LOCAL)
                        dp_detach_port(p, 1);
  
        dp_sysfs_del_dp(dp);
  
        rcu_assign_pointer(dps[dp->dp_idx], NULL);
  
 -      dp_detach_port(dp->ports[ODPP_LOCAL], 1);
 +      dp_detach_port(dp->ports[XFLOWP_LOCAL], 1);
  
        tbl_destroy(dp->table, flow_free_tbl);
  
@@@ -369,20 -367,20 +367,20 @@@ static struct kobj_type brport_ktype = 
  };
  
  /* Called with RTNL lock and dp_mutex. */
 -static int new_dp_port(struct datapath *dp, struct odp_port *odp_port, int port_no)
 +static int new_dp_port(struct datapath *dp, struct xflow_port *xflow_port, int port_no)
  {
        struct vport *vport;
        struct dp_port *p;
        int err;
  
 -      vport = vport_locate(odp_port->devname);
 +      vport = vport_locate(xflow_port->devname);
        if (!vport) {
                vport_lock();
  
 -              if (odp_port->flags & ODP_PORT_INTERNAL)
 -                      vport = vport_add(odp_port->devname, "internal", NULL);
 +              if (xflow_port->flags & XFLOW_PORT_INTERNAL)
 +                      vport = vport_add(xflow_port->devname, "internal", NULL);
                else
 -                      vport = vport_add(odp_port->devname, "netdev", NULL);
 +                      vport = vport_add(xflow_port->devname, "netdev", NULL);
  
                vport_unlock();
  
        return 0;
  }
  
 -static int attach_port(int dp_idx, struct odp_port __user *portp)
 +static int attach_port(int dp_idx, struct xflow_port __user *portp)
  {
        struct datapath *dp;
 -      struct odp_port port;
 +      struct xflow_port port;
        int port_no;
        int err;
  
@@@ -468,7 -466,7 +466,7 @@@ int dp_detach_port(struct dp_port *p, i
  
        ASSERT_RTNL();
  
 -      if (p->port_no != ODPP_LOCAL)
 +      if (p->port_no != XFLOWP_LOCAL)
                dp_sysfs_del_if(p);
        dp_ifinfo_notify(RTM_DELLINK, p);
  
@@@ -506,7 -504,7 +504,7 @@@ static int detach_port(int dp_idx, int 
        int err;
  
        err = -EINVAL;
 -      if (port_no < 0 || port_no >= DP_MAX_PORTS || port_no == ODPP_LOCAL)
 +      if (port_no < 0 || port_no >= DP_MAX_PORTS || port_no == XFLOWP_LOCAL)
                goto out;
  
        rtnl_lock();
@@@ -544,40 -542,45 +542,45 @@@ void dp_process_received_packet(struct 
        struct datapath *dp = p->dp;
        struct dp_stats_percpu *stats;
        int stats_counter_off;
-       struct xflow_key key;
-       struct tbl_node *flow_node;
-       struct sw_flow *flow;
        struct sw_flow_actions *acts;
        struct loop_counter *loop;
        int error;
  
        OVS_CB(skb)->dp_port = p;
  
-       /* Extract flow from 'skb' into 'key'. */
-       error = flow_extract(skb, p ? p->port_no : XFLOWP_NONE, &key);
-       if (unlikely(error)) {
-               kfree_skb(skb);
-               return;
-       }
+       if (!OVS_CB(skb)->flow) {
 -              struct odp_flow_key key;
++              struct xflow_key key;
+               struct tbl_node *flow_node;
+               bool is_frag;
  
-       if (OVS_CB(skb)->is_frag && dp->drop_frags) {
-               kfree_skb(skb);
-               stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
-               goto out;
-       }
+               /* Extract flow from 'skb' into 'key'. */
 -              error = flow_extract(skb, p ? p->port_no : ODPP_NONE, &key, &is_frag);
++              error = flow_extract(skb, p ? p->port_no : XFLOWP_NONE, &key, &is_frag);
+               if (unlikely(error)) {
+                       kfree_skb(skb);
+                       return;
+               }
  
-       /* Look up flow. */
-       flow_node = tbl_lookup(rcu_dereference(dp->table), &key, flow_hash(&key), flow_cmp);
-       if (unlikely(!flow_node)) {
-               dp_output_control(dp, skb, _XFLOWL_MISS_NR, OVS_CB(skb)->tun_id);
-               stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
-               goto out;
+               if (is_frag && dp->drop_frags) {
+                       kfree_skb(skb);
+                       stats_counter_off = offsetof(struct dp_stats_percpu, n_frags);
+                       goto out;
+               }
+               /* Look up flow. */
+               flow_node = tbl_lookup(rcu_dereference(dp->table), &key,
+                                       flow_hash(&key), flow_cmp);
+               if (unlikely(!flow_node)) {
 -                      dp_output_control(dp, skb, _ODPL_MISS_NR, OVS_CB(skb)->tun_id);
++                      dp_output_control(dp, skb, _XFLOWL_MISS_NR, OVS_CB(skb)->tun_id);
+                       stats_counter_off = offsetof(struct dp_stats_percpu, n_missed);
+                       goto out;
+               }
+               OVS_CB(skb)->flow = flow_cast(flow_node);
        }
  
-       flow = flow_cast(flow_node);
-       flow_used(flow, skb);
+       flow_used(OVS_CB(skb)->flow, skb);
  
-       acts = rcu_dereference(flow->sf_acts);
+       acts = rcu_dereference(OVS_CB(skb)->flow->sf_acts);
  
        /* Check whether we've looped too much. */
        loop = &get_cpu_var(dp_loop_counters).counters[!!in_interrupt()];
        }
  
        /* Execute actions. */
-       execute_actions(dp, skb, &key, acts->actions, acts->n_actions, GFP_ATOMIC);
+       execute_actions(dp, skb, &OVS_CB(skb)->flow->key, acts->actions,
+                       acts->n_actions, GFP_ATOMIC);
        stats_counter_off = offsetof(struct dp_stats_percpu, n_hit);
  
        /* Check whether sub-actions looped too much. */
@@@ -791,10 -795,10 +795,10 @@@ static int queue_control_packets(struc
        if (OVS_CB(skb)->dp_port)
                port_no = OVS_CB(skb)->dp_port->port_no;
        else
 -              port_no = ODPP_LOCAL;
 +              port_no = XFLOWP_LOCAL;
  
        do {
 -              struct odp_msg *header;
 +              struct xflow_msg *header;
  
                nskb = skb->next;
                skb->next = NULL;
                if (err)
                        goto err_kfree_skbs;
  
 -              header = (struct odp_msg*)__skb_push(skb, sizeof *header);
 +              header = (struct xflow_msg*)__skb_push(skb, sizeof *header);
                header->type = queue_no;
                header->length = skb->len;
                header->port = port_no;
@@@ -832,7 -836,7 +836,7 @@@ int dp_output_control(struct datapath *
        int err;
  
        WARN_ON_ONCE(skb_shared(skb));
 -      BUG_ON(queue_no != _ODPL_MISS_NR && queue_no != _ODPL_ACTION_NR && queue_no != _ODPL_SFLOW_NR);
 +      BUG_ON(queue_no != _XFLOWL_MISS_NR && queue_no != _XFLOWL_ACTION_NR && queue_no != _XFLOWL_SFLOW_NR);
        queue = &dp->queues[queue_no];
        err = -ENOBUFS;
        if (skb_queue_len(queue) >= DP_MAX_QUEUE_LEN)
@@@ -901,37 -905,36 +905,37 @@@ static int validate_actions(const struc
        unsigned int i;
  
        for (i = 0; i < actions->n_actions; i++) {
 -              const union odp_action *a = &actions->actions[i];
 +              const union xflow_action *a = &actions->actions[i];
 +              __be16 mask;
 +
                switch (a->type) {
 -              case ODPAT_OUTPUT:
 +              case XFLOWAT_OUTPUT:
                        if (a->output.port >= DP_MAX_PORTS)
                                return -EINVAL;
                        break;
  
 -              case ODPAT_OUTPUT_GROUP:
 +              case XFLOWAT_OUTPUT_GROUP:
                        if (a->output_group.group >= DP_MAX_GROUPS)
                                return -EINVAL;
                        break;
  
 -              case ODPAT_SET_VLAN_VID:
 -                      if (a->vlan_vid.vlan_vid & htons(~VLAN_VID_MASK))
 +              case XFLOWAT_SET_DL_TCI:
 +                      mask = a->dl_tci.mask;
 +                      if (mask != htons(VLAN_VID_MASK) &&
 +                          mask != htons(VLAN_PCP_MASK) &&
 +                          mask != htons(VLAN_VID_MASK | VLAN_PCP_MASK))
                                return -EINVAL;
 -                      break;
 -
 -              case ODPAT_SET_VLAN_PCP:
 -                      if (a->vlan_pcp.vlan_pcp
 -                          & ~(VLAN_PCP_MASK >> VLAN_PCP_SHIFT))
 +                      if (a->dl_tci.tci & ~mask)
                                return -EINVAL;
                        break;
  
 -              case ODPAT_SET_NW_TOS:
 +              case XFLOWAT_SET_NW_TOS:
                        if (a->nw_tos.nw_tos & INET_ECN_MASK)
                                return -EINVAL;
                        break;
  
                default:
 -                      if (a->type >= ODPAT_N_ACTIONS)
 +                      if (a->type >= XFLOWAT_N_ACTIONS)
                                return -EOPNOTSUPP;
                        break;
                }
        return 0;
  }
  
 -static struct sw_flow_actions *get_actions(const struct odp_flow *flow)
 +static struct sw_flow_actions *get_actions(const struct xflow_flow *flow)
  {
        struct sw_flow_actions *actions;
        int error;
  
        error = -EFAULT;
        if (copy_from_user(actions->actions, flow->actions,
 -                         flow->n_actions * sizeof(union odp_action)))
 +                         flow->n_actions * sizeof(union xflow_action)))
                goto error_free_actions;
        error = validate_actions(actions);
        if (error)
@@@ -975,7 -978,7 +979,7 @@@ static struct timespec get_time_offset(
        return timespec_sub(now_mono, now_jiffies);
  }
  
 -static void get_stats(struct sw_flow *flow, struct odp_flow_stats *stats,
 +static void get_stats(struct sw_flow *flow, struct xflow_flow_stats *stats,
                      struct timespec time_offset)
  {
        if (flow->used) {
@@@ -1022,14 -1025,16 +1026,14 @@@ static int expand_table(struct datapat
        return 0;
  }
  
 -static int do_put_flow(struct datapath *dp, struct odp_flow_put *uf,
 -                     struct odp_flow_stats *stats)
 +static int do_put_flow(struct datapath *dp, struct xflow_flow_put *uf,
 +                     struct xflow_flow_stats *stats)
  {
        struct tbl_node *flow_node;
        struct sw_flow *flow;
        struct tbl *table;
        int error;
  
 -      memset(uf->flow.key.reserved, 0, sizeof uf->flow.key.reserved);
 -
        table = rcu_dereference(dp->table);
        flow_node = tbl_lookup(table, &uf->flow.key, flow_hash(&uf->flow.key), flow_cmp);
        if (!flow_node) {
                struct sw_flow_actions *acts;
  
                error = -ENOENT;
 -              if (!(uf->flags & ODPPF_CREATE))
 +              if (!(uf->flags & XFLOWPF_CREATE))
                        goto error;
  
                /* Expand table, if necessary, to make room. */
                }
  
                /* Allocate flow. */
-               error = -ENOMEM;
-               flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
-               if (flow == NULL)
+               flow = flow_alloc();
+               if (IS_ERR(flow)) {
+                       error = PTR_ERR(flow);
                        goto error;
+               }
                flow->key = uf->flow.key;
-               spin_lock_init(&flow->lock);
                clear_stats(flow);
  
                /* Obtain actions. */
                if (error)
                        goto error_free_flow_acts;
  
 -              memset(stats, 0, sizeof(struct odp_flow_stats));
 +              memset(stats, 0, sizeof(struct xflow_flow_stats));
        } else {
                /* We found a matching flow. */
                struct sw_flow_actions *old_acts, *new_acts;
  
                /* Bail out if we're not allowed to modify an existing flow. */
                error = -EEXIST;
 -              if (!(uf->flags & ODPPF_MODIFY))
 +              if (!(uf->flags & XFLOWPF_MODIFY))
                        goto error;
  
                /* Swap actions. */
                old_acts = rcu_dereference(flow->sf_acts);
                if (old_acts->n_actions != new_acts->n_actions ||
                    memcmp(old_acts->actions, new_acts->actions,
 -                         sizeof(union odp_action) * old_acts->n_actions)) {
 +                         sizeof(union xflow_action) * old_acts->n_actions)) {
                        rcu_assign_pointer(flow->sf_acts, new_acts);
                        flow_deferred_free_acts(old_acts);
                } else {
                /* Fetch stats, then clear them if necessary. */
                spin_lock_bh(&flow->lock);
                get_stats(flow, stats, get_time_offset());
 -              if (uf->flags & ODPPF_ZERO_STATS)
 +              if (uf->flags & XFLOWPF_ZERO_STATS)
                        clear_stats(flow);
                spin_unlock_bh(&flow->lock);
        }
  error_free_flow_acts:
        kfree(flow->sf_acts);
  error_free_flow:
-       kmem_cache_free(flow_cache, flow);
+       flow->sf_acts = NULL;
+       flow_put(flow);
  error:
        return error;
  }
  
 -static int put_flow(struct datapath *dp, struct odp_flow_put __user *ufp)
 +static int put_flow(struct datapath *dp, struct xflow_flow_put __user *ufp)
  {
 -      struct odp_flow_stats stats;
 -      struct odp_flow_put uf;
 +      struct xflow_flow_stats stats;
 +      struct xflow_flow_put uf;
        int error;
  
 -      if (copy_from_user(&uf, ufp, sizeof(struct odp_flow_put)))
 +      if (copy_from_user(&uf, ufp, sizeof(struct xflow_flow_put)))
                return -EFAULT;
  
        error = do_put_flow(dp, &uf, &stats);
                return error;
  
        if (copy_to_user(&ufp->flow.stats, &stats,
 -                       sizeof(struct odp_flow_stats)))
 +                       sizeof(struct xflow_flow_stats)))
                return -EFAULT;
  
        return 0;
  
  static int do_answer_query(struct sw_flow *flow, u32 query_flags,
                           struct timespec time_offset,
 -                         struct odp_flow_stats __user *ustats,
 -                         union odp_action __user *actions,
 +                         struct xflow_flow_stats __user *ustats,
 +                         union xflow_action __user *actions,
                           u32 __user *n_actionsp)
  {
        struct sw_flow_actions *sf_acts;
 -      struct odp_flow_stats stats;
 +      struct xflow_flow_stats stats;
        u32 n_actions;
  
        spin_lock_bh(&flow->lock);
        get_stats(flow, &stats, time_offset);
 -      if (query_flags & ODPFF_ZERO_TCP_FLAGS)
 +      if (query_flags & XFLOWFF_ZERO_TCP_FLAGS)
                flow->tcp_flags = 0;
  
        spin_unlock_bh(&flow->lock);
  
 -      if (copy_to_user(ustats, &stats, sizeof(struct odp_flow_stats)) ||
 +      if (copy_to_user(ustats, &stats, sizeof(struct xflow_flow_stats)) ||
            get_user(n_actions, n_actionsp))
                return -EFAULT;
  
        sf_acts = rcu_dereference(flow->sf_acts);
        if (put_user(sf_acts->n_actions, n_actionsp) ||
            (actions && copy_to_user(actions, sf_acts->actions,
 -                                   sizeof(union odp_action) *
 +                                   sizeof(union xflow_action) *
                                     min(sf_acts->n_actions, n_actions))))
                return -EFAULT;
  
  
  static int answer_query(struct sw_flow *flow, u32 query_flags,
                        struct timespec time_offset,
 -                      struct odp_flow __user *ufp)
 +                      struct xflow_flow __user *ufp)
  {
 -      union odp_action *actions;
 +      union xflow_action *actions;
  
        if (get_user(actions, &ufp->actions))
                return -EFAULT;
                               &ufp->stats, actions, &ufp->n_actions);
  }
  
 -static struct sw_flow *do_del_flow(struct datapath *dp, struct odp_flow_key *key)
 +static struct sw_flow *do_del_flow(struct datapath *dp, struct xflow_key *key)
  {
        struct tbl *table = rcu_dereference(dp->table);
        struct tbl_node *flow_node;
        int error;
  
 -      memset(key->reserved, 0, sizeof key->reserved);
        flow_node = tbl_lookup(table, key, flow_hash(key), flow_cmp);
        if (!flow_node)
                return ERR_PTR(-ENOENT);
        return flow_cast(flow_node);
  }
  
 -static int del_flow(struct datapath *dp, struct odp_flow __user *ufp)
 +static int del_flow(struct datapath *dp, struct xflow_flow __user *ufp)
  {
        struct sw_flow *flow;
 -      struct odp_flow uf;
 +      struct xflow_flow uf;
        int error;
  
        if (copy_from_user(&uf, ufp, sizeof uf))
        return error;
  }
  
 -static int do_query_flows(struct datapath *dp, const struct odp_flowvec *flowvec)
 +static int do_query_flows(struct datapath *dp, const struct xflow_flowvec *flowvec)
  {
        struct tbl *table = rcu_dereference(dp->table);
        struct timespec time_offset;
        time_offset = get_time_offset();
  
        for (i = 0; i < flowvec->n_flows; i++) {
 -              struct odp_flow __user *ufp = &flowvec->flows[i];
 -              struct odp_flow uf;
 +              struct xflow_flow __user *ufp = &flowvec->flows[i];
 +              struct xflow_flow uf;
                struct tbl_node *flow_node;
                int error;
  
                if (copy_from_user(&uf, ufp, sizeof uf))
                        return -EFAULT;
 -              memset(uf.key.reserved, 0, sizeof uf.key.reserved);
  
                flow_node = tbl_lookup(table, &uf.key, flow_hash(&uf.key), flow_cmp);
                if (!flow_node)
  }
  
  struct list_flows_cbdata {
 -      struct odp_flow __user *uflows;
 +      struct xflow_flow __user *uflows;
        u32 n_flows;
        u32 listed_flows;
        struct timespec time_offset;
@@@ -1259,7 -1267,7 +1264,7 @@@ static int list_flow(struct tbl_node *n
  {
        struct sw_flow *flow = flow_cast(node);
        struct list_flows_cbdata *cbdata = cbdata_;
 -      struct odp_flow __user *ufp = &cbdata->uflows[cbdata->listed_flows++];
 +      struct xflow_flow __user *ufp = &cbdata->uflows[cbdata->listed_flows++];
        int error;
  
        if (copy_to_user(&ufp->key, &flow->key, sizeof flow->key))
        return 0;
  }
  
 -static int do_list_flows(struct datapath *dp, const struct odp_flowvec *flowvec)
 +static int do_list_flows(struct datapath *dp, const struct xflow_flowvec *flowvec)
  {
        struct list_flows_cbdata cbdata;
        int error;
  
  static int do_flowvec_ioctl(struct datapath *dp, unsigned long argp,
                            int (*function)(struct datapath *,
 -                                          const struct odp_flowvec *))
 +                                          const struct xflow_flowvec *))
  {
 -      struct odp_flowvec __user *uflowvec;
 -      struct odp_flowvec flowvec;
 +      struct xflow_flowvec __user *uflowvec;
 +      struct xflow_flowvec flowvec;
        int retval;
  
 -      uflowvec = (struct odp_flowvec __user *)argp;
 +      uflowvec = (struct xflow_flowvec __user *)argp;
        if (copy_from_user(&flowvec, uflowvec, sizeof flowvec))
                return -EFAULT;
  
 -      if (flowvec.n_flows > INT_MAX / sizeof(struct odp_flow))
 +      if (flowvec.n_flows > INT_MAX / sizeof(struct xflow_flow))
                return -EINVAL;
  
        retval = function(dp, &flowvec);
                : put_user(retval, &uflowvec->n_flows));
  }
  
 -static int do_execute(struct datapath *dp, const struct odp_execute *execute)
 +static int do_execute(struct datapath *dp, const struct xflow_execute *execute)
  {
 -      struct odp_flow_key key;
 +      struct xflow_key key;
        struct sk_buff *skb;
        struct sw_flow_actions *actions;
        struct ethhdr *eth;
+       bool is_frag;
        int err;
  
        err = -EINVAL;
        if (execute->length < ETH_HLEN || execute->length > 65535)
                goto error;
  
-       err = -ENOMEM;
        actions = flow_actions_alloc(execute->n_actions);
-       if (!actions)
+       if (IS_ERR(actions)) {
+               err = PTR_ERR(actions);
                goto error;
+       }
  
        err = -EFAULT;
        if (copy_from_user(actions->actions, execute->actions,
        else
                skb->protocol = htons(ETH_P_802_2);
  
-       err = flow_extract(skb, execute->in_port, &key);
+       err = flow_extract(skb, execute->in_port, &key, &is_frag);
        if (err)
                goto error_free_skb;
  
@@@ -1383,9 -1393,9 +1390,9 @@@ error
        return err;
  }
  
 -static int execute_packet(struct datapath *dp, const struct odp_execute __user *executep)
 +static int execute_packet(struct datapath *dp, const struct xflow_execute __user *executep)
  {
 -      struct odp_execute execute;
 +      struct xflow_execute execute;
  
        if (copy_from_user(&execute, executep, sizeof execute))
                return -EFAULT;
        return do_execute(dp, &execute);
  }
  
 -static int get_dp_stats(struct datapath *dp, struct odp_stats __user *statsp)
 +static int get_dp_stats(struct datapath *dp, struct xflow_stats __user *statsp)
  {
        struct tbl *table = rcu_dereference(dp->table);
 -      struct odp_stats stats;
 +      struct xflow_stats stats;
        int i;
  
        stats.n_flows = tbl_count(table);
@@@ -1469,9 -1479,9 +1476,9 @@@ void set_internal_devs_mtu(const struc
        }
  }
  
 -static int put_port(const struct dp_port *p, struct odp_port __user *uop)
 +static int put_port(const struct dp_port *p, struct xflow_port __user *uop)
  {
 -      struct odp_port op;
 +      struct xflow_port op;
  
        memset(&op, 0, sizeof op);
  
        rcu_read_unlock();
  
        op.port = p->port_no;
 -      op.flags = is_internal_vport(p->vport) ? ODP_PORT_INTERNAL : 0;
 +      op.flags = is_internal_vport(p->vport) ? XFLOW_PORT_INTERNAL : 0;
  
        return copy_to_user(uop, &op, sizeof op) ? -EFAULT : 0;
  }
  
 -static int query_port(struct datapath *dp, struct odp_port __user *uport)
 +static int query_port(struct datapath *dp, struct xflow_port __user *uport)
  {
 -      struct odp_port port;
 +      struct xflow_port port;
  
        if (copy_from_user(&port, uport, sizeof port))
                return -EFAULT;
@@@ -1532,7 -1542,7 +1539,7 @@@ error_unlock
        return put_port(dp->ports[port.port], uport);
  }
  
 -static int do_list_ports(struct datapath *dp, struct odp_port __user *uports,
 +static int do_list_ports(struct datapath *dp, struct xflow_port __user *uports,
                         int n_ports)
  {
        int idx = 0;
        return idx;
  }
  
 -static int list_ports(struct datapath *dp, struct odp_portvec __user *upv)
 +static int list_ports(struct datapath *dp, struct xflow_portvec __user *upv)
  {
 -      struct odp_portvec pv;
 +      struct xflow_portvec pv;
        int retval;
  
        if (copy_from_user(&pv, upv, sizeof pv))
@@@ -1604,9 -1614,9 +1611,9 @@@ error
  }
  
  static int set_port_group(struct datapath *dp,
 -                        const struct odp_port_group __user *upg)
 +                        const struct xflow_port_group __user *upg)
  {
 -      struct odp_port_group pg;
 +      struct xflow_port_group pg;
  
        if (copy_from_user(&pg, upg, sizeof pg))
                return -EFAULT;
@@@ -1635,9 -1645,9 +1642,9 @@@ static int do_get_port_group(struct dat
        return 0;
  }
  
 -static int get_port_group(struct datapath *dp, struct odp_port_group __user *upg)
 +static int get_port_group(struct datapath *dp, struct xflow_port_group __user *upg)
  {
 -      struct odp_port_group pg;
 +      struct xflow_port_group pg;
  
        if (copy_from_user(&pg, upg, sizeof pg))
                return -EFAULT;
@@@ -1666,58 -1676,58 +1673,58 @@@ static long openvswitch_ioctl(struct fi
  
        /* Handle commands with special locking requirements up front. */
        switch (cmd) {
 -      case ODP_DP_CREATE:
 +      case XFLOW_DP_CREATE:
                err = create_dp(dp_idx, (char __user *)argp);
                goto exit;
  
 -      case ODP_DP_DESTROY:
 +      case XFLOW_DP_DESTROY:
                err = destroy_dp(dp_idx);
                goto exit;
  
 -      case ODP_PORT_ATTACH:
 -              err = attach_port(dp_idx, (struct odp_port __user *)argp);
 +      case XFLOW_PORT_ATTACH:
 +              err = attach_port(dp_idx, (struct xflow_port __user *)argp);
                goto exit;
  
 -      case ODP_PORT_DETACH:
 +      case XFLOW_PORT_DETACH:
                err = get_user(port_no, (int __user *)argp);
                if (!err)
                        err = detach_port(dp_idx, port_no);
                goto exit;
  
 -      case ODP_VPORT_ADD:
 -              err = vport_user_add((struct odp_vport_add __user *)argp);
 +      case XFLOW_VPORT_ADD:
 +              err = vport_user_add((struct xflow_vport_add __user *)argp);
                goto exit;
  
 -      case ODP_VPORT_MOD:
 -              err = vport_user_mod((struct odp_vport_mod __user *)argp);
 +      case XFLOW_VPORT_MOD:
 +              err = vport_user_mod((struct xflow_vport_mod __user *)argp);
                goto exit;
  
 -      case ODP_VPORT_DEL:
 +      case XFLOW_VPORT_DEL:
                err = vport_user_del((char __user *)argp);
                goto exit;
  
 -      case ODP_VPORT_STATS_GET:
 -              err = vport_user_stats_get((struct odp_vport_stats_req __user *)argp);
 +      case XFLOW_VPORT_STATS_GET:
 +              err = vport_user_stats_get((struct xflow_vport_stats_req __user *)argp);
                goto exit;
  
 -      case ODP_VPORT_STATS_SET:
 -              err = vport_user_stats_set((struct odp_vport_stats_req __user *)argp);
 +      case XFLOW_VPORT_STATS_SET:
 +              err = vport_user_stats_set((struct xflow_vport_stats_req __user *)argp);
                goto exit;
  
 -      case ODP_VPORT_ETHER_GET:
 -              err = vport_user_ether_get((struct odp_vport_ether __user *)argp);
 +      case XFLOW_VPORT_ETHER_GET:
 +              err = vport_user_ether_get((struct xflow_vport_ether __user *)argp);
                goto exit;
  
 -      case ODP_VPORT_ETHER_SET:
 -              err = vport_user_ether_set((struct odp_vport_ether __user *)argp);
 +      case XFLOW_VPORT_ETHER_SET:
 +              err = vport_user_ether_set((struct xflow_vport_ether __user *)argp);
                goto exit;
  
 -      case ODP_VPORT_MTU_GET:
 -              err = vport_user_mtu_get((struct odp_vport_mtu __user *)argp);
 +      case XFLOW_VPORT_MTU_GET:
 +              err = vport_user_mtu_get((struct xflow_vport_mtu __user *)argp);
                goto exit;
  
 -      case ODP_VPORT_MTU_SET:
 -              err = vport_user_mtu_set((struct odp_vport_mtu __user *)argp);
 +      case XFLOW_VPORT_MTU_SET:
 +              err = vport_user_mtu_set((struct xflow_vport_mtu __user *)argp);
                goto exit;
        }
  
                goto exit;
  
        switch (cmd) {
 -      case ODP_DP_STATS:
 -              err = get_dp_stats(dp, (struct odp_stats __user *)argp);
 +      case XFLOW_DP_STATS:
 +              err = get_dp_stats(dp, (struct xflow_stats __user *)argp);
                break;
  
 -      case ODP_GET_DROP_FRAGS:
 +      case XFLOW_GET_DROP_FRAGS:
                err = put_user(dp->drop_frags, (int __user *)argp);
                break;
  
 -      case ODP_SET_DROP_FRAGS:
 +      case XFLOW_SET_DROP_FRAGS:
                err = get_user(drop_frags, (int __user *)argp);
                if (err)
                        break;
                err = 0;
                break;
  
 -      case ODP_GET_LISTEN_MASK:
 +      case XFLOW_GET_LISTEN_MASK:
                err = put_user(get_listen_mask(f), (int __user *)argp);
                break;
  
 -      case ODP_SET_LISTEN_MASK:
 +      case XFLOW_SET_LISTEN_MASK:
                err = get_user(listeners, (int __user *)argp);
                if (err)
                        break;
                err = -EINVAL;
 -              if (listeners & ~ODPL_ALL)
 +              if (listeners & ~XFLOWL_ALL)
                        break;
                err = 0;
                set_listen_mask(f, listeners);
                break;
  
 -      case ODP_GET_SFLOW_PROBABILITY:
 +      case XFLOW_GET_SFLOW_PROBABILITY:
                err = put_user(dp->sflow_probability, (unsigned int __user *)argp);
                break;
  
 -      case ODP_SET_SFLOW_PROBABILITY:
 +      case XFLOW_SET_SFLOW_PROBABILITY:
                err = get_user(sflow_probability, (unsigned int __user *)argp);
                if (!err)
                        dp->sflow_probability = sflow_probability;
                break;
  
 -      case ODP_PORT_QUERY:
 -              err = query_port(dp, (struct odp_port __user *)argp);
 +      case XFLOW_PORT_QUERY:
 +              err = query_port(dp, (struct xflow_port __user *)argp);
                break;
  
 -      case ODP_PORT_LIST:
 -              err = list_ports(dp, (struct odp_portvec __user *)argp);
 +      case XFLOW_PORT_LIST:
 +              err = list_ports(dp, (struct xflow_portvec __user *)argp);
                break;
  
 -      case ODP_PORT_GROUP_SET:
 -              err = set_port_group(dp, (struct odp_port_group __user *)argp);
 +      case XFLOW_PORT_GROUP_SET:
 +              err = set_port_group(dp, (struct xflow_port_group __user *)argp);
                break;
  
 -      case ODP_PORT_GROUP_GET:
 -              err = get_port_group(dp, (struct odp_port_group __user *)argp);
 +      case XFLOW_PORT_GROUP_GET:
 +              err = get_port_group(dp, (struct xflow_port_group __user *)argp);
                break;
  
 -      case ODP_FLOW_FLUSH:
 +      case XFLOW_FLOW_FLUSH:
                err = flush_flows(dp);
                break;
  
 -      case ODP_FLOW_PUT:
 -              err = put_flow(dp, (struct odp_flow_put __user *)argp);
 +      case XFLOW_FLOW_PUT:
 +              err = put_flow(dp, (struct xflow_flow_put __user *)argp);
                break;
  
 -      case ODP_FLOW_DEL:
 -              err = del_flow(dp, (struct odp_flow __user *)argp);
 +      case XFLOW_FLOW_DEL:
 +              err = del_flow(dp, (struct xflow_flow __user *)argp);
                break;
  
 -      case ODP_FLOW_GET:
 +      case XFLOW_FLOW_GET:
                err = do_flowvec_ioctl(dp, argp, do_query_flows);
                break;
  
 -      case ODP_FLOW_LIST:
 +      case XFLOW_FLOW_LIST:
                err = do_flowvec_ioctl(dp, argp, do_list_flows);
                break;
  
 -      case ODP_EXECUTE:
 -              err = execute_packet(dp, (struct odp_execute __user *)argp);
 +      case XFLOW_EXECUTE:
 +              err = execute_packet(dp, (struct xflow_execute __user *)argp);
                break;
  
        default:
@@@ -1831,9 -1841,9 +1838,9 @@@ static int dp_has_packet_of_interest(st
  }
  
  #ifdef CONFIG_COMPAT
 -static int compat_list_ports(struct datapath *dp, struct compat_odp_portvec __user *upv)
 +static int compat_list_ports(struct datapath *dp, struct compat_xflow_portvec __user *upv)
  {
 -      struct compat_odp_portvec pv;
 +      struct compat_xflow_portvec pv;
        int retval;
  
        if (copy_from_user(&pv, upv, sizeof pv))
        return put_user(retval, &upv->n_ports);
  }
  
 -static int compat_set_port_group(struct datapath *dp, const struct compat_odp_port_group __user *upg)
 +static int compat_set_port_group(struct datapath *dp, const struct compat_xflow_port_group __user *upg)
  {
 -      struct compat_odp_port_group pg;
 +      struct compat_xflow_port_group pg;
  
        if (copy_from_user(&pg, upg, sizeof pg))
                return -EFAULT;
        return do_set_port_group(dp, compat_ptr(pg.ports), pg.n_ports, pg.group);
  }
  
 -static int compat_get_port_group(struct datapath *dp, struct compat_odp_port_group __user *upg)
 +static int compat_get_port_group(struct datapath *dp, struct compat_xflow_port_group __user *upg)
  {
 -      struct compat_odp_port_group pg;
 +      struct compat_xflow_port_group pg;
  
        if (copy_from_user(&pg, upg, sizeof pg))
                return -EFAULT;
                                 pg.group, &upg->n_ports);
  }
  
 -static int compat_get_flow(struct odp_flow *flow, const struct compat_odp_flow __user *compat)
 +static int compat_get_flow(struct xflow_flow *flow, const struct compat_xflow_flow __user *compat)
  {
        compat_uptr_t actions;
  
 -      if (!access_ok(VERIFY_READ, compat, sizeof(struct compat_odp_flow)) ||
 -          __copy_from_user(&flow->stats, &compat->stats, sizeof(struct odp_flow_stats)) ||
 -          __copy_from_user(&flow->key, &compat->key, sizeof(struct odp_flow_key)) ||
 +      if (!access_ok(VERIFY_READ, compat, sizeof(struct compat_xflow_flow)) ||
 +          __copy_from_user(&flow->stats, &compat->stats, sizeof(struct xflow_flow_stats)) ||
 +          __copy_from_user(&flow->key, &compat->key, sizeof(struct xflow_key)) ||
            __get_user(actions, &compat->actions) ||
            __get_user(flow->n_actions, &compat->n_actions) ||
            __get_user(flow->flags, &compat->flags))
        return 0;
  }
  
 -static int compat_put_flow(struct datapath *dp, struct compat_odp_flow_put __user *ufp)
 +static int compat_put_flow(struct datapath *dp, struct compat_xflow_flow_put __user *ufp)
  {
 -      struct odp_flow_stats stats;
 -      struct odp_flow_put fp;
 +      struct xflow_flow_stats stats;
 +      struct xflow_flow_put fp;
        int error;
  
        if (compat_get_flow(&fp.flow, &ufp->flow) ||
                return error;
  
        if (copy_to_user(&ufp->flow.stats, &stats,
 -                       sizeof(struct odp_flow_stats)))
 +                       sizeof(struct xflow_flow_stats)))
                return -EFAULT;
  
        return 0;
  
  static int compat_answer_query(struct sw_flow *flow, u32 query_flags,
                               struct timespec time_offset,
 -                             struct compat_odp_flow __user *ufp)
 +                             struct compat_xflow_flow __user *ufp)
  {
        compat_uptr_t actions;
  
                               compat_ptr(actions), &ufp->n_actions);
  }
  
 -static int compat_del_flow(struct datapath *dp, struct compat_odp_flow __user *ufp)
 +static int compat_del_flow(struct datapath *dp, struct compat_xflow_flow __user *ufp)
  {
        struct sw_flow *flow;
 -      struct odp_flow uf;
 +      struct xflow_flow uf;
        int error;
  
        if (compat_get_flow(&uf, ufp))
        return error;
  }
  
 -static int compat_query_flows(struct datapath *dp, struct compat_odp_flow *flows, u32 n_flows)
 +static int compat_query_flows(struct datapath *dp, struct compat_xflow_flow *flows, u32 n_flows)
  {
        struct tbl *table = rcu_dereference(dp->table);
        struct timespec time_offset;
        time_offset = get_time_offset();
  
        for (i = 0; i < n_flows; i++) {
 -              struct compat_odp_flow __user *ufp = &flows[i];
 -              struct odp_flow uf;
 +              struct compat_xflow_flow __user *ufp = &flows[i];
 +              struct xflow_flow uf;
                struct tbl_node *flow_node;
                int error;
  
                if (compat_get_flow(&uf, ufp))
                        return -EFAULT;
 -              memset(uf.key.reserved, 0, sizeof uf.key.reserved);
  
                flow_node = tbl_lookup(table, &uf.key, flow_hash(&uf.key), flow_cmp);
                if (!flow_node)
  }
  
  struct compat_list_flows_cbdata {
 -      struct compat_odp_flow __user *uflows;
 +      struct compat_xflow_flow __user *uflows;
        u32 n_flows;
        u32 listed_flows;
        struct timespec time_offset;
@@@ -1974,7 -1985,7 +1981,7 @@@ static int compat_list_flow(struct tbl_
  {
        struct sw_flow *flow = flow_cast(node);
        struct compat_list_flows_cbdata *cbdata = cbdata_;
 -      struct compat_odp_flow __user *ufp = &cbdata->uflows[cbdata->listed_flows++];
 +      struct compat_xflow_flow __user *ufp = &cbdata->uflows[cbdata->listed_flows++];
        int error;
  
        if (copy_to_user(&ufp->key, &flow->key, sizeof flow->key))
        return 0;
  }
  
 -static int compat_list_flows(struct datapath *dp, struct compat_odp_flow *flows, u32 n_flows)
 +static int compat_list_flows(struct datapath *dp, struct compat_xflow_flow *flows, u32 n_flows)
  {
        struct compat_list_flows_cbdata cbdata;
        int error;
  
  static int compat_flowvec_ioctl(struct datapath *dp, unsigned long argp,
                                int (*function)(struct datapath *,
 -                                              struct compat_odp_flow *,
 +                                              struct compat_xflow_flow *,
                                                u32 n_flows))
  {
 -      struct compat_odp_flowvec __user *uflowvec;
 -      struct compat_odp_flow __user *flows;
 -      struct compat_odp_flowvec flowvec;
 +      struct compat_xflow_flowvec __user *uflowvec;
 +      struct compat_xflow_flow __user *flows;
 +      struct compat_xflow_flowvec flowvec;
        int retval;
  
        uflowvec = compat_ptr(argp);
            copy_from_user(&flowvec, uflowvec, sizeof flowvec))
                return -EFAULT;
  
 -      if (flowvec.n_flows > INT_MAX / sizeof(struct compat_odp_flow))
 +      if (flowvec.n_flows > INT_MAX / sizeof(struct compat_xflow_flow))
                return -EINVAL;
  
        flows = compat_ptr(flowvec.flows);
        if (!access_ok(VERIFY_WRITE, flows,
 -                     flowvec.n_flows * sizeof(struct compat_odp_flow)))
 +                     flowvec.n_flows * sizeof(struct compat_xflow_flow)))
                return -EFAULT;
  
        retval = function(dp, flows, flowvec.n_flows);
                : put_user(retval, &uflowvec->n_flows));
  }
  
 -static int compat_execute(struct datapath *dp, const struct compat_odp_execute __user *uexecute)
 +static int compat_execute(struct datapath *dp, const struct compat_xflow_execute __user *uexecute)
  {
 -      struct odp_execute execute;
 +      struct xflow_execute execute;
        compat_uptr_t actions;
        compat_uptr_t data;
  
 -      if (!access_ok(VERIFY_READ, uexecute, sizeof(struct compat_odp_execute)) ||
 +      if (!access_ok(VERIFY_READ, uexecute, sizeof(struct compat_xflow_execute)) ||
            __get_user(execute.in_port, &uexecute->in_port) ||
            __get_user(actions, &uexecute->actions) ||
            __get_user(execute.n_actions, &uexecute->n_actions) ||
@@@ -2061,36 -2072,36 +2068,36 @@@ static long openvswitch_compat_ioctl(st
        int err;
  
        switch (cmd) {
 -      case ODP_DP_DESTROY:
 -      case ODP_FLOW_FLUSH:
 +      case XFLOW_DP_DESTROY:
 +      case XFLOW_FLOW_FLUSH:
                /* Ioctls that don't need any translation at all. */
                return openvswitch_ioctl(f, cmd, argp);
  
 -      case ODP_DP_CREATE:
 -      case ODP_PORT_ATTACH:
 -      case ODP_PORT_DETACH:
 -      case ODP_VPORT_DEL:
 -      case ODP_VPORT_MTU_SET:
 -      case ODP_VPORT_MTU_GET:
 -      case ODP_VPORT_ETHER_SET:
 -      case ODP_VPORT_ETHER_GET:
 -      case ODP_VPORT_STATS_SET:
 -      case ODP_VPORT_STATS_GET:
 -      case ODP_DP_STATS:
 -      case ODP_GET_DROP_FRAGS:
 -      case ODP_SET_DROP_FRAGS:
 -      case ODP_SET_LISTEN_MASK:
 -      case ODP_GET_LISTEN_MASK:
 -      case ODP_SET_SFLOW_PROBABILITY:
 -      case ODP_GET_SFLOW_PROBABILITY:
 -      case ODP_PORT_QUERY:
 +      case XFLOW_DP_CREATE:
 +      case XFLOW_PORT_ATTACH:
 +      case XFLOW_PORT_DETACH:
 +      case XFLOW_VPORT_DEL:
 +      case XFLOW_VPORT_MTU_SET:
 +      case XFLOW_VPORT_MTU_GET:
 +      case XFLOW_VPORT_ETHER_SET:
 +      case XFLOW_VPORT_ETHER_GET:
 +      case XFLOW_VPORT_STATS_SET:
 +      case XFLOW_VPORT_STATS_GET:
 +      case XFLOW_DP_STATS:
 +      case XFLOW_GET_DROP_FRAGS:
 +      case XFLOW_SET_DROP_FRAGS:
 +      case XFLOW_SET_LISTEN_MASK:
 +      case XFLOW_GET_LISTEN_MASK:
 +      case XFLOW_SET_SFLOW_PROBABILITY:
 +      case XFLOW_GET_SFLOW_PROBABILITY:
 +      case XFLOW_PORT_QUERY:
                /* Ioctls that just need their pointer argument extended. */
                return openvswitch_ioctl(f, cmd, (unsigned long)compat_ptr(argp));
  
 -      case ODP_VPORT_ADD32:
 +      case XFLOW_VPORT_ADD32:
                return compat_vport_user_add(compat_ptr(argp));
  
 -      case ODP_VPORT_MOD32:
 +      case XFLOW_VPORT_MOD32:
                return compat_vport_user_mod(compat_ptr(argp));
        }
  
                goto exit;
  
        switch (cmd) {
 -      case ODP_PORT_LIST32:
 +      case XFLOW_PORT_LIST32:
                err = compat_list_ports(dp, compat_ptr(argp));
                break;
  
 -      case ODP_PORT_GROUP_SET32:
 +      case XFLOW_PORT_GROUP_SET32:
                err = compat_set_port_group(dp, compat_ptr(argp));
                break;
  
 -      case ODP_PORT_GROUP_GET32:
 +      case XFLOW_PORT_GROUP_GET32:
                err = compat_get_port_group(dp, compat_ptr(argp));
                break;
  
 -      case ODP_FLOW_PUT32:
 +      case XFLOW_FLOW_PUT32:
                err = compat_put_flow(dp, compat_ptr(argp));
                break;
  
 -      case ODP_FLOW_DEL32:
 +      case XFLOW_FLOW_DEL32:
                err = compat_del_flow(dp, compat_ptr(argp));
                break;
  
 -      case ODP_FLOW_GET32:
 +      case XFLOW_FLOW_GET32:
                err = compat_flowvec_ioctl(dp, argp, compat_query_flows);
                break;
  
 -      case ODP_FLOW_LIST32:
 +      case XFLOW_FLOW_LIST32:
                err = compat_flowvec_ioctl(dp, argp, compat_list_flows);
                break;
  
 -      case ODP_EXECUTE32:
 +      case XFLOW_EXECUTE32:
                err = compat_execute(dp, compat_ptr(argp));
                break;
  
diff --combined datapath/datapath.h
@@@ -73,14 -73,14 +73,14 @@@ struct dp_port_group 
   * @waitqueue: Waitqueue, for waiting for new packets in @queues.
   * @n_flows: Number of flows currently in flow table.
   * @table: Current flow table (RCU protected).
 - * @groups: Port groups, used by ODPAT_OUTPUT_GROUP action (RCU protected).
 + * @groups: Port groups, used by XFLOWAT_OUTPUT_GROUP action (RCU protected).
   * @n_ports: Number of ports currently in @ports.
 - * @ports: Map from port number to &struct dp_port.  %ODPP_LOCAL port
 + * @ports: Map from port number to &struct dp_port.  %XFLOWP_LOCAL port
   * always exists, other ports may be %NULL.
   * @port_list: List of all ports in @ports in arbitrary order.
   * @stats_percpu: Per-CPU datapath statistics.
   * @sflow_probability: Number of packets out of UINT_MAX to sample to the
 - * %ODPL_SFLOW queue, e.g. (@sflow_probability/UINT_MAX) is the probability of
 + * %XFLOWL_SFLOW queue, e.g. (@sflow_probability/UINT_MAX) is the probability of
   * sampling a given packet.
   */
  struct datapath {
@@@ -146,17 -146,17 +146,17 @@@ enum csum_type 
  /**
   * struct ovs_skb_cb - OVS data in skb CB
   * @dp_port: The datapath port on which the skb entered the switch.
+  * @flow: The flow associated with this packet.  May be %NULL if no flow.
   * @ip_summed: Consistently stores L4 checksumming status across different
   * kernel versions.
   * @tun_id: ID (in network byte order) of the tunnel that encapsulated this
   * packet. It is 0 if the packet was not received on a tunnel.
-  * @is_frag: %true if this packet is an IPv4 fragment, %false otherwise.
   */
  struct ovs_skb_cb {
        struct dp_port          *dp_port;
+       struct sw_flow          *flow;
        enum csum_type          ip_summed;
        __be32                  tun_id;
-       bool                    is_frag;
  };
  #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
  
diff --combined datapath/flow.c
@@@ -108,10 -108,13 +108,13 @@@ struct sw_flow_actions *flow_actions_al
  {
        struct sw_flow_actions *sfa;
  
-       if (n_actions > (PAGE_SIZE - sizeof *sfa) / sizeof(union xflow_action))
+       /* At least DP_MAX_PORTS actions are required to be able to flood a
+        * packet to every port.  Factor of 2 allows for setting VLAN tags,
+        * etc. */
+       if (n_actions > 2 * DP_MAX_PORTS)
                return ERR_PTR(-EINVAL);
  
 -      sfa = kmalloc(sizeof *sfa + n_actions * sizeof(union odp_action),
 +      sfa = kmalloc(sizeof *sfa + n_actions * sizeof(union xflow_action),
                      GFP_KERNEL);
        if (!sfa)
                return ERR_PTR(-ENOMEM);
        return sfa;
  }
  
- /* Frees 'flow' immediately. */
- static void flow_free(struct sw_flow *flow)
+ struct sw_flow *flow_alloc(void)
  {
-       if (unlikely(!flow))
-               return;
-       kfree(flow->sf_acts);
-       kmem_cache_free(flow_cache, flow);
+       struct sw_flow *flow;
+       flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
+       if (!flow)
+               return ERR_PTR(-ENOMEM);
+       spin_lock_init(&flow->lock);
+       atomic_set(&flow->refcnt, 1);
+       flow->dead = false;
+       return flow;
  }
  
  void flow_free_tbl(struct tbl_node *node)
  {
        struct sw_flow *flow = flow_cast(node);
-       flow_free(flow);
+       flow->dead = true;
+       flow_put(flow);
  }
  
  /* RCU callback used by flow_deferred_free. */
  static void rcu_free_flow_callback(struct rcu_head *rcu)
  {
        struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
-       flow_free(flow);
+       flow->dead = true;
+       flow_put(flow);
  }
  
  /* Schedules 'flow' to be freed after the next RCU grace period.
@@@ -150,6 -162,22 +162,22 @@@ void flow_deferred_free(struct sw_flow 
        call_rcu(&flow->rcu, rcu_free_flow_callback);
  }
  
+ void flow_hold(struct sw_flow *flow)
+ {
+       atomic_inc(&flow->refcnt);
+ }
+ void flow_put(struct sw_flow *flow)
+ {
+       if (unlikely(!flow))
+               return;
+       if (atomic_dec_and_test(&flow->refcnt)) {
+               kfree(flow->sf_acts);
+               kmem_cache_free(flow_cache, flow);
+       }
+ }
  /* RCU callback used by flow_deferred_free_acts. */
  static void rcu_free_acts_callback(struct rcu_head *rcu)
  {
@@@ -165,7 -193,7 +193,7 @@@ void flow_deferred_free_acts(struct sw_
        call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
  }
  
 -static void parse_vlan(struct sk_buff *skb, struct odp_flow_key *key)
 +static void parse_vlan(struct sk_buff *skb, struct xflow_key *key)
  {
        struct qtag_prefix {
                __be16 eth_type; /* ETH_P_8021Q */
                return;
  
        qp = (struct qtag_prefix *) skb->data;
 -      key->dl_vlan = qp->tci & htons(VLAN_VID_MASK);
 -      key->dl_vlan_pcp = (ntohs(qp->tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
 +      key->dl_tci = qp->tci | htons(XFLOW_TCI_PRESENT);
        __skb_pull(skb, sizeof(struct qtag_prefix));
  }
  
@@@ -196,17 -225,17 +224,17 @@@ static __be16 parse_ethertype(struct sk
        proto = *(__be16 *) skb->data;
        __skb_pull(skb, sizeof(__be16));
  
 -      if (ntohs(proto) >= ODP_DL_TYPE_ETH2_CUTOFF)
 +      if (ntohs(proto) >= XFLOW_DL_TYPE_ETH2_CUTOFF)
                return proto;
  
        if (unlikely(skb->len < sizeof(struct llc_snap_hdr)))
 -              return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
 +              return htons(XFLOW_DL_TYPE_NOT_ETH_TYPE);
  
        llc = (struct llc_snap_hdr *) skb->data;
        if (llc->dsap != LLC_SAP_SNAP ||
            llc->ssap != LLC_SAP_SNAP ||
            (llc->oui[0] | llc->oui[1] | llc->oui[2]) != 0)
 -              return htons(ODP_DL_TYPE_NOT_ETH_TYPE);
 +              return htons(XFLOW_DL_TYPE_NOT_ETH_TYPE);
  
        __skb_pull(skb, sizeof(struct llc_snap_hdr));
        return llc->ethertype;
   * Sets OVS_CB(skb)->is_frag to %true if @skb is an IPv4 fragment, otherwise to
   * %false.
   */
- int flow_extract(struct sk_buff *skb, u16 in_port, struct xflow_key *key)
 -int flow_extract(struct sk_buff *skb, u16 in_port, struct odp_flow_key *key,
++int flow_extract(struct sk_buff *skb, u16 in_port, struct xflow_key *key,
+                bool *is_frag)
  {
        struct ethhdr *eth;
  
        memset(key, 0, sizeof *key);
        key->tun_id = OVS_CB(skb)->tun_id;
        key->in_port = in_port;
-       OVS_CB(skb)->is_frag = false;
 -      key->dl_vlan = htons(ODP_VLAN_NONE);
+       *is_frag = false;
  
        /*
         * We would really like to pull as many bytes as we could possibly
        memcpy(key->dl_src, eth->h_source, ETH_ALEN);
        memcpy(key->dl_dst, eth->h_dest, ETH_ALEN);
  
 -      /* dl_type, dl_vlan, dl_vlan_pcp. */
 +      /* dl_type, dl_tci. */
        __skb_pull(skb, 2 * ETH_ALEN);
        if (eth->h_proto == htons(ETH_P_8021Q))
                parse_vlan(skb, key);
                                        key->tp_dst = htons(icmp->code);
                                }
                        }
-               } else {
-                       OVS_CB(skb)->is_frag = true;
-               }
+               } else
+                       *is_frag = true;
        } else if (key->dl_type == htons(ETH_P_ARP) && arphdr_ok(skb)) {
                struct arp_eth_header *arp;
  
                                && arp->ar_pln == 4) {
  
                        /* We only match on the lower 8 bits of the opcode. */
-                       if (ntohs(arp->ar_op) <= 0xff) {
+                       if (ntohs(arp->ar_op) <= 0xff)
                                key->nw_proto = ntohs(arp->ar_op);
-                       }
  
                        if (key->nw_proto == ARPOP_REQUEST
                                        || key->nw_proto == ARPOP_REPLY) {
        return 0;
  }
  
 -u32 flow_hash(const struct odp_flow_key *key)
 +u32 flow_hash(const struct xflow_key *key)
  {
        return jhash2((u32*)key, sizeof *key / sizeof(u32), hash_seed);
  }
  
  int flow_cmp(const struct tbl_node *node, void *key2_)
  {
 -      const struct odp_flow_key *key1 = &flow_cast(node)->key;
 -      const struct odp_flow_key *key2 = key2_;
 +      const struct xflow_key *key1 = &flow_cast(node)->key;
 +      const struct xflow_key *key2 = key2_;
  
 -      return !memcmp(key1, key2, sizeof(struct odp_flow_key));
 +      return !memcmp(key1, key2, sizeof(struct xflow_key));
  }
  
  /* Initializes the flow module.
diff --combined datapath/flow.h
@@@ -18,7 -18,7 +18,7 @@@
  #include <linux/jiffies.h>
  #include <linux/time.h>
  
 -#include "openvswitch/datapath-protocol.h"
 +#include "openvswitch/xflow.h"
  #include "table.h"
  
  struct sk_buff;
  struct sw_flow_actions {
        struct rcu_head rcu;
        unsigned int n_actions;
 -      union odp_action actions[];
 +      union xflow_action actions[];
  };
  
  struct sw_flow {
        struct rcu_head rcu;
        struct tbl_node tbl_node;
  
 -      struct odp_flow_key key;
 +      struct xflow_key key;
        struct sw_flow_actions *sf_acts;
  
+       atomic_t refcnt;
+       bool dead;
        spinlock_t lock;        /* Lock for values below. */
        unsigned long used;     /* Last used time (in jiffies). */
        u64 packet_count;       /* Number of packets matched. */
@@@ -58,20 -61,24 +61,24 @@@ struct arp_eth_heade
        unsigned char       ar_tip[4];          /* target IP address        */
  } __attribute__((packed));
  
- extern struct kmem_cache *flow_cache;
+ int flow_init(void);
+ void flow_exit(void);
  
- struct sw_flow_actions *flow_actions_alloc(size_t n_actions);
+ struct sw_flow *flow_alloc(void);
  void flow_deferred_free(struct sw_flow *);
+ void flow_free_tbl(struct tbl_node *);
+ struct sw_flow_actions *flow_actions_alloc(size_t n_actions);
  void flow_deferred_free_acts(struct sw_flow_actions *);
- int flow_extract(struct sk_buff *, u16 in_port, struct xflow_key *);
+ void flow_hold(struct sw_flow *);
+ void flow_put(struct sw_flow *);
 -int flow_extract(struct sk_buff *, u16 in_port, struct odp_flow_key *, bool *is_frag);
++int flow_extract(struct sk_buff *, u16 in_port, struct xflow_key *, bool *is_frag);
  void flow_used(struct sw_flow *, struct sk_buff *);
  
 -u32 flow_hash(const struct odp_flow_key *key);
 +u32 flow_hash(const struct xflow_key *key);
  int flow_cmp(const struct tbl_node *, void *target);
- void flow_free_tbl(struct tbl_node *);
- int flow_init(void);
- void flow_exit(void);
  
  static inline struct sw_flow *flow_cast(const struct tbl_node *node)
  {
diff --combined datapath/tunnel.c
@@@ -15,6 -15,7 +15,7 @@@
  #include <linux/jhash.h>
  #include <linux/kernel.h>
  #include <linux/version.h>
+ #include <linux/workqueue.h>
  
  #include <net/dsfield.h>
  #include <net/dst.h>
  #include "tunnel.h"
  #include "vport.h"
  #include "vport-generic.h"
+ #include "vport-internal_dev.h"
+ #ifdef NEED_CACHE_TIMEOUT
+ /*
+  * On kernels where we can't quickly detect changes in the rest of the system
+  * we use an expiration time to invalidate the cache.  A shorter expiration
+  * reduces the length of time that we may potentially blackhole packets while
+  * a longer time increases performance by reducing the frequency that the
+  * cache needs to be rebuilt.  A variety of factors may cause the cache to be
+  * invalidated before the expiration time but this is the maximum.  The time
+  * is expressed in jiffies.
+  */
+ #define MAX_CACHE_EXP HZ
+ #endif
+ /*
+  * Interval to check for and remove caches that are no longer valid.  Caches
+  * are checked for validity before they are used for packet encapsulation and
+  * old caches are removed at that time.  However, if no packets are sent through
+  * the tunnel then the cache will never be destroyed.  Since it holds
+  * references to a number of system objects, the cache will continue to use
+  * system resources by not allowing those objects to be destroyed.  The cache
+  * cleaner is periodically run to free invalid caches.  It does not
+  * significantly affect system performance.  A lower interval will release
+  * resources faster but will itself consume resources by requiring more frequent
+  * checks.  A longer interval may result in messages being printed to the kernel
+  * message buffer about unreleased resources.  The interval is expressed in
+  * jiffies.
+  */
+ #define CACHE_CLEANER_INTERVAL (5 * HZ)
+ #define CACHE_DATA_ALIGN 16
  
  /* Protected by RCU. */
  static struct tbl *port_table;
  
+ static void cache_cleaner(struct work_struct *work);
+ DECLARE_DELAYED_WORK(cache_cleaner_wq, cache_cleaner);
  /*
   * These are just used as an optimization: they don't require any kind of
   * synchronization because we could have just as easily read the value before
@@@ -63,22 -99,54 +99,54 @@@ static inline struct tnl_vport *tnl_vpo
        return container_of(node, struct tnl_vport, tbl_node);
  }
  
- /* RCU callback. */
- static void free_config(struct rcu_head *rcu)
+ static inline void schedule_cache_cleaner(void)
+ {
+       schedule_delayed_work(&cache_cleaner_wq, CACHE_CLEANER_INTERVAL);
+ }
+ static void free_cache(struct tnl_cache *cache)
+ {
+       if (!cache)
+               return;
+       flow_put(cache->flow);
+       ip_rt_put(cache->rt);
+       kfree(cache);
+ }
+ static void free_config_rcu(struct rcu_head *rcu)
  {
        struct tnl_mutable_config *c = container_of(rcu, struct tnl_mutable_config, rcu);
        kfree(c);
  }
  
+ static void free_cache_rcu(struct rcu_head *rcu)
+ {
+       struct tnl_cache *c = container_of(rcu, struct tnl_cache, rcu);
+       free_cache(c);
+ }
  static void assign_config_rcu(struct vport *vport,
                              struct tnl_mutable_config *new_config)
  {
        struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
        struct tnl_mutable_config *old_config;
  
-       old_config = rcu_dereference(tnl_vport->mutable);
+       old_config = tnl_vport->mutable;
        rcu_assign_pointer(tnl_vport->mutable, new_config);
-       call_rcu(&old_config->rcu, free_config);
+       call_rcu(&old_config->rcu, free_config_rcu);
+ }
+ static void assign_cache_rcu(struct vport *vport, struct tnl_cache *new_cache)
+ {
+       struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
+       struct tnl_cache *old_cache;
+       old_cache = tnl_vport->cache;
+       rcu_assign_pointer(tnl_vport->cache, new_cache);
+       if (old_cache)
+               call_rcu(&old_cache->rcu, free_cache_rcu);
  }
  
  static unsigned int *find_port_pool(const struct tnl_mutable_config *mutable)
@@@ -130,10 -198,32 +198,32 @@@ static u32 port_hash(struct port_lookup
        return jhash2(lookup->vals, ARRAY_SIZE(lookup->vals), 0);
  }
  
+ static u32 mutable_hash(const struct tnl_mutable_config *mutable)
+ {
+       struct port_lookup_key lookup;
+       lookup.vals[LOOKUP_SADDR] = mutable->port_config.saddr;
+       lookup.vals[LOOKUP_DADDR] = mutable->port_config.daddr;
+       lookup.vals[LOOKUP_KEY] = mutable->port_config.in_key;
+       lookup.vals[LOOKUP_TUNNEL_TYPE] = mutable->tunnel_type;
+       return port_hash(&lookup);
+ }
+ static void check_table_empty(void)
+ {
+       if (tbl_count(port_table) == 0) {
+               struct tbl *old_table = port_table;
+               cancel_delayed_work_sync(&cache_cleaner_wq);
+               rcu_assign_pointer(port_table, NULL);
+               tbl_deferred_destroy(old_table, NULL);
+       }
+ }
  static int add_port(struct vport *vport)
  {
        struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
-       struct port_lookup_key lookup;
        int err;
  
        if (!port_table) {
                        return -ENOMEM;
  
                rcu_assign_pointer(port_table, new_table);
+               schedule_cache_cleaner();
  
        } else if (tbl_count(port_table) > tbl_n_buckets(port_table)) {
                struct tbl *old_table = port_table;
                tbl_deferred_destroy(old_table, NULL);
        }
  
-       lookup.vals[LOOKUP_SADDR] = tnl_vport->mutable->port_config.saddr;
-       lookup.vals[LOOKUP_DADDR] = tnl_vport->mutable->port_config.daddr;
-       lookup.vals[LOOKUP_KEY] = tnl_vport->mutable->port_config.in_key;
-       lookup.vals[LOOKUP_TUNNEL_TYPE] = tnl_vport->mutable->tunnel_type;
+       err = tbl_insert(port_table, &tnl_vport->tbl_node, mutable_hash(tnl_vport->mutable));
+       if (err) {
+               check_table_empty();
+               return err;
+       }
+       (*find_port_pool(tnl_vport->mutable))++;
+       return 0;
+ }
+ static int move_port(struct vport *vport, struct tnl_mutable_config *new_mutable)
+ {
+       int err;
+       struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
+       u32 hash;
+       hash = mutable_hash(new_mutable);
+       if (hash == tnl_vport->tbl_node.hash)
+               goto table_updated;
  
-       err = tbl_insert(port_table, &tnl_vport->tbl_node, port_hash(&lookup));
+       /*
+        * Ideally we should make this move atomic to avoid having gaps in
+        * finding tunnels or the possibility of failure.  However, if we do
+        * find a tunnel it will always be consistent.
+        */
+       err = tbl_remove(port_table, &tnl_vport->tbl_node);
        if (err)
                return err;
  
-       (*find_port_pool(tnl_vport->mutable))++;
+       err = tbl_insert(port_table, &tnl_vport->tbl_node, hash);
+       if (err) {
+               check_table_empty();
+               return err;
+       }
+ table_updated:
+       assign_config_rcu(vport, new_mutable);
  
        return 0;
  }
@@@ -180,6 -299,7 +299,7 @@@ static int del_port(struct vport *vport
        if (err)
                return err;
  
+       check_table_empty();
        (*find_port_pool(tnl_vport->mutable))--;
  
        return 0;
@@@ -193,7 -313,7 +313,7 @@@ struct vport *tnl_find_port(__be32 sadd
        struct tbl *table = rcu_dereference(port_table);
        struct tbl_node *tbl_node;
  
-       if (!table)
+       if (unlikely(!table))
                return NULL;
  
        lookup.vals[LOOKUP_SADDR] = saddr;
@@@ -246,6 -366,60 +366,60 @@@ found
        return tnl_vport_to_vport(tnl_vport_table_cast(tbl_node));
  }
  
+ static inline void ecn_decapsulate(struct sk_buff *skb)
+ {
+       u8 tos = ip_hdr(skb)->tos;
+       if (INET_ECN_is_ce(tos)) {
+               __be16 protocol = skb->protocol;
+               unsigned int nw_header = skb_network_offset(skb);
+               if (skb->protocol == htons(ETH_P_8021Q)) {
+                       if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
+                               return;
+                       protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
+                       nw_header += VLAN_HLEN;
+               }
+               if (protocol == htons(ETH_P_IP)) {
+                       if (unlikely(!pskb_may_pull(skb, nw_header
+                           + sizeof(struct iphdr))))
+                               return;
+                       IP_ECN_set_ce((struct iphdr *)(skb->data + nw_header));
+               }
+ #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+               else if (protocol == htons(ETH_P_IPV6)) {
+                       if (unlikely(!pskb_may_pull(skb, nw_header
+                           + sizeof(struct ipv6hdr))))
+                               return;
+                       IP6_ECN_set_ce((struct ipv6hdr *)(skb->data + nw_header));
+               }
+ #endif
+       }
+ }
+ /* Called with rcu_read_lock. */
+ void tnl_rcv(struct vport *vport, struct sk_buff *skb)
+ {
+       skb->pkt_type = PACKET_HOST;
+       skb->protocol = eth_type_trans(skb, skb->dev);
+       skb_dst_drop(skb);
+       nf_reset(skb);
+       secpath_reset(skb);
+       skb_reset_network_header(skb);
+       ecn_decapsulate(skb);
+       skb_push(skb, ETH_HLEN);
+       compute_ip_summed(skb, false);
+       vport_receive(vport, skb);
+ }
  static bool check_ipv4_address(__be32 addr)
  {
        if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr)
@@@ -514,179 -688,412 +688,412 @@@ bool tnl_frag_needed(struct vport *vpor
        return true;
  }
  
- static struct sk_buff *check_headroom(struct sk_buff *skb, int headroom)
+ static bool check_mtu(struct sk_buff *skb,
+                     struct vport *vport,
+                     const struct tnl_mutable_config *mutable,
+                     const struct rtable *rt, __be16 *frag_offp)
  {
-       if (skb_headroom(skb) < headroom || skb_header_cloned(skb)) {
-               struct sk_buff *nskb = skb_realloc_headroom(skb, headroom + 16);
-               if (unlikely(!nskb)) {
-                       kfree_skb(skb);
-                       return ERR_PTR(-ENOMEM);
+       int mtu;
+       __be16 frag_off;
+       frag_off = (mutable->port_config.flags & TNL_F_PMTUD) ? htons(IP_DF) : 0;
+       if (frag_off)
+               mtu = dst_mtu(&rt_dst(rt))
+                       - ETH_HLEN
+                       - mutable->tunnel_hlen
+                       - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
+       else
+               mtu = mutable->mtu;
+       if (skb->protocol == htons(ETH_P_IP)) {
+               struct iphdr *old_iph = ip_hdr(skb);
+               frag_off |= old_iph->frag_off & htons(IP_DF);
+               mtu = max(mtu, IP_MIN_MTU);
+               if ((old_iph->frag_off & htons(IP_DF)) &&
+                   mtu < ntohs(old_iph->tot_len)) {
+                       if (tnl_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
+                               goto drop;
                }
+       }
+ #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+       else if (skb->protocol == htons(ETH_P_IPV6)) {
+               unsigned int packet_length = skb->len - ETH_HLEN
+                       - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
  
-               set_skb_csum_bits(skb, nskb);
+               mtu = max(mtu, IPV6_MIN_MTU);
  
-               if (skb->sk)
-                       skb_set_owner_w(nskb, skb->sk);
+               /* IPv6 requires PMTUD if the packet is above the minimum MTU. */
+               if (packet_length > IPV6_MIN_MTU)
+                       frag_off = htons(IP_DF);
  
-               dev_kfree_skb(skb);
-               return nskb;
+               if (mtu < packet_length) {
+                       if (tnl_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
+                               goto drop;
+               }
        }
+ #endif
  
-       return skb;
+       *frag_offp = frag_off;
+       return true;
+ drop:
+       *frag_offp = 0;
+       return false;
+ }
+ static void create_tunnel_header(const struct vport *vport,
+                                const struct tnl_mutable_config *mutable,
+                                const struct rtable *rt, void *header)
+ {
+       struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
+       struct iphdr *iph = header;
+       iph->version    = 4;
+       iph->ihl        = sizeof(struct iphdr) >> 2;
+       iph->frag_off   = htons(IP_DF);
+       iph->protocol   = tnl_vport->tnl_ops->ipproto;
+       iph->tos        = mutable->port_config.tos;
+       iph->daddr      = rt->rt_dst;
+       iph->saddr      = rt->rt_src;
+       iph->ttl        = mutable->port_config.ttl;
+       if (!iph->ttl)
+               iph->ttl = dst_metric(&rt_dst(rt), RTAX_HOPLIMIT);
+       tnl_vport->tnl_ops->build_header(vport, mutable, iph + 1);
  }
  
- static inline u8 ecn_encapsulate(u8 tos, struct sk_buff *skb)
+ static inline void *get_cached_header(const struct tnl_cache *cache)
  {
-       u8 inner;
+       return (void *)cache + ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN);
+ }
  
-       if (skb->protocol == htons(ETH_P_IP))
-               inner = ((struct iphdr *)skb_network_header(skb))->tos;
- #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
-       else if (skb->protocol == htons(ETH_P_IPV6))
-               inner = ipv6_get_dsfield((struct ipv6hdr *)skb_network_header(skb));
+ static inline bool check_cache_valid(const struct tnl_cache *cache,
+                                    const struct tnl_mutable_config *mutable)
+ {
+       return cache &&
+ #ifdef NEED_CACHE_TIMEOUT
+               time_before(jiffies, cache->expiration) &&
  #endif
-       else
-               inner = 0;
-       return INET_ECN_encapsulate(tos, inner);
+ #ifdef HAVE_RT_GENID
+               atomic_read(&init_net.ipv4.rt_genid) == cache->rt->rt_genid &&
+ #endif
+ #ifdef HAVE_HH_SEQ
+               rt_dst(cache->rt).hh->hh_lock.sequence == cache->hh_seq &&
+ #endif
+               mutable->seq == cache->mutable_seq &&
+               (!is_internal_dev(rt_dst(cache->rt).dev) ||
+               (cache->flow && !cache->flow->dead));
  }
  
- static inline void ecn_decapsulate(struct sk_buff *skb)
+ static int cache_cleaner_cb(struct tbl_node *tbl_node, void *aux)
  {
-       u8 tos = ip_hdr(skb)->tos;
+       struct tnl_vport *tnl_vport = tnl_vport_table_cast(tbl_node);
+       const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
+       const struct tnl_cache *cache = rcu_dereference(tnl_vport->cache);
  
-       if (INET_ECN_is_ce(tos)) {
-               __be16 protocol = skb->protocol;
-               unsigned int nw_header = skb_network_header(skb) - skb->data;
+       if (cache && !check_cache_valid(cache, mutable) &&
+           spin_trylock_bh(&tnl_vport->cache_lock)) {
+               assign_cache_rcu(tnl_vport_to_vport(tnl_vport), NULL);
+               spin_unlock_bh(&tnl_vport->cache_lock);
+       }
  
-               if (skb->protocol == htons(ETH_P_8021Q)) {
-                       if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
-                               return;
+       return 0;
+ }
  
-                       protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
-                       nw_header += VLAN_HLEN;
-               }
+ static void cache_cleaner(struct work_struct *work)
+ {
+       schedule_cache_cleaner();
  
-               if (protocol == htons(ETH_P_IP)) {
-                       if (unlikely(!pskb_may_pull(skb, nw_header
-                           + sizeof(struct iphdr))))
-                               return;
+       rcu_read_lock();
+       tbl_foreach(port_table, cache_cleaner_cb, NULL);
+       rcu_read_unlock();
+ }
  
-                       IP_ECN_set_ce((struct iphdr *)(nw_header + skb->data));
-               }
- #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
-               else if (protocol == htons(ETH_P_IPV6)) {
-                       if (unlikely(!pskb_may_pull(skb, nw_header
-                           + sizeof(struct ipv6hdr))))
-                               return;
+ static inline void create_eth_hdr(struct tnl_cache *cache,
+                                 const struct rtable *rt)
+ {
+       void *cache_data = get_cached_header(cache);
+       int hh_len = rt_dst(rt).hh->hh_len;
+       int hh_off = HH_DATA_ALIGN(rt_dst(rt).hh->hh_len) - hh_len;
  
-                       IP6_ECN_set_ce((struct ipv6hdr *)(nw_header
-                                                         + skb->data));
-               }
+ #ifdef HAVE_HH_SEQ
+       unsigned hh_seq;
+       do {
+               hh_seq = read_seqbegin(&rt_dst(rt).hh->hh_lock);
+               memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
+       } while (read_seqretry(&rt_dst(rt).hh->hh_lock, hh_seq));
+       cache->hh_seq = hh_seq;
+ #else
+       read_lock_bh(&rt_dst(rt).hh->hh_lock);
+       memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
+       read_unlock_bh(&rt_dst(rt).hh->hh_lock);
  #endif
-       }
  }
  
- static struct sk_buff *handle_gso(struct sk_buff *skb)
+ static struct tnl_cache *build_cache(struct vport *vport,
+                                    const struct tnl_mutable_config *mutable,
+                                    struct rtable *rt)
  {
-       if (skb_is_gso(skb)) {
-               struct sk_buff *nskb = skb_gso_segment(skb, 0);
+       struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
+       struct tnl_cache *cache;
+       void *cache_data;
+       int cache_len;
  
-               dev_kfree_skb(skb);
-               return nskb;
+       if (!(mutable->port_config.flags & TNL_F_HDR_CACHE))
+               return NULL;
+       /*
+        * If there is no entry in the ARP cache or if this device does not
+        * support hard header caching just fall back to the IP stack.
+        */
+       if (!rt_dst(rt).hh)
+               return NULL;
+       /*
+        * If lock is contended fall back to directly building the header.
+        * We're not going to help performance by sitting here spinning.
+        */
+       if (!spin_trylock_bh(&tnl_vport->cache_lock))
+               return NULL;
+       cache = tnl_vport->cache;
+       if (check_cache_valid(cache, mutable))
+               goto unlock;
+       else
+               cache = NULL;
+       cache_len = rt_dst(rt).hh->hh_len + mutable->tunnel_hlen;
+       cache = kzalloc(ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN) +
+                       cache_len, GFP_ATOMIC);
+       if (!cache)
+               goto unlock;
+       cache->len = cache_len;
+       create_eth_hdr(cache, rt);
+       cache_data = get_cached_header(cache) + rt_dst(rt).hh->hh_len;
+       create_tunnel_header(vport, mutable, rt, cache_data);
+       cache->mutable_seq = mutable->seq;
+       cache->rt = rt;
+ #ifdef NEED_CACHE_TIMEOUT
+       cache->expiration = jiffies + tnl_vport->cache_exp_interval;
+ #endif
+       if (is_internal_dev(rt_dst(rt).dev)) {
+               int err;
+               struct vport *vport;
+               struct dp_port *dp_port;
+               struct sk_buff *skb;
+               bool is_frag;
 -              struct odp_flow_key flow_key;
++              struct xflow_key flow_key;
+               struct tbl_node *flow_node;
+               vport = internal_dev_get_vport(rt_dst(rt).dev);
+               if (!vport)
+                       goto done;
+               dp_port = vport_get_dp_port(vport);
+               if (!dp_port)
+                       goto done;
+               skb = alloc_skb(cache->len, GFP_ATOMIC);
+               if (!skb)
+                       goto done;
+               __skb_put(skb, cache->len);
+               memcpy(skb->data, get_cached_header(cache), cache->len);
+               err = flow_extract(skb, dp_port->port_no, &flow_key, &is_frag);
+               kfree_skb(skb);
+               if (err || is_frag)
+                       goto done;
+               flow_node = tbl_lookup(rcu_dereference(dp_port->dp->table),
+                                      &flow_key, flow_hash(&flow_key),
+                                      flow_cmp);
+               if (flow_node) {
+                       struct sw_flow *flow = flow_cast(flow_node);
+                       cache->flow = flow;
+                       flow_hold(flow);
+               }
        }
  
-       return skb;
+ done:
+       assign_cache_rcu(vport, cache);
+ unlock:
+       spin_unlock_bh(&tnl_vport->cache_lock);
+       return cache;
  }
  
- static int handle_csum_offload(struct sk_buff *skb)
+ static struct rtable *find_route(struct vport *vport,
+                                const struct tnl_mutable_config *mutable,
+                                u8 tos, struct tnl_cache **cache)
  {
-       if (skb->ip_summed == CHECKSUM_PARTIAL)
-               return skb_checksum_help(skb);
-       else {
-               skb->ip_summed = CHECKSUM_NONE;
-               return 0;
+       struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
+       struct tnl_cache *cur_cache = rcu_dereference(tnl_vport->cache);
+       *cache = NULL;
+       tos = RT_TOS(tos);
+       if (likely(tos == mutable->port_config.tos &&
+                  check_cache_valid(cur_cache, mutable))) {
+               *cache = cur_cache;
+               return cur_cache->rt;
+       } else {
+               struct rtable *rt;
+               struct flowi fl = { .nl_u = { .ip4_u =
+                                             { .daddr = mutable->port_config.daddr,
+                                               .saddr = mutable->port_config.saddr,
+                                               .tos = tos } },
+                                   .proto = tnl_vport->tnl_ops->ipproto };
+               if (unlikely(ip_route_output_key(&init_net, &rt, &fl)))
+                       return NULL;
+               if (likely(tos == mutable->port_config.tos))
+                       *cache = build_cache(vport, mutable, rt);
+               return rt;
        }
  }
  
- /* Called with rcu_read_lock. */
- void tnl_rcv(struct vport *vport, struct sk_buff *skb)
+ static struct sk_buff *check_headroom(struct sk_buff *skb, int headroom)
  {
-       skb->pkt_type = PACKET_HOST;
-       skb->protocol = eth_type_trans(skb, skb->dev);
+       if (skb_headroom(skb) < headroom || skb_header_cloned(skb)) {
+               struct sk_buff *nskb = skb_realloc_headroom(skb, headroom + 16);
+               if (unlikely(!nskb)) {
+                       kfree_skb(skb);
+                       return ERR_PTR(-ENOMEM);
+               }
  
-       skb_dst_drop(skb);
-       nf_reset(skb);
-       secpath_reset(skb);
-       skb_reset_network_header(skb);
+               set_skb_csum_bits(skb, nskb);
  
-       ecn_decapsulate(skb);
+               if (skb->sk)
+                       skb_set_owner_w(nskb, skb->sk);
  
-       skb_push(skb, ETH_HLEN);
-       compute_ip_summed(skb, false);
+               kfree_skb(skb);
+               return nskb;
+       }
  
-       vport_receive(vport, skb);
+       return skb;
  }
  
- static int build_packet(struct vport *vport, const struct tnl_mutable_config *mutable,
-                       struct iphdr *iph, struct rtable *rt, int max_headroom,
-                       int mtu, struct sk_buff *skb)
+ static inline bool need_linearize(const struct sk_buff *skb)
  {
-       struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
+       int i;
+       if (unlikely(skb_shinfo(skb)->frag_list))
+               return true;
+       /*
+        * Generally speaking we should linearize if there are paged frags.
+        * However, if all of the refcounts are 1 we know nobody else can
+        * change them from underneath us and we can skip the linearization.
+        */
+       for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
+               if (unlikely(page_count(skb_shinfo(skb)->frags[0].page) > 1))
+                       return true;
+       return false;
+ }
+ static struct sk_buff *handle_offloads(struct sk_buff *skb,
+                                      const struct tnl_mutable_config *mutable,
+                                      const struct rtable *rt)
+ {
+       int min_headroom;
        int err;
-       struct iphdr *new_iph;
-       int orig_len = skb->len;
-       __be16 frag_off = iph->frag_off;
  
-       skb = check_headroom(skb, max_headroom);
-       if (unlikely(IS_ERR(skb)))
-               goto error;
+       forward_ip_summed(skb);
  
-       err = handle_csum_offload(skb);
+       err = vswitch_skb_checksum_setup(skb);
        if (unlikely(err))
                goto error_free;
  
-       if (skb->protocol == htons(ETH_P_IP)) {
-               struct iphdr *old_iph = ip_hdr(skb);
+       min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
+                       + mutable->tunnel_hlen;
  
-               if ((old_iph->frag_off & htons(IP_DF)) &&
-                   mtu < ntohs(old_iph->tot_len)) {
-                       if (tnl_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
-                               goto error_free;
+       if (skb_is_gso(skb)) {
+               struct sk_buff *nskb;
+               /*
+                * If we are doing GSO on a pskb it is better to make sure that
+                * the headroom is correct now.  We will only have to copy the
+                * portion in the linear data area and GSO will preserve
+                * headroom when it creates the segments.  This is particularly
+                * beneficial on Xen where we get a lot of GSO pskbs.
+                * Conversely, we avoid copying if it is just to get our own
+                * writable clone because GSO will do the copy for us.
+                */
+               if (skb_headroom(skb) < min_headroom) {
+                       skb = check_headroom(skb, min_headroom);
+                       if (unlikely(IS_ERR(skb))) {
+                               err = PTR_ERR(skb);
+                               goto error;
+                       }
                }
  
-       }
- #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
-       else if (skb->protocol == htons(ETH_P_IPV6)) {
-               unsigned int packet_length = skb->len - ETH_HLEN
-                       - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
+               nskb = skb_gso_segment(skb, 0);
+               kfree_skb(skb);
+               if (unlikely(IS_ERR(nskb))) {
+                       err = PTR_ERR(nskb);
+                       goto error;
+               }
  
-               /* IPv6 requires PMTUD if the packet is above the minimum MTU. */
-               if (packet_length > IPV6_MIN_MTU)
-                       frag_off = htons(IP_DF);
+               skb = nskb;
+       } else {
+               skb = check_headroom(skb, min_headroom);
+               if (unlikely(IS_ERR(skb))) {
+                       err = PTR_ERR(skb);
+                       goto error;
+               }
  
-               if (mtu < packet_length) {
-                       if (tnl_frag_needed(vport, mutable, skb, mtu, OVS_CB(skb)->tun_id))
+               if (skb->ip_summed == CHECKSUM_PARTIAL) {
+                       /*
+                        * Pages aren't locked and could change at any time.
+                        * If this happens after we compute the checksum, the
+                        * checksum will be wrong.  We linearize now to avoid
+                        * this problem.
+                        */
+                       if (unlikely(need_linearize(skb))) {
+                               err = __skb_linearize(skb);
+                               if (unlikely(err))
+                                       goto error_free;
+                       }
+                       err = skb_checksum_help(skb);
+                       if (unlikely(err))
                                goto error_free;
-               }
+               } else if (skb->ip_summed == CHECKSUM_COMPLETE)
+                       skb->ip_summed = CHECKSUM_NONE;
        }
- #endif
  
-       new_iph = (struct iphdr *)skb_push(skb, mutable->tunnel_hlen);
-       skb_reset_network_header(skb);
-       skb_set_transport_header(skb, sizeof(struct iphdr));
-       memcpy(new_iph, iph, sizeof(struct iphdr));
-       new_iph->frag_off = frag_off;
-       ip_select_ident(new_iph, &rt_dst(rt), NULL);
+       return skb;
  
-       memset(&IPCB(skb)->opt, 0, sizeof(IPCB(skb)->opt));
-       IPCB(skb)->flags = 0;
+ error_free:
+       kfree_skb(skb);
+ error:
+       return ERR_PTR(err);
+ }
  
-       skb = tnl_vport->tnl_ops->build_header(skb, vport, mutable, &rt_dst(rt));
-       if (unlikely(!skb))
-               goto error;
+ static int send_frags(struct sk_buff *skb,
+                     const struct tnl_mutable_config *mutable)
+ {
+       int sent_len;
+       int err;
  
+       sent_len = 0;
        while (skb) {
                struct sk_buff *next = skb->next;
                int frag_len = skb->len - mutable->tunnel_hlen;
                skb->next = NULL;
  
                err = ip_local_out(skb);
-               if (unlikely(net_xmit_eval(err) != 0)) {
-                       orig_len -= frag_len;
+               if (likely(net_xmit_eval(err) == 0))
+                       sent_len += frag_len;
+               else {
                        skb = next;
                        goto free_frags;
                }
  
                skb = next;
-       };
+       }
  
-       return orig_len;
+       return sent_len;
  
- error_free:
-       kfree_skb(skb);
- error:
-       return 0;
  free_frags:
        /*
         * There's no point in continuing to send fragments once one has been
         * dropped so just free the rest.  This may help improve the congestion
         * that caused the first packet to be dropped.
         */
-       while (skb) {
-               struct sk_buff *next = skb->next;
-               orig_len -= skb->len - mutable->tunnel_hlen;
-               kfree_skb(skb);
-               skb = next;
-       };
-       return orig_len;
+       tnl_free_linked_skbs(skb);
+       return sent_len;
  }
  
  int tnl_send(struct vport *vport, struct sk_buff *skb)
        struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
        const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
  
-       struct iphdr *old_iph;
-       int orig_len;
-       struct iphdr iph;
+       enum vport_err_type err = VPORT_E_TX_ERROR;
        struct rtable *rt;
-       int max_headroom;
-       int mtu;
+       struct dst_entry *unattached_dst = NULL;
+       struct tnl_cache *cache;
+       int sent_len = 0;
+       __be16 frag_off;
+       u8 ttl;
+       u8 inner_tos;
+       u8 tos;
  
        /* Validate the protocol headers before we try to use them. */
        if (skb->protocol == htons(ETH_P_8021Q)) {
        }
  
        if (skb->protocol == htons(ETH_P_IP)) {
-               if (unlikely(!pskb_may_pull(skb, skb_network_header(skb)
-                   + sizeof(struct iphdr) - skb->data)))
+               if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
+                   + sizeof(struct iphdr))))
                        skb->protocol = 0;
        }
  #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
        else if (skb->protocol == htons(ETH_P_IPV6)) {
-               if (unlikely(!pskb_may_pull(skb, skb_network_header(skb)
-                   + sizeof(struct ipv6hdr) - skb->data)))
+               if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
+                   + sizeof(struct ipv6hdr))))
                        skb->protocol = 0;
        }
  #endif
-       old_iph = ip_hdr(skb);
-       iph.tos = mutable->port_config.tos;
-       if (mutable->port_config.flags & TNL_F_TOS_INHERIT) {
-               if (skb->protocol == htons(ETH_P_IP))
-                       iph.tos = old_iph->tos;
- #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
-               else if (skb->protocol == htons(ETH_P_IPV6))
-                       iph.tos = ipv6_get_dsfield(ipv6_hdr(skb));
- #endif
-       }
-       iph.tos = ecn_encapsulate(iph.tos, skb);
-       {
-               struct flowi fl = { .nl_u = { .ip4_u =
-                                             { .daddr = mutable->port_config.daddr,
-                                               .saddr = mutable->port_config.saddr,
-                                               .tos = RT_TOS(iph.tos) } },
-                                   .proto = tnl_vport->tnl_ops->ipproto };
-               if (unlikely(ip_route_output_key(&init_net, &rt, &fl)))
-                       goto error_free;
-       }
  
-       iph.ttl = mutable->port_config.ttl;
-       if (mutable->port_config.flags & TNL_F_TTL_INHERIT) {
-               if (skb->protocol == htons(ETH_P_IP))
-                       iph.ttl = old_iph->ttl;
+       /* ToS */
+       if (skb->protocol == htons(ETH_P_IP))
+               inner_tos = ip_hdr(skb)->tos;
  #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
-               else if (skb->protocol == htons(ETH_P_IPV6))
-                       iph.ttl = ipv6_hdr(skb)->hop_limit;
+       else if (skb->protocol == htons(ETH_P_IPV6))
+               inner_tos = ipv6_get_dsfield(ipv6_hdr(skb));
  #endif
-       }
-       if (!iph.ttl)
-               iph.ttl = dst_metric(&rt_dst(rt), RTAX_HOPLIMIT);
+       else
+               inner_tos = 0;
  
-       iph.frag_off = (mutable->port_config.flags & TNL_F_PMTUD) ? htons(IP_DF) : 0;
-       if (iph.frag_off)
-               mtu = dst_mtu(&rt_dst(rt))
-                       - ETH_HLEN
-                       - mutable->tunnel_hlen
-                       - (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q) ? VLAN_HLEN : 0);
+       if (mutable->port_config.flags & TNL_F_TOS_INHERIT)
+               tos = inner_tos;
        else
-               mtu = mutable->mtu;
+               tos = mutable->port_config.tos;
  
-       if (skb->protocol == htons(ETH_P_IP)) {
-               iph.frag_off |= old_iph->frag_off & htons(IP_DF);
-               mtu = max(mtu, IP_MIN_MTU);
-       }
- #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
-       else if (skb->protocol == htons(ETH_P_IPV6))
-               mtu = max(mtu, IPV6_MIN_MTU);
- #endif
+       tos = INET_ECN_encapsulate(tos, inner_tos);
  
-       iph.version = 4;
-       iph.ihl = sizeof(struct iphdr) >> 2;
-       iph.protocol = tnl_vport->tnl_ops->ipproto;
-       iph.daddr = rt->rt_dst;
-       iph.saddr = rt->rt_src;
+       /* Route lookup */
+       rt = find_route(vport, mutable, tos, &cache);
+       if (unlikely(!rt))
+               goto error_free;
+       if (unlikely(!cache))
+               unattached_dst = &rt_dst(rt);
  
+       /* Reset SKB */
        nf_reset(skb);
        secpath_reset(skb);
        skb_dst_drop(skb);
-       skb_dst_set(skb, &rt_dst(rt));
  
-       /*
-        * If we are doing GSO on a pskb it is better to make sure that the
-        * headroom is correct now.  We will only have to copy the portion in
-        * the linear data area and GSO will preserve headroom when it creates
-        * the segments.  This is particularly beneficial on Xen where we get
-        * lots of GSO pskbs.  Conversely, we delay copying if it is just to
-        * get our own writable clone because GSO may do the copy for us.
-        */
-       max_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
-                       + mutable->tunnel_hlen;
+       /* Offloading */
+       skb = handle_offloads(skb, mutable, rt);
+       if (unlikely(IS_ERR(skb)))
+               goto error;
  
-       if (skb_headroom(skb) < max_headroom) {
-               skb = check_headroom(skb, max_headroom);
-               if (unlikely(IS_ERR(skb))) {
-                       vport_record_error(vport, VPORT_E_TX_DROPPED);
-                       goto error;
-               }
+       /* MTU */
+       if (unlikely(!check_mtu(skb, vport, mutable, rt, &frag_off))) {
+               err = VPORT_E_TX_DROPPED;
+               goto error_free;
        }
  
-       forward_ip_summed(skb);
+       /*
+        * If we are over the MTU, allow the IP stack to handle fragmentation.
+        * Fragmentation is a slow path anyways.
+        */
+       if (unlikely(skb->len + mutable->tunnel_hlen > dst_mtu(&rt_dst(rt)) &&
+                    cache)) {
+               unattached_dst = &rt_dst(rt);
+               dst_hold(unattached_dst);
+               cache = NULL;
+       }
  
-       if (unlikely(vswitch_skb_checksum_setup(skb)))
-               goto error_free;
+       /* TTL */
+       ttl = mutable->port_config.ttl;
+       if (!ttl)
+               ttl = dst_metric(&rt_dst(rt), RTAX_HOPLIMIT);
  
-       skb = handle_gso(skb);
-       if (unlikely(IS_ERR(skb))) {
-               vport_record_error(vport, VPORT_E_TX_DROPPED);
-               goto error;
+       if (mutable->port_config.flags & TNL_F_TTL_INHERIT) {
+               if (skb->protocol == htons(ETH_P_IP))
+                       ttl = ip_hdr(skb)->ttl;
+ #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+               else if (skb->protocol == htons(ETH_P_IPV6))
+                       ttl = ipv6_hdr(skb)->hop_limit;
+ #endif
        }
  
-       /*
-        * Process GSO segments.  Try to do any work for the entire packet that
-        * doesn't involve actually writing to it before this point.
-        */
-       orig_len = 0;
-       do {
+       while (skb) {
+               struct iphdr *iph;
                struct sk_buff *next_skb = skb->next;
                skb->next = NULL;
  
-               orig_len += build_packet(vport, mutable, &iph, rt, max_headroom, mtu, skb);
+               if (likely(cache)) {
+                       skb_push(skb, cache->len);
+                       memcpy(skb->data, get_cached_header(cache), cache->len);
+                       skb_reset_mac_header(skb);
+                       skb_set_network_header(skb, rt_dst(rt).hh->hh_len);
+               } else {
+                       skb_push(skb, mutable->tunnel_hlen);
+                       create_tunnel_header(vport, mutable, rt, skb->data);
+                       skb_reset_network_header(skb);
+                       if (next_skb)
+                               skb_dst_set(skb, dst_clone(unattached_dst));
+                       else {
+                               skb_dst_set(skb, unattached_dst);
+                               unattached_dst = NULL;
+                       }
  
+                       memset(&IPCB(skb)->opt, 0, sizeof(IPCB(skb)->opt));
+                       IPCB(skb)->flags = 0;
+               }
+               skb_set_transport_header(skb, skb_network_offset(skb) + sizeof(struct iphdr));
+               iph = ip_hdr(skb);
+               iph->tos = tos;
+               iph->ttl = ttl;
+               iph->frag_off = frag_off;
+               ip_select_ident(iph, &rt_dst(rt), NULL);
+               skb = tnl_vport->tnl_ops->update_header(vport, mutable, &rt_dst(rt), skb);
+               if (unlikely(!skb))
+                       goto next;
+               if (likely(cache)) {
+                       int orig_len = skb->len - cache->len;
+                       skb->protocol = htons(ETH_P_IP);
+                       iph->tot_len = htons(skb->len - skb_network_offset(skb));
+                       ip_send_check(iph);
+                       if (is_internal_dev(rt_dst(rt).dev)) {
+                               OVS_CB(skb)->flow = cache->flow;
+                               compute_ip_summed(skb, true);
+                               vport_receive(internal_dev_get_vport(rt_dst(rt).dev), skb);
+                               sent_len += orig_len;
+                       } else {
+                               int err;
+                               skb->dev = rt_dst(rt).dev;
+                               err = dev_queue_xmit(skb);
+                               if (likely(net_xmit_eval(err) == 0))
+                                       sent_len += orig_len;
+                       }
+               } else
+                       sent_len += send_frags(skb, mutable);
+ next:
                skb = next_skb;
-       } while (skb);
+       }
  
-       if (unlikely(orig_len == 0))
+       if (unlikely(sent_len == 0))
                vport_record_error(vport, VPORT_E_TX_DROPPED);
  
-       return orig_len;
+       goto out;
  
  error_free:
-       kfree_skb(skb);
-       vport_record_error(vport, VPORT_E_TX_ERROR);
+       tnl_free_linked_skbs(skb);
  error:
-       return 0;
- }
- int tnl_init(void)
- {
-       return 0;
- }
- void tnl_exit(void)
- {
-       tbl_destroy(port_table, NULL);
-       port_table = NULL;
+       dst_release(unattached_dst);
+       vport_record_error(vport, err);
+ out:
+       return sent_len;
  }
  
  static int set_config(const void __user *uconfig, const struct tnl_ops *tnl_ops,
        if (copy_from_user(&mutable->port_config, uconfig, sizeof(struct tnl_port_config)))
                return -EFAULT;
  
+       if (mutable->port_config.daddr == 0)
+               return -EINVAL;
+       if (mutable->port_config.tos != RT_TOS(mutable->port_config.tos))
+               return -EINVAL;
        mutable->tunnel_hlen = tnl_ops->hdr_len(&mutable->port_config);
        if (mutable->tunnel_hlen < 0)
                return mutable->tunnel_hlen;
  
        mutable->tunnel_hlen += sizeof(struct iphdr);
  
-       if (mutable->port_config.daddr == 0)
-               return -EINVAL;
        mutable->tunnel_type = tnl_ops->tunnel_type;
        if (mutable->port_config.flags & TNL_F_IN_KEY_MATCH) {
                mutable->tunnel_type |= TNL_T_KEY_MATCH;
@@@ -950,7 -1370,7 +1370,7 @@@ struct vport *tnl_create(const char *na
        strcpy(tnl_vport->name, name);
        tnl_vport->tnl_ops = tnl_ops;
  
-       tnl_vport->mutable = kmalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
+       tnl_vport->mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
        if (!tnl_vport->mutable) {
                err = -ENOMEM;
                goto error_free_vport;
        if (err)
                goto error_free_mutable;
  
+       spin_lock_init(&tnl_vport->cache_lock);
+ #ifdef NEED_CACHE_TIMEOUT
+       tnl_vport->cache_exp_interval = MAX_CACHE_EXP -
+                                       (net_random() % (MAX_CACHE_EXP / 2));
+ #endif
        err = add_port(vport);
        if (err)
                goto error_free_mutable;
@@@ -985,7 -1412,6 +1412,6 @@@ int tnl_modify(struct vport *vport, con
        struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
        struct tnl_mutable_config *mutable;
        int err;
-       bool update_hash = false;
  
        mutable = kmemdup(tnl_vport->mutable, sizeof(struct tnl_mutable_config), GFP_KERNEL);
        if (!mutable) {
        if (err)
                goto error_free;
  
-       /*
-        * Only remove the port from the hash table if something that would
-        * affect the lookup has changed.
-        */
-       if (tnl_vport->mutable->port_config.saddr != mutable->port_config.saddr ||
-           tnl_vport->mutable->port_config.daddr != mutable->port_config.daddr ||
-           tnl_vport->mutable->port_config.in_key != mutable->port_config.in_key ||
-           (tnl_vport->mutable->port_config.flags & TNL_F_IN_KEY_MATCH) !=
-           (mutable->port_config.flags & TNL_F_IN_KEY_MATCH))
-               update_hash = true;
-       /*
-        * This update is not atomic but the lookup uses the config, which
-        * serves as an inherent double check.
-        */
-       if (update_hash) {
-               err = del_port(vport);
-               if (err)
-                       goto error_free;
-       }
-       assign_config_rcu(vport, mutable);
+       mutable->seq++;
  
-       if (update_hash) {
-               err = add_port(vport);
-               if (err)
-                       goto error_free;
-       }
+       err = move_port(vport, mutable);
+       if (err)
+               goto error_free;
  
        return 0;
  
@@@ -1035,10 -1437,14 +1437,14 @@@ error
        return err;
  }
  
- static void free_port(struct rcu_head *rcu)
+ static void free_port_rcu(struct rcu_head *rcu)
  {
        struct tnl_vport *tnl_vport = container_of(rcu, struct tnl_vport, rcu);
  
+       spin_lock_bh(&tnl_vport->cache_lock);
+       free_cache(tnl_vport->cache);
+       spin_unlock_bh(&tnl_vport->cache_lock);
        kfree(tnl_vport->mutable);
        vport_free(tnl_vport_to_vport(tnl_vport));
  }
@@@ -1055,7 -1461,7 +1461,7 @@@ int tnl_destroy(struct vport *vport
            &old_mutable))
                del_port(vport);
  
-       call_rcu(&tnl_vport->rcu, free_port);
+       call_rcu(&tnl_vport->rcu, free_port_rcu);
  
        return 0;
  }
@@@ -1090,7 -1496,6 +1496,6 @@@ int tnl_set_addr(struct vport *vport, c
        return 0;
  }
  
  const char *tnl_get_name(const struct vport *vport)
  {
        const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
@@@ -1108,3 -1513,15 +1513,15 @@@ int tnl_get_mtu(const struct vport *vpo
        const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
        return rcu_dereference(tnl_vport->mutable)->mtu;
  }
+ void tnl_free_linked_skbs(struct sk_buff *skb)
+ {
+       if (unlikely(!skb))
+               return;
+       while (skb) {
+               struct sk_buff *next = skb->next;
+               kfree_skb(skb);
+               skb = next;
+       }
+ }
@@@ -37,7 -37,7 +37,7 @@@ static struct net_device_stats *interna
        struct net_device_stats *stats = &internal_dev_priv(netdev)->stats;
  
        if (vport) {
 -              struct odp_vport_stats vport_stats;
 +              struct xflow_vport_stats vport_stats;
  
                vport_get_stats(vport, &vport_stats);
  
@@@ -82,6 -82,7 +82,7 @@@ static int internal_dev_xmit(struct sk_
  
        skb_reset_mac_header(skb);
        compute_ip_summed(skb, true);
+       OVS_CB(skb)->flow = NULL;
  
        vport_receive(vport, skb);
  
@@@ -293,7 -294,7 +294,7 @@@ static int internal_dev_recv(struct vpo
  
  struct vport_ops internal_vport_ops = {
        .type           = "internal",
-       .flags          = VPORT_F_REQUIRED | VPORT_F_GEN_STATS,
+       .flags          = VPORT_F_REQUIRED | VPORT_F_GEN_STATS | VPORT_F_FLOW,
        .create         = internal_dev_create,
        .destroy        = internal_dev_destroy,
        .attach         = internal_dev_attach,
diff --combined datapath/vport.c
@@@ -177,7 -177,7 +177,7 @@@ void vport_exit(void
        kfree(dev_table);
  }
  
 -static int do_vport_add(struct odp_vport_add *vport_config)
 +static int do_vport_add(struct xflow_vport_add *vport_config)
  {
        struct vport *vport;
        int err = 0;
@@@ -215,23 -215,23 +215,23 @@@ out
   * on device type).  This function is for userspace callers and assumes no
   * locks are held.
   */
 -int vport_user_add(const struct odp_vport_add __user *uvport_config)
 +int vport_user_add(const struct xflow_vport_add __user *uvport_config)
  {
 -      struct odp_vport_add vport_config;
 +      struct xflow_vport_add vport_config;
  
 -      if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_add)))
 +      if (copy_from_user(&vport_config, uvport_config, sizeof(struct xflow_vport_add)))
                return -EFAULT;
  
        return do_vport_add(&vport_config);
  }
  
  #ifdef CONFIG_COMPAT
 -int compat_vport_user_add(struct compat_odp_vport_add *ucompat)
 +int compat_vport_user_add(struct compat_xflow_vport_add *ucompat)
  {
 -      struct compat_odp_vport_add compat;
 -      struct odp_vport_add vport_config;
 +      struct compat_xflow_vport_add compat;
 +      struct xflow_vport_add vport_config;
  
 -      if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_add)))
 +      if (copy_from_user(&compat, ucompat, sizeof(struct compat_xflow_vport_add)))
                return -EFAULT;
  
        memcpy(vport_config.port_type, compat.port_type, VPORT_TYPE_SIZE);
  }
  #endif
  
 -static int do_vport_mod(struct odp_vport_mod *vport_config)
 +static int do_vport_mod(struct xflow_vport_mod *vport_config)
  {
        struct vport *vport;
        int err;
@@@ -275,23 -275,23 +275,23 @@@ out
   * dependent on device type).  This function is for userspace callers and
   * assumes no locks are held.
   */
 -int vport_user_mod(const struct odp_vport_mod __user *uvport_config)
 +int vport_user_mod(const struct xflow_vport_mod __user *uvport_config)
  {
 -      struct odp_vport_mod vport_config;
 +      struct xflow_vport_mod vport_config;
  
 -      if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_mod)))
 +      if (copy_from_user(&vport_config, uvport_config, sizeof(struct xflow_vport_mod)))
                return -EFAULT;
  
        return do_vport_mod(&vport_config);
  }
  
  #ifdef CONFIG_COMPAT
 -int compat_vport_user_mod(struct compat_odp_vport_mod *ucompat)
 +int compat_vport_user_mod(struct compat_xflow_vport_mod *ucompat)
  {
 -      struct compat_odp_vport_mod compat;
 -      struct odp_vport_mod vport_config;
 +      struct compat_xflow_vport_mod compat;
 +      struct xflow_vport_mod vport_config;
  
 -      if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_mod)))
 +      if (copy_from_user(&compat, ucompat, sizeof(struct compat_xflow_vport_mod)))
                return -EFAULT;
  
        memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
@@@ -371,13 -371,13 +371,13 @@@ out
   * Retrieves transmit, receive, and error stats for the given device.  This
   * function is for userspace callers and assumes no locks are held.
   */
 -int vport_user_stats_get(struct odp_vport_stats_req __user *ustats_req)
 +int vport_user_stats_get(struct xflow_vport_stats_req __user *ustats_req)
  {
 -      struct odp_vport_stats_req stats_req;
 +      struct xflow_vport_stats_req stats_req;
        struct vport *vport;
        int err;
  
 -      if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
 +      if (copy_from_user(&stats_req, ustats_req, sizeof(struct xflow_vport_stats_req)))
                return -EFAULT;
  
        stats_req.devname[IFNAMSIZ - 1] = '\0';
@@@ -396,7 -396,7 +396,7 @@@ out
        vport_unlock();
  
        if (!err)
 -              if (copy_to_user(ustats_req, &stats_req, sizeof(struct odp_vport_stats_req)))
 +              if (copy_to_user(ustats_req, &stats_req, sizeof(struct xflow_vport_stats_req)))
                        err = -EFAULT;
  
        return err;
   * -EOPNOTSUPP.  This function is for userspace callers and assumes no locks
   * are held.
   */
 -int vport_user_stats_set(struct odp_vport_stats_req __user *ustats_req)
 +int vport_user_stats_set(struct xflow_vport_stats_req __user *ustats_req)
  {
 -      struct odp_vport_stats_req stats_req;
 +      struct xflow_vport_stats_req stats_req;
        struct vport *vport;
        int err;
  
 -      if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
 +      if (copy_from_user(&stats_req, ustats_req, sizeof(struct xflow_vport_stats_req)))
                return -EFAULT;
  
        stats_req.devname[IFNAMSIZ - 1] = '\0';
@@@ -450,13 -450,13 +450,13 @@@ out
   * Retrieves the Ethernet address of the given device.  This function is for
   * userspace callers and assumes no locks are held.
   */
 -int vport_user_ether_get(struct odp_vport_ether __user *uvport_ether)
 +int vport_user_ether_get(struct xflow_vport_ether __user *uvport_ether)
  {
 -      struct odp_vport_ether vport_ether;
 +      struct xflow_vport_ether vport_ether;
        struct vport *vport;
        int err = 0;
  
 -      if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
 +      if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct xflow_vport_ether)))
                return -EFAULT;
  
        vport_ether.devname[IFNAMSIZ - 1] = '\0';
@@@ -477,7 -477,7 +477,7 @@@ out
        vport_unlock();
  
        if (!err)
 -              if (copy_to_user(uvport_ether, &vport_ether, sizeof(struct odp_vport_ether)))
 +              if (copy_to_user(uvport_ether, &vport_ether, sizeof(struct xflow_vport_ether)))
                        err = -EFAULT;
  
        return err;
   * -EOPNOTSUPP.  This function is for userspace callers and assumes no locks
   * are held.
   */
 -int vport_user_ether_set(struct odp_vport_ether __user *uvport_ether)
 +int vport_user_ether_set(struct xflow_vport_ether __user *uvport_ether)
  {
 -      struct odp_vport_ether vport_ether;
 +      struct xflow_vport_ether vport_ether;
        struct vport *vport;
        int err;
  
 -      if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
 +      if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct xflow_vport_ether)))
                return -EFAULT;
  
        vport_ether.devname[IFNAMSIZ - 1] = '\0';
@@@ -529,13 -529,13 +529,13 @@@ out
   * Retrieves the MTU of the given device.  This function is for userspace
   * callers and assumes no locks are held.
   */
 -int vport_user_mtu_get(struct odp_vport_mtu __user *uvport_mtu)
 +int vport_user_mtu_get(struct xflow_vport_mtu __user *uvport_mtu)
  {
 -      struct odp_vport_mtu vport_mtu;
 +      struct xflow_vport_mtu vport_mtu;
        struct vport *vport;
        int err = 0;
  
 -      if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
 +      if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct xflow_vport_mtu)))
                return -EFAULT;
  
        vport_mtu.devname[IFNAMSIZ - 1] = '\0';
@@@ -554,7 -554,7 +554,7 @@@ out
        vport_unlock();
  
        if (!err)
 -              if (copy_to_user(uvport_mtu, &vport_mtu, sizeof(struct odp_vport_mtu)))
 +              if (copy_to_user(uvport_mtu, &vport_mtu, sizeof(struct xflow_vport_mtu)))
                        err = -EFAULT;
  
        return err;
   * MTU, in which case the result will always be -EOPNOTSUPP.  This function is
   * for userspace callers and assumes no locks are held.
   */
 -int vport_user_mtu_set(struct odp_vport_mtu __user *uvport_mtu)
 +int vport_user_mtu_set(struct xflow_vport_mtu __user *uvport_mtu)
  {
 -      struct odp_vport_mtu vport_mtu;
 +      struct xflow_vport_mtu vport_mtu;
        struct vport *vport;
        int err;
  
 -      if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
 +      if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct xflow_vport_mtu)))
                return -EFAULT;
  
        vport_mtu.devname[IFNAMSIZ - 1] = '\0';
@@@ -904,13 -904,13 +904,13 @@@ int vport_set_addr(struct vport *vport
   * support setting the stats, in which case the result will always be
   * -EOPNOTSUPP.  RTNL lock must be held.
   */
 -int vport_set_stats(struct vport *vport, struct odp_vport_stats *stats)
 +int vport_set_stats(struct vport *vport, struct xflow_vport_stats *stats)
  {
        ASSERT_RTNL();
  
        if (vport->ops->flags & VPORT_F_GEN_STATS) {
                spin_lock_bh(&vport->stats_lock);
 -              memcpy(&vport->offset_stats, stats, sizeof(struct odp_vport_stats));
 +              memcpy(&vport->offset_stats, stats, sizeof(struct xflow_vport_stats));
                spin_unlock_bh(&vport->stats_lock);
  
                return 0;
@@@ -998,10 -998,10 +998,10 @@@ struct kobject *vport_get_kobj(const st
   *
   * Retrieves transmit, receive, and error stats for the given device.
   */
 -int vport_get_stats(struct vport *vport, struct odp_vport_stats *stats)
 +int vport_get_stats(struct vport *vport, struct xflow_vport_stats *stats)
  {
 -      struct odp_vport_stats dev_stats;
 -      struct odp_vport_stats *dev_statsp = NULL;
 +      struct xflow_vport_stats dev_stats;
 +      struct xflow_vport_stats *dev_statsp = NULL;
        int err;
  
        if (vport->ops->get_stats) {
  
                spin_lock_bh(&vport->stats_lock);
  
 -              memcpy(stats, &vport->offset_stats, sizeof(struct odp_vport_stats));
 +              memcpy(stats, &vport->offset_stats, sizeof(struct xflow_vport_stats));
  
                stats->rx_errors        += vport->err_stats.rx_errors
                                                + vport->err_stats.rx_frame_err
@@@ -1145,7 -1145,7 +1145,7 @@@ int vport_get_ifindex(const struct vpor
        if (!dp_port)
                return -EAGAIN;
  
 -      return vport_get_ifindex(dp_port->dp->ports[ODPP_LOCAL]->vport);
 +      return vport_get_ifindex(dp_port->dp->ports[XFLOWP_LOCAL]->vport);
  }
  
  /**
@@@ -1217,6 -1217,9 +1217,9 @@@ void vport_receive(struct vport *vport
                local_bh_enable();
        }
  
+       if (!(vport->ops->flags & VPORT_F_FLOW))
+               OVS_CB(skb)->flow = NULL;
        if (!(vport->ops->flags & VPORT_F_TUN_ID))
                OVS_CB(skb)->tun_id = 0;
  
diff --combined datapath/vport.h
  #include <linux/spinlock.h>
  
  #include "datapath.h"
 -#include "openvswitch/datapath-protocol.h"
 -#include "odp-compat.h"
 +#include "openvswitch/xflow.h"
 +#include "xflow-compat.h"
  
  struct vport;
  struct dp_port;
  
  /* The following definitions are for users of the vport subsytem: */
  
 -int vport_user_add(const struct odp_vport_add __user *);
 -int vport_user_mod(const struct odp_vport_mod __user *);
 +int vport_user_add(const struct xflow_vport_add __user *);
 +int vport_user_mod(const struct xflow_vport_mod __user *);
  int vport_user_del(const char __user *udevname);
  
  #ifdef CONFIG_COMPAT
 -int compat_vport_user_add(struct compat_odp_vport_add __user *);
 -int compat_vport_user_mod(struct compat_odp_vport_mod __user *);
 +int compat_vport_user_add(struct compat_xflow_vport_add __user *);
 +int compat_vport_user_mod(struct compat_xflow_vport_mod __user *);
  #endif
  
 -int vport_user_stats_get(struct odp_vport_stats_req __user *);
 -int vport_user_stats_set(struct odp_vport_stats_req __user *);
 -int vport_user_ether_get(struct odp_vport_ether __user *);
 -int vport_user_ether_set(struct odp_vport_ether __user *);
 -int vport_user_mtu_get(struct odp_vport_mtu __user *);
 -int vport_user_mtu_set(struct odp_vport_mtu __user *);
 +int vport_user_stats_get(struct xflow_vport_stats_req __user *);
 +int vport_user_stats_set(struct xflow_vport_stats_req __user *);
 +int vport_user_ether_get(struct xflow_vport_ether __user *);
 +int vport_user_ether_set(struct xflow_vport_ether __user *);
 +int vport_user_mtu_get(struct xflow_vport_mtu __user *);
 +int vport_user_mtu_set(struct xflow_vport_mtu __user *);
  
  void vport_lock(void);
  void vport_unlock(void);
@@@ -56,7 -56,7 +56,7 @@@ int vport_detach(struct vport *)
  
  int vport_set_mtu(struct vport *, int mtu);
  int vport_set_addr(struct vport *, const unsigned char *);
 -int vport_set_stats(struct vport *, struct odp_vport_stats *);
 +int vport_set_stats(struct vport *, struct xflow_vport_stats *);
  
  const char *vport_get_name(const struct vport *);
  const char *vport_get_type(const struct vport *);
@@@ -64,7 -64,7 +64,7 @@@ const unsigned char *vport_get_addr(con
  
  struct dp_port *vport_get_dp_port(const struct vport *);
  struct kobject *vport_get_kobj(const struct vport *);
 -int vport_get_stats(struct vport *, struct odp_vport_stats *);
 +int vport_get_stats(struct vport *, struct xflow_vport_stats *);
  
  unsigned vport_get_flags(const struct vport *);
  int vport_is_running(const struct vport *);
@@@ -107,12 -107,13 +107,13 @@@ struct vport 
  
        spinlock_t stats_lock;
        struct vport_err_stats err_stats;
 -      struct odp_vport_stats offset_stats;
 +      struct xflow_vport_stats offset_stats;
  };
  
  #define VPORT_F_REQUIRED      (1 << 0) /* If init fails, module loading fails. */
  #define VPORT_F_GEN_STATS     (1 << 1) /* Track stats at the generic layer. */
- #define VPORT_F_TUN_ID                (1 << 2) /* Sets OVS_CB(skb)->tun_id. */
+ #define VPORT_F_FLOW          (1 << 2) /* Sets OVS_CB(skb)->flow. */
+ #define VPORT_F_TUN_ID                (1 << 3) /* Sets OVS_CB(skb)->tun_id. */
  
  /**
   * struct vport_ops - definition of a type of virtual port
@@@ -176,13 -177,13 +177,13 @@@ struct vport_ops 
  
        int (*set_mtu)(struct vport *, int mtu);
        int (*set_addr)(struct vport *, const unsigned char *);
 -      int (*set_stats)(const struct vport *, struct odp_vport_stats *);
 +      int (*set_stats)(const struct vport *, struct xflow_vport_stats *);
  
        /* Called with rcu_read_lock or RTNL lock. */
        const char *(*get_name)(const struct vport *);
        const unsigned char *(*get_addr)(const struct vport *);
        struct kobject *(*get_kobj)(const struct vport *);
 -      int (*get_stats)(const struct vport *, struct odp_vport_stats *);
 +      int (*get_stats)(const struct vport *, struct xflow_vport_stats *);
  
        unsigned (*get_dev_flags)(const struct vport *);
        int (*is_running)(const struct vport *);
  
  #include "openflow/openflow.h"
  
 -#define NICIRA_OUI_STR "002320"
 -
  /* The following vendor extensions, proposed by Nicira Networks, are not yet
 - * ready for standardization (and may never be), so they are not included in
 - * openflow.h. */
 + * standardized, so they are not included in openflow.h.  Some of them may be
 + * suitable for standardization; others we never expect to standardize. */
  
  #define NX_VENDOR_ID 0x00002320
 +\f
 +/* Nicira vendor-specific error messages extension.
 + *
 + * OpenFlow 1.0 has a set of predefined error types (OFPET_*) and codes (which
 + * are specific to each type).  It does not have any provision for
 + * vendor-specific error codes, and it does not even provide "generic" error
 + * codes that can apply to problems not anticipated by the OpenFlow
 + * specification authors.
 + *
 + * This extension attempts to address the problem by adding a generic "error
 + * vendor extension".  The extension works as follows: use NXET_VENDOR as type
 + * and NXVC_VENDOR_CODE as code, followed by struct nx_vendor_error with
 + * vendor-specific details, followed by at least 64 bytes of the failed
 + * request.
 + *
 + * It would be better to have type-specific vendor extension, e.g. so that
 + * OFPET_BAD_ACTION could be used with vendor-specific code values.  But
 + * OFPET_BAD_ACTION and most other standardized types already specify that
 + * their 'data' values are (the start of) the OpenFlow message being replied
 + * to, so there is no room to insert a vendor ID.
 + *
 + * Currently this extension is only implemented by Open vSwitch, but it seems
 + * like a reasonable candidate for future standardization.
 + */
 +
 +/* This is a random number to avoid accidental collision with any other
 + * vendor's extension. */
 +#define NXET_VENDOR 0xb0c2
 +
 +/* ofp_error msg 'code' values for NXET_VENDOR. */
 +enum nx_vendor_code {
 +    NXVC_VENDOR_ERROR           /* 'data' contains struct nx_vendor_error. */
 +};
 +
 +/* 'data' for 'type' == NXET_VENDOR, 'code' == NXVC_VENDOR_ERROR. */
 +struct nx_vendor_error {
 +    uint32_t vendor;            /* Vendor ID as in struct ofp_vendor_header. */
 +    uint16_t type;              /* Vendor-defined type. */
 +    uint16_t code;              /* Vendor-defined subtype. */
 +    /* Followed by at least the first 64 bytes of the failed request. */
 +};
 +\f
 +/* Specific Nicira extension error numbers.
 + *
 + * These are the "code" values used in nx_vendor_error.  So far, the "type"
 + * values in nx_vendor_error are the same as those in ofp_error_msg.  That is,
 + * at Nicira so far we've only needed additional vendor-specific 'code' values,
 + * so we're using the existing 'type' values to avoid having to invent new ones
 + * that duplicate the current ones' meanings. */
 +
 +/* Additional "code" values for OFPET_FLOW_MOD_FAILED. */
 +enum {
 +    /* Generic hardware error. */
 +    NXFMFC_HARDWARE = 0x100,
 +
 +    /* A nonexistent table ID was specified in the "command" field of struct
 +     * ofp_flow_mod, when the nxt_flow_mod_table_id extension is enabled. */
 +    NXFMFC_BAD_TABLE_ID
 +};
 +\f
 +/* Nicira vendor requests and replies. */
  
  enum nicira_type {
      /* Switch status request.  The request body is an ASCII string that
      /* Controller role support.  The request body is struct nx_role_request.
       * The reply echos the request. */
      NXT_ROLE_REQUEST,
 -    NXT_ROLE_REPLY
 +    NXT_ROLE_REPLY,
 +
 +    /* Use the upper 8 bits of the 'command' member in struct ofp_flow_mod to
 +     * designate the table to which a flow is to be added?  See the big comment
 +     * on struct nxt_flow_mod_table_id for more information. */
 +    NXT_FLOW_MOD_TABLE_ID
  };
  
  struct nicira_header {
@@@ -136,53 -72,6 +136,53 @@@ struct nxt_tun_id_cookie 
  };
  OFP_ASSERT(sizeof(struct nxt_tun_id_cookie) == 24);
  
 +/* This command enables or disables an Open vSwitch extension that allows a
 + * controller to specify the OpenFlow table to which a flow should be added,
 + * instead of having the switch decide which table is most appropriate as
 + * required by OpenFlow 1.0.  By default, the extension is disabled.
 + *
 + * When this feature is enabled, Open vSwitch treats struct ofp_flow_mod's
 + * 16-bit 'command' member as two separate fields.  The upper 8 bits are used
 + * as the table ID, the lower 8 bits specify the command as usual.  A table ID
 + * of 0xff is treated like a wildcarded table ID.
 + *
 + * The specific treatment of the table ID depends on the type of flow mod:
 + *
 + *    - OFPFC_ADD: Given a specific table ID, the flow is always placed in that
 + *      table.  If an identical flow already exists in that table only, then it
 + *      is replaced.  If the flow cannot be placed in the specified table,
 + *      either because the table is full or because the table cannot support
 + *      flows of the given type, the switch replies with an
 + *      OFPFMFC_ALL_TABLES_FULL error.  (A controller can distinguish these
 + *      cases by comparing the current and maximum number of entries reported
 + *      in ofp_table_stats.)
 + *
 + *      If the table ID is wildcarded, the switch picks an appropriate table
 + *      itself.  If an identical flow or flows already exist in some flow
 + *      table, then one of them is replaced.  The choice of table might depend
 + *      on the flows that are already in the switch; for example, if one table
 + *      fills up then the switch might fall back to another one.
 + *
 + *    - OFPFC_MODIFY, OFPFC_DELETE: Given a specific table ID, only flows
 + *      within that table are matched and modified or deleted.  If the table ID
 + *      is wildcarded, flows within any table may be matched and modified or
 + *      deleted.
 + *
 + *    - OFPFC_MODIFY_STRICT, OFPFC_DELETE_STRICT: Given a specific table ID,
 + *      only a flow within that table may be matched and modified or deleted.
 + *      If the table ID is wildcarded and exactly one flow within any table
 + *      matches, then it is modified or deleted; if flows in more than one
 + *      table match, then none is modified or deleted.
 + */
 +struct nxt_flow_mod_table_id {
 +    struct ofp_header header;
 +    uint32_t vendor;            /* NX_VENDOR_ID. */
 +    uint32_t subtype;           /* NXT_FLOW_MOD_TABLE_ID. */
 +    uint8_t set;                /* Nonzero to enable, zero to disable. */
 +    uint8_t pad[7];
 +};
 +OFP_ASSERT(sizeof(struct nxt_flow_mod_table_id) == 24);
 +
  /* Configures the "role" of the sending controller.  The default role is:
   *
   *    - Other (NX_ROLE_OTHER), which allows the controller access to all
@@@ -212,8 -101,6 +212,8 @@@ enum nx_role 
      NX_ROLE_MASTER,             /* Full access, at most one. */
      NX_ROLE_SLAVE               /* Read-only access. */
  };
 +\f
 +/* Nicira vendor flow actions. */
  
  enum nx_action_subtype {
      NXAST_SNAT__OBSOLETE,           /* No longer used. */
       *
       * This is useful because OpenFlow does not provide a way to match on the
       * Ethernet addresses inside ARP packets, so there is no other way to drop
-      * spoofed ARPs other than sending every packet up to the controller. */
-     NXAST_DROP_SPOOFED_ARP
+      * spoofed ARPs other than sending every ARP packet to a controller. */
+     NXAST_DROP_SPOOFED_ARP,
+     /* Set the queue that should be used when packets are output.  This
+      * is similar to the OpenFlow OFPAT_ENQUEUE action, but does not
+      * take the output port as an argument.  This allows the queue
+      * to be defined before the port is known. */
+     NXAST_SET_QUEUE,
+     /* Restore the queue to the value it was before any NXAST_SET_QUEUE
+      * actions were used. */
+     NXAST_POP_QUEUE
  };
  
  /* Action structure for NXAST_RESUBMIT. */
@@@ -280,6 -177,17 +290,17 @@@ struct nx_action_set_tunnel 
  };
  OFP_ASSERT(sizeof(struct nx_action_set_tunnel) == 16);
  
+ /* Action structure for NXAST_SET_QUEUE. */
+ struct nx_action_set_queue {
+     uint16_t type;                  /* OFPAT_VENDOR. */
+     uint16_t len;                   /* Length is 16. */
+     uint32_t vendor;                /* NX_VENDOR_ID. */
+     uint16_t subtype;               /* NXAST_SET_QUEUE. */
+     uint8_t pad[2];
+     uint32_t queue_id;              /* Where to enqueue packets. */
+ };
+ OFP_ASSERT(sizeof(struct nx_action_set_queue) == 16);
  /* Header for Nicira-defined actions. */
  struct nx_action_header {
      uint16_t type;                  /* OFPAT_VENDOR. */
diff --combined lib/classifier.c
@@@ -52,16 -52,17 +52,16 @@@ static bool rules_match_1wild(const str
  static bool rules_match_2wild(const struct cls_rule *wild1,
                                const struct cls_rule *wild2, int field_idx);
  
 -/* Converts the flow in 'flow' into a cls_rule in 'rule', with the given
 - * 'wildcards' and 'priority'.*/
 +/* Converts the flow in 'flow' into a cls_rule in 'rule'. */
  void
 -cls_rule_from_flow(const flow_t *flow, uint32_t wildcards,
 -                   unsigned int priority, struct cls_rule *rule)
 +cls_rule_from_flow(const flow_t *flow, struct cls_rule *rule)
  {
 -    assert(!flow->reserved[0] && !flow->reserved[1] && !flow->reserved[2]);
      rule->flow = *flow;
 -    flow_wildcards_init(&rule->wc, wildcards);
 -    rule->priority = priority;
 -    rule->table_idx = table_idx_from_wildcards(rule->wc.wildcards);
 +    if (!rule->flow.wildcards && rule->flow.priority < UINT16_MAX) {
 +        rule->flow.priority = UINT16_MAX;
 +    }
 +    flow_wildcards_init(&rule->wc, flow->wildcards);
 +    rule->table_idx = table_idx_from_wildcards(flow->wildcards);
  }
  
  /* Converts the ofp_match in 'match' into a cls_rule in 'rule', with the given
@@@ -72,10 -73,11 +72,10 @@@ cls_rule_from_match(const struct ofp_ma
                      bool tun_id_from_cookie, uint64_t cookie,
                      struct cls_rule *rule)
  {
 -    uint32_t wildcards;
 -    flow_from_match(match, tun_id_from_cookie, cookie, &rule->flow, &wildcards);
 -    flow_wildcards_init(&rule->wc, wildcards);
 -    rule->priority = rule->wc.wildcards ? priority : UINT16_MAX;
 -    rule->table_idx = table_idx_from_wildcards(rule->wc.wildcards);
 +    flow_from_match(match, rule->flow.wildcards ? priority : UINT16_MAX,
 +                    tun_id_from_cookie, cookie, &rule->flow);
 +    flow_wildcards_init(&rule->wc, rule->flow.wildcards);
 +    rule->table_idx = table_idx_from_wildcards(rule->flow.wildcards);
  }
  
  /* Converts 'rule' to a string and returns the string.  The caller must free
@@@ -85,7 -87,7 +85,7 @@@ cls_rule_to_string(const struct cls_rul
  {
      struct ds s = DS_EMPTY_INITIALIZER;
      ds_put_format(&s, "wildcards=%x priority=%u ",
 -                  rule->wc.wildcards, rule->priority);
 +                  rule->flow.wildcards, rule->flow.priority);
      flow_format(&s, &rule->flow);
      return ds_cstr(&s);
  }
@@@ -97,8 -99,7 +97,8 @@@
  void
  cls_rule_print(const struct cls_rule *rule)
  {
 -    printf("wildcards=%x priority=%u ", rule->wc.wildcards, rule->priority);
 +    printf("wildcards=%x priority=%u ",
 +           rule->flow.wildcards, rule->flow.priority);
      flow_print(stdout, &rule->flow);
      putc('\n', stdout);
  }
@@@ -115,7 -116,7 +115,7 @@@ cls_rule_moved(struct classifier *cls, 
                 struct cls_rule *new)
  {
      if (old != new) {
 -        if (new->wc.wildcards) {
 +        if (new->flow.wildcards) {
              list_moved(&new->node.list);
          } else {
              hmap_node_moved(&cls->exact_table,
@@@ -142,10 -143,10 +142,10 @@@ cls_rule_replace(struct classifier *cls
                   struct cls_rule *new)
  {
      assert(old != new);
 -    assert(old->wc.wildcards == new->wc.wildcards);
 -    assert(old->priority == new->priority);
 +    assert(old->flow.wildcards == new->flow.wildcards);
 +    assert(old->flow.priority == new->flow.priority);
  
 -    if (new->wc.wildcards) {
 +    if (new->flow.wildcards) {
          list_replace(&new->node.list, &old->node.list);
      } else {
          hmap_replace(&cls->exact_table, &old->node.hmap, &new->node.hmap);
@@@ -176,8 -177,7 +176,7 @@@ classifier_destroy(struct classifier *c
          struct hmap *tbl;
  
          for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
-             HMAP_FOR_EACH_SAFE (bucket, next_bucket,
-                                 struct cls_bucket, hmap_node, tbl) {
+             HMAP_FOR_EACH_SAFE (bucket, next_bucket, hmap_node, tbl) {
                  free(bucket);
              }
              hmap_destroy(tbl);
@@@ -208,14 -208,6 +207,14 @@@ classifier_count_exact(const struct cla
      return hmap_count(&cls->exact_table);
  }
  
 +/* Returns the number of rules in 'classifier' that have at least one
 + * wildcard. */
 +int
 +classifier_count_wild(const struct classifier *cls)
 +{
 +    return classifier_count(cls) - classifier_count_exact(cls);
 +}
 +
  /* Inserts 'rule' into 'cls'.  Transfers ownership of 'rule' to 'cls'.
   *
   * If 'cls' already contains an identical rule (including wildcards, values of
@@@ -231,8 -223,8 +230,8 @@@ struct cls_rule 
  classifier_insert(struct classifier *cls, struct cls_rule *rule)
  {
      struct cls_rule *old;
 -    assert((rule->wc.wildcards == 0) == (rule->table_idx == CLS_F_IDX_EXACT));
 -    old = (rule->wc.wildcards
 +    assert((rule->flow.wildcards == 0) == (rule->table_idx == CLS_F_IDX_EXACT));
 +    old = (rule->flow.wildcards
             ? table_insert(&cls->tables[rule->table_idx], rule)
             : insert_exact_rule(cls, rule));
      if (!old) {
  
  /* Inserts 'rule' into 'cls'.  Transfers ownership of 'rule' to 'cls'.
   *
 - * 'rule' must be an exact-match rule (rule->wc.wildcards must be 0) and 'cls'
 + * 'rule' must be an exact-match rule (rule->flow.wildcards must be 0) and 'cls'
   * must not contain any rule with an identical key. */
  void
  classifier_insert_exact(struct classifier *cls, struct cls_rule *rule)
  {
      hmap_insert(&cls->exact_table, &rule->node.hmap,
 -                flow_hash(&rule->flow, 0));
 +                flow_hash_headers(&rule->flow, 0));
      cls->n_rules++;
  }
  
  void
  classifier_remove(struct classifier *cls, struct cls_rule *rule)
  {
 -    if (rule->wc.wildcards) {
 +    if (rule->flow.wildcards) {
          /* Remove 'rule' from bucket.  If that empties the bucket, remove the
           * bucket from its table. */
          struct hmap *table = &cls->tables[rule->table_idx];
@@@ -302,7 -294,7 +301,7 @@@ struct cls_rule 
  classifier_lookup_exact(const struct classifier *cls, const flow_t *flow)
  {
      return (!hmap_is_empty(&cls->exact_table)
 -            ? search_exact_table(cls, flow_hash(flow, 0), flow)
 +            ? search_exact_table(cls, flow_hash_headers(flow, 0), flow)
              : NULL);
  }
  
@@@ -314,10 -306,10 +313,10 @@@ classifier_lookup_wild(const struct cla
          struct cls_rule target;
          int i;
  
 -        cls_rule_from_flow(flow, 0, 0, &target);
 +        cls_rule_from_flow(flow, &target);
          for (i = 0; i < CLS_N_FIELDS; i++) {
              struct cls_rule *rule = search_table(&cls->tables[i], i, &target);
 -            if (rule && (!best || rule->priority > best->priority)) {
 +            if (rule && (!best || rule->flow.priority > best->flow.priority)) {
                  best = rule;
              }
          }
  
  struct cls_rule *
  classifier_find_rule_exactly(const struct classifier *cls,
 -                             const flow_t *target, uint32_t wildcards,
 -                             unsigned int priority)
 +                             const flow_t *target)
  {
      struct cls_bucket *bucket;
      int table_idx;
      uint32_t hash;
  
 -    if (!wildcards) {
 +    if (!target->wildcards) {
          /* Ignores 'priority'. */
 -        return search_exact_table(cls, flow_hash(target, 0), target);
 +        return search_exact_table(cls, flow_hash_headers(target, 0), target);
      }
  
 -    assert(wildcards == (wildcards & OVSFW_ALL));
 -    table_idx = table_idx_from_wildcards(wildcards);
 +    assert(target->wildcards == (target->wildcards & OVSFW_ALL));
 +    table_idx = table_idx_from_wildcards(target->wildcards);
      hash = hash_fields(target, table_idx);
-     HMAP_FOR_EACH_WITH_HASH (bucket, struct cls_bucket, hmap_node, hash,
+     HMAP_FOR_EACH_WITH_HASH (bucket, hmap_node, hash,
                               &cls->tables[table_idx]) {
          if (equal_fields(&bucket->fixed, target, table_idx)) {
              struct cls_rule *pos;
-             LIST_FOR_EACH (pos, struct cls_rule, node.list, &bucket->rules) {
+             LIST_FOR_EACH (pos, node.list, &bucket->rules) {
 -                if (pos->priority < priority) {
 +                if (pos->flow.priority < target->priority) {
                      return NULL;
 -                } else if (pos->priority == priority &&
 -                           pos->wc.wildcards == wildcards &&
 -                           flow_equal(target, &pos->flow)) {
 +                } else if (pos->flow.priority == target->priority &&
 +                           pos->flow.wildcards == target->wildcards &&
 +                           flow_equal_headers(target, &pos->flow)) {
                      return pos;
                  }
              }
      return NULL;
  }
  
 -/* Checks if the flow defined by 'target' with 'wildcards' at 'priority'
 - * overlaps with any other rule at the same priority in the classifier.
 - * Two rules are considered overlapping if a packet could match both. */
 +/* Checks if the flow defined by 'target' overlaps with any other rule at the
 + * same priority in the classifier.  Two rules are considered overlapping if a
 + * packet could match both. */
  bool
 -classifier_rule_overlaps(const struct classifier *cls,
 -                         const flow_t *target, uint32_t wildcards,
 -                         unsigned int priority)
 +classifier_rule_overlaps(const struct classifier *cls, const flow_t *target)
  {
      struct cls_rule target_rule;
      const struct hmap *tbl;
  
 -    if (!wildcards) {
 -        return search_exact_table(cls, flow_hash(target, 0), target) ?
 +    if (!target->wildcards) {
 +        return search_exact_table(cls, flow_hash_headers(target, 0), target) ?
              true : false;
      }
  
 -    cls_rule_from_flow(target, wildcards, priority, &target_rule);
 +    cls_rule_from_flow(target, &target_rule);
  
      for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
          struct cls_bucket *bucket;
  
-         HMAP_FOR_EACH (bucket, struct cls_bucket, hmap_node, tbl) {
+         HMAP_FOR_EACH (bucket, hmap_node, tbl) {
              struct cls_rule *rule;
  
-             LIST_FOR_EACH (rule, struct cls_rule, node.list,
-                            &bucket->rules) {
+             LIST_FOR_EACH (rule, node.list, &bucket->rules) {
 -                if (rule->priority == priority
 +                if (rule->flow.priority == target->priority
-                         && rules_match_2wild(rule, &target_rule, 0)) {
+                     && rules_match_2wild(rule, &target_rule, 0)) {
                      return true;
                  }
              }
      return false;
  }
  
 -/* Ignores target->priority.
 +/* Ignores target->flow.priority.
   *
   * 'callback' is allowed to delete the rule that is passed as its argument, but
   * it must not delete (or move) any other rules in 'cls' that are in the same
   * table as the argument rule.  Two rules are in the same table if their
   * cls_rule structs have the same table_idx; as a special case, a rule with
 - * wildcards and an exact-match rule will never be in the same table. */
 -void
 + * wildcards and an exact-match rule will never be in the same table.
 + *
 + * If 'callback' returns nonzero then the iteration stops immediately and
 + * classifier_for_each_match() passes up the return value.  Otherwise,
 + * classifier_for_each_match() returns 0 after completing the iteration. */
 +int
  classifier_for_each_match(const struct classifier *cls,
 -                          const struct cls_rule *target,
 +                          const flow_t *target_flow,
                            int include, cls_cb_func *callback, void *aux)
  {
 +    struct cls_rule target;
 +
 +    cls_rule_from_flow(target_flow, &target);
      if (include & CLS_INC_WILD) {
          const struct hmap *table;
  
               table++) {
              struct cls_bucket *bucket, *next_bucket;
  
-             HMAP_FOR_EACH_SAFE (bucket, next_bucket,
-                                 struct cls_bucket, hmap_node, table) {
+             HMAP_FOR_EACH_SAFE (bucket, next_bucket, hmap_node, table) {
                  /* XXX there is a bit of room for optimization here based on
                   * rejecting entire buckets on their fixed fields, but it will
                   * only be worthwhile for big buckets (which we hope we won't
                   * bucket itself will be destroyed.  The bucket contains the
                   * list head so that's a use-after-free error. */
                  prev_rule = NULL;
-                 LIST_FOR_EACH (rule, struct cls_rule, node.list,
-                                &bucket->rules) {
+                 LIST_FOR_EACH (rule, node.list, &bucket->rules) {
 -                    if (rules_match_1wild(rule, target, 0)) {
 +                    if (rules_match_1wild(rule, &target, 0)) {
                          if (prev_rule) {
 -                            callback(prev_rule, aux);
 +                            int retval = callback(prev_rule, aux);
 +                            if (retval) {
 +                                return retval;
 +                            }
                          }
                          prev_rule = rule;
                      }
                  }
                  if (prev_rule) {
 -                    callback(prev_rule, aux);
 +                    int retval = callback(prev_rule, aux);
 +                    if (retval) {
 +                        return retval;
 +                    }
                  }
              }
          }
      }
  
      if (include & CLS_INC_EXACT) {
 -        if (target->wc.wildcards) {
 +        if (target.flow.wildcards) {
              struct cls_rule *rule, *next_rule;
  
-             HMAP_FOR_EACH_SAFE (rule, next_rule, struct cls_rule, node.hmap,
+             HMAP_FOR_EACH_SAFE (rule, next_rule, node.hmap,
                                  &cls->exact_table) {
 -                if (rules_match_1wild(rule, target, 0)) {
 -                    callback(rule, aux);
 +                if (rules_match_1wild(rule, &target, 0)) {
 +                    int retval = callback(rule, aux);
 +                    if (retval) {
 +                        return retval;
 +                    }
                  }
              }
          } else {
              /* Optimization: there can be at most one match in the exact
               * table. */
 -            size_t hash = flow_hash(&target->flow, 0);
 +            size_t hash = flow_hash_headers(&target.flow, 0);
              struct cls_rule *rule = search_exact_table(cls, hash,
 -                                                       &target->flow);
 +                                                       &target.flow);
              if (rule) {
 -                callback(rule, aux);
 +                int retval = callback(rule, aux);
 +                if (retval) {
 +                    return retval;
 +                }
              }
          }
      }
 +
 +    return 0;
  }
  
  /* 'callback' is allowed to delete the rule that is passed as its argument, but
   * it must not delete (or move) any other rules in 'cls' that are in the same
   * table as the argument rule.  Two rules are in the same table if their
   * cls_rule structs have the same table_idx; as a special case, a rule with
 - * wildcards and an exact-match rule will never be in the same table. */
 -void
 + * wildcards and an exact-match rule will never be in the same table.
 + *
 + * If 'callback' returns nonzero then the iteration stops immediately and
 + * classifier_for_each() passes up the return value.  Otherwise,
 + * classifier_for_each() returns 0 after completing the iteration. */
 +int
  classifier_for_each(const struct classifier *cls, int include,
 -                    void (*callback)(struct cls_rule *, void *aux),
 +                    int (*callback)(struct cls_rule *, void *aux),
                      void *aux)
  {
      if (include & CLS_INC_WILD) {
          for (tbl = &cls->tables[0]; tbl < &cls->tables[CLS_N_FIELDS]; tbl++) {
              struct cls_bucket *bucket, *next_bucket;
  
-             HMAP_FOR_EACH_SAFE (bucket, next_bucket,
-                                 struct cls_bucket, hmap_node, tbl) {
+             HMAP_FOR_EACH_SAFE (bucket, next_bucket, hmap_node, tbl) {
                  struct cls_rule *prev_rule, *rule;
  
                  /* We can't just use LIST_FOR_EACH_SAFE here because, if the
                   * bucket itself will be destroyed.  The bucket contains the
                   * list head so that's a use-after-free error. */
                  prev_rule = NULL;
-                 LIST_FOR_EACH (rule, struct cls_rule, node.list,
-                                &bucket->rules) {
+                 LIST_FOR_EACH (rule, node.list, &bucket->rules) {
                      if (prev_rule) {
 -                        callback(prev_rule, aux);
 +                        int retval = callback(prev_rule, aux);
 +                        if (retval) {
 +                            return retval;
 +                        }
                      }
                      prev_rule = rule;
                  }
                  if (prev_rule) {
 -                    callback(prev_rule, aux);
 +                    int retval = callback(prev_rule, aux);
 +                    if (retval) {
 +                        return retval;
 +                    }
                  }
              }
          }
      if (include & CLS_INC_EXACT) {
          struct cls_rule *rule, *next_rule;
  
-         HMAP_FOR_EACH_SAFE (rule, next_rule,
-                             struct cls_rule, node.hmap, &cls->exact_table) {
+         HMAP_FOR_EACH_SAFE (rule, next_rule, node.hmap, &cls->exact_table) {
 -            callback(rule, aux);
 +            int retval = callback(rule, aux);
 +            if (retval) {
 +                return retval;
 +            }
          }
      }
 +
 +    return 0;
  }
  \f
  static struct cls_bucket *create_bucket(struct hmap *, size_t hash,
@@@ -681,15 -634,15 +674,15 @@@ static struct cls_rule 
  bucket_insert(struct cls_bucket *bucket, struct cls_rule *rule)
  {
      struct cls_rule *pos;
-     LIST_FOR_EACH (pos, struct cls_rule, node.list, &bucket->rules) {
+     LIST_FOR_EACH (pos, node.list, &bucket->rules) {
 -        if (pos->priority == rule->priority) {
 -            if (pos->wc.wildcards == rule->wc.wildcards
 +        if (pos->flow.priority == rule->flow.priority) {
 +            if (pos->flow.wildcards == rule->flow.wildcards
                  && rules_match_1wild(pos, rule, rule->table_idx))
              {
                  list_replace(&rule->node.list, &pos->node.list);
                  return pos;
              }
 -        } else if (pos->priority < rule->priority) {
 +        } else if (pos->flow.priority < rule->flow.priority) {
              break;
          }
      }
@@@ -703,7 -656,7 +696,7 @@@ insert_exact_rule(struct classifier *cl
      struct cls_rule *old_rule;
      size_t hash;
  
 -    hash = flow_hash(&rule->flow, 0);
 +    hash = flow_hash_headers(&rule->flow, 0);
      old_rule = search_exact_table(cls, hash, &rule->flow);
      if (old_rule) {
          hmap_remove(&cls->exact_table, &old_rule->node.hmap);
@@@ -719,8 -672,7 +712,7 @@@ static struct cls_bucket 
  find_bucket(struct hmap *table, size_t hash, const struct cls_rule *rule)
  {
      struct cls_bucket *bucket;
-     HMAP_FOR_EACH_WITH_HASH (bucket, struct cls_bucket, hmap_node, hash,
-                              table) {
+     HMAP_FOR_EACH_WITH_HASH (bucket, hmap_node, hash, table) {
          if (equal_fields(&bucket->fixed, &rule->flow, rule->table_idx)) {
              return bucket;
          }
@@@ -854,7 -806,7 +846,7 @@@ static boo
  rules_match_1wild(const struct cls_rule *fixed, const struct cls_rule *wild,
                    int field_idx)
  {
 -    return rules_match(fixed, wild, wild->wc.wildcards, wild->wc.nw_src_mask,
 +    return rules_match(fixed, wild, wild->flow.wildcards, wild->wc.nw_src_mask,
                         wild->wc.nw_dst_mask, field_idx);
  }
  
@@@ -869,7 -821,7 +861,7 @@@ rules_match_2wild(const struct cls_rul
                    int field_idx)
  {
      return rules_match(wild1, wild2,
 -                       wild1->wc.wildcards | wild2->wc.wildcards,
 +                       wild1->flow.wildcards | wild2->flow.wildcards,
                         wild1->wc.nw_src_mask & wild2->wc.nw_src_mask,
                         wild1->wc.nw_dst_mask & wild2->wc.nw_dst_mask,
                         field_idx);
@@@ -890,7 -842,7 +882,7 @@@ search_bucket(struct cls_bucket *bucket
          return NULL;
      }
  
-     LIST_FOR_EACH (pos, struct cls_rule, node.list, &bucket->rules) {
+     LIST_FOR_EACH (pos, node.list, &bucket->rules) {
          if (rules_match_1wild(target, pos, field_idx)) {
              return pos;
          }
@@@ -918,7 -870,7 +910,7 @@@ search_table(const struct hmap *table, 
          return search_bucket(bucket, field_idx, target);
      }
  
-     HMAP_FOR_EACH_WITH_HASH (bucket, struct cls_bucket, hmap_node,
+     HMAP_FOR_EACH_WITH_HASH (bucket, hmap_node,
                               hash_fields(&target->flow, field_idx), table) {
          struct cls_rule *rule = search_bucket(bucket, field_idx, target);
          if (rule) {
@@@ -934,9 -886,8 +926,8 @@@ search_exact_table(const struct classif
  {
      struct cls_rule *rule;
  
-     HMAP_FOR_EACH_WITH_HASH (rule, struct cls_rule, node.hmap,
-                              hash, &cls->exact_table) {
+     HMAP_FOR_EACH_WITH_HASH (rule, node.hmap, hash, &cls->exact_table) {
 -        if (flow_equal(&rule->flow, target)) {
 +        if (flow_equal_headers(&rule->flow, target)) {
              return rule;
          }
      }
diff --combined lib/hmap.h
@@@ -69,6 -69,7 +69,7 @@@ struct hmap 
  /* Initialization. */
  void hmap_init(struct hmap *);
  void hmap_destroy(struct hmap *);
+ void hmap_clear(struct hmap *);
  void hmap_swap(struct hmap *a, struct hmap *b);
  void hmap_moved(struct hmap *hmap);
  static inline size_t hmap_count(const struct hmap *);
@@@ -87,7 -88,7 +88,7 @@@ static inline void hmap_remove(struct h
  
  void hmap_node_moved(struct hmap *, struct hmap_node *, struct hmap_node *);
  static inline void hmap_replace(struct hmap *, const struct hmap_node *old,
 -                                struct hmap_node *new);
 +                                struct hmap_node *new_node);
  
  struct hmap_node *hmap_random_node(const struct hmap *);
  
@@@ -95,9 -96,8 +96,8 @@@
   *
   * HMAP_FOR_EACH_WITH_HASH iterates NODE over all of the nodes in HMAP that
   * have hash value equal to HASH.  HMAP_FOR_EACH_IN_BUCKET iterates NODE over
-  * all of the nodes in HMAP that would fall in the same bucket as HASH.  STRUCT
-  * and MEMBER must be the name of the struct that contains the 'struct
-  * hmap_node' and the name of the 'struct hmap_node' member, respectively.
+  * all of the nodes in HMAP that would fall in the same bucket as HASH.  MEMBER
+  * must be the name of the 'struct hmap_node' member within NODE.
   *
   * These macros may be used interchangeably to search for a particular value in
   * an hmap, see, e.g. shash_find() for an example.  Usually, using
   *
   * HASH is only evaluated once.
   */
- #define HMAP_FOR_EACH_WITH_HASH(NODE, STRUCT, MEMBER, HASH, HMAP)       \
-     for ((NODE) = CONTAINER_OF(hmap_first_with_hash(HMAP, HASH),        \
-                                STRUCT, MEMBER);                         \
+ #define HMAP_FOR_EACH_WITH_HASH(NODE, MEMBER, HASH, HMAP)               \
+     for ((NODE) = OBJECT_CONTAINING(hmap_first_with_hash(HMAP, HASH),   \
+                                   NODE, MEMBER);                        \
           &(NODE)->MEMBER != NULL;                                       \
-          (NODE) = CONTAINER_OF(hmap_next_with_hash(&(NODE)->MEMBER),    \
-                                STRUCT, MEMBER))
- #define HMAP_FOR_EACH_IN_BUCKET(NODE, STRUCT, MEMBER, HASH, HMAP)       \
-     for ((NODE) = CONTAINER_OF(hmap_first_in_bucket(HMAP, HASH),        \
-                                STRUCT, MEMBER);                         \
+          (NODE) = OBJECT_CONTAINING(hmap_next_with_hash(&(NODE)->MEMBER), \
+                                   NODE, MEMBER))
+ #define HMAP_FOR_EACH_IN_BUCKET(NODE, MEMBER, HASH, HMAP)               \
+     for ((NODE) = OBJECT_CONTAINING(hmap_first_in_bucket(HMAP, HASH),   \
+                                   NODE, MEMBER);                        \
           &(NODE)->MEMBER != NULL;                                       \
-          (NODE) = CONTAINER_OF(hmap_next_in_bucket(&(NODE)->MEMBER),    \
-                                STRUCT, MEMBER))
+          (NODE) = OBJECT_CONTAINING(hmap_next_in_bucket(&(NODE)->MEMBER), \
+                                NODE, MEMBER))
  
  static inline struct hmap_node *hmap_first_with_hash(const struct hmap *,
                                                       size_t hash);
@@@ -137,18 -137,18 +137,18 @@@ static inline struct hmap_node *hmap_ne
   * The _SAFE version is needed when NODE may be freed.  It is not needed when
   * NODE may be removed from the hash map but its members remain accessible and
   * intact. */
- #define HMAP_FOR_EACH(NODE, STRUCT, MEMBER, HMAP)                   \
-     for ((NODE) = CONTAINER_OF(hmap_first(HMAP), STRUCT, MEMBER);   \
-          &(NODE)->MEMBER != NULL;                                   \
-          (NODE) = CONTAINER_OF(hmap_next(HMAP, &(NODE)->MEMBER),    \
-                                STRUCT, MEMBER))
- #define HMAP_FOR_EACH_SAFE(NODE, NEXT, STRUCT, MEMBER, HMAP)        \
-     for ((NODE) = CONTAINER_OF(hmap_first(HMAP), STRUCT, MEMBER);   \
-          (&(NODE)->MEMBER != NULL                                   \
-           ? (NEXT) = CONTAINER_OF(hmap_next(HMAP, &(NODE)->MEMBER), \
-                                   STRUCT, MEMBER), 1                \
-           : 0);                                                     \
+ #define HMAP_FOR_EACH(NODE, MEMBER, HMAP)                               \
+     for ((NODE) = OBJECT_CONTAINING(hmap_first(HMAP), NODE, MEMBER);    \
+          &(NODE)->MEMBER != NULL;                                       \
+          (NODE) = OBJECT_CONTAINING(hmap_next(HMAP, &(NODE)->MEMBER),   \
+                                     NODE, MEMBER))
+ #define HMAP_FOR_EACH_SAFE(NODE, NEXT, MEMBER, HMAP)                    \
+     for ((NODE) = OBJECT_CONTAINING(hmap_first(HMAP), NODE, MEMBER);    \
+          (&(NODE)->MEMBER != NULL                                       \
+           ? (NEXT) = OBJECT_CONTAINING(hmap_next(HMAP, &(NODE)->MEMBER), \
+                                        NODE, MEMBER), 1                 \
+           : 0);                                                         \
           (NODE) = (NEXT))
  
  static inline struct hmap_node *hmap_first(const struct hmap *);
diff --combined lib/learning-switch.c
@@@ -24,6 -24,7 +24,7 @@@
  #include <time.h>
  
  #include "flow.h"
+ #include "hmap.h"
  #include "mac-learning.h"
  #include "ofpbuf.h"
  #include "ofp-parse.h"
@@@ -33,6 -34,7 +34,7 @@@
  #include "poll-loop.h"
  #include "queue.h"
  #include "rconn.h"
+ #include "shash.h"
  #include "timeval.h"
  #include "vconn.h"
  #include "vlog.h"
  
  VLOG_DEFINE_THIS_MODULE(learning_switch)
  
+ struct lswitch_port {
+     struct hmap_node hmap_node; /* Hash node for port number. */
+     uint16_t port_no;           /* OpenFlow port number, in host byte order. */
+     uint32_t queue_id;          /* OpenFlow queue number. */
+ };
  struct lswitch {
      /* If nonnegative, the switch sets up flows that expire after the given
       * number of seconds (or never expire, if the value is OFP_FLOW_PERMANENT).
      struct mac_learning *ml;    /* NULL to act as hub instead of switch. */
      uint32_t wildcards;         /* Wildcards to apply to flows. */
      bool action_normal;         /* Use OFPP_NORMAL? */
-     uint32_t queue;             /* OpenFlow queue to use, or UINT32_MAX. */
+     /* Queue distribution. */
+     uint32_t default_queue;     /* Default OpenFlow queue, or UINT32_MAX. */
+     struct hmap queue_numbers;  /* Map from port number to lswitch_port. */
+     struct shash queue_names;   /* Map from port name to lswitch_port. */
  
      /* Number of outgoing queued packets on the rconn. */
      struct rconn_packet_counter *queued;
@@@ -63,44 -75,29 +75,29 @@@ static struct vlog_rate_limit rl = VLOG
  
  static void queue_tx(struct lswitch *, struct rconn *, struct ofpbuf *);
  static void send_features_request(struct lswitch *, struct rconn *);
- static void send_default_flows(struct lswitch *sw, struct rconn *rconn,
-                                FILE *default_flows);
  
  typedef void packet_handler_func(struct lswitch *, struct rconn *, void *);
  static packet_handler_func process_switch_features;
  static packet_handler_func process_packet_in;
  static packet_handler_func process_echo_request;
  
- /* Creates and returns a new learning switch.
-  *
-  * If 'learn_macs' is true, the new switch will learn the ports on which MAC
-  * addresses appear.  Otherwise, the new switch will flood all packets.
-  *
-  * If 'max_idle' is nonnegative, the new switch will set up flows that expire
-  * after the given number of seconds (or never expire, if 'max_idle' is
-  * OFP_FLOW_PERMANENT).  Otherwise, the new switch will process every packet.
-  *
-  * The caller may provide the file stream 'default_flows' that defines
-  * default flows that should be pushed when a switch connects.  Each
-  * line is a flow entry in the format described for "add-flows" command
-  * in the Flow Syntax section of the ovs-ofct(8) man page.  The caller
-  * is responsible for closing the stream.
+ /* Creates and returns a new learning switch whose configuration is given by
+  * 'cfg'.
   *
   * 'rconn' is used to send out an OpenFlow features request. */
  struct lswitch *
- lswitch_create(struct rconn *rconn, bool learn_macs,
-                bool exact_flows, int max_idle, bool action_normal,
-                FILE *default_flows)
+ lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
  {
+     const struct ofpbuf *b;
      struct lswitch *sw;
  
      sw = xzalloc(sizeof *sw);
-     sw->max_idle = max_idle;
+     sw->max_idle = cfg->max_idle;
      sw->datapath_id = 0;
      sw->last_features_request = time_now() - 1;
-     sw->ml = learn_macs ? mac_learning_create() : NULL;
-     sw->action_normal = action_normal;
-     if (exact_flows) {
+     sw->ml = cfg->mode == LSW_LEARN ? mac_learning_create() : NULL;
+     sw->action_normal = cfg->mode == LSW_NORMAL;
+     if (cfg->exact_flows) {
          /* Exact match. */
          sw->wildcards = 0;
      } else {
          sw->wildcards = (OFPFW_DL_TYPE | OFPFW_NW_SRC_MASK | OFPFW_NW_DST_MASK
                           | OFPFW_NW_PROTO | OFPFW_TP_SRC | OFPFW_TP_DST);
      }
-     sw->queue = UINT32_MAX;
+     sw->default_queue = cfg->default_queue;
+     hmap_init(&sw->queue_numbers);
+     shash_init(&sw->queue_names);
+     if (cfg->port_queues) {
+         struct shash_node *node;
+         SHASH_FOR_EACH (node, cfg->port_queues) {
+             struct lswitch_port *port = xmalloc(sizeof *port);
+             hmap_node_nullify(&port->hmap_node);
+             port->queue_id = (uintptr_t) node->data;
+             shash_add(&sw->queue_names, node->name, port);
+         }
+     }
      sw->queued = rconn_packet_counter_create();
      send_features_request(sw, rconn);
-     if (default_flows) {
-         send_default_flows(sw, rconn, default_flows);
+     for (b = cfg->default_flows; b; b = b->next) {
+         queue_tx(sw, rconn, ofpbuf_clone(b));
      }
      return sw;
  }
  
@@@ -124,28 -137,26 +137,26 @@@ voi
  lswitch_destroy(struct lswitch *sw)
  {
      if (sw) {
+         struct lswitch_port *node, *next;
+         HMAP_FOR_EACH_SAFE (node, next, hmap_node, &sw->queue_numbers) {
+             hmap_remove(&sw->queue_numbers, &node->hmap_node);
+             free(node);
+         }
+         shash_destroy(&sw->queue_names);
          mac_learning_destroy(sw->ml);
          rconn_packet_counter_destroy(sw->queued);
          free(sw);
      }
  }
  
- /* Sets 'queue' as the OpenFlow queue used by packets and flows set up by 'sw'.
-  * Specify UINT32_MAX to avoid specifying a particular queue, which is also the
-  * default if this function is never called for 'sw'.  */
- void
- lswitch_set_queue(struct lswitch *sw, uint32_t queue)
- {
-     sw->queue = queue;
- }
  /* Takes care of necessary 'sw' activity, except for receiving packets (which
   * the caller must do). */
  void
  lswitch_run(struct lswitch *sw)
  {
      if (sw->ml) {
 -        mac_learning_run(sw->ml, NULL);
 +        mac_learning_run(sw->ml);
      }
  }
  
@@@ -220,10 -231,10 +231,10 @@@ lswitch_process_packet(struct lswitch *
          }
      }
      if (VLOG_IS_DBG_ENABLED()) {
-         char *p = ofp_to_string(msg->data, msg->size, 2);
+         char *s = ofp_to_string(msg->data, msg->size, 2);
          VLOG_DBG_RL(&rl, "%016llx: OpenFlow packet ignored: %s",
-                     sw->datapath_id, p);
-         free(p);
+                     sw->datapath_id, s);
+         free(s);
      }
  }
  \f
@@@ -248,53 -259,6 +259,6 @@@ send_features_request(struct lswitch *s
      }
  }
  
- static void
- send_default_flows(struct lswitch *sw, struct rconn *rconn,
-                    FILE *default_flows)
- {
-     char line[1024];
-     while (fgets(line, sizeof line, default_flows)) {
-         struct ofpbuf *b;
-         struct ofp_flow_mod *ofm;
-         uint16_t priority, idle_timeout, hard_timeout;
-         uint64_t cookie;
-         struct ofp_match match;
-         char *comment;
-         /* Delete comments. */
-         comment = strchr(line, '#');
-         if (comment) {
-             *comment = '\0';
-         }
-         /* Drop empty lines. */
-         if (line[strspn(line, " \t\n")] == '\0') {
-             continue;
-         }
-         /* Parse and send.  str_to_flow() will expand and reallocate the data
-          * in 'buffer', so we can't keep pointers to across the str_to_flow()
-          * call. */
-         make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &b);
-         parse_ofp_str(line, &match, b,
-                       NULL, NULL, &priority, &idle_timeout, &hard_timeout,
-                       &cookie);
-         ofm = b->data;
-         ofm->match = match;
-         ofm->command = htons(OFPFC_ADD);
-         ofm->cookie = htonll(cookie);
-         ofm->idle_timeout = htons(idle_timeout);
-         ofm->hard_timeout = htons(hard_timeout);
-         ofm->buffer_id = htonl(UINT32_MAX);
-         ofm->priority = htons(priority);
-         update_openflow_length(b);
-         queue_tx(sw, rconn, b);
-     }
- }
  static void
  queue_tx(struct lswitch *sw, struct rconn *rconn, struct ofpbuf *b)
  {
@@@ -316,8 -280,28 +280,28 @@@ process_switch_features(struct lswitch 
                          void *osf_)
  {
      struct ofp_switch_features *osf = osf_;
+     size_t n_ports;
+     size_t i;
+     if (check_ofp_message_array(&osf->header, OFPT_FEATURES_REPLY,
+                                 sizeof *osf, sizeof *osf->ports, &n_ports)) {
+         return;
+     }
  
      sw->datapath_id = ntohll(osf->datapath_id);
+     for (i = 0; i < n_ports; i++) {
+         struct ofp_phy_port *opp = &osf->ports[i];
+         struct lswitch_port *lp;
+         opp->name[OFP_MAX_PORT_NAME_LEN - 1] = '\0';
+         lp = shash_find_data(&sw->queue_names, (char *) opp->name);
+         if (lp && hmap_node_is_null(&lp->hmap_node)) {
+             lp->port_no = ntohs(opp->port_no);
+             hmap_insert(&sw->queue_numbers, &lp->hmap_node,
+                         hash_int(lp->port_no, 0));
+         }
+     }
  }
  
  static uint16_t
@@@ -360,11 -344,27 +344,27 @@@ lswitch_choose_destination(struct lswit
      return out_port;
  }
  
+ static uint32_t
+ get_queue_id(const struct lswitch *sw, uint16_t in_port)
+ {
+     const struct lswitch_port *port;
+     HMAP_FOR_EACH_WITH_HASH (port, hmap_node, hash_int(in_port, 0),
+                              &sw->queue_numbers) {
+         if (port->port_no == in_port) {
+             return port->queue_id;
+         }
+     }
+     return sw->default_queue;
+ }
  static void
  process_packet_in(struct lswitch *sw, struct rconn *rconn, void *opi_)
  {
      struct ofp_packet_in *opi = opi_;
      uint16_t in_port = ntohs(opi->in_port);
+     uint32_t queue_id;
      uint16_t out_port;
  
      struct ofp_action_header actions[2];
      out_port = lswitch_choose_destination(sw, &flow);
  
      /* Make actions. */
+     queue_id = get_queue_id(sw, in_port);
      if (out_port == OFPP_NONE) {
          actions_len = 0;
-     } else if (sw->queue == UINT32_MAX || out_port >= OFPP_MAX) {
+     } else if (queue_id == UINT32_MAX || out_port >= OFPP_MAX) {
          struct ofp_action_output oao;
  
          memset(&oao, 0, sizeof oao);
          oae.type = htons(OFPAT_ENQUEUE);
          oae.len = htons(sizeof oae);
          oae.port = htons(out_port);
-         oae.queue_id = htonl(sw->queue);
+         oae.queue_id = htonl(queue_id);
  
          memcpy(actions, &oae, sizeof oae);
          actions_len = sizeof oae;
diff --combined lib/list.h
  #include <stdbool.h>
  #include <stddef.h>
  #include "util.h"
 + 
 +#ifdef  __cplusplus
 +extern "C" {
 +#endif
  
  /* Doubly linked list head or element. */
  struct list {
@@@ -57,22 -53,18 +57,22 @@@ struct list *list_back(struct list *)
  size_t list_size(const struct list *);
  bool list_is_empty(const struct list *);
  
- #define LIST_FOR_EACH(ITER, STRUCT, MEMBER, LIST)                   \
-     for (ITER = CONTAINER_OF((LIST)->next, STRUCT, MEMBER);         \
-          &(ITER)->MEMBER != (LIST);                                 \
-          ITER = CONTAINER_OF((ITER)->MEMBER.next, STRUCT, MEMBER))
- #define LIST_FOR_EACH_REVERSE(ITER, STRUCT, MEMBER, LIST)           \
-     for (ITER = CONTAINER_OF((LIST)->prev, STRUCT, MEMBER);         \
-          &(ITER)->MEMBER != (LIST);                                 \
-          ITER = CONTAINER_OF((ITER)->MEMBER.prev, STRUCT, MEMBER))
- #define LIST_FOR_EACH_SAFE(ITER, NEXT, STRUCT, MEMBER, LIST)        \
-     for (ITER = CONTAINER_OF((LIST)->next, STRUCT, MEMBER);         \
-          (NEXT = CONTAINER_OF((ITER)->MEMBER.next, STRUCT, MEMBER), \
-           &(ITER)->MEMBER != (LIST));                               \
+ #define LIST_FOR_EACH(ITER, MEMBER, LIST)                               \
+     for (ITER = OBJECT_CONTAINING((LIST)->next, ITER, MEMBER);          \
+          &(ITER)->MEMBER != (LIST);                                     \
+          ITER = OBJECT_CONTAINING((ITER)->MEMBER.next, ITER, MEMBER))
+ #define LIST_FOR_EACH_REVERSE(ITER, MEMBER, LIST)                       \
+     for (ITER = OBJECT_CONTAINING((LIST)->prev, ITER, MEMBER);          \
+          &(ITER)->MEMBER != (LIST);                                     \
+          ITER = OBJECT_CONTAINING((ITER)->MEMBER.prev, ITER, MEMBER))
+ #define LIST_FOR_EACH_SAFE(ITER, NEXT, MEMBER, LIST)                    \
+     for (ITER = OBJECT_CONTAINING((LIST)->next, ITER, MEMBER);          \
+          (NEXT = OBJECT_CONTAINING((ITER)->MEMBER.next, ITER, MEMBER),  \
+           &(ITER)->MEMBER != (LIST));                                   \
           ITER = NEXT)
 + 
 +#ifdef  __cplusplus
 +}
 +#endif
  
  #endif /* list.h */
diff --combined lib/mac-learning.c
@@@ -79,7 -79,7 +79,7 @@@ search_bucket(struct list *bucket, cons
                uint16_t vlan)
  {
      struct mac_entry *e;
-     LIST_FOR_EACH (e, struct mac_entry, hash_node, bucket) {
+     LIST_FOR_EACH (e, hash_node, bucket) {
          if (eth_addr_equals(e->mac, mac) && e->vlan == vlan) {
              return e;
          }
@@@ -296,18 -296,17 +296,18 @@@ mac_learning_flush(struct mac_learning 
      }
  }
  
 -void
 -mac_learning_run(struct mac_learning *ml, struct tag_set *set)
 +tag_type
 +mac_learning_run(struct mac_learning *ml)
  {
      struct mac_entry *e;
 +    tag_type tags = 0;
 +
      while (get_lru(ml, &e) && time_now() >= e->expires) {
          COVERAGE_INC(mac_learning_expired);
 -        if (set) {
 -            tag_set_add(set, e->tag);
 -        }
 +        tags |= e->tag;
          free_mac_entry(ml, e);
      }
 +    return tags;
  }
  
  void
diff --combined lib/netdev-tunnel.c
@@@ -23,8 -23,8 +23,8 @@@
  #include "netdev-provider.h"
  #include "netdev-vport.h"
  #include "openflow/openflow.h"
 -#include "openvswitch/datapath-protocol.h"
  #include "openvswitch/tunnel.h"
 +#include "openvswitch/xflow.h"
  #include "packets.h"
  #include "socket-util.h"
  #include "vlog.h"
@@@ -62,10 -62,13 +62,13 @@@ parse_config(const char *name, const ch
               struct tnl_port_config *config)
  {
      struct shash_node *node;
+     bool ipsec_ip_set = false;
+     bool ipsec_mech_set = false;
  
      memset(config, 0, sizeof *config);
  
      config->flags |= TNL_F_PMTUD;
+     config->flags |= TNL_F_HDR_CACHE;
  
      SHASH_FOR_EACH (node, args) {
          if (!strcmp(node->name, "remote_ip")) {
              if (!strcmp(node->data, "false")) {
                  config->flags &= ~TNL_F_PMTUD;
              }
+         } else if (!strcmp(node->name, "header_cache")) {
+             if (!strcmp(node->data, "false")) {
+                 config->flags &= ~TNL_F_HDR_CACHE;
+             }
+         } else if (!strcmp(node->name, "ipsec_local_ip")) {
+             ipsec_ip_set = true;
+         } else if (!strcmp(node->name, "ipsec_cert")
+                    || !strcmp(node->name, "ipsec_psk")) {
+             ipsec_mech_set = true;
          } else {
              VLOG_WARN("%s: unknown %s argument '%s'", name, type, node->name);
          }
      }
  
+     /* IPsec doesn't work when header caching is enabled.  Disable it if
+      * the IPsec local IP address and authentication mechanism have been
+      * defined. */
+     if (ipsec_ip_set && ipsec_mech_set) {
+         VLOG_INFO("%s: header caching disabled due to use of IPsec", name);
+         config->flags &= ~TNL_F_HDR_CACHE;
+     }
      if (!config->daddr) {
          VLOG_WARN("%s: %s type requires valid 'remote_ip' argument", name, type);
          return EINVAL;
@@@ -139,7 -159,7 +159,7 @@@ netdev_tunnel_create(const char *name, 
                       const struct shash *args, struct netdev_dev **netdev_devp)
  {
      int err;
 -    struct odp_vport_add ova;
 +    struct xflow_vport_add ova;
      struct tnl_port_config port_config;
      struct netdev_dev_tunnel *netdev_dev;
  
          return err;
      }
  
 -    err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
 +    err = netdev_vport_do_ioctl(XFLOW_VPORT_ADD, &ova);
      if (err == EBUSY) {
          VLOG_WARN("%s: destroying existing device", name);
  
 -        err = netdev_vport_do_ioctl(ODP_VPORT_DEL, ova.devname);
 +        err = netdev_vport_do_ioctl(XFLOW_VPORT_DEL, ova.devname);
          if (err) {
              return err;
          }
  
 -        err = netdev_vport_do_ioctl(ODP_VPORT_ADD, &ova);
 +        err = netdev_vport_do_ioctl(XFLOW_VPORT_ADD, &ova);
      }
  
      if (err) {
@@@ -184,7 -204,7 +204,7 @@@ static in
  netdev_tunnel_reconfigure(struct netdev_dev *netdev_dev_, const struct shash *args)
  {
      const char *name = netdev_dev_get_name(netdev_dev_);
 -    struct odp_vport_mod ovm;
 +    struct xflow_vport_mod ovm;
      struct tnl_port_config port_config;
      int err;
  
          return err;
      }
  
 -    return netdev_vport_do_ioctl(ODP_VPORT_MOD, &ovm);
 +    return netdev_vport_do_ioctl(XFLOW_VPORT_MOD, &ovm);
  }
  
  static void
@@@ -205,7 -225,7 +225,7 @@@ netdev_tunnel_destroy(struct netdev_de
  {
      struct netdev_dev_tunnel *netdev_dev = netdev_dev_tunnel_cast(netdev_dev_);
  
 -    netdev_vport_do_ioctl(ODP_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
 +    netdev_vport_do_ioctl(XFLOW_VPORT_DEL, (char *)netdev_dev_get_name(netdev_dev_));
      free(netdev_dev);
  }
  
diff --combined lib/netdev-vport.c
@@@ -21,7 -21,7 +21,7 @@@
  
  #include "list.h"
  #include "netdev-vport.h"
 -#include "openvswitch/datapath-protocol.h"
 +#include "openvswitch/xflow.h"
  #include "shash.h"
  #include "socket-util.h"
  #include "vlog.h"
@@@ -59,7 -59,7 +59,7 @@@ in
  netdev_vport_set_etheraddr(struct netdev *netdev,
                             const uint8_t mac[ETH_ADDR_LEN])
  {
 -    struct odp_vport_ether vport_ether;
 +    struct xflow_vport_ether vport_ether;
      int err;
  
      ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
@@@ -67,7 -67,7 +67,7 @@@
  
      memcpy(vport_ether.ether_addr, mac, ETH_ADDR_LEN);
  
 -    err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_SET, &vport_ether);
 +    err = netdev_vport_do_ioctl(XFLOW_VPORT_ETHER_SET, &vport_ether);
      if (err) {
          return err;
      }
@@@ -80,13 -80,13 +80,13 @@@ in
  netdev_vport_get_etheraddr(const struct netdev *netdev,
                             uint8_t mac[ETH_ADDR_LEN])
  {
 -    struct odp_vport_ether vport_ether;
 +    struct xflow_vport_ether vport_ether;
      int err;
  
      ovs_strlcpy(vport_ether.devname, netdev_get_name(netdev),
                  sizeof vport_ether.devname);
  
 -    err = netdev_vport_do_ioctl(ODP_VPORT_ETHER_GET, &vport_ether);
 +    err = netdev_vport_do_ioctl(XFLOW_VPORT_ETHER_GET, &vport_ether);
      if (err) {
          return err;
      }
  int
  netdev_vport_get_mtu(const struct netdev *netdev, int *mtup)
  {
 -    struct odp_vport_mtu vport_mtu;
 +    struct xflow_vport_mtu vport_mtu;
      int err;
  
      ovs_strlcpy(vport_mtu.devname, netdev_get_name(netdev),
                  sizeof vport_mtu.devname);
  
 -    err = netdev_vport_do_ioctl(ODP_VPORT_MTU_GET, &vport_mtu);
 +    err = netdev_vport_do_ioctl(XFLOW_VPORT_MTU_GET, &vport_mtu);
      if (err) {
          return err;
      }
@@@ -124,11 -124,11 +124,11 @@@ in
  netdev_vport_get_stats(const struct netdev *netdev, struct netdev_stats *stats)
  {
      const char *name = netdev_get_name(netdev);
 -    struct odp_vport_stats_req ovsr;
 +    struct xflow_vport_stats_req ovsr;
      int err;
  
      ovs_strlcpy(ovsr.devname, name, sizeof ovsr.devname);
 -    err = netdev_vport_do_ioctl(ODP_VPORT_STATS_GET, &ovsr);
 +    err = netdev_vport_do_ioctl(XFLOW_VPORT_STATS_GET, &ovsr);
      if (err) {
          return err;
      }
  int
  netdev_vport_set_stats(struct netdev *netdev, const struct netdev_stats *stats)
  {
 -    struct odp_vport_stats_req ovsr;
 +    struct xflow_vport_stats_req ovsr;
      int err;
  
      ovs_strlcpy(ovsr.devname, netdev_get_name(netdev), sizeof ovsr.devname);
      ovsr.stats.rx_crc_err = stats->rx_crc_errors;
      ovsr.stats.rx_frame_err = stats->rx_frame_errors;
  
 -    err = netdev_vport_do_ioctl(ODP_VPORT_STATS_SET, &ovsr);
 +    err = netdev_vport_do_ioctl(XFLOW_VPORT_STATS_SET, &ovsr);
  
      /* If the vport layer doesn't know about the device, that doesn't mean it
       * doesn't exist (after all were able to open it when netdev_open() was
@@@ -269,8 -269,7 +269,7 @@@ netdev_vport_poll_notify(const struct n
      if (list) {
          struct netdev_vport_notifier *notifier;
  
-         LIST_FOR_EACH (notifier, struct netdev_vport_notifier,
-                        list_node, list) {
+         LIST_FOR_EACH (notifier, list_node, list) {
              struct netdev_notifier *n = &notifier->notifier;
              n->cb(n);
          }
diff --combined lib/netdev.c
@@@ -285,7 -285,7 +285,7 @@@ create_device(struct netdev_options *op
   * to the new network device, otherwise to null.
   *
   * If this is the first time the device has been opened, then create is called
-  * before opening.  The device is  created using the given type and arguments.
+  * before opening.  The device is created using the given type and arguments.
   *
   * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
   * capture frames of that type received on the device.  It may also be one of
@@@ -352,14 -352,6 +352,14 @@@ netdev_open_default(const char *name, s
      return netdev_open(&options, netdevp);
  }
  
 +/* Increments the reference count on 'netdev'.  Returns 'netdev'. */
 +struct netdev *
 +netdev_reopen(struct netdev *netdev)
 +{
 +    netdev->ref_cnt++;
 +    return netdev;
 +}
 +
  /* Reconfigures the device 'netdev' with 'args'.  'args' may be empty
   * or NULL if none are needed. */
  int
@@@ -385,13 -377,11 +385,13 @@@ netdev_reconfigure(struct netdev *netde
      return 0;
  }
  
 -/* Closes and destroys 'netdev'. */
 +/* Decrements the reference count on 'netdev'.  If the reference count reaches
 + * zero, closes and destroys 'netdev'. */
  void
  netdev_close(struct netdev *netdev)
  {
 -    if (netdev) {
 +    assert(!netdev || netdev->ref_cnt > 0);
 +    if (netdev && !--netdev->ref_cnt) {
          struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
  
          assert(netdev_dev->ref_cnt);
@@@ -474,8 -464,7 +474,7 @@@ netdev_enumerate(struct svec *svec
   * be returned.
   *
   * Some network devices may not implement support for this function.  In such
-  * cases this function will always return EOPNOTSUPP.
-  */
+  * cases this function will always return EOPNOTSUPP. */
  int
  netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
  {
@@@ -641,8 -630,7 +640,7 @@@ netdev_get_ifindex(const struct netdev 
   * passed-in values are set to 0.
   *
   * Some network devices may not implement support for this function.  In such
-  * cases this function will always return EOPNOTSUPP.
-  */
+  * cases this function will always return EOPNOTSUPP. */
  int
  netdev_get_features(struct netdev *netdev,
                      uint32_t *current, uint32_t *advertised,
@@@ -727,8 -715,8 +725,8 @@@ netdev_set_advertisements(struct netde
   *
   *   - EOPNOTSUPP: No IPv4 network stack attached to 'netdev'.
   *
-  * 'address' or 'netmask' or both may be null, in which case the address or netmask
-  * is not reported. */
+  * 'address' or 'netmask' or both may be null, in which case the address or 
+  * netmask is not reported. */
  int
  netdev_get_in4(const struct netdev *netdev,
                 struct in_addr *address_, struct in_addr *netmask_)
@@@ -1159,8 -1147,7 +1157,7 @@@ netdev_get_queue(const struct netdev *n
   * the current form of QoS (e.g. as returned by netdev_get_n_queues(netdev)).
   *
   * This function does not modify 'details', and the caller retains ownership of
-  * it.
-  */
+  * it. */
  int
  netdev_set_queue(struct netdev *netdev,
                   unsigned int queue_id, const struct shash *details)
@@@ -1402,7 -1389,6 +1399,7 @@@ netdev_init(struct netdev *netdev, stru
  {
      memset(netdev, 0, sizeof *netdev);
      netdev->netdev_dev = netdev_dev;
 +    netdev->ref_cnt = 1;
      list_push_back(&netdev_list, &netdev->node);
  }
  
   *
   * Normally this function only needs to be called from netdev_close().
   * However, it may be called by providers due to an error on opening
 - * that occurs after initialization.  It this case netdev_close() would
 + * that occurs after initialization.  In this case netdev_close() would
   * never be called. */
  void
  netdev_uninit(struct netdev *netdev, bool close)
@@@ -1553,8 -1539,7 +1550,7 @@@ netdev_monitor_remove(struct netdev_mon
   * sets '*devnamep' to the name of a device that has changed and returns 0.
   * The caller is responsible for freeing '*devnamep' (with free()).
   *
-  * If no devices have changed, sets '*devnamep' to NULL and returns EAGAIN.
-  */
+  * If no devices have changed, sets '*devnamep' to NULL and returns EAGAIN. */
  int
  netdev_monitor_poll(struct netdev_monitor *monitor, char **devnamep)
  {
          *devnamep = NULL;
          return EAGAIN;
      } else {
-         *devnamep = xstrdup(node->name);
-         shash_delete(&monitor->changed_netdevs, node);
+         *devnamep = shash_steal(&monitor->changed_netdevs, node);
          return 0;
      }
  }
@@@ -1607,7 -1591,7 +1602,7 @@@ static voi
  close_all_netdevs(void *aux OVS_UNUSED)
  {
      struct netdev *netdev, *next;
-     LIST_FOR_EACH_SAFE(netdev, next, struct netdev, node, &netdev_list) {
+     LIST_FOR_EACH_SAFE(netdev, next, node, &netdev_list) {
          netdev_close(netdev);
      }
  }
diff --combined lib/ofp-parse.c
@@@ -21,6 -21,7 +21,7 @@@
  #include <errno.h>
  #include <stdlib.h>
  
+ #include "dynamic-string.h"
  #include "netdev.h"
  #include "ofp-util.h"
  #include "ofpbuf.h"
  #include "socket-util.h"
  #include "vconn.h"
  #include "vlog.h"
+ #include "xtoxll.h"
  
  VLOG_DEFINE_THIS_MODULE(ofp_parse)
  
- #define DEFAULT_IDLE_TIMEOUT 60
  static uint32_t
  str_to_u32(const char *str)
  {
@@@ -263,16 -262,32 +262,32 @@@ str_to_action(char *str, struct ofpbuf 
              nast->vendor = htonl(NX_VENDOR_ID);
              nast->subtype = htons(NXAST_SET_TUNNEL);
              nast->tun_id = htonl(str_to_u32(arg));
+         } else if (!strcasecmp(act, "drop_spoofed_arp")) {
+             struct nx_action_header *nah;
+             nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
+             nah->vendor = htonl(NX_VENDOR_ID);
+             nah->subtype = htons(NXAST_DROP_SPOOFED_ARP);
+         } else if (!strcasecmp(act, "set_queue")) {
+             struct nx_action_set_queue *nasq;
+             nasq = put_action(b, sizeof *nasq, OFPAT_VENDOR);
+             nasq->vendor = htonl(NX_VENDOR_ID);
+             nasq->subtype = htons(NXAST_SET_QUEUE);
+             nasq->queue_id = htonl(str_to_u32(arg));
+         } else if (!strcasecmp(act, "pop_queue")) {
+             struct nx_action_header *nah;
+             nah = put_action(b, sizeof *nah, OFPAT_VENDOR);
+             nah->vendor = htonl(NX_VENDOR_ID);
+             nah->subtype = htons(NXAST_POP_QUEUE);
          } else if (!strcasecmp(act, "output")) {
              put_output_action(b, str_to_u32(arg));
          } else if (!strcasecmp(act, "enqueue")) {
              char *sp = NULL;
-             char *port = strtok_r(arg, ":q", &sp);
+             char *port_s = strtok_r(arg, ":q", &sp);
              char *queue = strtok_r(NULL, "", &sp);
-             if (port == NULL || queue == NULL) {
+             if (port_s == NULL || queue == NULL) {
                  ovs_fatal(0, "\"enqueue\" syntax is \"enqueue:PORT:QUEUE\"");
              }
-             put_enqueue_action(b, str_to_u32(port), str_to_u32(queue));
+             put_enqueue_action(b, str_to_u32(port_s), str_to_u32(queue));
          } else if (!strcasecmp(act, "drop")) {
              /* A drop action in OpenFlow occurs by just not setting
               * an action. */
@@@ -397,7 -412,7 +412,7 @@@ parse_ofp_str(char *string, struct ofp_
          *priority = OFP_DEFAULT_PRIORITY;
      }
      if (idle_timeout) {
-         *idle_timeout = DEFAULT_IDLE_TIMEOUT;
+         *idle_timeout = OFP_FLOW_PERMANENT;
      }
      if (hard_timeout) {
          *hard_timeout = OFP_FLOW_PERMANENT;
  
              if (table_idx && !strcmp(name, "table")) {
                  *table_idx = atoi(value);
 +                if (*table_idx > 31) {
 +                    ovs_fatal(0, "table %s is invalid, "
 +                              "must be between 0 and 31", value);
 +                }
              } else if (out_port && !strcmp(name, "out_port")) {
                  *out_port = atoi(value);
              } else if (priority && !strcmp(name, "priority")) {
                  if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
                      wildcards |= f->wildcard;
                  } else {
+                     uint16_t port_no;
                      wildcards &= ~f->wildcard;
                      if (f->wildcard == OFPFW_IN_PORT
-                         && parse_port_name(value, (uint16_t *) data)) {
-                         /* Nothing to do. */
+                         && parse_port_name(value, &port_no)) {
+                         match->in_port = htons(port_no);
                      } else if (f->type == F_U8) {
                          *(uint8_t *) data = str_to_u32(value);
                      } else if (f->type == F_U16) {
          free(new);
      }
  }
 - * ofpbuf that contains it. */
+ /* Parses 'string' as a OFPT_FLOW_MOD with subtype OFPFC_ADD and returns an
 -parse_ofp_add_flow_str(char *string)
++ * ofpbuf that contains it.  Sets '*table_idx' to the index of the table to
++ * which the flow should be added, or to 0xff if none was specified. */
+ struct ofpbuf *
 -    parse_ofp_str(string, &match, buffer,
 -                  NULL, NULL, &priority, &idle_timeout, &hard_timeout,
 -                  &cookie);
++parse_ofp_add_flow_str(char *string, uint8_t *table_idx)
+ {
+     struct ofpbuf *buffer;
+     struct ofp_flow_mod *ofm;
+     uint16_t priority, idle_timeout, hard_timeout;
+     uint64_t cookie;
+     struct ofp_match match;
+     /* parse_ofp_str() will expand and reallocate the data in 'buffer', so we
+      * can't keep pointers to across the parse_ofp_str() call. */
+     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
 -parse_ofp_add_flow_file(FILE *stream)
++    parse_ofp_str(string, &match, buffer, table_idx, NULL, &priority,
++                  &idle_timeout, &hard_timeout, &cookie);
+     ofm = buffer->data;
+     ofm->match = match;
+     ofm->command = htons(OFPFC_ADD);
++    if (*table_idx != 0xff) {
++        ofm->command |= htons(*table_idx << 8);
++    }
+     ofm->cookie = htonll(cookie);
+     ofm->idle_timeout = htons(idle_timeout);
+     ofm->hard_timeout = htons(hard_timeout);
+     ofm->buffer_id = htonl(UINT32_MAX);
+     ofm->priority = htons(priority);
+     update_openflow_length(buffer);
+     return buffer;
+ }
+ /* Parses an OFPT_FLOW_MOD with subtype OFPFC_ADD from 'stream' and returns an
+  * ofpbuf that contains it.  Returns a null pointer if end-of-file is reached
+  * before reading a flow. */
+ struct ofpbuf *
 -        b = parse_ofp_add_flow_str(line);
++parse_ofp_add_flow_file(FILE *stream, uint8_t *table_idx)
+ {
+     struct ofpbuf *b = NULL;
+     struct ds s = DS_EMPTY_INITIALIZER;
+     while (!ds_get_line(&s, stream)) {
+         char *line = ds_cstr(&s);
+         char *comment;
+         /* Delete comments. */
+         comment = strchr(line, '#');
+         if (comment) {
+             *comment = '\0';
+         }
+         /* Drop empty lines. */
+         if (line[strspn(line, " \t\n")] == '\0') {
+             continue;
+         }
++        b = parse_ofp_add_flow_str(line, table_idx);
+         break;
+     }
+     ds_destroy(&s);
+     return b;
+ }
diff --combined lib/ofp-parse.h
@@@ -20,6 -20,7 +20,7 @@@
  #define OFP_PARSE_H 1
  
  #include <stdint.h>
+ #include <stdio.h>
  
  struct ofp_match;
  struct ofpbuf;
@@@ -29,5 -30,7 +30,7 @@@ void parse_ofp_str(char *string, struc
                     uint16_t *out_port, uint16_t *priority,
                     uint16_t *idle_timeout, uint16_t *hard_timeout,
                     uint64_t *cookie);
 -struct ofpbuf *parse_ofp_add_flow_str(char *string);
 -struct ofpbuf *parse_ofp_add_flow_file(FILE *);
++struct ofpbuf *parse_ofp_add_flow_str(char *string, uint8_t *table_idx);
++struct ofpbuf *parse_ofp_add_flow_file(FILE *, uint8_t *table_idx);
  
  #endif /* ofp-parse.h */
diff --combined lib/ofp-print.c
@@@ -38,6 -38,7 +38,7 @@@
  #include "xtoxll.h"
  
  static void ofp_print_port_name(struct ds *string, uint16_t port);
+ static void ofp_print_queue_name(struct ds *string, uint32_t port);
  
  /* Returns a string that represents the contents of the Ethernet frame in the
   * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
@@@ -135,7 -136,7 +136,7 @@@ ofp_packet_in(struct ds *string, const 
          packet.data = (void *) op->data;
          packet.size = data_len;
          flow_extract(&packet, 0, ntohs(op->in_port), &flow);
 -        flow_to_match(&flow, 0, false, &match);
 +        flow_to_match(&flow, false, &match);
          ofp_print_match(string, &match, verbosity);
          ds_put_char(string, '\n');
      }
@@@ -200,8 -201,23 +201,23 @@@ ofp_print_nx_action(struct ds *string, 
          break;
      }
  
+     case NXAST_DROP_SPOOFED_ARP:
+         ds_put_cstr(string, "drop_spoofed_arp");
+         break;
+     case NXAST_SET_QUEUE: {
+         const struct nx_action_set_queue *nasq =
+                                             (struct nx_action_set_queue *)nah;
+         ds_put_format(string, "set_queue:%u", ntohl(nasq->queue_id));
+         break;
+     }
+     case NXAST_POP_QUEUE:
+         ds_put_cstr(string, "pop_queue");
+         break;
      default:
-         ds_put_format(string, "***unknown Nicira action:%d***\n",
+         ds_put_format(string, "***unknown Nicira action:%d***",
                        ntohs(nah->subtype));
      }
  }
@@@ -746,36 -762,51 +762,55 @@@ ofp_print_flow_mod(struct ds *string, c
                     int verbosity)
  {
      const struct ofp_flow_mod *ofm = oh;
 +    unsigned int command = ntohs(ofm->command);
  
+     ds_put_char(string, ' ');
      ofp_print_match(string, &ofm->match, verbosity);
 -    switch (ntohs(ofm->command)) {
+     if (ds_last(string) != ' ') {
+         ds_put_char(string, ' ');
+     }
 +    switch (command & 0xff) {
      case OFPFC_ADD:
-         ds_put_cstr(string, " ADD: ");
+         ds_put_cstr(string, "ADD:");
          break;
      case OFPFC_MODIFY:
-         ds_put_cstr(string, " MOD: ");
+         ds_put_cstr(string, "MOD:");
          break;
      case OFPFC_MODIFY_STRICT:
-         ds_put_cstr(string, " MOD_STRICT: ");
+         ds_put_cstr(string, "MOD_STRICT:");
          break;
      case OFPFC_DELETE:
-         ds_put_cstr(string, " DEL: ");
+         ds_put_cstr(string, "DEL:");
          break;
      case OFPFC_DELETE_STRICT:
-         ds_put_cstr(string, " DEL_STRICT: ");
+         ds_put_cstr(string, "DEL_STRICT:");
          break;
      default:
-         ds_put_format(string, " cmd:%u ", command);
 -        ds_put_format(string, "cmd:%d", ntohs(ofm->command));
++        ds_put_format(string, "cmd:%u", command & 0xff);
 +    }
 +    if (command & 0xff00) {
-         ds_put_format(string, "table_id:%u ", command >> 8);
++        ds_put_format(string, " table_id:%u", command >> 8);
+     }
+     if (ofm->cookie != htonll(0)) {
+         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(ofm->cookie));
+     }
+     if (ofm->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
+         ds_put_format(string, " idle:%d", ntohs(ofm->idle_timeout));
      }
-     ds_put_format(string, "cookie:0x%"PRIx64" idle:%d hard:%d pri:%d "
-             "buf:%#x flags:%"PRIx16" ", ntohll(ofm->cookie),
-             ntohs(ofm->idle_timeout), ntohs(ofm->hard_timeout),
-             ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
-             ntohl(ofm->buffer_id), ntohs(ofm->flags));
+     if (ofm->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
+         ds_put_format(string, " hard:%d", ntohs(ofm->hard_timeout));
+     }
+     if (ofm->priority != htons(32768)) {
+         ds_put_format(string, " pri:%"PRIu16, ntohs(ofm->priority));
+     }
+     if (ofm->buffer_id != htonl(UINT32_MAX)) {
+         ds_put_format(string, " buf:%#"PRIx32, ntohl(ofm->buffer_id));
+     }
+     if (ofm->flags != htons(0)) {
+         ds_put_format(string, " flags:%"PRIx16, ntohs(ofm->flags));
+     }
+     ds_put_cstr(string, " ");
      ofp_print_actions(string, ofm->actions,
                        len - offsetof(struct ofp_flow_mod, actions));
      ds_put_char(string, '\n');
@@@ -805,11 -836,15 +840,15 @@@ ofp_print_flow_removed(struct ds *strin
          ds_put_format(string, "**%"PRIu8"**", ofr->reason);
          break;
      }
-     ds_put_format(string,
-          " cookie0x%"PRIx64" pri%"PRIu16" secs%"PRIu32" nsecs%"PRIu32
+     if (ofr->cookie != htonll(0)) {
+         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(ofr->cookie));
+     }
+     if (ofr->priority != htons(32768)) {
+         ds_put_format(string, " pri:%"PRIu16, ntohs(ofr->priority));
+     }
+     ds_put_format(string, " secs%"PRIu32" nsecs%"PRIu32
           " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
-          ntohll(ofr->cookie),
-          ofr->match.wildcards ? ntohs(ofr->priority) : (uint16_t)-1,
           ntohl(ofr->duration_sec), ntohl(ofr->duration_nsec),
           ntohs(ofr->idle_timeout), ntohll(ofr->packet_count),
           ntohll(ofr->byte_count));
@@@ -1165,6 -1200,53 +1204,53 @@@ ofp_table_stats_reply(struct ds *string
       }
  }
  
+ static void
+ ofp_print_queue_name(struct ds *string, uint32_t queue_id)
+ {
+     if (queue_id == OFPQ_ALL) {
+         ds_put_cstr(string, "ALL");
+     } else {
+         ds_put_format(string, "%"PRIu32, queue_id);
+     }
+ }
+ static void
+ ofp_queue_stats_request(struct ds *string, const void *body_,
+                        size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
+ {
+     const struct ofp_queue_stats_request *qsr = body_;
+     ds_put_cstr(string, "port=");
+     ofp_print_port_name(string, ntohs(qsr->port_no));
+     ds_put_cstr(string, " queue=");
+     ofp_print_queue_name(string, ntohl(qsr->queue_id));
+ }
+ static void
+ ofp_queue_stats_reply(struct ds *string, const void *body, size_t len,
+                      int verbosity)
+ {
+     const struct ofp_queue_stats *qs = body;
+     size_t n = len / sizeof *qs;
+     ds_put_format(string, " %zu queues\n", n);
+     if (verbosity < 1) {
+         return;
+     }
+     for (; n--; qs++) {
+         ds_put_cstr(string, "  port ");
+         ofp_print_port_name(string, ntohs(qs->port_no));
+         ds_put_cstr(string, " queue ");
+         ofp_print_queue_name(string, ntohl(qs->queue_id));
+         ds_put_cstr(string, ": ");
+         print_port_stat(string, "bytes=", ntohll(qs->tx_bytes), 1);
+         print_port_stat(string, "pkts=", ntohll(qs->tx_packets), 1);
+         print_port_stat(string, "errors=", ntohll(qs->tx_errors), 0);
+     }
+ }
  static void
  vendor_stat(struct ds *string, const void *body, size_t len,
              int verbosity OVS_UNUSED)
@@@ -1235,6 -1317,14 +1321,14 @@@ print_stats(struct ds *string, int type
              { 0, SIZE_MAX, ofp_port_stats_reply },
          },
          {
+             OFPST_QUEUE,
+             "queue",
+             { sizeof(struct ofp_queue_stats_request),
+               sizeof(struct ofp_queue_stats_request),
+               ofp_queue_stats_request },
+             { 0, SIZE_MAX, ofp_queue_stats_reply },
+         },
+         {
              OFPST_VENDOR,
              "vendor-specific",
              { sizeof(uint32_t), SIZE_MAX, vendor_stat },
diff --combined lib/ofp-util.c
@@@ -143,7 -143,7 +143,7 @@@ make_flow_mod(uint16_t command, const f
      ofm->header.length = htons(size);
      ofm->cookie = 0;
      ofm->match.wildcards = htonl(0);
 -    ofm->match.in_port = htons(flow->in_port == ODPP_LOCAL ? OFPP_LOCAL
 +    ofm->match.in_port = htons(flow->in_port == XFLOWP_LOCAL ? OFPP_LOCAL
                                 : flow->in_port);
      memcpy(ofm->match.dl_src, flow->dl_src, sizeof ofm->match.dl_src);
      memcpy(ofm->match.dl_dst, flow->dl_dst, sizeof ofm->match.dl_dst);
@@@ -239,7 -239,7 +239,7 @@@ make_packet_out(const struct ofpbuf *pa
      opo->header.length = htons(size);
      opo->header.xid = htonl(0);
      opo->buffer_id = htonl(buffer_id);
 -    opo->in_port = htons(in_port == ODPP_LOCAL ? OFPP_LOCAL : in_port);
 +    opo->in_port = htons(in_port == XFLOWP_LOCAL ? XFLOWP_LOCAL : in_port);
      opo->actions_len = htons(actions_len);
      ofpbuf_put(out, actions, actions_len);
      if (packet) {
@@@ -438,6 -438,6 +438,21 @@@ check_ofp_packet_out(const struct ofp_h
      return 0;
  }
  
++struct ofpbuf *
++make_nxt_flow_mod_table_id(bool enable)
++{
++    struct nxt_flow_mod_table_id *flow_mod_table_id;
++    struct ofpbuf *buffer;
++
++    flow_mod_table_id = make_openflow(sizeof *flow_mod_table_id, OFPT_VENDOR,
++                                      &buffer);
++
++    flow_mod_table_id->vendor = htonl(NX_VENDOR_ID);
++    flow_mod_table_id->subtype = htonl(NXT_FLOW_MOD_TABLE_ID);
++    flow_mod_table_id->set = enable;
++    return buffer;
++}
++
  const struct ofp_flow_stats *
  flow_stats_first(struct flow_stats_iterator *iter,
                   const struct ofp_stats_reply *osr)
@@@ -564,6 -564,9 +579,9 @@@ check_nicira_action(const union ofp_act
      switch (ntohs(nah->subtype)) {
      case NXAST_RESUBMIT:
      case NXAST_SET_TUNNEL:
+     case NXAST_DROP_SPOOFED_ARP:
+     case NXAST_SET_QUEUE:
+     case NXAST_POP_QUEUE:
          return check_action_exact_len(a, len, 16);
      default:
          return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE);
@@@ -763,27 -766,6 +781,27 @@@ normalize_match(struct ofp_match *m
      m->wildcards = htonl(wc);
  }
  
 +/* Converts all of the fields in 'opp' from host to native byte-order. */
 +void
 +hton_ofp_phy_port(struct ofp_phy_port *opp)
 +{
 +    opp->port_no = htons(opp->port_no);
 +    opp->config = htonl(opp->config);
 +    opp->state = htonl(opp->state);
 +    opp->curr = htonl(opp->curr);
 +    opp->advertised = htonl(opp->advertised);
 +    opp->supported = htonl(opp->supported);
 +    opp->peer = htonl(opp->peer);
 +}
 +
 +/* Converts all of the fields in 'opp' from native to host byte-order. */
 +void
 +ntoh_ofp_phy_port(struct ofp_phy_port *opp)
 +{
 +    /* ntohX and htonX are really the same functions. */
 +    hton_ofp_phy_port(opp);
 +}
 +
  /* Returns a string that describes 'match' in a very literal way, without
   * interpreting its contents except in a very basic fashion.  The returned
   * string is intended to be fixed-length, so that it is easy to see differences
@@@ -821,93 -803,3 +839,93 @@@ ofp_match_to_literal_string(const struc
                       ntohs(match->tp_src),
                       ntohs(match->tp_dst));
  }
 +
 +static uint32_t
 +vendor_code_to_id(uint8_t code)
 +{
 +    switch (code) {
 +#define OFPUTIL_VENDOR(NAME, VENDOR_ID) case NAME: return VENDOR_ID;
 +    default:
 +        return UINT32_MAX;
 +    }
 +}
 +
 +/* Creates and returns an OpenFlow message of type OFPT_ERROR with the error
 + * information taken from 'error', whose encoding must be as described in the
 + * large comment in ofp-util.h.  If 'oh' is nonnull, then the error will use
 + * oh->xid as its transaction ID, and it will include up to the first 64 bytes
 + * of 'oh'.
 + *
 + * Returns NULL if 'error' is not an OpenFlow error code. */
 +struct ofpbuf *
 +make_ofp_error_msg(int error, const struct ofp_header *oh)
 +{
 +    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
 +
 +    struct ofpbuf *buf;
 +    const void *data;
 +    size_t len;
 +    uint8_t vendor;
 +    uint16_t type;
 +    uint16_t code;
 +    uint32_t xid;
 +
 +    if (!is_ofp_error(error)) {
 +        /* We format 'error' with strerror() here since it seems likely to be
 +         * a system errno value. */
 +        VLOG_WARN_RL(&rl, "invalid OpenFlow error code %d (%s)",
 +                     error, strerror(error));
 +        return NULL;
 +    }
 +
 +    if (oh) {
 +        xid = oh->xid;
 +        data = oh;
 +        len = ntohs(oh->length);
 +        if (len > 64) {
 +            len = 64;
 +        }
 +    } else {
 +        xid = 0;
 +        data = NULL;
 +        len = 0;
 +    }
 +
 +    vendor = get_ofp_err_vendor(error);
 +    type = get_ofp_err_type(error);
 +    code = get_ofp_err_code(error);
 +    if (vendor == OFPUTIL_VENDOR_OPENFLOW) {
 +        struct ofp_error_msg *oem;
 +
 +        oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR, xid, &buf);
 +        oem->type = htons(type);
 +        oem->code = htons(code);
 +    } else {
 +        struct ofp_error_msg *oem;
 +        struct nx_vendor_error *ove;
 +        uint32_t vendor_id;
 +
 +        vendor_id = vendor_code_to_id(vendor);
 +        if (vendor_id == UINT32_MAX) {
 +            VLOG_WARN_RL(&rl, "error %x contains invalid vendor code %d",
 +                         error, vendor);
 +            return NULL;
 +        }
 +
 +        oem = make_openflow_xid(len + sizeof *oem + sizeof *ove,
 +                                OFPT_ERROR, xid, &buf);
 +        oem->type = htons(NXET_VENDOR);
 +        oem->code = htons(NXVC_VENDOR_ERROR);
 +
 +        ove = ofpbuf_put_uninit(buf, sizeof *ove);
 +        ove->vendor = htonl(vendor_id);
 +        ove->type = htons(type);
 +        ove->code = htons(code);
 +    }
 +
 +    if (len) {
 +        ofpbuf_put(buf, data, len);
 +    }
 +
 +    return buf;
 +}
diff --combined lib/ofp-util.h
@@@ -62,6 -62,6 +62,8 @@@ int check_ofp_message_array(const struc
  int check_ofp_packet_out(const struct ofp_header *, struct ofpbuf *data,
                           int *n_actions, int max_ports);
  
++struct ofpbuf *make_nxt_flow_mod_table_id(bool enable);
++
  struct flow_stats_iterator {
      const uint8_t *pos, *end;
  };
@@@ -83,132 -83,11 +85,132 @@@ bool action_outputs_to_port(const unio
  void normalize_match(struct ofp_match *);
  char *ofp_match_to_literal_string(const struct ofp_match *match);
  
 +void hton_ofp_phy_port(struct ofp_phy_port *);
 +void ntoh_ofp_phy_port(struct ofp_phy_port *);
 +\f
 +/* OpenFlow vendors.
 + *
 + * These functions map vendor */
 +/* Vendor error numbers currently used in Open vSwitch. */
 +#define OFPUTIL_VENDORS                                     \
 +    /*             vendor name              vendor value */ \
 +    OFPUTIL_VENDOR(OFPUTIL_VENDOR_OPENFLOW, 0x00000000)     \
 +    OFPUTIL_VENDOR(OFPUTIL_VENDOR_NICIRA,   0x00002320)
 +
 +/* OFPUTIL_VENDOR_* definitions. */
 +enum ofputil_vendor_codes {
 +#define OFPUTIL_VENDOR(NAME, VENDOR_ID) NAME,
 +    OFPUTIL_VENDORS
 +    OFPUTIL_N_VENDORS
 +#undef OFPUTIL_VENDOR
 +};
 +\f
 +/* Error codes.
 + *
 + * We embed system errno values and OpenFlow standard and vendor extension
 + * error codes into a single 31-bit space using the following encoding.
 + * (Bit 31 is unused and assumed 0 to avoid negative "int" values.)
 + *
 + *   31                                                   0
 + *  +------------------------------------------------------+
 + *  |                           0                          |  success
 + *  +------------------------------------------------------+
 + *
 + *   30 29                                                0
 + *  +--+---------------------------------------------------+
 + *  |0 |                    errno value                    |  errno value
 + *  +--+---------------------------------------------------+
 + *
 + *   30 29   26 25            16 15                       0
 + *  +--+-------+----------------+--------------------------+
 + *  |1 |   0   |      type      |           code           |  standard OpenFlow
 + *  +--+-------+----------------+--------------------------+  error
 + *
 + *   30 29   26 25            16 15                       0
 + *  +--+-------+----------------+--------------------------+  Nicira
 + *  | 1| vendor|      type      |           code           |  NXET_VENDOR
 + *  +--+-------+----------------+--------------------------+  error extension
 + *
 + * C and POSIX say that errno values are positive.  We assume that they are
 + * less than 2**29.  They are actually less than 65536 on at least Linux,
 + * FreeBSD, OpenBSD, and Windows.
 + *
 + * The 'vendor' field holds one of the OFPUTIL_VENDOR_* codes defined above.
 + * It must be nonzero.
 + *
 + * Negative values are not defined.
 + */
 +
 +/* Currently 4 bits are allocated to the "vendor" field.  Make sure that all
 + * the vendor codes can fit. */
 +BUILD_ASSERT_DECL(OFPUTIL_N_VENDORS <= 16);
 +
 +/* Returns the standard OpenFlow error with the specified 'type' and 'code' as
 + * an integer. */
  static inline int
  ofp_mkerr(uint16_t type, uint16_t code)
  {
 -    assert(type > 0 && type <= 0x7fff);
 -    return (type << 16) | code;
 +    return (1 << 30) | (type << 16) | code;
  }
  
 +/* Returns the OpenFlow vendor error with the specified 'vendor', 'type', and
 + * 'code' as an integer.  'vendor' must be an OFPUTIL_VENDOR_* constant. */
 +static inline int
 +ofp_mkerr_vendor(uint8_t vendor, uint16_t type, uint16_t code)
 +{
 +    assert(vendor < OFPUTIL_N_VENDORS);
 +    return (1 << 30) | (vendor << 26) | (type << 16) | code;
 +}
 +
 +/* Returns the OpenFlow vendor error with Nicira as vendor, with the specific
 + * 'type' and 'code', as an integer. */
 +static inline int
 +ofp_mkerr_nicira(uint16_t type, uint16_t code)
 +{
 +    return ofp_mkerr_vendor(OFPUTIL_VENDOR_NICIRA, type, code);
 +}
 +
 +/* Returns true if 'error' encodes an OpenFlow standard or vendor extension
 + * error codes as documented above. */
 +static inline bool
 +is_ofp_error(int error)
 +{
 +    return (error & (1 << 30)) != 0;
 +}
 +
 +/* Returns true if 'error' appears to be a system errno value. */
 +static inline bool
 +is_errno(int error)
 +{
 +    return !is_ofp_error(error);
 +}
 +
 +/* Returns the "vendor" part of the OpenFlow error code 'error' (which must be
 + * in the format explained above).  This is normally one of the
 + * OFPUTIL_VENDOR_* constants.  Returns OFPUTIL_VENDOR_OPENFLOW (0) for a
 + * standard OpenFlow error. */
 +static inline uint8_t
 +get_ofp_err_vendor(int error)
 +{
 +    return (error >> 26) & 0xf;
 +}
 +
 +/* Returns the "type" part of the OpenFlow error code 'error' (which must be in
 + * the format explained above). */
 +static inline uint16_t
 +get_ofp_err_type(int error)
 +{
 +    return (error >> 16) & 0x3ff;
 +}
 +
 +/* Returns the "code" part of the OpenFlow error code 'error' (which must be in
 + * the format explained above). */
 +static inline uint16_t
 +get_ofp_err_code(int error)
 +{
 +    return error & 0xffff;
 +}
 +
 +struct ofpbuf *make_ofp_error_msg(int error, const struct ofp_header *);
 +
  #endif /* ofp-util.h */
diff --combined lib/rtnetlink.c
@@@ -47,7 -47,7 +47,7 @@@ static void rtnetlink_report_notify_err
   * caller must not modify or free.
   *
   * This is probably not the function that you want.  You should probably be
 - * using dpif_port_poll() or netdev_monitor_create(), which unlike this
 + * using xfif_port_poll() or netdev_monitor_create(), which unlike this
   * function are not Linux-specific.
   *
   * Returns 0 if successful, otherwise a positive errno value. */
@@@ -166,8 -166,7 +166,7 @@@ rtnetlink_report_change(const struct nl
      change.master_ifindex = (attrs[IFLA_MASTER]
                               ? nl_attr_get_u32(attrs[IFLA_MASTER]) : 0);
  
-     LIST_FOR_EACH (notifier, struct rtnetlink_notifier, node,
-                    &all_notifiers) {
+     LIST_FOR_EACH (notifier, node, &all_notifiers) {
          notifier->cb(&change, notifier->aux);
      }
  }
@@@ -177,8 -176,7 +176,7 @@@ rtnetlink_report_notify_error(void
  {
      struct rtnetlink_notifier *notifier;
  
-     LIST_FOR_EACH (notifier, struct rtnetlink_notifier, node,
-                    &all_notifiers) {
+     LIST_FOR_EACH (notifier, node, &all_notifiers) {
          notifier->cb(NULL, notifier->aux);
      }
  }
diff --combined lib/vlog-modules.def
@@@ -25,6 -25,9 +25,6 @@@ VLOG_MODULE(daemon
  VLOG_MODULE(dhcp)
  VLOG_MODULE(dhcp_client)
  VLOG_MODULE(discovery)
 -VLOG_MODULE(dpif)
 -VLOG_MODULE(dpif_linux)
 -VLOG_MODULE(dpif_netdev)
  VLOG_MODULE(dpctl)
  VLOG_MODULE(entropy)
  VLOG_MODULE(fail_open)
@@@ -46,19 -49,19 +46,19 @@@ VLOG_MODULE(netlink
  VLOG_MODULE(ofctl)
  VLOG_MODULE(ofp_parse)
  VLOG_MODULE(ofp_util)
 -VLOG_MODULE(ovs_discover)
  VLOG_MODULE(ofproto)
  VLOG_MODULE(openflowd)
 +VLOG_MODULE(ovs_discover)
  VLOG_MODULE(ovsdb_client)
  VLOG_MODULE(ovsdb_error)
  VLOG_MODULE(ovsdb_file)
  VLOG_MODULE(ovsdb_idl)
 -VLOG_MODULE(ovsdb_log)
  VLOG_MODULE(ovsdb_jsonrpc_server)
 +VLOG_MODULE(ovsdb_log)
  VLOG_MODULE(ovsdb_server)
  VLOG_MODULE(ovsdb_tool)
 -VLOG_MODULE(pktbuf)
  VLOG_MODULE(pcap)
 +VLOG_MODULE(pktbuf)
  VLOG_MODULE(poll_loop)
  VLOG_MODULE(proc_net_compat)
  VLOG_MODULE(process)
@@@ -66,28 -69,23 +66,29 @@@ VLOG_MODULE(rconn
  VLOG_MODULE(reconnect)
  VLOG_MODULE(rtnetlink)
  VLOG_MODULE(sflow)
 +VLOG_MODULE(socket_util)
 +VLOG_MODULE(status)
 +VLOG_MODULE(stream)
  VLOG_MODULE(stream_fd)
  VLOG_MODULE(stream_ssl)
  VLOG_MODULE(stream_tcp)
  VLOG_MODULE(stream_unix)
 -VLOG_MODULE(stream)
 -VLOG_MODULE(status)
  VLOG_MODULE(svec)
  VLOG_MODULE(timeval)
  VLOG_MODULE(socket_util)
+ VLOG_MODULE(system_stats)
  VLOG_MODULE(unixctl)
  VLOG_MODULE(util)
 -VLOG_MODULE(vconn_stream)
  VLOG_MODULE(vconn)
 -VLOG_MODULE(vsctl)
 +VLOG_MODULE(vconn_stream)
  VLOG_MODULE(vlog)
 +VLOG_MODULE(vsctl)
  VLOG_MODULE(vswitchd)
 +VLOG_MODULE(wdp)
 +VLOG_MODULE(wdp_xflow)
  VLOG_MODULE(xenserver)
 +VLOG_MODULE(xfif)
 +VLOG_MODULE(xfif_linux)
 +VLOG_MODULE(xfif_netdev)
  
  #undef VLOG_MODULE
diff --combined lib/xfif-linux.c
@@@ -15,7 -15,7 +15,7 @@@
   */
  
  #include <config.h>
 -#include "dpif.h"
 +#include "xfif.h"
  
  #include <assert.h>
  #include <ctype.h>
@@@ -33,6 -33,7 +33,6 @@@
  #include <sys/stat.h>
  #include <unistd.h>
  
 -#include "dpif-provider.h"
  #include "netdev.h"
  #include "ofpbuf.h"
  #include "poll-loop.h"
  #include "svec.h"
  #include "util.h"
  #include "vlog.h"
 +#include "xfif-provider.h"
  
 -VLOG_DEFINE_THIS_MODULE(dpif_linux)
 +VLOG_DEFINE_THIS_MODULE(xfif_linux)
  
  /* Datapath interface for the openvswitch Linux kernel module. */
 -struct dpif_linux {
 -    struct dpif dpif;
 +struct xfif_linux {
 +    struct xfif xfif;
      int fd;
  
 -    /* Used by dpif_linux_get_all_names(). */
 +    /* Used by xfif_linux_get_all_names(). */
      char *local_ifname;
      int minor;
  
  
  static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
  
 -static int do_ioctl(const struct dpif *, int cmd, const void *arg);
 +static int do_ioctl(const struct xfif *, int cmd, const void *arg);
  static int lookup_minor(const char *name, int *minor);
 -static int finish_open(struct dpif *, const char *local_ifname);
 +static int finish_open(struct xfif *, const char *local_ifname);
  static int get_openvswitch_major(void);
 -static int create_minor(const char *name, int minor, struct dpif **dpifp);
 -static int open_minor(int minor, struct dpif **dpifp);
 +static int create_minor(const char *name, int minor, struct xfif **xfifp);
 +static int open_minor(int minor, struct xfif **xfifp);
  static int make_openvswitch_device(int minor, char **fnp);
 -static void dpif_linux_port_changed(const struct rtnetlink_change *,
 -                                    void *dpif);
 +static void xfif_linux_port_changed(const struct rtnetlink_change *,
 +                                    void *xfif);
  
 -static struct dpif_linux *
 -dpif_linux_cast(const struct dpif *dpif)
 +static struct xfif_linux *
 +xfif_linux_cast(const struct xfif *xfif)
  {
 -    dpif_assert_class(dpif, &dpif_linux_class);
 -    return CONTAINER_OF(dpif, struct dpif_linux, dpif);
 +    xfif_assert_class(xfif, &xfif_linux_class);
 +    return CONTAINER_OF(xfif, struct xfif_linux, xfif);
  }
  
  static int
 -dpif_linux_enumerate(struct svec *all_dps)
 +xfif_linux_enumerate(struct svec *all_dps)
  {
      int major;
      int error;
      }
  
      error = 0;
 -    for (i = 0; i < ODP_MAX; i++) {
 -        struct dpif *dpif;
 +    for (i = 0; i < XFLOW_MAX; i++) {
 +        struct xfif *xfif;
          char devname[16];
          int retval;
  
          sprintf(devname, "dp%d", i);
 -        retval = dpif_open(devname, "system", &dpif);
 +        retval = xfif_open(devname, "system", &xfif);
          if (!retval) {
              svec_add(all_dps, devname);
 -            dpif_uninit(dpif, true);
 +            xfif_uninit(xfif, true);
          } else if (retval != ENODEV && !error) {
              error = retval;
          }
  }
  
  static int
 -dpif_linux_open(const char *name, const char *type OVS_UNUSED, bool create,
 -                struct dpif **dpifp)
 +xfif_linux_open(const char *name, const char *type OVS_UNUSED, bool create,
 +                struct xfif **xfifp)
  {
      int minor;
  
              && isdigit((unsigned char)name[2]) ? atoi(name + 2) : -1;
      if (create) {
          if (minor >= 0) {
 -            return create_minor(name, minor, dpifp);
 +            return create_minor(name, minor, xfifp);
          } else {
              /* Scan for unused minor number. */
 -            for (minor = 0; minor < ODP_MAX; minor++) {
 -                int error = create_minor(name, minor, dpifp);
 +            for (minor = 0; minor < XFLOW_MAX; minor++) {
 +                int error = create_minor(name, minor, xfifp);
                  if (error != EBUSY) {
                      return error;
                  }
              return ENOBUFS;
          }
      } else {
 -        struct dpif_linux *dpif;
 -        struct odp_port port;
 +        struct xfif_linux *xfif;
 +        struct xflow_port port;
          int error;
  
          if (minor < 0) {
              }
          }
  
 -        error = open_minor(minor, dpifp);
 +        error = open_minor(minor, xfifp);
          if (error) {
              return error;
          }
 -        dpif = dpif_linux_cast(*dpifp);
 +        xfif = xfif_linux_cast(*xfifp);
  
          /* We need the local port's ifindex for the poll function.  Start by
           * getting the local port's name. */
          memset(&port, 0, sizeof port);
 -        port.port = ODPP_LOCAL;
 -        if (ioctl(dpif->fd, ODP_PORT_QUERY, &port)) {
 +        port.port = XFLOWP_LOCAL;
 +        if (ioctl(xfif->fd, XFLOW_PORT_QUERY, &port)) {
              error = errno;
              if (error != ENODEV) {
                  VLOG_WARN("%s: probe returned unexpected error: %s",
 -                          dpif_name(*dpifp), strerror(error));
 +                          xfif_name(*xfifp), strerror(error));
              }
 -            dpif_uninit(*dpifp, true);
 +            xfif_uninit(*xfifp, true);
              return error;
          }
  
          /* Then use that to finish up opening. */
 -        return finish_open(&dpif->dpif, port.devname);
 +        return finish_open(&xfif->xfif, port.devname);
      }
  }
  
  static void
 -dpif_linux_close(struct dpif *dpif_)
 +xfif_linux_close(struct xfif *xfif_)
  {
 -    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 -    rtnetlink_notifier_unregister(&dpif->port_notifier);
 -    shash_destroy(&dpif->changed_ports);
 -    free(dpif->local_ifname);
 -    close(dpif->fd);
 -    free(dpif);
 +    struct xfif_linux *xfif = xfif_linux_cast(xfif_);
 +    rtnetlink_notifier_unregister(&xfif->port_notifier);
 +    shash_destroy(&xfif->changed_ports);
 +    free(xfif->local_ifname);
 +    close(xfif->fd);
 +    free(xfif);
  }
  
  static int
 -dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
 +xfif_linux_get_all_names(const struct xfif *xfif_, struct svec *all_names)
  {
 -    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 +    struct xfif_linux *xfif = xfif_linux_cast(xfif_);
  
 -    svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
 -    svec_add(all_names, dpif->local_ifname);
 +    svec_add_nocopy(all_names, xasprintf("dp%d", xfif->minor));
 +    svec_add(all_names, xfif->local_ifname);
      return 0;
  }
  
  static int
 -dpif_linux_destroy(struct dpif *dpif_)
 +xfif_linux_destroy(struct xfif *xfif_)
  {
 -    struct odp_port *ports;
 +    struct xflow_port *ports;
      size_t n_ports;
      int err;
      int i;
  
 -    err = dpif_port_list(dpif_, &ports, &n_ports);
 +    err = xfif_port_list(xfif_, &ports, &n_ports);
      if (err) {
          return err;
      }
  
      for (i = 0; i < n_ports; i++) {
 -        if (ports[i].port != ODPP_LOCAL) {
 -            err = do_ioctl(dpif_, ODP_VPORT_DEL, ports[i].devname);
 +        if (ports[i].port != XFLOWP_LOCAL) {
 +            err = do_ioctl(xfif_, XFLOW_VPORT_DEL, ports[i].devname);
              if (err) {
                  VLOG_WARN_RL(&error_rl, "%s: error deleting port %s (%s)",
 -                             dpif_name(dpif_), ports[i].devname, strerror(err));
 +                             xfif_name(xfif_), ports[i].devname, strerror(err));
              }
          }
      }
  
      free(ports);
  
 -    return do_ioctl(dpif_, ODP_DP_DESTROY, NULL);
 +    return do_ioctl(xfif_, XFLOW_DP_DESTROY, NULL);
  }
  
  static int
 -dpif_linux_get_stats(const struct dpif *dpif_, struct odp_stats *stats)
 +xfif_linux_get_stats(const struct xfif *xfif_, struct xflow_stats *stats)
  {
      memset(stats, 0, sizeof *stats);
 -    return do_ioctl(dpif_, ODP_DP_STATS, stats);
 +    return do_ioctl(xfif_, XFLOW_DP_STATS, stats);
  }
  
  static int
 -dpif_linux_get_drop_frags(const struct dpif *dpif_, bool *drop_fragsp)
 +xfif_linux_get_drop_frags(const struct xfif *xfif_, bool *drop_fragsp)
  {
      int drop_frags;
      int error;
  
 -    error = do_ioctl(dpif_, ODP_GET_DROP_FRAGS, &drop_frags);
 +    error = do_ioctl(xfif_, XFLOW_GET_DROP_FRAGS, &drop_frags);
      if (!error) {
          *drop_fragsp = drop_frags & 1;
      }
  }
  
  static int
 -dpif_linux_set_drop_frags(struct dpif *dpif_, bool drop_frags)
 +xfif_linux_set_drop_frags(struct xfif *xfif_, bool drop_frags)
  {
      int drop_frags_int = drop_frags;
 -    return do_ioctl(dpif_, ODP_SET_DROP_FRAGS, &drop_frags_int);
 +    return do_ioctl(xfif_, XFLOW_SET_DROP_FRAGS, &drop_frags_int);
  }
  
  static int
 -dpif_linux_port_add(struct dpif *dpif_, const char *devname, uint16_t flags,
 +xfif_linux_port_add(struct xfif *xfif_, const char *devname, uint16_t flags,
                      uint16_t *port_no)
  {
 -    struct odp_port port;
 +    struct xflow_port port;
      int error;
  
      memset(&port, 0, sizeof port);
      strncpy(port.devname, devname, sizeof port.devname);
      port.flags = flags;
 -    error = do_ioctl(dpif_, ODP_PORT_ATTACH, &port);
 +    error = do_ioctl(xfif_, XFLOW_PORT_ATTACH, &port);
      if (!error) {
          *port_no = port.port;
      }
  }
  
  static int
 -dpif_linux_port_del(struct dpif *dpif_, uint16_t port_no)
 +xfif_linux_port_del(struct xfif *xfif_, uint16_t port_no)
  {
      int tmp = port_no;
      int err;
 -    struct odp_port port;
 +    struct xflow_port port;
  
 -    err = dpif_port_query_by_number(dpif_, port_no, &port);
 +    err = xfif_port_query_by_number(xfif_, port_no, &port);
      if (err) {
          return err;
      }
  
 -    err = do_ioctl(dpif_, ODP_PORT_DETACH, &tmp);
 +    err = do_ioctl(xfif_, XFLOW_PORT_DETACH, &tmp);
      if (err) {
          return err;
      }
          /* Try deleting the port if no one has it open.  This shouldn't
           * actually be necessary unless the config changed while we weren't
           * running but it won't hurt anything if the port is already gone. */
 -        do_ioctl(dpif_, ODP_VPORT_DEL, port.devname);
 +        do_ioctl(xfif_, XFLOW_VPORT_DEL, port.devname);
      }
  
      return 0;
  }
  
  static int
 -dpif_linux_port_query_by_number(const struct dpif *dpif_, uint16_t port_no,
 -                          struct odp_port *port)
 +xfif_linux_port_query_by_number(const struct xfif *xfif_, uint16_t port_no,
 +                          struct xflow_port *port)
  {
      memset(port, 0, sizeof *port);
      port->port = port_no;
 -    return do_ioctl(dpif_, ODP_PORT_QUERY, port);
 +    return do_ioctl(xfif_, XFLOW_PORT_QUERY, port);
  }
  
  static int
 -dpif_linux_port_query_by_name(const struct dpif *dpif_, const char *devname,
 -                              struct odp_port *port)
 +xfif_linux_port_query_by_name(const struct xfif *xfif_, const char *devname,
 +                              struct xflow_port *port)
  {
      memset(port, 0, sizeof *port);
      strncpy(port->devname, devname, sizeof port->devname);
 -    return do_ioctl(dpif_, ODP_PORT_QUERY, port);
 +    return do_ioctl(xfif_, XFLOW_PORT_QUERY, port);
  }
  
  static int
 -dpif_linux_flow_flush(struct dpif *dpif_)
 +xfif_linux_flow_flush(struct xfif *xfif_)
  {
 -    return do_ioctl(dpif_, ODP_FLOW_FLUSH, NULL);
 +    return do_ioctl(xfif_, XFLOW_FLOW_FLUSH, NULL);
  }
  
  static int
 -dpif_linux_port_list(const struct dpif *dpif_, struct odp_port *ports, int n)
 +xfif_linux_port_list(const struct xfif *xfif_, struct xflow_port *ports, int n)
  {
 -    struct odp_portvec pv;
 +    struct xflow_portvec pv;
      int error;
  
      pv.ports = ports;
      pv.n_ports = n;
 -    error = do_ioctl(dpif_, ODP_PORT_LIST, &pv);
 +    error = do_ioctl(xfif_, XFLOW_PORT_LIST, &pv);
      return error ? -error : pv.n_ports;
  }
  
  static int
 -dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
 +xfif_linux_port_poll(const struct xfif *xfif_, char **devnamep)
  {
 -    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 +    struct xfif_linux *xfif = xfif_linux_cast(xfif_);
  
 -    if (dpif->change_error) {
 -        dpif->change_error = false;
 -        shash_clear(&dpif->changed_ports);
 +    if (xfif->change_error) {
 +        xfif->change_error = false;
 +        shash_clear(&xfif->changed_ports);
          return ENOBUFS;
 -    } else if (!shash_is_empty(&dpif->changed_ports)) {
 -        struct shash_node *node = shash_first(&dpif->changed_ports);
 -        *devnamep = shash_steal(&dpif->changed_ports, node);
 +    } else if (!shash_is_empty(&xfif->changed_ports)) {
 +        struct shash_node *node = shash_first(&xfif->changed_ports);
-         *devnamep = xstrdup(node->name);
-         shash_delete(&xfif->changed_ports, node);
++        *devnamep = shash_steal(&xfif->changed_ports, node);
          return 0;
      } else {
          return EAGAIN;
  }
  
  static void
 -dpif_linux_port_poll_wait(const struct dpif *dpif_)
 +xfif_linux_port_poll_wait(const struct xfif *xfif_)
  {
 -    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 -    if (!shash_is_empty(&dpif->changed_ports) || dpif->change_error) {
 +    struct xfif_linux *xfif = xfif_linux_cast(xfif_);
 +    if (!shash_is_empty(&xfif->changed_ports) || xfif->change_error) {
          poll_immediate_wake();
      } else {
          rtnetlink_notifier_wait();
  }
  
  static int
 -dpif_linux_port_group_get(const struct dpif *dpif_, int group,
 +xfif_linux_port_group_get(const struct xfif *xfif_, int group,
                            uint16_t ports[], int n)
  {
 -    struct odp_port_group pg;
 +    struct xflow_port_group pg;
      int error;
  
      assert(n <= UINT16_MAX);
      pg.group = group;
      pg.ports = ports;
      pg.n_ports = n;
 -    error = do_ioctl(dpif_, ODP_PORT_GROUP_GET, &pg);
 +    error = do_ioctl(xfif_, XFLOW_PORT_GROUP_GET, &pg);
      return error ? -error : pg.n_ports;
  }
  
  static int
 -dpif_linux_port_group_set(struct dpif *dpif_, int group,
 +xfif_linux_port_group_set(struct xfif *xfif_, int group,
                            const uint16_t ports[], int n)
  {
 -    struct odp_port_group pg;
 +    struct xflow_port_group pg;
  
      assert(n <= UINT16_MAX);
      pg.group = group;
      pg.ports = (uint16_t *) ports;
      pg.n_ports = n;
 -    return do_ioctl(dpif_, ODP_PORT_GROUP_SET, &pg);
 +    return do_ioctl(xfif_, XFLOW_PORT_GROUP_SET, &pg);
  }
  
  static int
 -dpif_linux_flow_get(const struct dpif *dpif_, struct odp_flow flows[], int n)
 +xfif_linux_flow_get(const struct xfif *xfif_, struct xflow_flow flows[], int n)
  {
 -    struct odp_flowvec fv;
 +    struct xflow_flowvec fv;
      fv.flows = flows;
      fv.n_flows = n;
 -    return do_ioctl(dpif_, ODP_FLOW_GET, &fv);
 +    return do_ioctl(xfif_, XFLOW_FLOW_GET, &fv);
  }
  
  static int
 -dpif_linux_flow_put(struct dpif *dpif_, struct odp_flow_put *put)
 +xfif_linux_flow_put(struct xfif *xfif_, struct xflow_flow_put *put)
  {
 -    return do_ioctl(dpif_, ODP_FLOW_PUT, put);
 +    return do_ioctl(xfif_, XFLOW_FLOW_PUT, put);
  }
  
  static int
 -dpif_linux_flow_del(struct dpif *dpif_, struct odp_flow *flow)
 +xfif_linux_flow_del(struct xfif *xfif_, struct xflow_flow *flow)
  {
 -    return do_ioctl(dpif_, ODP_FLOW_DEL, flow);
 +    return do_ioctl(xfif_, XFLOW_FLOW_DEL, flow);
  }
  
  static int
 -dpif_linux_flow_list(const struct dpif *dpif_, struct odp_flow flows[], int n)
 +xfif_linux_flow_list(const struct xfif *xfif_, struct xflow_flow flows[], int n)
  {
 -    struct odp_flowvec fv;
 +    struct xflow_flowvec fv;
      int error;
  
      fv.flows = flows;
      fv.n_flows = n;
 -    error = do_ioctl(dpif_, ODP_FLOW_LIST, &fv);
 +    error = do_ioctl(xfif_, XFLOW_FLOW_LIST, &fv);
      return error ? -error : fv.n_flows;
  }
  
  static int
 -dpif_linux_execute(struct dpif *dpif_, uint16_t in_port,
 -                   const union odp_action actions[], int n_actions,
 +xfif_linux_execute(struct xfif *xfif_, uint16_t in_port,
 +                   const union xflow_action actions[], int n_actions,
                     const struct ofpbuf *buf)
  {
 -    struct odp_execute execute;
 +    struct xflow_execute execute;
      memset(&execute, 0, sizeof execute);
      execute.in_port = in_port;
 -    execute.actions = (union odp_action *) actions;
 +    execute.actions = (union xflow_action *) actions;
      execute.n_actions = n_actions;
      execute.data = buf->data;
      execute.length = buf->size;
 -    return do_ioctl(dpif_, ODP_EXECUTE, &execute);
 +    return do_ioctl(xfif_, XFLOW_EXECUTE, &execute);
  }
  
  static int
 -dpif_linux_recv_get_mask(const struct dpif *dpif_, int *listen_mask)
 +xfif_linux_recv_get_mask(const struct xfif *xfif_, int *listen_mask)
  {
 -    return do_ioctl(dpif_, ODP_GET_LISTEN_MASK, listen_mask);
 +    return do_ioctl(xfif_, XFLOW_GET_LISTEN_MASK, listen_mask);
  }
  
  static int
 -dpif_linux_recv_set_mask(struct dpif *dpif_, int listen_mask)
 +xfif_linux_recv_set_mask(struct xfif *xfif_, int listen_mask)
  {
 -    return do_ioctl(dpif_, ODP_SET_LISTEN_MASK, &listen_mask);
 +    return do_ioctl(xfif_, XFLOW_SET_LISTEN_MASK, &listen_mask);
  }
  
  static int
 -dpif_linux_get_sflow_probability(const struct dpif *dpif_,
 +xfif_linux_get_sflow_probability(const struct xfif *xfif_,
                                   uint32_t *probability)
  {
 -    return do_ioctl(dpif_, ODP_GET_SFLOW_PROBABILITY, probability);
 +    return do_ioctl(xfif_, XFLOW_GET_SFLOW_PROBABILITY, probability);
  }
  
  static int
 -dpif_linux_set_sflow_probability(struct dpif *dpif_, uint32_t probability)
 +xfif_linux_set_sflow_probability(struct xfif *xfif_, uint32_t probability)
  {
 -    return do_ioctl(dpif_, ODP_SET_SFLOW_PROBABILITY, &probability);
 +    return do_ioctl(xfif_, XFLOW_SET_SFLOW_PROBABILITY, &probability);
  }
  
  static int
 -dpif_linux_queue_to_priority(const struct dpif *dpif OVS_UNUSED,
 +xfif_linux_queue_to_priority(const struct xfif *xfif OVS_UNUSED,
                               uint32_t queue_id, uint32_t *priority)
  {
      if (queue_id < 0xf000) {
  }
  
  static int
 -dpif_linux_recv(struct dpif *dpif_, struct ofpbuf **bufp)
 +xfif_linux_recv(struct xfif *xfif_, struct ofpbuf **bufp)
  {
 -    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 +    struct xfif_linux *xfif = xfif_linux_cast(xfif_);
      struct ofpbuf *buf;
      int retval;
      int error;
  
-     buf = ofpbuf_new(65536 + XFIF_RECV_MSG_PADDING);
-     ofpbuf_reserve(buf, XFIF_RECV_MSG_PADDING);
 -    buf = ofpbuf_new_with_headroom(65536, DPIF_RECV_MSG_PADDING);
 -    retval = read(dpif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
++    buf = ofpbuf_new_with_headroom(65536, XFIF_RECV_MSG_PADDING);
 +    retval = read(xfif->fd, ofpbuf_tail(buf), ofpbuf_tailroom(buf));
      if (retval < 0) {
          error = errno;
          if (error != EAGAIN) {
              VLOG_WARN_RL(&error_rl, "%s: read failed: %s",
 -                         dpif_name(dpif_), strerror(error));
 +                         xfif_name(xfif_), strerror(error));
          }
 -    } else if (retval >= sizeof(struct odp_msg)) {
 -        struct odp_msg *msg = buf->data;
 +    } else if (retval >= sizeof(struct xflow_msg)) {
 +        struct xflow_msg *msg = buf->data;
          if (msg->length <= retval) {
              buf->size += retval;
              *bufp = buf;
          } else {
              VLOG_WARN_RL(&error_rl, "%s: discarding message truncated "
                           "from %"PRIu32" bytes to %d",
 -                         dpif_name(dpif_), msg->length, retval);
 +                         xfif_name(xfif_), msg->length, retval);
              error = ERANGE;
          }
      } else if (!retval) {
 -        VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", dpif_name(dpif_));
 +        VLOG_WARN_RL(&error_rl, "%s: unexpected end of file", xfif_name(xfif_));
          error = EPROTO;
      } else {
          VLOG_WARN_RL(&error_rl,
                       "%s: discarding too-short message (%d bytes)",
 -                     dpif_name(dpif_), retval);
 +                     xfif_name(xfif_), retval);
          error = ERANGE;
      }
  
  }
  
  static void
 -dpif_linux_recv_wait(struct dpif *dpif_)
 +xfif_linux_recv_wait(struct xfif *xfif_)
  {
 -    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 -    poll_fd_wait(dpif->fd, POLLIN);
 +    struct xfif_linux *xfif = xfif_linux_cast(xfif_);
 +    poll_fd_wait(xfif->fd, POLLIN);
  }
  
 -const struct dpif_class dpif_linux_class = {
 +const struct xfif_class xfif_linux_class = {
      "system",
      NULL,
      NULL,
 -    dpif_linux_enumerate,
 -    dpif_linux_open,
 -    dpif_linux_close,
 -    dpif_linux_get_all_names,
 -    dpif_linux_destroy,
 -    dpif_linux_get_stats,
 -    dpif_linux_get_drop_frags,
 -    dpif_linux_set_drop_frags,
 -    dpif_linux_port_add,
 -    dpif_linux_port_del,
 -    dpif_linux_port_query_by_number,
 -    dpif_linux_port_query_by_name,
 -    dpif_linux_port_list,
 -    dpif_linux_port_poll,
 -    dpif_linux_port_poll_wait,
 -    dpif_linux_port_group_get,
 -    dpif_linux_port_group_set,
 -    dpif_linux_flow_get,
 -    dpif_linux_flow_put,
 -    dpif_linux_flow_del,
 -    dpif_linux_flow_flush,
 -    dpif_linux_flow_list,
 -    dpif_linux_execute,
 -    dpif_linux_recv_get_mask,
 -    dpif_linux_recv_set_mask,
 -    dpif_linux_get_sflow_probability,
 -    dpif_linux_set_sflow_probability,
 -    dpif_linux_queue_to_priority,
 -    dpif_linux_recv,
 -    dpif_linux_recv_wait,
 +    xfif_linux_enumerate,
 +    xfif_linux_open,
 +    xfif_linux_close,
 +    xfif_linux_get_all_names,
 +    xfif_linux_destroy,
 +    xfif_linux_get_stats,
 +    xfif_linux_get_drop_frags,
 +    xfif_linux_set_drop_frags,
 +    xfif_linux_port_add,
 +    xfif_linux_port_del,
 +    xfif_linux_port_query_by_number,
 +    xfif_linux_port_query_by_name,
 +    xfif_linux_port_list,
 +    xfif_linux_port_poll,
 +    xfif_linux_port_poll_wait,
 +    xfif_linux_port_group_get,
 +    xfif_linux_port_group_set,
 +    xfif_linux_flow_get,
 +    xfif_linux_flow_put,
 +    xfif_linux_flow_del,
 +    xfif_linux_flow_flush,
 +    xfif_linux_flow_list,
 +    xfif_linux_execute,
 +    xfif_linux_recv_get_mask,
 +    xfif_linux_recv_set_mask,
 +    xfif_linux_get_sflow_probability,
 +    xfif_linux_set_sflow_probability,
 +    xfif_linux_queue_to_priority,
 +    xfif_linux_recv,
 +    xfif_linux_recv_wait,
  };
  \f
  static int get_openvswitch_major(void);
  static int get_major(const char *target);
  
  static int
 -do_ioctl(const struct dpif *dpif_, int cmd, const void *arg)
 +do_ioctl(const struct xfif *xfif_, int cmd, const void *arg)
  {
 -    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 -    return ioctl(dpif->fd, cmd, arg) ? errno : 0;
 +    struct xfif_linux *xfif = xfif_linux_cast(xfif_);
 +    return ioctl(xfif->fd, cmd, arg) ? errno : 0;
  }
  
  static int
@@@ -605,7 -603,7 +603,7 @@@ lookup_minor(const char *name, int *min
          VLOG_WARN("%s ethtool bus_info has unexpected format", name);
          error = EPROTOTYPE;
          goto error_close_sock;
 -    } else if (port_no != ODPP_LOCAL) {
 +    } else if (port_no != XFLOWP_LOCAL) {
          /* This is an Open vSwitch device but not the local port.  We
           * intentionally support only using the name of the local port as the
           * name of a datapath; otherwise, it would be too difficult to
@@@ -732,11 -730,7 +730,7 @@@ get_major(const char *target
                  return major;
              }
          } else {
-             static bool warned;
-             if (!warned) {
-                 VLOG_WARN("%s:%d: syntax error", fn, ln);
-             }
-             warned = true;
+             VLOG_WARN_ONCE("%s:%d: syntax error", fn, ln);
          }
      }
  
  }
  
  static int
 -finish_open(struct dpif *dpif_, const char *local_ifname)
 +finish_open(struct xfif *xfif_, const char *local_ifname)
  {
 -    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
 -    dpif->local_ifname = xstrdup(local_ifname);
 -    dpif->local_ifindex = if_nametoindex(local_ifname);
 -    if (!dpif->local_ifindex) {
 +    struct xfif_linux *xfif = xfif_linux_cast(xfif_);
 +    xfif->local_ifname = xstrdup(local_ifname);
 +    xfif->local_ifindex = if_nametoindex(local_ifname);
 +    if (!xfif->local_ifindex) {
          int error = errno;
 -        dpif_uninit(dpif_, true);
 +        xfif_uninit(xfif_, true);
          VLOG_WARN("could not get ifindex of %s device: %s",
                    local_ifname, strerror(errno));
          return error;
  }
  
  static int
 -create_minor(const char *name, int minor, struct dpif **dpifp)
 +create_minor(const char *name, int minor, struct xfif **xfifp)
  {
 -    int error = open_minor(minor, dpifp);
 +    int error = open_minor(minor, xfifp);
      if (!error) {
 -        error = do_ioctl(*dpifp, ODP_DP_CREATE, name);
 +        error = do_ioctl(*xfifp, XFLOW_DP_CREATE, name);
          if (!error) {
 -            error = finish_open(*dpifp, name);
 +            error = finish_open(*xfifp, name);
          } else {
 -            dpif_uninit(*dpifp, true);
 +            xfif_uninit(*xfifp, true);
          }
      }
      return error;
  }
  
  static int
 -open_minor(int minor, struct dpif **dpifp)
 +open_minor(int minor, struct xfif **xfifp)
  {
      int error;
      char *fn;
  
      fd = open(fn, O_RDONLY | O_NONBLOCK);
      if (fd >= 0) {
 -        struct dpif_linux *dpif = xmalloc(sizeof *dpif);
 -        error = rtnetlink_notifier_register(&dpif->port_notifier,
 -                                           dpif_linux_port_changed, dpif);
 +        struct xfif_linux *xfif = xmalloc(sizeof *xfif);
 +        error = rtnetlink_notifier_register(&xfif->port_notifier,
 +                                           xfif_linux_port_changed, xfif);
          if (!error) {
              char *name;
  
              name = xasprintf("dp%d", minor);
 -            dpif_init(&dpif->dpif, &dpif_linux_class, name, minor, minor);
 +            xfif_init(&xfif->xfif, &xfif_linux_class, name, minor, minor);
              free(name);
  
 -            dpif->fd = fd;
 -            dpif->local_ifname = NULL;
 -            dpif->minor = minor;
 -            dpif->local_ifindex = 0;
 -            shash_init(&dpif->changed_ports);
 -            dpif->change_error = false;
 -            *dpifp = &dpif->dpif;
 +            xfif->fd = fd;
 +            xfif->local_ifname = NULL;
 +            xfif->minor = minor;
 +            xfif->local_ifindex = 0;
 +            shash_init(&xfif->changed_ports);
 +            xfif->change_error = false;
 +            *xfifp = &xfif->xfif;
          } else {
 -            free(dpif);
 +            free(xfif);
          }
      } else {
          error = errno;
  }
  
  static void
 -dpif_linux_port_changed(const struct rtnetlink_change *change, void *dpif_)
 +xfif_linux_port_changed(const struct rtnetlink_change *change, void *xfif_)
  {
 -    struct dpif_linux *dpif = dpif_;
 +    struct xfif_linux *xfif = xfif_;
  
      if (change) {
 -        if (change->master_ifindex == dpif->local_ifindex
 +        if (change->master_ifindex == xfif->local_ifindex
              && (change->nlmsg_type == RTM_NEWLINK
                  || change->nlmsg_type == RTM_DELLINK))
          {
              /* Our datapath changed, either adding a new port or deleting an
               * existing one. */
 -            shash_add_once(&dpif->changed_ports, change->ifname, NULL);
 +            shash_add_once(&xfif->changed_ports, change->ifname, NULL);
          }
      } else {
 -        dpif->change_error = true;
 +        xfif->change_error = true;
      }
  }
diff --combined lib/xfif-netdev.c
index 1afb08c,0000000..25493fb
mode 100644,000000..100644
--- /dev/null
@@@ -1,1402 -1,0 +1,1400 @@@
-     LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
 +/*
 + * Copyright (c) 2009, 2010 Nicira Networks.
 + *
 + * Licensed under the Apache License, Version 2.0 (the "License");
 + * you may not use this file except in compliance with the License.
 + * You may obtain a copy of the License at:
 + *
 + *     http://www.apache.org/licenses/LICENSE-2.0
 + *
 + * Unless required by applicable law or agreed to in writing, software
 + * distributed under the License is distributed on an "AS IS" BASIS,
 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 + * See the License for the specific language governing permissions and
 + * limitations under the License.
 + */
 +
 +#include <config.h>
 +#include "xfif.h"
 +
 +#include <assert.h>
 +#include <ctype.h>
 +#include <errno.h>
 +#include <fcntl.h>
 +#include <inttypes.h>
 +#include <netinet/in.h>
 +#include <sys/socket.h>
 +#include <net/if.h>
 +#include <stdlib.h>
 +#include <string.h>
 +#include <sys/ioctl.h>
 +#include <sys/stat.h>
 +#include <unistd.h>
 +
 +#include "csum.h"
 +#include "flow.h"
 +#include "hmap.h"
 +#include "list.h"
 +#include "netdev.h"
 +#include "xflow-util.h"
 +#include "ofp-print.h"
 +#include "ofpbuf.h"
 +#include "packets.h"
 +#include "poll-loop.h"
 +#include "queue.h"
 +#include "timeval.h"
 +#include "util.h"
 +#include "vlog.h"
 +#include "xfif-provider.h"
 +
 +VLOG_DEFINE_THIS_MODULE(xfif_netdev)
 +
 +/* Configuration parameters. */
 +enum { N_QUEUES = 2 };          /* Number of queues for xfif_recv(). */
 +enum { MAX_QUEUE_LEN = 100 };   /* Maximum number of packets per queue. */
 +enum { N_GROUPS = 16 };         /* Number of port groups. */
 +enum { MAX_PORTS = 256 };       /* Maximum number of ports. */
 +enum { MAX_FLOWS = 65536 };     /* Maximum number of flows in flow table. */
 +
 +/* Enough headroom to add a vlan tag, plus an extra 2 bytes to allow IP
 + * headers to be aligned on a 4-byte boundary.  */
 +enum { XF_NETDEV_HEADROOM = 2 + VLAN_HEADER_LEN };
 +
 +/* Datapath based on the network device interface from netdev.h. */
 +struct xf_netdev {
 +    struct list node;
 +    int xf_idx;
 +    int open_cnt;
 +    bool destroyed;
 +
 +    bool drop_frags;            /* Drop all IP fragments, if true. */
 +    struct ovs_queue queues[N_QUEUES]; /* Messages queued for xfif_recv(). */
 +    struct hmap flow_table;     /* Flow table. */
 +    struct xflow_port_group groups[N_GROUPS];
 +
 +    /* Statistics. */
 +    long long int n_frags;      /* Number of dropped IP fragments. */
 +    long long int n_hit;        /* Number of flow table matches. */
 +    long long int n_missed;     /* Number of flow table misses. */
 +    long long int n_lost;       /* Number of misses not passed to client. */
 +
 +    /* Ports. */
 +    int n_ports;
 +    struct xf_netdev_port *ports[MAX_PORTS];
 +    struct list port_list;
 +    unsigned int serial;
 +};
 +
 +/* A port in a netdev-based datapath. */
 +struct xf_netdev_port {
 +    int port_no;                /* Index into xf_netdev's 'ports'. */
 +    struct list node;           /* Element in xf_netdev's 'port_list'. */
 +    struct netdev *netdev;
 +    bool internal;              /* Internal port (as XFLOW_PORT_INTERNAL)? */
 +};
 +
 +/* A flow in xf_netdev's 'flow_table'. */
 +struct xf_netdev_flow {
 +    struct hmap_node node;      /* Element in xf_netdev's 'flow_table'. */
 +    struct xflow_key key;
 +
 +    /* Statistics. */
 +    struct timespec used;       /* Last used time. */
 +    long long int packet_count; /* Number of packets matched. */
 +    long long int byte_count;   /* Number of bytes matched. */
 +    uint16_t tcp_ctl;           /* Bitwise-OR of seen tcp_ctl values. */
 +
 +    /* Actions. */
 +    union xflow_action *actions;
 +    unsigned int n_actions;
 +};
 +
 +/* Interface to netdev-based datapath. */
 +struct xfif_netdev {
 +    struct xfif xfif;
 +    struct xf_netdev *xf;
 +    int listen_mask;
 +    unsigned int xf_serial;
 +};
 +
 +/* All netdev-based datapaths. */
 +static struct xf_netdev *xf_netdevs[256];
 +struct list xf_netdev_list = LIST_INITIALIZER(&xf_netdev_list);
 +enum { N_XF_NETDEVS = ARRAY_SIZE(xf_netdevs) };
 +
 +/* Maximum port MTU seen so far. */
 +static int max_mtu = ETH_PAYLOAD_MAX;
 +
 +static int get_port_by_number(struct xf_netdev *, uint16_t port_no,
 +                              struct xf_netdev_port **portp);
 +static int get_port_by_name(struct xf_netdev *, const char *devname,
 +                            struct xf_netdev_port **portp);
 +static void xf_netdev_free(struct xf_netdev *);
 +static void xf_netdev_flow_flush(struct xf_netdev *);
 +static int do_add_port(struct xf_netdev *, const char *devname, uint16_t flags,
 +                       uint16_t port_no);
 +static int do_del_port(struct xf_netdev *, uint16_t port_no);
 +static int xf_netdev_output_control(struct xf_netdev *, const struct ofpbuf *,
 +                                    int queue_no, int port_no, uint32_t arg);
 +static int xf_netdev_execute_actions(struct xf_netdev *,
 +                                     struct ofpbuf *, struct xflow_key *,
 +                                     const union xflow_action *, int n);
 +
 +static struct xfif_netdev *
 +xfif_netdev_cast(const struct xfif *xfif)
 +{
 +    xfif_assert_class(xfif, &xfif_netdev_class);
 +    return CONTAINER_OF(xfif, struct xfif_netdev, xfif);
 +}
 +
 +static struct xf_netdev *
 +get_xf_netdev(const struct xfif *xfif)
 +{
 +    return xfif_netdev_cast(xfif)->xf;
 +}
 +
 +static int
 +name_to_xf_idx(const char *name)
 +{
 +    if (!strncmp(name, "xf", 2) && isdigit((unsigned char)name[2])) {
 +        int xf_idx = atoi(name + 2);
 +        if (xf_idx >= 0 && xf_idx < N_XF_NETDEVS) {
 +            return xf_idx;
 +        }
 +    }
 +    return -1;
 +}
 +
 +static struct xf_netdev *
 +find_xf_netdev(const char *name)
 +{
 +    int xf_idx;
 +    size_t i;
 +
 +    xf_idx = name_to_xf_idx(name);
 +    if (xf_idx >= 0) {
 +        return xf_netdevs[xf_idx];
 +    }
 +
 +    for (i = 0; i < N_XF_NETDEVS; i++) {
 +        struct xf_netdev *xf = xf_netdevs[i];
 +        if (xf) {
 +            struct xf_netdev_port *port;
 +            if (!get_port_by_name(xf, name, &port)) {
 +                return xf;
 +            }
 +        }
 +    }
 +    return NULL;
 +}
 +
 +static struct xfif *
 +create_xfif_netdev(struct xf_netdev *xf)
 +{
 +    struct xfif_netdev *xfif;
 +    char *xfname;
 +
 +    xf->open_cnt++;
 +
 +    xfname = xasprintf("xf%d", xf->xf_idx);
 +    xfif = xmalloc(sizeof *xfif);
 +    xfif_init(&xfif->xfif, &xfif_netdev_class, xfname, xf->xf_idx, xf->xf_idx);
 +    xfif->xf = xf;
 +    xfif->listen_mask = 0;
 +    xfif->xf_serial = xf->serial;
 +    free(xfname);
 +
 +    return &xfif->xfif;
 +}
 +
 +static int
 +create_xf_netdev(const char *name, int xf_idx, struct xfif **xfifp)
 +{
 +    struct xf_netdev *xf;
 +    int error;
 +    int i;
 +
 +    if (xf_netdevs[xf_idx]) {
 +        return EBUSY;
 +    }
 +
 +    /* Create datapath. */
 +    xf_netdevs[xf_idx] = xf = xzalloc(sizeof *xf);
 +    list_push_back(&xf_netdev_list, &xf->node);
 +    xf->xf_idx = xf_idx;
 +    xf->open_cnt = 0;
 +    xf->drop_frags = false;
 +    for (i = 0; i < N_QUEUES; i++) {
 +        queue_init(&xf->queues[i]);
 +    }
 +    hmap_init(&xf->flow_table);
 +    for (i = 0; i < N_GROUPS; i++) {
 +        xf->groups[i].ports = NULL;
 +        xf->groups[i].n_ports = 0;
 +        xf->groups[i].group = i;
 +    }
 +    list_init(&xf->port_list);
 +    error = do_add_port(xf, name, XFLOW_PORT_INTERNAL, XFLOWP_LOCAL);
 +    if (error) {
 +        xf_netdev_free(xf);
 +        return ENODEV;
 +    }
 +
 +    *xfifp = create_xfif_netdev(xf);
 +    return 0;
 +}
 +
 +static int
 +xfif_netdev_open(const char *name, const char *type OVS_UNUSED, bool create,
 +                 struct xfif **xfifp)
 +{
 +    if (create) {
 +        if (find_xf_netdev(name)) {
 +            return EEXIST;
 +        } else {
 +            int xf_idx = name_to_xf_idx(name);
 +            if (xf_idx >= 0) {
 +                return create_xf_netdev(name, xf_idx, xfifp);
 +            } else {
 +                /* Scan for unused xf_idx number. */
 +                for (xf_idx = 0; xf_idx < N_XF_NETDEVS; xf_idx++) {
 +                    int error = create_xf_netdev(name, xf_idx, xfifp);
 +                    if (error != EBUSY) {
 +                        return error;
 +                    }
 +                }
 +
 +                /* All datapath numbers in use. */
 +                return ENOBUFS;
 +            }
 +        }
 +    } else {
 +        struct xf_netdev *xf = find_xf_netdev(name);
 +        if (xf) {
 +            *xfifp = create_xfif_netdev(xf);
 +            return 0;
 +        } else {
 +            return ENODEV;
 +        }
 +    }
 +}
 +
 +static void
 +xf_netdev_free(struct xf_netdev *xf)
 +{
 +    int i;
 +
 +    xf_netdev_flow_flush(xf);
 +    while (xf->n_ports > 0) {
 +        struct xf_netdev_port *port = CONTAINER_OF(
 +            xf->port_list.next, struct xf_netdev_port, node);
 +        do_del_port(xf, port->port_no);
 +    }
 +    for (i = 0; i < N_QUEUES; i++) {
 +        queue_destroy(&xf->queues[i]);
 +    }
 +    hmap_destroy(&xf->flow_table);
 +    for (i = 0; i < N_GROUPS; i++) {
 +        free(xf->groups[i].ports);
 +    }
 +    xf_netdevs[xf->xf_idx] = NULL;
 +    list_remove(&xf->node);
 +    free(xf);
 +}
 +
 +static void
 +xfif_netdev_close(struct xfif *xfif)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    assert(xf->open_cnt > 0);
 +    if (--xf->open_cnt == 0 && xf->destroyed) {
 +        xf_netdev_free(xf);
 +    }
 +    free(xfif);
 +}
 +
 +static int
 +xfif_netdev_destroy(struct xfif *xfif)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    xf->destroyed = true;
 +    return 0;
 +}
 +
 +static int
 +xfif_netdev_get_stats(const struct xfif *xfif, struct xflow_stats *stats)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    memset(stats, 0, sizeof *stats);
 +    stats->n_flows = hmap_count(&xf->flow_table);
 +    stats->cur_capacity = hmap_capacity(&xf->flow_table);
 +    stats->max_capacity = MAX_FLOWS;
 +    stats->n_ports = xf->n_ports;
 +    stats->max_ports = MAX_PORTS;
 +    stats->max_groups = N_GROUPS;
 +    stats->n_frags = xf->n_frags;
 +    stats->n_hit = xf->n_hit;
 +    stats->n_missed = xf->n_missed;
 +    stats->n_lost = xf->n_lost;
 +    stats->max_miss_queue = MAX_QUEUE_LEN;
 +    stats->max_action_queue = MAX_QUEUE_LEN;
 +    return 0;
 +}
 +
 +static int
 +xfif_netdev_get_drop_frags(const struct xfif *xfif, bool *drop_fragsp)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    *drop_fragsp = xf->drop_frags;
 +    return 0;
 +}
 +
 +static int
 +xfif_netdev_set_drop_frags(struct xfif *xfif, bool drop_frags)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    xf->drop_frags = drop_frags;
 +    return 0;
 +}
 +
 +static int
 +do_add_port(struct xf_netdev *xf, const char *devname, uint16_t flags,
 +            uint16_t port_no)
 +{
 +    bool internal = (flags & XFLOW_PORT_INTERNAL) != 0;
 +    struct xf_netdev_port *port;
 +    struct netdev_options netdev_options;
 +    struct netdev *netdev;
 +    int mtu;
 +    int error;
 +
 +    /* XXX reject devices already in some xf_netdev. */
 +
 +    /* Open and validate network device. */
 +    memset(&netdev_options, 0, sizeof netdev_options);
 +    netdev_options.name = devname;
 +    netdev_options.ethertype = NETDEV_ETH_TYPE_ANY;
 +    if (internal) {
 +        netdev_options.type = "tap";
 +    }
 +
 +    error = netdev_open(&netdev_options, &netdev);
 +    if (error) {
 +        return error;
 +    }
 +    /* XXX reject loopback devices */
 +    /* XXX reject non-Ethernet devices */
 +
 +    error = netdev_turn_flags_on(netdev, NETDEV_PROMISC, false);
 +    if (error) {
 +        netdev_close(netdev);
 +        return error;
 +    }
 +
 +    port = xmalloc(sizeof *port);
 +    port->port_no = port_no;
 +    port->netdev = netdev;
 +    port->internal = internal;
 +
 +    netdev_get_mtu(netdev, &mtu);
 +    if (mtu > max_mtu) {
 +        max_mtu = mtu;
 +    }
 +
 +    list_push_back(&xf->port_list, &port->node);
 +    xf->ports[port_no] = port;
 +    xf->n_ports++;
 +    xf->serial++;
 +
 +    return 0;
 +}
 +
 +static int
 +xfif_netdev_port_add(struct xfif *xfif, const char *devname, uint16_t flags,
 +                     uint16_t *port_nop)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    int port_no;
 +
 +    for (port_no = 0; port_no < MAX_PORTS; port_no++) {
 +        if (!xf->ports[port_no]) {
 +            *port_nop = port_no;
 +            return do_add_port(xf, devname, flags, port_no);
 +        }
 +    }
 +    return EFBIG;
 +}
 +
 +static int
 +xfif_netdev_port_del(struct xfif *xfif, uint16_t port_no)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    return port_no == XFLOWP_LOCAL ? EINVAL : do_del_port(xf, port_no);
 +}
 +
 +static bool
 +is_valid_port_number(uint16_t port_no)
 +{
 +    return port_no < MAX_PORTS;
 +}
 +
 +static int
 +get_port_by_number(struct xf_netdev *xf,
 +                   uint16_t port_no, struct xf_netdev_port **portp)
 +{
 +    if (!is_valid_port_number(port_no)) {
 +        *portp = NULL;
 +        return EINVAL;
 +    } else {
 +        *portp = xf->ports[port_no];
 +        return *portp ? 0 : ENOENT;
 +    }
 +}
 +
 +static int
 +get_port_by_name(struct xf_netdev *xf,
 +                 const char *devname, struct xf_netdev_port **portp)
 +{
 +    struct xf_netdev_port *port;
 +
-     HMAP_FOR_EACH_SAFE (flow, next, struct xf_netdev_flow, node,
-                         &xf->flow_table) {
++    LIST_FOR_EACH (port, node, &xf->port_list) {
 +        if (!strcmp(netdev_get_name(port->netdev), devname)) {
 +            *portp = port;
 +            return 0;
 +        }
 +    }
 +    return ENOENT;
 +}
 +
 +static int
 +do_del_port(struct xf_netdev *xf, uint16_t port_no)
 +{
 +    struct xf_netdev_port *port;
 +    char *name;
 +    int error;
 +
 +    error = get_port_by_number(xf, port_no, &port);
 +    if (error) {
 +        return error;
 +    }
 +
 +    list_remove(&port->node);
 +    xf->ports[port->port_no] = NULL;
 +    xf->n_ports--;
 +    xf->serial++;
 +
 +    name = xstrdup(netdev_get_name(port->netdev));
 +    netdev_close(port->netdev);
 +
 +    free(name);
 +    free(port);
 +
 +    return 0;
 +}
 +
 +static void
 +answer_port_query(const struct xf_netdev_port *port, struct xflow_port *xflow_port)
 +{
 +    memset(xflow_port, 0, sizeof *xflow_port);
 +    ovs_strlcpy(xflow_port->devname, netdev_get_name(port->netdev),
 +                sizeof xflow_port->devname);
 +    xflow_port->port = port->port_no;
 +    xflow_port->flags = port->internal ? XFLOW_PORT_INTERNAL : 0;
 +}
 +
 +static int
 +xfif_netdev_port_query_by_number(const struct xfif *xfif, uint16_t port_no,
 +                                 struct xflow_port *xflow_port)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    struct xf_netdev_port *port;
 +    int error;
 +
 +    error = get_port_by_number(xf, port_no, &port);
 +    if (!error) {
 +        answer_port_query(port, xflow_port);
 +    }
 +    return error;
 +}
 +
 +static int
 +xfif_netdev_port_query_by_name(const struct xfif *xfif, const char *devname,
 +                               struct xflow_port *xflow_port)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    struct xf_netdev_port *port;
 +    int error;
 +
 +    error = get_port_by_name(xf, devname, &port);
 +    if (!error) {
 +        answer_port_query(port, xflow_port);
 +    }
 +    return error;
 +}
 +
 +static void
 +xf_netdev_free_flow(struct xf_netdev *xf, struct xf_netdev_flow *flow)
 +{
 +    hmap_remove(&xf->flow_table, &flow->node);
 +    free(flow->actions);
 +    free(flow);
 +}
 +
 +static void
 +xf_netdev_flow_flush(struct xf_netdev *xf)
 +{
 +    struct xf_netdev_flow *flow, *next;
 +
-     LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
++    HMAP_FOR_EACH_SAFE (flow, next, node, &xf->flow_table) {
 +        xf_netdev_free_flow(xf, flow);
 +    }
 +}
 +
 +static int
 +xfif_netdev_flow_flush(struct xfif *xfif)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    xf_netdev_flow_flush(xf);
 +    return 0;
 +}
 +
 +static int
 +xfif_netdev_port_list(const struct xfif *xfif, struct xflow_port *ports, int n)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    struct xf_netdev_port *port;
 +    int i;
 +
 +    i = 0;
-     HMAP_FOR_EACH_WITH_HASH (flow, struct xf_netdev_flow, node,
++    LIST_FOR_EACH (port, node, &xf->port_list) {
 +        struct xflow_port *xflow_port = &ports[i];
 +        if (i >= n) {
 +            break;
 +        }
 +        answer_port_query(port, xflow_port);
 +        i++;
 +    }
 +    return xf->n_ports;
 +}
 +
 +static int
 +xfif_netdev_port_poll(const struct xfif *xfif_, char **devnamep OVS_UNUSED)
 +{
 +    struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
 +    if (xfif->xf_serial != xfif->xf->serial) {
 +        xfif->xf_serial = xfif->xf->serial;
 +        return ENOBUFS;
 +    } else {
 +        return EAGAIN;
 +    }
 +}
 +
 +static void
 +xfif_netdev_port_poll_wait(const struct xfif *xfif_)
 +{
 +    struct xfif_netdev *xfif = xfif_netdev_cast(xfif_);
 +    if (xfif->xf_serial != xfif->xf->serial) {
 +        poll_immediate_wake();
 +    }
 +}
 +
 +static int
 +get_port_group(const struct xfif *xfif, int group_no,
 +               struct xflow_port_group **groupp)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +
 +    if (group_no >= 0 && group_no < N_GROUPS) {
 +        *groupp = &xf->groups[group_no];
 +        return 0;
 +    } else {
 +        *groupp = NULL;
 +        return EINVAL;
 +    }
 +}
 +
 +static int
 +xfif_netdev_port_group_get(const struct xfif *xfif, int group_no,
 +                           uint16_t ports[], int n)
 +{
 +    struct xflow_port_group *group;
 +    int error;
 +
 +    if (n < 0) {
 +        return -EINVAL;
 +    }
 +
 +    error = get_port_group(xfif, group_no, &group);
 +    if (!error) {
 +        memcpy(ports, group->ports, MIN(n, group->n_ports) * sizeof *ports);
 +        return group->n_ports;
 +    } else {
 +        return -error;
 +    }
 +}
 +
 +static int
 +xfif_netdev_port_group_set(struct xfif *xfif, int group_no,
 +                           const uint16_t ports[], int n)
 +{
 +    struct xflow_port_group *group;
 +    int error;
 +
 +    if (n < 0 || n > MAX_PORTS) {
 +        return EINVAL;
 +    }
 +
 +    error = get_port_group(xfif, group_no, &group);
 +    if (!error) {
 +        free(group->ports);
 +        group->ports = xmemdup(ports, n * sizeof *group->ports);
 +        group->n_ports = n;
 +        group->group = group_no;
 +    }
 +    return error;
 +}
 +
 +static struct xf_netdev_flow *
 +xf_netdev_lookup_flow(const struct xf_netdev *xf,
 +                      const struct xflow_key *key)
 +{
 +    struct xf_netdev_flow *flow;
 +
-     HMAP_FOR_EACH (flow, struct xf_netdev_flow, node, &xf->flow_table) {
++    HMAP_FOR_EACH_WITH_HASH (flow, node,
 +                             xflow_key_hash(key, 0), &xf->flow_table) {
 +        if (xflow_key_equal(&flow->key, key)) {
 +            return flow;
 +        }
 +    }
 +    return NULL;
 +}
 +
 +static void
 +answer_flow_query(struct xf_netdev_flow *flow, uint32_t query_flags,
 +                  struct xflow_flow *xflow_flow)
 +{
 +    if (flow) {
 +        xflow_flow->key = flow->key;
 +        xflow_flow->stats.n_packets = flow->packet_count;
 +        xflow_flow->stats.n_bytes = flow->byte_count;
 +        xflow_flow->stats.used_sec = flow->used.tv_sec;
 +        xflow_flow->stats.used_nsec = flow->used.tv_nsec;
 +        xflow_flow->stats.tcp_flags = TCP_FLAGS(flow->tcp_ctl);
 +        xflow_flow->stats.reserved = 0;
 +        xflow_flow->stats.error = 0;
 +        if (xflow_flow->n_actions > 0) {
 +            unsigned int n = MIN(xflow_flow->n_actions, flow->n_actions);
 +            memcpy(xflow_flow->actions, flow->actions,
 +                   n * sizeof *xflow_flow->actions);
 +            xflow_flow->n_actions = flow->n_actions;
 +        }
 +
 +        if (query_flags & XFLOWFF_ZERO_TCP_FLAGS) {
 +            flow->tcp_ctl = 0;
 +        }
 +
 +    } else {
 +        xflow_flow->stats.error = ENOENT;
 +    }
 +}
 +
 +static int
 +xfif_netdev_flow_get(const struct xfif *xfif, struct xflow_flow flows[], int n)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    int i;
 +
 +    for (i = 0; i < n; i++) {
 +        struct xflow_flow *xflow_flow = &flows[i];
 +        answer_flow_query(xf_netdev_lookup_flow(xf, &xflow_flow->key),
 +                          xflow_flow->flags, xflow_flow);
 +    }
 +    return 0;
 +}
 +
 +static int
 +xfif_netdev_validate_actions(const union xflow_action *actions, int n_actions,
 +                             bool *mutates)
 +{
 +    unsigned int i;
 +
 +    *mutates = false;
 +    for (i = 0; i < n_actions; i++) {
 +        const union xflow_action *a = &actions[i];
 +        switch (a->type) {
 +        case XFLOWAT_OUTPUT:
 +            if (a->output.port >= MAX_PORTS) {
 +                return EINVAL;
 +            }
 +            break;
 +
 +        case XFLOWAT_OUTPUT_GROUP:
 +            *mutates = true;
 +            if (a->output_group.group >= N_GROUPS) {
 +                return EINVAL;
 +            }
 +            break;
 +
 +        case XFLOWAT_CONTROLLER:
 +            break;
 +
 +        case XFLOWAT_SET_DL_TCI:
 +            *mutates = true;
 +            if (a->dl_tci.mask != htons(VLAN_VID_MASK)
 +                && a->dl_tci.mask != htons(VLAN_PCP_MASK)
 +                && a->dl_tci.mask != htons(VLAN_VID_MASK | VLAN_PCP_MASK)) {
 +                return EINVAL;
 +            }
 +            if (a->dl_tci.tci & ~a->dl_tci.mask){
 +                return EINVAL;
 +            }
 +            break;
 +
 +        case XFLOWAT_SET_NW_TOS:
 +            *mutates = true;
 +            if (a->nw_tos.nw_tos & IP_ECN_MASK) {
 +                return EINVAL;
 +            }
 +            break;
 +
 +        case XFLOWAT_STRIP_VLAN:
 +        case XFLOWAT_SET_DL_SRC:
 +        case XFLOWAT_SET_DL_DST:
 +        case XFLOWAT_SET_NW_SRC:
 +        case XFLOWAT_SET_NW_DST:
 +        case XFLOWAT_SET_TP_SRC:
 +        case XFLOWAT_SET_TP_DST:
 +            *mutates = true;
 +            break;
 +
 +        default:
 +            return EOPNOTSUPP;
 +        }
 +    }
 +    return 0;
 +}
 +
 +static int
 +set_flow_actions(struct xf_netdev_flow *flow, struct xflow_flow *xflow_flow)
 +{
 +    size_t n_bytes;
 +    bool mutates;
 +    int error;
 +
 +    if (xflow_flow->n_actions >= 4096 / sizeof *xflow_flow->actions) {
 +        return EINVAL;
 +    }
 +    error = xfif_netdev_validate_actions(xflow_flow->actions,
 +                                         xflow_flow->n_actions, &mutates);
 +    if (error) {
 +        return error;
 +    }
 +
 +    n_bytes = xflow_flow->n_actions * sizeof *flow->actions;
 +    flow->actions = xrealloc(flow->actions, n_bytes);
 +    flow->n_actions = xflow_flow->n_actions;
 +    memcpy(flow->actions, xflow_flow->actions, n_bytes);
 +    return 0;
 +}
 +
 +static int
 +add_flow(struct xfif *xfif, struct xflow_flow *xflow_flow)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    struct xf_netdev_flow *flow;
 +    int error;
 +
 +    flow = xzalloc(sizeof *flow);
 +    flow->key = xflow_flow->key;
 +
 +    error = set_flow_actions(flow, xflow_flow);
 +    if (error) {
 +        free(flow);
 +        return error;
 +    }
 +
 +    hmap_insert(&xf->flow_table, &flow->node,
 +                xflow_key_hash(&flow->key, 0));
 +    return 0;
 +}
 +
 +static void
 +clear_stats(struct xf_netdev_flow *flow)
 +{
 +    flow->used.tv_sec = 0;
 +    flow->used.tv_nsec = 0;
 +    flow->packet_count = 0;
 +    flow->byte_count = 0;
 +    flow->tcp_ctl = 0;
 +}
 +
 +static int
 +xfif_netdev_flow_put(struct xfif *xfif, struct xflow_flow_put *put)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    struct xf_netdev_flow *flow;
 +
 +    flow = xf_netdev_lookup_flow(xf, &put->flow.key);
 +    if (!flow) {
 +        if (put->flags & XFLOWPF_CREATE) {
 +            if (hmap_count(&xf->flow_table) < MAX_FLOWS) {
 +                return add_flow(xfif, &put->flow);
 +            } else {
 +                return EFBIG;
 +            }
 +        } else {
 +            return ENOENT;
 +        }
 +    } else {
 +        if (put->flags & XFLOWPF_MODIFY) {
 +            int error = set_flow_actions(flow, &put->flow);
 +            if (!error && put->flags & XFLOWPF_ZERO_STATS) {
 +                clear_stats(flow);
 +            }
 +            return error;
 +        } else {
 +            return EEXIST;
 +        }
 +    }
 +}
 +
 +
 +static int
 +xfif_netdev_flow_del(struct xfif *xfif, struct xflow_flow *xflow_flow)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    struct xf_netdev_flow *flow;
 +
 +    flow = xf_netdev_lookup_flow(xf, &xflow_flow->key);
 +    if (flow) {
 +        answer_flow_query(flow, 0, xflow_flow);
 +        xf_netdev_free_flow(xf, flow);
 +        return 0;
 +    } else {
 +        return ENOENT;
 +    }
 +}
 +
 +static int
 +xfif_netdev_flow_list(const struct xfif *xfif, struct xflow_flow flows[], int n)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    struct xf_netdev_flow *flow;
 +    int i;
 +
 +    i = 0;
-     LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
++    HMAP_FOR_EACH (flow, node, &xf->flow_table) {
 +        if (i >= n) {
 +            break;
 +        }
 +        answer_flow_query(flow, 0, &flows[i++]);
 +    }
 +    return hmap_count(&xf->flow_table);
 +}
 +
 +static int
 +xfif_netdev_execute(struct xfif *xfif, uint16_t in_port,
 +                    const union xflow_action actions[], int n_actions,
 +                    const struct ofpbuf *packet)
 +{
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    struct ofpbuf copy;
 +    bool mutates;
 +    struct xflow_key key;
 +    flow_t flow;
 +    int error;
 +
 +    if (packet->size < ETH_HEADER_LEN || packet->size > UINT16_MAX) {
 +        return EINVAL;
 +    }
 +
 +    error = xfif_netdev_validate_actions(actions, n_actions, &mutates);
 +    if (error) {
 +        return error;
 +    }
 +
 +    if (mutates) {
 +        /* We need a deep copy of 'packet' since we're going to modify its
 +         * data. */
 +        ofpbuf_init(&copy, XF_NETDEV_HEADROOM + packet->size);
 +        copy.data = (char*)copy.base + XF_NETDEV_HEADROOM;
 +        ofpbuf_put(&copy, packet->data, packet->size);
 +    } else {
 +        /* We still need a shallow copy of 'packet', even though we won't
 +         * modify its data, because flow_extract() modifies packet->l2, etc.
 +         * We could probably get away with modifying those but it's more polite
 +         * if we don't. */
 +        copy = *packet;
 +    }
 +    flow_extract(&copy, 0, in_port, &flow);
 +    xflow_key_from_flow(&key, &flow);
 +    error = xf_netdev_execute_actions(xf, &copy, &key, actions, n_actions);
 +    if (mutates) {
 +        ofpbuf_uninit(&copy);
 +    }
 +    return error;
 +}
 +
 +static int
 +xfif_netdev_recv_get_mask(const struct xfif *xfif, int *listen_mask)
 +{
 +    struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
 +    *listen_mask = xfif_netdev->listen_mask;
 +    return 0;
 +}
 +
 +static int
 +xfif_netdev_recv_set_mask(struct xfif *xfif, int listen_mask)
 +{
 +    struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
 +    if (!(listen_mask & ~XFLOWL_ALL)) {
 +        xfif_netdev->listen_mask = listen_mask;
 +        return 0;
 +    } else {
 +        return EINVAL;
 +    }
 +}
 +
 +static struct ovs_queue *
 +find_nonempty_queue(struct xfif *xfif)
 +{
 +    struct xfif_netdev *xfif_netdev = xfif_netdev_cast(xfif);
 +    struct xf_netdev *xf = get_xf_netdev(xfif);
 +    int mask = xfif_netdev->listen_mask;
 +    int i;
 +
 +    for (i = 0; i < N_QUEUES; i++) {
 +        struct ovs_queue *q = &xf->queues[i];
 +        if (q->n && mask & (1u << i)) {
 +            return q;
 +        }
 +    }
 +    return NULL;
 +}
 +
 +static int
 +xfif_netdev_recv(struct xfif *xfif, struct ofpbuf **bufp)
 +{
 +    struct ovs_queue *q = find_nonempty_queue(xfif);
 +    if (q) {
 +        *bufp = queue_pop_head(q);
 +        return 0;
 +    } else {
 +        return EAGAIN;
 +    }
 +}
 +
 +static void
 +xfif_netdev_recv_wait(struct xfif *xfif)
 +{
 +    struct ovs_queue *q = find_nonempty_queue(xfif);
 +    if (q) {
 +        poll_immediate_wake();
 +    } else {
 +        /* No messages ready to be received, and xf_wait() will ensure that we
 +         * wake up to queue new messages, so there is nothing to do. */
 +    }
 +}
 +\f
 +static void
 +xf_netdev_flow_used(struct xf_netdev_flow *flow,
 +                    const struct xflow_key *key,
 +                    const struct ofpbuf *packet)
 +{
 +    time_timespec(&flow->used);
 +    flow->packet_count++;
 +    flow->byte_count += packet->size;
 +    if (key->dl_type == htons(ETH_TYPE_IP) && key->nw_proto == IPPROTO_TCP) {
 +        struct tcp_header *th = packet->l4;
 +        flow->tcp_ctl |= th->tcp_ctl;
 +    }
 +}
 +
 +static void
 +xf_netdev_port_input(struct xf_netdev *xf, struct xf_netdev_port *port,
 +                     struct ofpbuf *packet)
 +{
 +    struct xf_netdev_flow *flow;
 +    struct xflow_key key;
 +    flow_t f;
 +
 +    if (packet->size < ETH_HEADER_LEN) {
 +        return;
 +    }
 +    if (flow_extract(packet, 0, port->port_no, &f) && xf->drop_frags) {
 +        xf->n_frags++;
 +        return;
 +    }
 +    xflow_key_from_flow(&key, &f);
 +
 +    flow = xf_netdev_lookup_flow(xf, &key);
 +    if (flow) {
 +        xf_netdev_flow_used(flow, &key, packet);
 +        xf_netdev_execute_actions(xf, packet, &key,
 +                                  flow->actions, flow->n_actions);
 +        xf->n_hit++;
 +    } else {
 +        xf->n_missed++;
 +        xf_netdev_output_control(xf, packet, _XFLOWL_MISS_NR, port->port_no, 0);
 +    }
 +}
 +
 +static void
 +xf_netdev_run(void)
 +{
 +    struct ofpbuf packet;
 +    struct xf_netdev *xf;
 +
 +    ofpbuf_init(&packet, XF_NETDEV_HEADROOM + max_mtu);
-         LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
++    LIST_FOR_EACH (xf, node, &xf_netdev_list) {
 +        struct xf_netdev_port *port;
 +
-     LIST_FOR_EACH (xf, struct xf_netdev, node, &xf_netdev_list) {
++        LIST_FOR_EACH (port, node, &xf->port_list) {
 +            int error;
 +
 +            /* Reset packet contents. */
 +            packet.data = (char*)packet.base + XF_NETDEV_HEADROOM;
 +            packet.size = 0;
 +
 +            error = netdev_recv(port->netdev, &packet);
 +            if (!error) {
 +                xf_netdev_port_input(xf, port, &packet);
 +            } else if (error != EAGAIN) {
 +                struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
 +                VLOG_ERR_RL(&rl, "error receiving data from %s: %s",
 +                            netdev_get_name(port->netdev), strerror(error));
 +            }
 +        }
 +    }
 +    ofpbuf_uninit(&packet);
 +}
 +
 +static void
 +xf_netdev_wait(void)
 +{
 +    struct xf_netdev *xf;
 +
-         LIST_FOR_EACH (port, struct xf_netdev_port, node, &xf->port_list) {
++    LIST_FOR_EACH (xf, node, &xf_netdev_list) {
 +        struct xf_netdev_port *port;
-         struct eth_header *eh = packet->l2;
++        LIST_FOR_EACH (port, node, &xf->port_list) {
 +            netdev_recv_wait(port->netdev);
 +        }
 +    }
 +}
 +
 +
 +/* Modify or add a 802.1Q header in 'packet' according to 'a'. */
 +static void
 +xf_netdev_set_dl_tci(struct ofpbuf *packet,
 +                     const struct xflow_action_dl_tci *a)
 +{
 +    struct vlan_eth_header *veh;
 +    struct eth_header *eh;
 +
 +    eh = packet->l2;
 +    if (packet->size >= sizeof(struct vlan_eth_header)
 +        && eh->eth_type == htons(ETH_TYPE_VLAN)) {
 +        veh = packet->l2;
 +        veh->veth_tci = (veh->veth_tci & ~a->mask) | a->tci;
 +    } else {
 +        /* Insert new 802.1Q header. */
 +        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 = htons(a->tci);
 +        tmp.veth_next_type = eh->eth_type;
 +
 +        veh = ofpbuf_push_uninit(packet, VLAN_HEADER_LEN);
 +        memcpy(veh, &tmp, sizeof tmp);
 +        packet->l2 = (char*)packet->l2 - VLAN_HEADER_LEN;
 +    }
 +}
 +
 +static void
 +xf_netdev_strip_vlan(struct ofpbuf *packet, struct xflow_key *key)
 +{
 +    struct vlan_eth_header *veh = packet->l2;
 +    if (packet->size >= sizeof *veh
 +        && 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;
 +
 +        packet->size -= VLAN_HEADER_LEN;
 +        packet->data = (char*)packet->data + VLAN_HEADER_LEN;
 +        packet->l2 = (char*)packet->l2 + VLAN_HEADER_LEN;
 +        memcpy(packet->data, &tmp, sizeof tmp);
 +
 +        key->dl_tci = htons(0);
 +    }
 +}
 +
 +static void
 +xf_netdev_set_dl_src(struct ofpbuf *packet,
 +                     const uint8_t dl_addr[ETH_ADDR_LEN])
 +{
 +    struct eth_header *eh = packet->l2;
 +    memcpy(eh->eth_src, dl_addr, sizeof eh->eth_src);
 +}
 +
 +static void
 +xf_netdev_set_dl_dst(struct ofpbuf *packet,
 +                     const uint8_t dl_addr[ETH_ADDR_LEN])
 +{
 +    struct eth_header *eh = packet->l2;
 +    memcpy(eh->eth_dst, dl_addr, sizeof eh->eth_dst);
 +}
 +
 +static bool
 +is_ip(const struct ofpbuf *packet, const struct xflow_key *key)
 +{
 +    return key->dl_type == htons(ETH_TYPE_IP) && packet->l4;
 +}
 +
 +static void
 +xf_netdev_set_nw_addr(struct ofpbuf *packet, const struct xflow_key *key,
 +                      const struct xflow_action_nw_addr *a)
 +{
 +    if (is_ip(packet, key)) {
 +        struct ip_header *nh = packet->l3;
 +        uint32_t *field;
 +
 +        field = a->type == XFLOWAT_SET_NW_SRC ? &nh->ip_src : &nh->ip_dst;
 +        if (key->nw_proto == IP_TYPE_TCP && packet->l7) {
 +            struct tcp_header *th = packet->l4;
 +            th->tcp_csum = recalc_csum32(th->tcp_csum, *field, a->nw_addr);
 +        } else if (key->nw_proto == IP_TYPE_UDP && packet->l7) {
 +            struct udp_header *uh = packet->l4;
 +            if (uh->udp_csum) {
 +                uh->udp_csum = recalc_csum32(uh->udp_csum, *field, a->nw_addr);
 +                if (!uh->udp_csum) {
 +                    uh->udp_csum = 0xffff;
 +                }
 +            }
 +        }
 +        nh->ip_csum = recalc_csum32(nh->ip_csum, *field, a->nw_addr);
 +        *field = a->nw_addr;
 +    }
 +}
 +
 +static void
 +xf_netdev_set_nw_tos(struct ofpbuf *packet, const struct xflow_key *key,
 +                     const struct xflow_action_nw_tos *a)
 +{
 +    if (is_ip(packet, key)) {
 +        struct ip_header *nh = packet->l3;
 +        uint8_t *field = &nh->ip_tos;
 +
 +        /* Set the DSCP bits and preserve the ECN bits. */
 +        uint8_t new = a->nw_tos | (nh->ip_tos & IP_ECN_MASK);
 +
 +        nh->ip_csum = recalc_csum16(nh->ip_csum, htons((uint16_t)*field),
 +                htons((uint16_t)a->nw_tos));
 +        *field = new;
 +    }
 +}
 +
 +static void
 +xf_netdev_set_tp_port(struct ofpbuf *packet, const struct xflow_key *key,
 +                      const struct xflow_action_tp_port *a)
 +{
 +    if (is_ip(packet, key)) {
 +        uint16_t *field;
 +        if (key->nw_proto == IPPROTO_TCP && packet->l7) {
 +            struct tcp_header *th = packet->l4;
 +            field = a->type == XFLOWAT_SET_TP_SRC ? &th->tcp_src : &th->tcp_dst;
 +            th->tcp_csum = recalc_csum16(th->tcp_csum, *field, a->tp_port);
 +            *field = a->tp_port;
 +        } else if (key->nw_proto == IPPROTO_UDP && packet->l7) {
 +            struct udp_header *uh = packet->l4;
 +            field = a->type == XFLOWAT_SET_TP_SRC ? &uh->udp_src : &uh->udp_dst;
 +            uh->udp_csum = recalc_csum16(uh->udp_csum, *field, a->tp_port);
 +            *field = a->tp_port;
 +        } else {
 +            return;
 +        }
 +    }
 +}
 +
 +static void
 +xf_netdev_output_port(struct xf_netdev *xf, struct ofpbuf *packet,
 +                      uint16_t out_port)
 +{
 +    struct xf_netdev_port *p = xf->ports[out_port];
 +    if (p) {
 +        netdev_send(p->netdev, packet);
 +    }
 +}
 +
 +static void
 +xf_netdev_output_group(struct xf_netdev *xf, uint16_t group, uint16_t in_port,
 +                       struct ofpbuf *packet)
 +{
 +    struct xflow_port_group *g = &xf->groups[group];
 +    int i;
 +
 +    for (i = 0; i < g->n_ports; i++) {
 +        uint16_t out_port = g->ports[i];
 +        if (out_port != in_port) {
 +            xf_netdev_output_port(xf, packet, out_port);
 +        }
 +    }
 +}
 +
 +static int
 +xf_netdev_output_control(struct xf_netdev *xf, const struct ofpbuf *packet,
 +                         int queue_no, int port_no, uint32_t arg)
 +{
 +    struct ovs_queue *q = &xf->queues[queue_no];
 +    struct xflow_msg *header;
 +    struct ofpbuf *msg;
 +    size_t msg_size;
 +
 +    if (q->n >= MAX_QUEUE_LEN) {
 +        xf->n_lost++;
 +        return ENOBUFS;
 +    }
 +
 +    msg_size = sizeof *header + packet->size;
 +    msg = ofpbuf_new(msg_size + XFIF_RECV_MSG_PADDING);
 +    header = ofpbuf_put_uninit(msg, sizeof *header);
 +    ofpbuf_reserve(msg, XFIF_RECV_MSG_PADDING);
 +    header->type = queue_no;
 +    header->length = msg_size;
 +    header->port = port_no;
 +    header->arg = arg;
 +    ofpbuf_put(msg, packet->data, packet->size);
 +    queue_push_tail(q, msg);
 +
 +    return 0;
 +}
 +
 +/* Returns true if 'packet' is an invalid Ethernet+IPv4 ARP packet: one with
 + * screwy or truncated header fields or one whose inner and outer Ethernet
 + * address differ. */
 +static bool
 +xf_netdev_is_spoofed_arp(struct ofpbuf *packet, const struct xflow_key *key)
 +{
 +    struct arp_eth_header *arp;
 +    struct eth_header *eth;
 +    ptrdiff_t l3_size;
 +
 +    if (key->dl_type != htons(ETH_TYPE_ARP)) {
 +        return false;
 +    }
 +
 +    l3_size = (char *) ofpbuf_end(packet) - (char *) packet->l3;
 +    if (l3_size < sizeof(struct arp_eth_header)) {
 +        return true;
 +    }
 +
 +    eth = packet->l2;
 +    arp = packet->l3;
 +    return (arp->ar_hrd != htons(ARP_HRD_ETHERNET)
 +            || arp->ar_pro != htons(ARP_PRO_IP)
 +            || arp->ar_hln != ETH_HEADER_LEN
 +            || arp->ar_pln != 4
 +            || !eth_addr_equals(arp->ar_sha, eth->eth_src));
 +}
 +
 +static int
 +xf_netdev_execute_actions(struct xf_netdev *xf,
 +                          struct ofpbuf *packet, struct xflow_key *key,
 +                          const union xflow_action *actions, int n_actions)
 +{
 +    int i;
 +    for (i = 0; i < n_actions; i++) {
 +        const union xflow_action *a = &actions[i];
 +
 +        switch (a->type) {
 +        case XFLOWAT_OUTPUT:
 +            xf_netdev_output_port(xf, packet, a->output.port);
 +            break;
 +
 +        case XFLOWAT_OUTPUT_GROUP:
 +            xf_netdev_output_group(xf, a->output_group.group, key->in_port,
 +                                   packet);
 +            break;
 +
 +        case XFLOWAT_CONTROLLER:
 +            xf_netdev_output_control(xf, packet, _XFLOWL_ACTION_NR,
 +                                     key->in_port, a->controller.arg);
 +            break;
 +
 +        case XFLOWAT_SET_DL_TCI:
 +            xf_netdev_set_dl_tci(packet, &a->dl_tci);
 +            break;
 +
 +        case XFLOWAT_STRIP_VLAN:
 +            xf_netdev_strip_vlan(packet, key);
 +            break;
 +
 +        case XFLOWAT_SET_DL_SRC:
 +            xf_netdev_set_dl_src(packet, a->dl_addr.dl_addr);
 +            break;
 +
 +        case XFLOWAT_SET_DL_DST:
 +            xf_netdev_set_dl_dst(packet, a->dl_addr.dl_addr);
 +            break;
 +
 +        case XFLOWAT_SET_NW_SRC:
 +        case XFLOWAT_SET_NW_DST:
 +            xf_netdev_set_nw_addr(packet, key, &a->nw_addr);
 +            break;
 +
 +        case XFLOWAT_SET_NW_TOS:
 +            xf_netdev_set_nw_tos(packet, key, &a->nw_tos);
 +            break;
 +
 +        case XFLOWAT_SET_TP_SRC:
 +        case XFLOWAT_SET_TP_DST:
 +            xf_netdev_set_tp_port(packet, key, &a->tp_port);
 +            break;
 +
 +        case XFLOWAT_DROP_SPOOFED_ARP:
 +            if (xf_netdev_is_spoofed_arp(packet, key)) {
 +                return 0;
 +            }
 +        }
 +    }
 +    return 0;
 +}
 +
 +const struct xfif_class xfif_netdev_class = {
 +    "netdev",
 +    xf_netdev_run,
 +    xf_netdev_wait,
 +    NULL,                       /* enumerate */
 +    xfif_netdev_open,
 +    xfif_netdev_close,
 +    NULL,                       /* get_all_names */
 +    xfif_netdev_destroy,
 +    xfif_netdev_get_stats,
 +    xfif_netdev_get_drop_frags,
 +    xfif_netdev_set_drop_frags,
 +    xfif_netdev_port_add,
 +    xfif_netdev_port_del,
 +    xfif_netdev_port_query_by_number,
 +    xfif_netdev_port_query_by_name,
 +    xfif_netdev_port_list,
 +    xfif_netdev_port_poll,
 +    xfif_netdev_port_poll_wait,
 +    xfif_netdev_port_group_get,
 +    xfif_netdev_port_group_set,
 +    xfif_netdev_flow_get,
 +    xfif_netdev_flow_put,
 +    xfif_netdev_flow_del,
 +    xfif_netdev_flow_flush,
 +    xfif_netdev_flow_list,
 +    xfif_netdev_execute,
 +    xfif_netdev_recv_get_mask,
 +    xfif_netdev_recv_set_mask,
 +    NULL,                       /* get_sflow_probability */
 +    NULL,                       /* set_sflow_probability */
 +    NULL,                       /* queue_to_priority */
 +    xfif_netdev_recv,
 +    xfif_netdev_recv_wait,
 +};
diff --combined lib/xflow-util.c
index e11da07,0000000..622f9ef
mode 100644,000000..100644
--- /dev/null
@@@ -1,207 -1,0 +1,210 @@@
 +/*
 + * Copyright (c) 2009, 2010 Nicira Networks.
 + *
 + * Licensed under the Apache License, Version 2.0 (the "License");
 + * you may not use this file except in compliance with the License.
 + * You may obtain a copy of the License at:
 + *
 + *     http://www.apache.org/licenses/LICENSE-2.0
 + *
 + * Unless required by applicable law or agreed to in writing, software
 + * distributed under the License is distributed on an "AS IS" BASIS,
 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 + * See the License for the specific language governing permissions and
 + * limitations under the License.
 + */
 +
 +#include <config.h>
 +#include "xflow-util.h"
 +#include <inttypes.h>
 +#include <stdlib.h>
 +#include <string.h>
 +#include "coverage.h"
 +#include "dynamic-string.h"
 +#include "flow.h"
 +#include "packets.h"
 +#include "timeval.h"
 +#include "util.h"
 +
 +union xflow_action *
 +xflow_actions_add(struct xflow_actions *actions, uint16_t type)
 +{
 +    union xflow_action *a;
 +    size_t idx;
 +
 +    idx = actions->n_actions++ & (MAX_XFLOW_ACTIONS - 1);
 +    a = &actions->actions[idx];
 +    memset(a, 0, sizeof *a);
 +    a->type = type;
 +    return a;
 +}
 +
 +void
 +format_xflow_key(struct ds *ds, const struct xflow_key *key)
 +{
 +    ds_put_format(ds, "tunnel%"PRIx32":in_port%04x",
 +                  key->tun_id, key->in_port);
 +    if (key->dl_tci) {
 +        ds_put_format(ds, ":vlan%"PRIu16":pcp%d",
 +                      vlan_tci_to_vid(key->dl_tci),
 +                      vlan_tci_to_pcp(key->dl_tci));
 +    }
 +    ds_put_format(ds, " mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT" type%04x "
 +                  "proto%"PRId8" tos%"PRIu8" ip"IP_FMT"->"IP_FMT" port%d->%d",
 +                  ETH_ADDR_ARGS(key->dl_src), ETH_ADDR_ARGS(key->dl_dst),
 +                  ntohs(key->dl_type), key->nw_proto, key->nw_tos,
 +                  IP_ARGS(&key->nw_src), IP_ARGS(&key->nw_dst),
 +                  ntohs(key->tp_src), ntohs(key->tp_dst));
 +}
 +
 +void
 +format_xflow_action(struct ds *ds, const union xflow_action *a)
 +{
 +    switch (a->type) {
 +    case XFLOWAT_OUTPUT:
 +        ds_put_format(ds, "%"PRIu16, a->output.port);
 +        break;
 +    case XFLOWAT_OUTPUT_GROUP:
 +        ds_put_format(ds, "g%"PRIu16, a->output_group.group);
 +        break;
 +    case XFLOWAT_CONTROLLER:
 +        ds_put_format(ds, "ctl(%"PRIu32")", a->controller.arg);
 +        break;
 +    case XFLOWAT_SET_TUNNEL:
 +        ds_put_format(ds, "set_tunnel(0x%08"PRIx32")", ntohl(a->tunnel.tun_id));
 +        break;
 +    case XFLOWAT_SET_DL_TCI:
 +        ds_put_format(ds, "set_tci(%04"PRIx16",mask=%04"PRIx16")",
 +                      ntohs(a->dl_tci.tci), ntohs(a->dl_tci.mask));
 +        break;
 +    case XFLOWAT_STRIP_VLAN:
 +        ds_put_format(ds, "strip_vlan");
 +        break;
 +    case XFLOWAT_SET_DL_SRC:
 +        ds_put_format(ds, "set_dl_src("ETH_ADDR_FMT")",
 +               ETH_ADDR_ARGS(a->dl_addr.dl_addr));
 +        break;
 +    case XFLOWAT_SET_DL_DST:
 +        ds_put_format(ds, "set_dl_dst("ETH_ADDR_FMT")",
 +               ETH_ADDR_ARGS(a->dl_addr.dl_addr));
 +        break;
 +    case XFLOWAT_SET_NW_SRC:
 +        ds_put_format(ds, "set_nw_src("IP_FMT")",
 +                      IP_ARGS(&a->nw_addr.nw_addr));
 +        break;
 +    case XFLOWAT_SET_NW_DST:
 +        ds_put_format(ds, "set_nw_dst("IP_FMT")",
 +                      IP_ARGS(&a->nw_addr.nw_addr));
 +        break;
 +    case XFLOWAT_SET_NW_TOS:
 +        ds_put_format(ds, "set_nw_tos(%"PRIu8")", a->nw_tos.nw_tos);
 +        break;
 +    case XFLOWAT_SET_TP_SRC:
 +        ds_put_format(ds, "set_tp_src(%"PRIu16")", ntohs(a->tp_port.tp_port));
 +        break;
 +    case XFLOWAT_SET_TP_DST:
 +        ds_put_format(ds, "set_tp_dst(%"PRIu16")", ntohs(a->tp_port.tp_port));
 +        break;
 +    case XFLOWAT_SET_PRIORITY:
 +        ds_put_format(ds, "set_priority(0x%"PRIx32")", a->priority.priority);
 +        break;
 +    case XFLOWAT_POP_PRIORITY:
 +        ds_put_cstr(ds, "pop_priority");
 +        break;
++    case XFLOWAT_DROP_SPOOFED_ARP:
++        ds_put_cstr(ds, "drop_spoofed_arp");
++        break;
 +    default:
 +        ds_put_format(ds, "***bad action 0x%"PRIx16"***", a->type);
 +        break;
 +    }
 +}
 +
 +void
 +format_xflow_actions(struct ds *ds, const union xflow_action *actions,
 +                   size_t n_actions)
 +{
 +    size_t i;
 +    for (i = 0; i < n_actions; i++) {
 +        if (i) {
 +            ds_put_char(ds, ',');
 +        }
 +        format_xflow_action(ds, &actions[i]);
 +    }
 +    if (!n_actions) {
 +        ds_put_cstr(ds, "drop");
 +    }
 +}
 +
 +void
 +format_xflow_flow_stats(struct ds *ds, const struct xflow_flow_stats *s)
 +{
 +    ds_put_format(ds, "packets:%llu, bytes:%llu, used:",
 +                  (unsigned long long int) s->n_packets,
 +                  (unsigned long long int) s->n_bytes);
 +    if (s->used_sec) {
 +        long long int used = s->used_sec * 1000 + s->used_nsec / 1000000;
 +        ds_put_format(ds, "%.3fs", (time_msec() - used) / 1000.0);
 +    } else {
 +        ds_put_format(ds, "never");
 +    }
 +}
 +
 +void
 +format_xflow_flow(struct ds *ds, const struct xflow_flow *f)
 +{
 +    format_xflow_key(ds, &f->key);
 +    ds_put_cstr(ds, ", ");
 +    format_xflow_flow_stats(ds, &f->stats);
 +    ds_put_cstr(ds, ", actions:");
 +    format_xflow_actions(ds, f->actions, f->n_actions);
 +}
 +\f
 +void
 +xflow_key_from_flow(struct xflow_key *key, const struct flow *flow)
 +{
 +    key->tun_id = flow->tun_id;
 +    key->nw_src = flow->nw_src;
 +    key->nw_dst = flow->nw_dst;
 +    key->in_port = ofp_port_to_xflow_port(flow->in_port);
 +    if (flow->dl_vlan == htons(OFP_VLAN_NONE)) {
 +        key->dl_tci = htons(0);
 +    } else {
 +        uint16_t vid = flow->dl_vlan & htons(VLAN_VID_MASK);
 +        uint16_t pcp = htons((flow->dl_vlan_pcp << VLAN_PCP_SHIFT)
 +                             & VLAN_PCP_MASK);
 +        key->dl_tci = vid | pcp | htons(XFLOW_TCI_PRESENT);
 +    }
 +    key->dl_type = flow->dl_type;
 +    key->tp_src = flow->tp_src;
 +    key->tp_dst = flow->tp_dst;
 +    memcpy(key->dl_src, flow->dl_src, ETH_ADDR_LEN);
 +    memcpy(key->dl_dst, flow->dl_dst, ETH_ADDR_LEN);
 +    key->nw_proto = flow->nw_proto;
 +    key->nw_tos = flow->nw_tos;
 +}
 +
 +void
 +xflow_key_to_flow(const struct xflow_key *key, struct flow *flow)
 +{
 +    flow->wildcards = 0;
 +    flow->priority = 0xffff;
 +    flow->tun_id = key->tun_id;
 +    flow->nw_src = key->nw_src;
 +    flow->nw_dst = key->nw_dst;
 +    flow->in_port = xflow_port_to_ofp_port(key->in_port);
 +    if (key->dl_tci) {
 +        flow->dl_vlan = htons(vlan_tci_to_vid(key->dl_tci));
 +        flow->dl_vlan_pcp = vlan_tci_to_pcp(key->dl_tci);
 +    } else {
 +        flow->dl_vlan = htons(OFP_VLAN_NONE);
 +        flow->dl_vlan_pcp = 0;
 +    }
 +    flow->dl_type = key->dl_type;
 +    flow->tp_src = key->tp_src;
 +    flow->tp_dst = key->tp_dst;
 +    memcpy(flow->dl_src, key->dl_src, ETH_ADDR_LEN);
 +    memcpy(flow->dl_dst, key->dl_dst, ETH_ADDR_LEN);
 +    flow->nw_proto = key->nw_proto;
 +    flow->nw_tos = key->nw_tos;
 +}
diff --combined ofproto/ofproto-sflow.c
  #include <inttypes.h>
  #include <stdlib.h>
  #include "collectors.h"
 -#include "dpif.h"
  #include "compiler.h"
+ #include "hash.h"
+ #include "hmap.h"
  #include "netdev.h"
  #include "ofpbuf.h"
  #include "ofproto.h"
 +#include "packets.h"
  #include "poll-loop.h"
- #include "port-array.h"
  #include "sflow_api.h"
  #include "socket-util.h"
  #include "timeval.h"
  #include "vlog.h"
 +#include "wdp.h"
 +#include "xfif.h"
  
  VLOG_DEFINE_THIS_MODULE(sflow)
  
  struct ofproto_sflow_port {
+     struct hmap_node hmap_node; /* In struct ofproto_sflow's "ports" hmap. */
      struct netdev *netdev;      /* Underlying network device, for stats. */
      SFLDataSource_instance dsi; /* sFlow library's notion of port number. */
 -    uint16_t odp_port;          /* ODP port number. */
++    uint16_t xflow_port;        /* xflow port number. */
  };
  
  struct ofproto_sflow {
      struct collectors *collectors;
      SFLAgent *sflow_agent;
      struct ofproto_sflow_options *options;
 -    struct dpif *dpif;
 +    struct wdp *wdp;
      time_t next_tick;
      size_t n_flood, n_all;
-     struct port_array ports;    /* Indexed by XFLOW port number. */
+     struct hmap ports;          /* Contains "struct ofproto_sflow_port"s. */
  };
  
+ static void ofproto_sflow_del_port__(struct ofproto_sflow *,
+                                      struct ofproto_sflow_port *);
  #define RECEIVER_INDEX 1
  
  static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
@@@ -131,6 -135,20 +137,20 @@@ sflow_agent_send_packet_cb(void *os_, S
      collectors_send(os->collectors, pkt, pktLen);
  }
  
 -ofproto_sflow_find_port(const struct ofproto_sflow *os, uint16_t odp_port)
+ static struct ofproto_sflow_port *
 -                             hash_int(odp_port, 0), &os->ports) {
 -        if (osp->odp_port == odp_port) {
++ofproto_sflow_find_port(const struct ofproto_sflow *os, uint16_t xflow_port)
+ {
+     struct ofproto_sflow_port *osp;
+     HMAP_FOR_EACH_IN_BUCKET (osp, hmap_node,
++                             hash_int(xflow_port, 0), &os->ports) {
++        if (osp->xflow_port == xflow_port) {
+             return osp;
+         }
+     }
+     return NULL;
+ }
  static void
  sflow_agent_get_counters(void *os_, SFLPoller *poller,
                           SFL_COUNTERS_SAMPLE_TYPE *cs)
      enum netdev_flags flags;
      uint32_t current;
  
-     osp = port_array_get(&os->ports, poller->bridgePort);
+     osp = ofproto_sflow_find_port(os, poller->bridgePort);
      if (!osp) {
          return;
      }
@@@ -251,7 -269,7 +271,7 @@@ ofproto_sflow_clear(struct ofproto_sflo
      os->options = NULL;
  
      /* Turn off sampling to save CPU cycles. */
 -    dpif_set_sflow_probability(os->dpif, 0);
 +    wdp_set_sflow_probability(os->wdp, 0);
  }
  
  bool
@@@ -261,14 -279,14 +281,14 @@@ ofproto_sflow_is_enabled(const struct o
  }
  
  struct ofproto_sflow *
 -ofproto_sflow_create(struct dpif *dpif)
 +ofproto_sflow_create(struct wdp *wdp)
  {
      struct ofproto_sflow *os;
  
      os = xcalloc(1, sizeof *os);
 -    os->dpif = dpif;
 +    os->wdp = wdp;
      os->next_tick = time_now() + 1;
-     port_array_init(&os->ports);
+     hmap_init(&os->ports);
      return os;
  }
  
@@@ -276,27 -294,26 +296,26 @@@ voi
  ofproto_sflow_destroy(struct ofproto_sflow *os)
  {
      if (os) {
-         struct ofproto_sflow_port *osp;
-         unsigned int xflow_port;
+         struct ofproto_sflow_port *osp, *next;
  
          ofproto_sflow_clear(os);
-         PORT_ARRAY_FOR_EACH (osp, &os->ports, xflow_port) {
-             ofproto_sflow_del_port(os, xflow_port);
+         HMAP_FOR_EACH_SAFE (osp, next, hmap_node, &os->ports) {
+             ofproto_sflow_del_port__(os, osp);
          }
-         port_array_destroy(&os->ports);
+         hmap_destroy(&os->ports);
          free(os);
      }
  }
  
  static void
  ofproto_sflow_add_poller(struct ofproto_sflow *os,
 -                         struct ofproto_sflow_port *osp, uint16_t odp_port)
 +                         struct ofproto_sflow_port *osp, uint16_t xflow_port)
  {
      SFLPoller *poller = sfl_agent_addPoller(os->sflow_agent, &osp->dsi, os,
                                              sflow_agent_get_counters);
      sfl_poller_set_sFlowCpInterval(poller, os->options->polling_interval);
      sfl_poller_set_sFlowCpReceiver(poller, RECEIVER_INDEX);
 -    sfl_poller_set_bridgePort(poller, odp_port);
 +    sfl_poller_set_bridgePort(poller, xflow_port);
  }
  
  static void
@@@ -310,7 -327,7 +329,7 @@@ ofproto_sflow_add_sampler(struct ofprot
  }
  
  void
 -ofproto_sflow_add_port(struct ofproto_sflow *os, uint16_t odp_port,
 +ofproto_sflow_add_port(struct ofproto_sflow *os, uint16_t xflow_port,
                         const char *netdev_name)
  {
      struct ofproto_sflow_port *osp;
      uint32_t ifindex;
      int error;
  
 -    ofproto_sflow_del_port(os, odp_port);
 +    ofproto_sflow_del_port(os, xflow_port);
  
      /* Open network device. */
      error = netdev_open_default(netdev_name, &netdev);
      osp->netdev = netdev;
      ifindex = netdev_get_ifindex(netdev);
      if (ifindex <= 0) {
 -        ifindex = (os->sflow_agent->subId << 16) + odp_port;
 +        ifindex = (os->sflow_agent->subId << 16) + xflow_port;
      }
      SFL_DS_SET(osp->dsi, 0, ifindex, 0);
-     port_array_set(&os->ports, xflow_port, osp);
 -    osp->odp_port = odp_port;
 -    hmap_insert(&os->ports, &osp->hmap_node, hash_int(odp_port, 0));
++    osp->xflow_port = xflow_port;
++    hmap_insert(&os->ports, &osp->hmap_node, hash_int(xflow_port, 0));
  
      /* Add poller and sampler. */
      if (os->sflow_agent) {
 -        ofproto_sflow_add_poller(os, osp, odp_port);
 +        ofproto_sflow_add_poller(os, osp, xflow_port);
          ofproto_sflow_add_sampler(os, osp);
      }
  }
  
+ static void
+ ofproto_sflow_del_port__(struct ofproto_sflow *os,
+                          struct ofproto_sflow_port *osp)
+ {
+     if (os->sflow_agent) {
+         sfl_agent_removePoller(os->sflow_agent, &osp->dsi);
+         sfl_agent_removeSampler(os->sflow_agent, &osp->dsi);
+     }
+     netdev_close(osp->netdev);
+     hmap_remove(&os->ports, &osp->hmap_node);
+     free(osp);
+ }
  void
 -ofproto_sflow_del_port(struct ofproto_sflow *os, uint16_t odp_port)
 +ofproto_sflow_del_port(struct ofproto_sflow *os, uint16_t xflow_port)
  {
-     struct ofproto_sflow_port *osp = port_array_get(&os->ports, xflow_port);
 -    struct ofproto_sflow_port *osp = ofproto_sflow_find_port(os, odp_port);
++    struct ofproto_sflow_port *osp = ofproto_sflow_find_port(os, xflow_port);
      if (osp) {
-         if (os->sflow_agent) {
-             sfl_agent_removePoller(os->sflow_agent, &osp->dsi);
-             sfl_agent_removeSampler(os->sflow_agent, &osp->dsi);
-         }
-         netdev_close(osp->netdev);
-         free(osp);
-         port_array_delete(&os->ports, xflow_port);
+         ofproto_sflow_del_port__(os, osp);
      }
  }
  
@@@ -367,7 -392,6 +394,6 @@@ ofproto_sflow_set_options(struct ofprot
      struct ofproto_sflow_port *osp;
      bool options_changed;
      SFLReceiver *receiver;
-     unsigned int xflow_port;
      SFLAddress agentIP;
      time_t now;
  
      sfl_receiver_set_sFlowRcvrTimeout(receiver, 0xffffffff);
  
      /* Set the sampling_rate down in the datapath. */
 -    dpif_set_sflow_probability(os->dpif,
 -                               MAX(1, UINT32_MAX / options->sampling_rate));
 +    wdp_set_sflow_probability(os->wdp,
 +                              MAX(1, UINT32_MAX / options->sampling_rate));
  
      /* Add samplers and pollers for the currently known ports. */
-     PORT_ARRAY_FOR_EACH (osp, &os->ports, xflow_port) {
-         ofproto_sflow_add_poller(os, osp, xflow_port);
+     HMAP_FOR_EACH (osp, hmap_node, &os->ports) {
 -        ofproto_sflow_add_poller(os, osp, osp->odp_port);
++        ofproto_sflow_add_poller(os, osp, osp->xflow_port);
          ofproto_sflow_add_sampler(os, osp);
      }
  }
  
  static int
 -ofproto_sflow_odp_port_to_ifindex(const struct ofproto_sflow *os,
 -                                  uint16_t odp_port)
 +ofproto_sflow_xflow_port_to_ifindex(const struct ofproto_sflow *os,
-                                   uint16_t xflow_port)
++                                    uint16_t xflow_port)
  {
-     struct ofproto_sflow_port *osp = port_array_get(&os->ports, xflow_port);
 -    struct ofproto_sflow_port *osp = ofproto_sflow_find_port(os, odp_port);
++    struct ofproto_sflow_port *osp = ofproto_sflow_find_port(os, xflow_port);
      return osp ? SFL_DS_INDEX(osp->dsi) : 0;
  }
  
  void
 -ofproto_sflow_received(struct ofproto_sflow *os, struct odp_msg *msg)
 +ofproto_sflow_received(struct ofproto_sflow *os, struct xflow_msg *msg)
  {
      SFL_FLOW_SAMPLE_TYPE fs;
      SFLFlow_sample_element hdrElem;
      SFLSampled_header *header;
      SFLFlow_sample_element switchElem;
      SFLSampler *sampler;
 -    const struct odp_sflow_sample_header *hdr;
 -    const union odp_action *actions;
 +    const struct xflow_sflow_sample_header *hdr;
 +    const union xflow_action *actions;
      struct ofpbuf payload;
      size_t n_actions, n_outputs;
      size_t min_size;
      flow_t flow;
      size_t i;
  
 -    /* Get odp_sflow_sample_header. */
 +    /* Get xflow_sflow_sample_header. */
      min_size = sizeof *msg + sizeof *hdr;
      if (min_size > msg->length) {
          VLOG_WARN_RL(&rl, "sFlow packet too small (%"PRIu32" < %zu)",
                       msg->length, min_size);
          return;
      }
 -    hdr = (const struct odp_sflow_sample_header *) (msg + 1);
 +    hdr = (const struct xflow_sflow_sample_header *) (msg + 1);
  
      /* Get actions. */
      n_actions = hdr->n_actions;
                       n_actions, msg->length, min_size);
          return;
      }
 -    actions = (const union odp_action *) (hdr + 1);
 +    actions = (const union xflow_action *) (hdr + 1);
  
      /* Get packet payload and extract flow. */
 -    payload.data = (union odp_action *) (actions + n_actions);
 +    payload.data = (union xflow_action *) (actions + n_actions);
      payload.size = msg->length - min_size;
      flow_extract(&payload, 0, msg->port, &flow);
  
      /* Build a flow sample */
      memset(&fs, 0, sizeof fs);
 -    fs.input = ofproto_sflow_odp_port_to_ifindex(os, msg->port);
 +    fs.input = ofproto_sflow_xflow_port_to_ifindex(os, msg->port);
      fs.output = 0;              /* Filled in correctly below. */
      fs.sample_pool = hdr->sample_pool;
  
      /* Figure out the output ports. */
      n_outputs = 0;
      for (i = 0; i < n_actions; i++) {
 -        const union odp_action *a = &actions[i];
 +        const union xflow_action *a = &actions[i];
  
          switch (a->type) {
 -        case ODPAT_OUTPUT:
 -            fs.output = ofproto_sflow_odp_port_to_ifindex(os, a->output.port);
 +        case XFLOWAT_OUTPUT:
 +            fs.output = ofproto_sflow_xflow_port_to_ifindex(os, a->output.port);
              n_outputs++;
              break;
  
 -        case ODPAT_OUTPUT_GROUP:
 +        case XFLOWAT_OUTPUT_GROUP:
 +#if 0
              n_outputs += (a->output_group.group == DP_GROUP_FLOOD ? os->n_flood
                            : a->output_group.group == DP_GROUP_ALL ? os->n_all
                            : 0);
 +#endif
              break;
  
 -        case ODPAT_SET_VLAN_VID:
 -            switchElem.flowType.sw.dst_vlan = ntohs(a->vlan_vid.vlan_vid);
 -            break;
 -
 -        case ODPAT_SET_VLAN_PCP:
 -            switchElem.flowType.sw.dst_priority = a->vlan_pcp.vlan_pcp;
 +        case XFLOWAT_SET_DL_TCI:
 +            if (a->dl_tci.mask & htons(VLAN_VID_MASK)) {
 +                switchElem.flowType.sw.dst_vlan = vlan_tci_to_vid(a->dl_tci.tci);
 +            }
 +            if (a->dl_tci.mask & htons(VLAN_PCP_MASK)) {
 +                switchElem.flowType.sw.dst_priority = vlan_tci_to_pcp(a->dl_tci.tci);
 +            }
              break;
  
          default:
diff --combined ofproto/ofproto.c
  #include "classifier.h"
  #include "coverage.h"
  #include "discovery.h"
 -#include "dpif.h"
  #include "dynamic-string.h"
  #include "fail-open.h"
+ #include "hash.h"
+ #include "hmap.h"
  #include "in-band.h"
  #include "mac-learning.h"
  #include "netdev.h"
  #include "netflow.h"
 -#include "odp-util.h"
  #include "ofp-print.h"
  #include "ofp-util.h"
  #include "ofproto-sflow.h"
  #include "ofpbuf.h"
  #include "openflow/nicira-ext.h"
  #include "openflow/openflow.h"
 -#include "openvswitch/datapath-protocol.h"
 +#include "openvswitch/xflow.h"
  #include "packets.h"
  #include "pinsched.h"
  #include "pktbuf.h"
  #include "poll-loop.h"
- #include "port-array.h"
  #include "rconn.h"
  #include "shash.h"
  #include "status.h"
  #include "unixctl.h"
  #include "vconn.h"
  #include "vlog.h"
 +#include "wdp.h"
 +#include "xfif.h"
  #include "xtoxll.h"
  
  VLOG_DEFINE_THIS_MODULE(ofproto)
  
  #include "sflow_api.h"
  
 -enum {
 -    TABLEID_HASH = 0,
 -    TABLEID_CLASSIFIER = 1
 -};
 -
 -
 -struct ofport {
 -    struct hmap_node hmap_node; /* In struct ofproto's "ports" hmap. */
 -    struct netdev *netdev;
 -    struct ofp_phy_port opp;    /* In host byte order. */
 -    uint16_t odp_port;
 -};
 -
 -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 flow_t *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);
 -
 -struct rule {
 -    struct cls_rule cr;
 -
 +struct ofproto_rule {
      uint64_t flow_cookie;       /* Controller-issued identifier.
                                     (Kept in network-byte order.) */
 -    uint16_t idle_timeout;      /* In seconds from time of last use. */
 -    uint16_t hard_timeout;      /* In seconds from time of creation. */
      bool send_flow_removed;     /* Send a flow removed message? */
 -    long long int used;         /* Last-used time (0 if never used). */
 -    long long int created;      /* Creation time. */
 -    uint64_t packet_count;      /* Number of packets received. */
 -    uint64_t byte_count;        /* Number of bytes received. */
 -    uint64_t accounted_bytes;   /* Number of bytes passed to account_cb. */
      tag_type tags;              /* Tags (set only by hooks). */
 -    struct netflow_flow nf_flow; /* Per-flow NetFlow tracking data. */
 -
 -    /* If 'super' is non-NULL, this rule is a subrule, that is, it is an
 -     * exact-match rule (having cr.wc.wildcards of 0) generated from the
 -     * wildcard rule 'super'.  In this case, 'list' is an element of the
 -     * super-rule's list.
 -     *
 -     * If 'super' is NULL, this rule is a super-rule, and 'list' is the head of
 -     * a list of subrules.  A super-rule with no wildcards (where
 -     * cr.wc.wildcards is 0) will never have any subrules. */
 -    struct rule *super;
 -    struct list list;
 +};
  
 -    /* OpenFlow actions.
 -     *
 -     * 'n_actions' is the number of elements in the 'actions' array.  A single
 -     * action may take up more more than one element's worth of space.
 -     *
 -     * A subrule has no actions (it uses the super-rule's actions). */
 -    int n_actions;
 -    union ofp_action *actions;
 +static struct ofproto_rule *
 +ofproto_rule_cast(const struct wdp_rule *wdp_rule)
 +{
 +    return wdp_rule->client_data;
 +}
  
 -    /* Datapath actions.
 -     *
 -     * A super-rule with wildcard fields never has ODP actions (since the
 -     * datapath only supports exact-match flows). */
 -    bool installed;             /* Installed in datapath? */
 -    bool may_install;           /* True ordinarily; false if actions must
 -                                 * be reassessed for every packet. */
 -    int n_odp_actions;
 -    union odp_action *odp_actions;
 -};
 +static void
 +ofproto_rule_init(struct wdp_rule *wdp_rule)
 +{
 +    wdp_rule->client_data = xzalloc(sizeof(struct ofproto_rule));
 +}
  
  static inline bool
 -rule_is_hidden(const struct rule *rule)
 +rule_is_hidden(const struct wdp_rule *rule)
  {
 -    /* Subrules are merely an implementation detail, so hide them from the
 -     * controller. */
 -    if (rule->super != NULL) {
 -        return true;
 -    }
 -
      /* Rules with priority higher than UINT16_MAX are set up by ofproto itself
       * (e.g. by in-band control) and are intentionally hidden from the
       * controller. */
 -    if (rule->cr.priority > UINT16_MAX) {
 +    if (rule->cr.flow.priority > UINT16_MAX) {
          return true;
      }
  
      return false;
  }
  
 -static struct rule *rule_create(struct ofproto *, struct rule *super,
 -                                const union ofp_action *, size_t n_actions,
 -                                uint16_t idle_timeout, uint16_t hard_timeout,
 -                                uint64_t flow_cookie, bool send_flow_removed);
 -static void rule_free(struct rule *);
 -static void rule_destroy(struct ofproto *, struct rule *);
 -static struct rule *rule_from_cls_rule(const struct cls_rule *);
 -static void rule_insert(struct ofproto *, struct rule *,
 -                        struct ofpbuf *packet, uint16_t in_port);
 -static void rule_remove(struct ofproto *, struct rule *);
 -static bool rule_make_actions(struct ofproto *, struct rule *,
 -                              const struct ofpbuf *packet);
 -static void rule_install(struct ofproto *, struct rule *,
 -                         struct rule *displaced_rule);
 -static void rule_uninstall(struct ofproto *, struct rule *);
 -static void rule_post_uninstall(struct ofproto *, struct rule *);
 -static void send_flow_removed(struct ofproto *p, struct rule *rule,
 -                              long long int now, uint8_t reason);
 +static int delete_flow(struct ofproto *, struct wdp_rule *, uint8_t reason);
  
  /* ofproto supports two kinds of OpenFlow connections:
   *
@@@ -143,7 -217,6 +144,7 @@@ struct ofconn 
      struct list node;           /* In struct ofproto's "all_conns" list. */
      struct rconn *rconn;        /* OpenFlow connection. */
      enum ofconn_type type;      /* Type. */
 +    bool flow_mod_table_id;     /* NXT_FLOW_MOD_TABLE_ID enabled? */
  
      /* OFPT_PACKET_IN related data. */
      struct rconn_packet_counter *packet_in_counter; /* # queued on 'rconn'. */
  
  /* We use OFPR_NO_MATCH and OFPR_ACTION as indexes into struct ofconn's
   * "schedulers" array.  Their values are 0 and 1, and their meanings and values
 - * coincide with _ODPL_MISS_NR and _ODPL_ACTION_NR, so this is convenient.  In
 + * coincide with WDP_CHAN_MISS and WDP_CHAN_ACTION, so this is convenient.  In
   * case anything ever changes, check their values here.  */
  #define N_SCHEDULERS 2
  BUILD_ASSERT_DECL(OFPR_NO_MATCH == 0);
 -BUILD_ASSERT_DECL(OFPR_NO_MATCH == _ODPL_MISS_NR);
 +BUILD_ASSERT_DECL(OFPR_NO_MATCH == WDP_CHAN_MISS);
  BUILD_ASSERT_DECL(OFPR_ACTION == 1);
 -BUILD_ASSERT_DECL(OFPR_ACTION == _ODPL_ACTION_NR);
 +BUILD_ASSERT_DECL(OFPR_ACTION == WDP_CHAN_ACTION);
  
  static struct ofconn *ofconn_create(struct ofproto *, struct rconn *,
                                      enum ofconn_type);
@@@ -187,8 -260,8 +188,8 @@@ static void ofconn_set_rate_limit(struc
  static void queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn,
                       struct rconn_packet_counter *counter);
  
 -static void send_packet_in(struct ofproto *, struct ofpbuf *odp_msg);
 -static void do_send_packet_in(struct ofpbuf *odp_msg, void *ofconn);
 +static void send_packet_in(struct ofproto *, struct wdp_packet *);
 +static void do_send_packet_in(struct wdp_packet *, void *ofconn);
  
  struct ofproto {
      /* Settings. */
      char *dp_desc;              /* Datapath description. */
  
      /* Datapath. */
 -    struct dpif *dpif;
 -    struct netdev_monitor *netdev_monitor;
 -    struct hmap ports;          /* Contains "struct ofport"s. */
 -    struct shash port_by_name;
 +    struct wdp *wdp;
      uint32_t max_ports;
  
      /* Configuration. */
      struct fail_open *fail_open;
      struct netflow *netflow;
      struct ofproto_sflow *sflow;
 +    bool tun_id_from_cookie;    /* NXT_TUN_ID_FROM_COOKIE enabled? */
  
      /* In-band control. */
      struct in_band *in_band;
      struct sockaddr_in *extra_in_band_remotes;
      size_t n_extra_remotes;
  
 -    /* Flow table. */
 -    struct classifier cls;
 -    bool need_revalidate;
 -    long long int next_expiration;
 -    struct tag_set revalidate_set;
 -    bool tun_id_from_cookie;
 -
      /* OpenFlow connections. */
      struct hmap controllers;   /* Controller "struct ofconn"s. */
      struct list all_conns;     /* Contains "struct ofconn"s. */
      struct hmap services;       /* Contains "struct ofservice"s. */
      struct pvconn **snoops;
      size_t n_snoops;
 -
 -    /* Hooks for ovs-vswitchd. */
 -    const struct ofhooks *ofhooks;
 -    void *aux;
 -
 -    /* Used by default ofhooks. */
 -    struct mac_learning *ml;
  };
  
  static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
  
 -static const struct ofhooks default_ofhooks;
 -
  static uint64_t pick_datapath_id(const struct ofproto *);
  static uint64_t pick_fallback_dpid(void);
  
 -static void update_used(struct ofproto *);
 -static void update_stats(struct ofproto *, struct rule *,
 -                         const struct odp_flow_stats *);
 -static void expire_rule(struct cls_rule *, void *ofproto);
 -static void active_timeout(struct ofproto *ofproto, struct rule *rule);
 -static bool revalidate_rule(struct ofproto *p, struct rule *rule);
 -static void revalidate_cb(struct cls_rule *rule_, void *p_);
 -
 -static void handle_odp_msg(struct ofproto *, struct ofpbuf *);
 +static void handle_wdp_packet(struct ofproto *, struct wdp_packet *);
  
  static void handle_openflow(struct ofconn *, struct ofproto *,
                              struct ofpbuf *);
  
 -static void refresh_port_groups(struct ofproto *);
 -
 -static struct ofport *get_port(const struct ofproto *, uint16_t odp_port);
 -static void update_port(struct ofproto *, const char *devname);
 -static int init_ports(struct ofproto *);
 -static void reinit_ports(struct ofproto *);
 -
  int
  ofproto_create(const char *datapath, const char *datapath_type,
                 const struct ofhooks *ofhooks, void *aux,
                 struct ofproto **ofprotop)
  {
 -    struct odp_stats stats;
 +    struct wdp_stats stats;
      struct ofproto *p;
 -    struct dpif *dpif;
 +    struct wdp *wdp;
      int error;
  
      *ofprotop = NULL;
  
      /* Connect to datapath and start listening for messages. */
 -    error = dpif_open(datapath, datapath_type, &dpif);
 +    error = wdp_open(datapath, datapath_type, &wdp);
      if (error) {
          VLOG_ERR("failed to open datapath %s: %s", datapath, strerror(error));
          return error;
      }
 -    error = dpif_get_dp_stats(dpif, &stats);
 +    error = wdp_get_wdp_stats(wdp, &stats);
      if (error) {
          VLOG_ERR("failed to obtain stats for datapath %s: %s",
                   datapath, strerror(error));
 -        dpif_close(dpif);
 +        wdp_close(wdp);
          return error;
      }
 -    error = dpif_recv_set_mask(dpif, ODPL_MISS | ODPL_ACTION | ODPL_SFLOW);
 +    error = wdp_recv_set_mask(wdp, ((1 << WDP_CHAN_MISS)
 +                                    | (1 << WDP_CHAN_ACTION)
 +                                    | (1 << WDP_CHAN_SFLOW)));
      if (error) {
          VLOG_ERR("failed to listen on datapath %s: %s",
                   datapath, strerror(error));
 -        dpif_close(dpif);
 +        wdp_close(wdp);
          return error;
      }
 -    dpif_flow_flush(dpif);
 -    dpif_recv_purge(dpif);
 +    wdp_flow_flush(wdp);
 +    wdp_recv_purge(wdp);
 +    wdp_set_ofhooks(wdp, ofhooks, aux);
  
      /* Initialize settings. */
      p = xzalloc(sizeof *p);
      p->dp_desc = xstrdup(DEFAULT_DP_DESC);
  
      /* Initialize datapath. */
 -    p->dpif = dpif;
 -    p->netdev_monitor = netdev_monitor_create();
 -    hmap_init(&p->ports);
 -    shash_init(&p->port_by_name);
 +    p->wdp = wdp;
      p->max_ports = stats.max_ports;
  
      /* Initialize submodules. */
      p->netflow = NULL;
      p->sflow = NULL;
  
 -    /* Initialize flow table. */
 -    classifier_init(&p->cls);
 -    p->need_revalidate = false;
 -    p->next_expiration = time_msec() + 1000;
 -    tag_set_init(&p->revalidate_set);
 -
      /* Initialize OpenFlow connections. */
      list_init(&p->all_conns);
      hmap_init(&p->controllers);
      p->snoops = NULL;
      p->n_snoops = 0;
  
 -    /* Initialize hooks. */
 -    if (ofhooks) {
 -        p->ofhooks = ofhooks;
 -        p->aux = aux;
 -        p->ml = NULL;
 -    } else {
 -        p->ofhooks = &default_ofhooks;
 -        p->aux = p;
 -        p->ml = mac_learning_create();
 -    }
 -
      /* Pick final datapath ID. */
      p->datapath_id = pick_datapath_id(p);
      VLOG_INFO("using datapath ID %016"PRIx64, p->datapath_id);
@@@ -349,7 -472,7 +350,7 @@@ add_controller(struct ofproto *ofproto
  
      if (is_discovery_controller(c)) {
          int error = discovery_create(c->accept_re, c->update_resolv_conf,
 -                                     ofproto->dpif, ofproto->switch_status,
 +                                     ofproto->wdp, ofproto->switch_status,
                                       &discovery);
          if (error) {
              return;
@@@ -409,7 -532,7 +410,7 @@@ find_controller_by_target(struct ofprot
  {
      struct ofconn *ofconn;
  
-     HMAP_FOR_EACH_WITH_HASH (ofconn, struct ofconn, hmap_node,
+     HMAP_FOR_EACH_WITH_HASH (ofconn, hmap_node,
                               hash_string(target, 0), &ofproto->controllers) {
          if (!strcmp(ofconn_get_target(ofconn), target)) {
              return ofconn;
@@@ -434,7 -557,7 +435,7 @@@ update_in_band_remotes(struct ofproto *
  
      /* Add all the remotes. */
      discovery = false;
-     HMAP_FOR_EACH (ofconn, struct ofconn, hmap_node, &ofproto->controllers) {
+     HMAP_FOR_EACH (ofconn, hmap_node, &ofproto->controllers) {
          struct sockaddr_in *sin = &addrs[n_addrs];
  
          if (ofconn->band == OFPROTO_OUT_OF_BAND) {
       * even before we know any remote addresses. */
      if (n_addrs || discovery) {
          if (!ofproto->in_band) {
 -            in_band_create(ofproto, ofproto->dpif, ofproto->switch_status,
 +            in_band_create(ofproto, ofproto->wdp, ofproto->switch_status,
                             &ofproto->in_band);
          }
          if (ofproto->in_band) {
@@@ -493,7 -616,7 +494,7 @@@ update_fail_open(struct ofproto *p
  
          n = 0;
          rconns = xmalloc(hmap_count(&p->controllers) * sizeof *rconns);
-         HMAP_FOR_EACH (ofconn, struct ofconn, hmap_node, &p->controllers) {
+         HMAP_FOR_EACH (ofconn, hmap_node, &p->controllers) {
              rconns[n++] = ofconn->rconn;
          }
  
@@@ -532,7 -655,7 +533,7 @@@ ofproto_set_controllers(struct ofproto 
              }
          } else {
              VLOG_WARN_RL(&rl, "%s: unsupported controller \"%s\"",
 -                         dpif_name(p->dpif), c->target);
 +                         wdp_name(p->wdp), c->target);
              continue;
          }
  
      /* Delete controllers that are no longer configured.
       * Update configuration of all now-existing controllers. */
      ss_exists = false;
-     HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, hmap_node,
-                         &p->controllers) {
+     HMAP_FOR_EACH_SAFE (ofconn, next_ofconn, hmap_node, &p->controllers) {
          struct ofproto_controller *c;
  
          c = shash_find_data(&new_controllers, ofconn_get_target(ofconn));
  
      /* Delete services that are no longer configured.
       * Update configuration of all now-existing services. */
-     HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, struct ofservice, node,
-                         &p->services) {
+     HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &p->services) {
          struct ofproto_controller *c;
  
          c = shash_find_data(&new_controllers,
@@@ -599,7 -720,7 +598,7 @@@ ofproto_reconnect_controllers(struct of
  {
      struct ofconn *ofconn;
  
-     LIST_FOR_EACH (ofconn, struct ofconn, node, &ofproto->all_conns) {
+     LIST_FOR_EACH (ofconn, node, &ofproto->all_conns) {
          rconn_reconnect(ofconn->rconn);
      }
  }
@@@ -763,8 -884,14 +762,8 @@@ ofproto_set_sflow(struct ofproto *ofpro
      struct ofproto_sflow *os = ofproto->sflow;
      if (oso) {
          if (!os) {
 -            struct ofport *ofport;
 -
 -            os = ofproto->sflow = ofproto_sflow_create(ofproto->dpif);
 -            refresh_port_groups(ofproto);
 -            HMAP_FOR_EACH (ofport, hmap_node, &ofproto->ports) {
 -                ofproto_sflow_add_port(os, ofport->odp_port,
 -                                       netdev_get_name(ofport->netdev));
 -            }
 +            os = ofproto->sflow = ofproto_sflow_create(ofproto->wdp);
 +            /* XXX ofport */
          }
          ofproto_sflow_set_options(os, oso);
      } else {
@@@ -806,6 -933,7 +805,6 @@@ ofproto_destroy(struct ofproto *p
  {
      struct ofservice *ofservice, *next_ofservice;
      struct ofconn *ofconn, *next_ofconn;
 -    struct ofport *ofport, *next_ofport;
      size_t i;
  
      if (!p) {
      free(p->extra_in_band_remotes);
  
      ofproto_flush_flows(p);
 -    classifier_destroy(&p->cls);
  
-     LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node,
-                         &p->all_conns) {
+     LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &p->all_conns) {
          ofconn_destroy(ofconn);
      }
      hmap_destroy(&p->controllers);
  
 -    dpif_close(p->dpif);
 -    netdev_monitor_destroy(p->netdev_monitor);
 -    HMAP_FOR_EACH_SAFE (ofport, next_ofport, hmap_node, &p->ports) {
 -        hmap_remove(&p->ports, &ofport->hmap_node);
 -        ofport_free(ofport);
 -    }
 -    shash_destroy(&p->port_by_name);
 +    wdp_close(p->wdp);
  
      switch_status_destroy(p->switch_status);
      netflow_destroy(p->netflow);
      ofproto_sflow_destroy(p->sflow);
  
-     HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, struct ofservice, node,
-                         &p->services) {
+     HMAP_FOR_EACH_SAFE (ofservice, next_ofservice, node, &p->services) {
          ofservice_destroy(p, ofservice);
      }
      hmap_destroy(&p->services);
      }
      free(p->snoops);
  
 -    mac_learning_destroy(p->ml);
 -
      free(p->mfr_desc);
      free(p->hw_desc);
      free(p->sw_desc);
      free(p->serial_desc);
      free(p->dp_desc);
  
 -    hmap_destroy(&p->ports);
 -
      free(p);
  }
  
@@@ -864,6 -1001,17 +861,6 @@@ ofproto_run(struct ofproto *p
      return error;
  }
  
 -static void
 -process_port_change(struct ofproto *ofproto, int error, char *devname)
 -{
 -    if (error == ENOBUFS) {
 -        reinit_ports(ofproto);
 -    } else if (!error) {
 -        update_port(ofproto, devname);
 -        free(devname);
 -    }
 -}
 -
  /* Returns a "preference level" for snooping 'ofconn'.  A higher return value
   * means that 'ofconn' is more interesting for monitoring than a lower return
   * value. */
@@@ -892,7 -1040,7 +889,7 @@@ add_snooper(struct ofproto *ofproto, st
  
      /* Pick a controller for monitoring. */
      best = NULL;
-     LIST_FOR_EACH (ofconn, struct ofconn, node, &ofproto->all_conns) {
+     LIST_FOR_EACH (ofconn, node, &ofproto->all_conns) {
          if (ofconn->type == OFCONN_PRIMARY
              && (!best || snoop_preference(ofconn) > snoop_preference(best))) {
              best = ofconn;
      }
  }
  
-     LIST_FOR_EACH (ofconn, struct ofconn, node, &ofproto->all_conns) {
 +static void
 +ofproto_port_poll_cb(const struct ofp_phy_port *opp, uint8_t reason,
 +                     void *ofproto_)
 +{
 +    /* XXX Should limit the number of queued port status change messages. */
 +    struct ofproto *ofproto = ofproto_;
 +    struct ofconn *ofconn;
 +
++    LIST_FOR_EACH (ofconn, node, &ofproto->all_conns) {
 +        struct ofp_port_status *ops;
 +        struct ofpbuf *b;
 +
 +        if (!ofconn_receives_async_msgs(ofconn)) {
 +            continue;
 +        }
 +
 +        ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &b);
 +        ops->reason = reason;
 +        ops->desc = *opp;
 +        hton_ofp_phy_port(&ops->desc);
 +        queue_tx(b, ofconn, NULL);
 +    }
 +}
 +
  int
  ofproto_run1(struct ofproto *p)
  {
      struct ofconn *ofconn, *next_ofconn;
      struct ofservice *ofservice;
 -    char *devname;
 -    int error;
      int i;
  
 -    if (shash_is_empty(&p->port_by_name)) {
 -        init_ports(p);
 -    }
 -
      for (i = 0; i < 50; i++) {
 -        struct ofpbuf *buf;
 +        struct wdp_packet packet;
 +        int error;
  
 -        error = dpif_recv(p->dpif, &buf);
 +        error = wdp_recv(p->wdp, &packet);
          if (error) {
              if (error == ENODEV) {
                  /* Someone destroyed the datapath behind our back.  The caller
                   * spin from here on out. */
                  static struct vlog_rate_limit rl2 = VLOG_RATE_LIMIT_INIT(1, 5);
                  VLOG_ERR_RL(&rl2, "%s: datapath was destroyed externally",
 -                            dpif_name(p->dpif));
 +                            wdp_name(p->wdp));
                  return ENODEV;
              }
              break;
          }
  
 -        handle_odp_msg(p, buf);
 +        handle_wdp_packet(p, xmemdup(&packet, sizeof packet));
      }
  
 -    while ((error = dpif_port_poll(p->dpif, &devname)) != EAGAIN) {
 -        process_port_change(p, error, devname);
 -    }
 -    while ((error = netdev_monitor_poll(p->netdev_monitor,
 -                                        &devname)) != EAGAIN) {
 -        process_port_change(p, error, devname);
 -    }
 +    wdp_port_poll(p->wdp, ofproto_port_poll_cb, p);
  
      if (p->in_band) {
          if (time_msec() >= p->next_in_band_update) {
          in_band_run(p->in_band);
      }
  
-     LIST_FOR_EACH_SAFE (ofconn, next_ofconn, struct ofconn, node,
-                         &p->all_conns) {
+     LIST_FOR_EACH_SAFE (ofconn, next_ofconn, node, &p->all_conns) {
          ofconn_run(ofconn, p);
      }
  
          fail_open_run(p->fail_open);
      }
  
-     HMAP_FOR_EACH (ofservice, struct ofservice, node, &p->services) {
+     HMAP_FOR_EACH (ofservice, node, &p->services) {
          struct vconn *vconn;
          int retval;
  
          retval = pvconn_accept(ofservice->pvconn, OFP_VERSION, &vconn);
          if (!retval) {
-             struct ofconn *ofconn;
              struct rconn *rconn;
              char *name;
  
          }
      }
  
 -    if (time_msec() >= p->next_expiration) {
 -        COVERAGE_INC(ofproto_expiration);
 -        p->next_expiration = time_msec() + 1000;
 -        update_used(p);
 -
 -        classifier_for_each(&p->cls, CLS_INC_ALL, expire_rule, p);
 -
 -        /* Let the hook know that we're at a stable point: all outstanding data
 -         * in existing flows has been accounted to the account_cb.  Thus, the
 -         * hook can now reasonably do operations that depend on having accurate
 -         * flow volume accounting (currently, that's just bond rebalancing). */
 -        if (p->ofhooks->account_checkpoint_cb) {
 -            p->ofhooks->account_checkpoint_cb(p->aux);
 -        }
 -    }
 -
      if (p->netflow) {
          netflow_run(p->netflow);
      }
      return 0;
  }
  
 -struct revalidate_cbdata {
 -    struct ofproto *ofproto;
 -    bool revalidate_all;        /* Revalidate all exact-match rules? */
 -    bool revalidate_subrules;   /* Revalidate all exact-match subrules? */
 -    struct tag_set revalidate_set; /* Set of tags to revalidate. */
 -};
 -
  int
 -ofproto_run2(struct ofproto *p, bool revalidate_all)
 +ofproto_run2(struct ofproto *p OVS_UNUSED, bool revalidate_all OVS_UNUSED)
  {
 -    if (p->need_revalidate || revalidate_all
 -        || !tag_set_is_empty(&p->revalidate_set)) {
 -        struct revalidate_cbdata cbdata;
 -        cbdata.ofproto = p;
 -        cbdata.revalidate_all = revalidate_all;
 -        cbdata.revalidate_subrules = p->need_revalidate;
 -        cbdata.revalidate_set = p->revalidate_set;
 -        tag_set_init(&p->revalidate_set);
 -        COVERAGE_INC(ofproto_revalidate);
 -        classifier_for_each(&p->cls, CLS_INC_EXACT, revalidate_cb, &cbdata);
 -        p->need_revalidate = false;
 -    }
 -
      return 0;
  }
  
@@@ -1037,9 -1206,10 +1032,9 @@@ ofproto_wait(struct ofproto *p
      struct ofconn *ofconn;
      size_t i;
  
 -    dpif_recv_wait(p->dpif);
 -    dpif_port_poll_wait(p->dpif);
 -    netdev_monitor_poll_wait(p->netdev_monitor);
 +    wdp_recv_wait(p->wdp);
 +    wdp_port_poll_wait(p->wdp);
-     LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
+     LIST_FOR_EACH (ofconn, node, &p->all_conns) {
          ofconn_wait(ofconn);
      }
      if (p->in_band) {
      if (p->sflow) {
          ofproto_sflow_wait(p->sflow);
      }
-     HMAP_FOR_EACH (ofservice, struct ofservice, node, &p->services) {
 -    if (!tag_set_is_empty(&p->revalidate_set)) {
 -        poll_immediate_wake();
 -    }
 -    if (p->need_revalidate) {
 -        /* Shouldn't happen, but if it does just go around again. */
 -        VLOG_DBG_RL(&rl, "need revalidate in ofproto_wait_cb()");
 -        poll_immediate_wake();
 -    } else if (p->next_expiration != LLONG_MAX) {
 -        poll_timer_wait_until(p->next_expiration);
 -    }
+     HMAP_FOR_EACH (ofservice, node, &p->services) {
          pvconn_wait(ofservice->pvconn);
      }
      for (i = 0; i < p->n_snoops; i++) {
  void
  ofproto_revalidate(struct ofproto *ofproto, tag_type tag)
  {
 -    tag_set_add(&ofproto->revalidate_set, tag);
 +    wdp_revalidate(ofproto->wdp, tag);
  }
  
 -struct tag_set *
 -ofproto_get_revalidate_set(struct ofproto *ofproto)
 +void
 +ofproto_revalidate_all(struct ofproto *ofproto)
  {
 -    return &ofproto->revalidate_set;
 +    wdp_revalidate_all(ofproto->wdp);
  }
  
  bool
@@@ -1083,53 -1263,71 +1078,53 @@@ ofproto_send_packet(struct ofproto *p, 
                      const union ofp_action *actions, size_t n_actions,
                      const struct ofpbuf *packet)
  {
 -    struct odp_actions odp_actions;
 -    int error;
 -
 -    error = xlate_actions(actions, n_actions, flow, p, packet, &odp_actions,
 -                          NULL, NULL, NULL);
 -    if (error) {
 -        return error;
 -    }
 -
 -    /* XXX Should we translate the dpif_execute() errno value into an OpenFlow
 +    /* XXX Should we translate the wdp_execute() errno value into an OpenFlow
       * error code? */
 -    dpif_execute(p->dpif, flow->in_port, odp_actions.actions,
 -                 odp_actions.n_actions, packet);
 +    wdp_execute(p->wdp, flow->in_port, actions, n_actions, packet);
      return 0;
  }
  
 +/* Intended for used by ofproto clients and ofproto submodules to add flows to
 + * the flow table. */
  void
 -ofproto_add_flow(struct ofproto *p,
 -                 const flow_t *flow, uint32_t wildcards, unsigned int priority,
 +ofproto_add_flow(struct ofproto *p, const flow_t *flow,
                   const union ofp_action *actions, size_t n_actions,
                   int idle_timeout)
  {
 -    struct rule *rule;
 -    rule = rule_create(p, NULL, actions, n_actions,
 -                       idle_timeout >= 0 ? idle_timeout : 5 /* XXX */,
 -                       0, 0, false);
 -    cls_rule_from_flow(flow, wildcards, priority, &rule->cr);
 -    rule_insert(p, rule, NULL, 0);
 -}
 +    struct wdp_flow_put put;
 +    struct wdp_rule *rule;
  
 -void
 -ofproto_delete_flow(struct ofproto *ofproto, const flow_t *flow,
 -                    uint32_t wildcards, unsigned int priority)
 -{
 -    struct rule *rule;
 +    put.flags = WDP_PUT_CREATE | WDP_PUT_MODIFY | WDP_PUT_ALL;
 +    put.flow = flow;
 +    put.actions = actions;
 +    put.n_actions = n_actions;
 +    put.idle_timeout = idle_timeout;
 +    put.hard_timeout = 0;
 +    put.ofp_table_id = 0xff;
 +    put.cookie = htonll(0);
 +    put.xid = htonl(0);
  
 -    rule = rule_from_cls_rule(classifier_find_rule_exactly(&ofproto->cls,
 -                                                           flow, wildcards,
 -                                                           priority));
 -    if (rule) {
 -        rule_remove(ofproto, rule);
 +    if (!wdp_flow_put(p->wdp, &put, NULL, &rule)) {
 +        ofproto_rule_init(rule);
      }
  }
  
 -static void
 -destroy_rule(struct cls_rule *rule_, void *ofproto_)
 +/* Intended for used by ofproto clients and ofproto submodules to delete flows
 + * that they earlier added to the flow table. */
 +void
 +ofproto_delete_flow(struct ofproto *ofproto, const flow_t *flow)
  {
 -    struct rule *rule = rule_from_cls_rule(rule_);
 -    struct ofproto *ofproto = ofproto_;
 -
 -    /* Mark the flow as not installed, even though it might really be
 -     * installed, so that rule_remove() doesn't bother trying to uninstall it.
 -     * There is no point in uninstalling it individually since we are about to
 -     * blow away all the flows with dpif_flow_flush(). */
 -    rule->installed = false;
 -
 -    rule_remove(ofproto, rule);
 +    struct wdp_rule *rule = wdp_flow_get(ofproto->wdp, flow, UINT_MAX);
 +    if (rule) {
 +        delete_flow(ofproto, rule, OFPRR_DELETE);
 +    }
  }
  
  void
  ofproto_flush_flows(struct ofproto *ofproto)
  {
      COVERAGE_INC(ofproto_flush);
 -    classifier_for_each(&ofproto->cls, CLS_INC_ALL, destroy_rule, ofproto);
 -    dpif_flow_flush(ofproto->dpif);
 +    wdp_flow_flush(ofproto->wdp);
      if (ofproto->in_band) {
          in_band_flushed(ofproto->in_band);
      }
      }
  }
  \f
 -static void
 -reinit_ports(struct ofproto *p)
 -{
 -    struct svec devnames;
 -    struct ofport *ofport;
 -    struct odp_port *odp_ports;
 -    size_t n_odp_ports;
 -    size_t i;
 -
 -    svec_init(&devnames);
 -    HMAP_FOR_EACH (ofport, hmap_node, &p->ports) {
 -        svec_add (&devnames, (char *) ofport->opp.name);
 -    }
 -    dpif_port_list(p->dpif, &odp_ports, &n_odp_ports);
 -    for (i = 0; i < n_odp_ports; i++) {
 -        svec_add (&devnames, odp_ports[i].devname);
 -    }
 -    free(odp_ports);
 -
 -    svec_sort_unique(&devnames);
 -    for (i = 0; i < devnames.n; i++) {
 -        update_port(p, devnames.names[i]);
 -    }
 -    svec_destroy(&devnames);
 -}
 -
 -static size_t
 -refresh_port_group(struct ofproto *p, unsigned int group)
 -{
 -    uint16_t *ports;
 -    size_t n_ports;
 -    struct ofport *port;
 -
 -    assert(group == DP_GROUP_ALL || group == DP_GROUP_FLOOD);
 -
 -    ports = xmalloc(hmap_count(&p->ports) * sizeof *ports);
 -    n_ports = 0;
 -    HMAP_FOR_EACH (port, hmap_node, &p->ports) {
 -        if (group == DP_GROUP_ALL || !(port->opp.config & OFPPC_NO_FLOOD)) {
 -            ports[n_ports++] = port->odp_port;
 -        }
 -    }
 -    dpif_port_group_set(p->dpif, group, ports, n_ports);
 -    free(ports);
 -
 -    return n_ports;
 -}
 -
 -static void
 -refresh_port_groups(struct ofproto *p)
 -{
 -    size_t n_flood = refresh_port_group(p, DP_GROUP_FLOOD);
 -    size_t n_all = refresh_port_group(p, DP_GROUP_ALL);
 -    if (p->sflow) {
 -        ofproto_sflow_set_group_sizes(p->sflow, n_flood, n_all);
 -    }
 -}
 -
 -static struct ofport *
 -make_ofport(const struct odp_port *odp_port)
 -{
 -    struct netdev_options netdev_options;
 -    enum netdev_flags flags;
 -    struct ofport *ofport;
 -    struct netdev *netdev;
 -    bool carrier;
 -    int error;
 -
 -    memset(&netdev_options, 0, sizeof netdev_options);
 -    netdev_options.name = odp_port->devname;
 -    netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
 -
 -    error = netdev_open(&netdev_options, &netdev);
 -    if (error) {
 -        VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
 -                     "cannot be opened (%s)",
 -                     odp_port->devname, odp_port->port,
 -                     odp_port->devname, strerror(error));
 -        return NULL;
 -    }
 -
 -    ofport = xmalloc(sizeof *ofport);
 -    ofport->netdev = netdev;
 -    ofport->odp_port = odp_port->port;
 -    ofport->opp.port_no = odp_port_to_ofp_port(odp_port->port);
 -    netdev_get_etheraddr(netdev, ofport->opp.hw_addr);
 -    memcpy(ofport->opp.name, odp_port->devname,
 -           MIN(sizeof ofport->opp.name, sizeof odp_port->devname));
 -    ofport->opp.name[sizeof ofport->opp.name - 1] = '\0';
 -
 -    netdev_get_flags(netdev, &flags);
 -    ofport->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
 -
 -    netdev_get_carrier(netdev, &carrier);
 -    ofport->opp.state = carrier ? 0 : OFPPS_LINK_DOWN;
 -
 -    netdev_get_features(netdev,
 -                        &ofport->opp.curr, &ofport->opp.advertised,
 -                        &ofport->opp.supported, &ofport->opp.peer);
 -    return ofport;
 -}
 -
 -static bool
 -ofport_conflicts(const struct ofproto *p, const struct odp_port *odp_port)
 -{
 -    if (get_port(p, odp_port->port)) {
 -        VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
 -                     odp_port->port);
 -        return true;
 -    } else if (shash_find(&p->port_by_name, odp_port->devname)) {
 -        VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
 -                     odp_port->devname);
 -        return true;
 -    } else {
 -        return false;
 -    }
 -}
 -
 -static int
 -ofport_equal(const struct ofport *a_, const struct ofport *b_)
 -{
 -    const struct ofp_phy_port *a = &a_->opp;
 -    const struct ofp_phy_port *b = &b_->opp;
 -
 -    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)
 -            && a->state == b->state
 -            && a->config == b->config
 -            && a->curr == b->curr
 -            && a->advertised == b->advertised
 -            && a->supported == b->supported
 -            && a->peer == b->peer);
 -}
 -
 -static void
 -send_port_status(struct ofproto *p, const struct ofport *ofport,
 -                 uint8_t reason)
 -{
 -    /* XXX Should limit the number of queued port status change messages. */
 -    struct ofconn *ofconn;
 -    LIST_FOR_EACH (ofconn, node, &p->all_conns) {
 -        struct ofp_port_status *ops;
 -        struct ofpbuf *b;
 -
 -        if (!ofconn_receives_async_msgs(ofconn)) {
 -            continue;
 -        }
 -
 -        ops = make_openflow_xid(sizeof *ops, OFPT_PORT_STATUS, 0, &b);
 -        ops->reason = reason;
 -        ops->desc = ofport->opp;
 -        hton_ofp_phy_port(&ops->desc);
 -        queue_tx(b, ofconn, NULL);
 -    }
 -    if (p->ofhooks->port_changed_cb) {
 -        p->ofhooks->port_changed_cb(reason, &ofport->opp, p->aux);
 -    }
 -}
 -
 -static void
 -ofport_install(struct ofproto *p, struct ofport *ofport)
 -{
 -    const char *netdev_name = (const char *) ofport->opp.name;
 -
 -    netdev_monitor_add(p->netdev_monitor, ofport->netdev);
 -    hmap_insert(&p->ports, &ofport->hmap_node, hash_int(ofport->odp_port, 0));
 -    shash_add(&p->port_by_name, netdev_name, ofport);
 -    if (p->sflow) {
 -        ofproto_sflow_add_port(p->sflow, ofport->odp_port, netdev_name);
 -    }
 -}
 -
 -static void
 -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));
 -    if (p->sflow) {
 -        ofproto_sflow_del_port(p->sflow, ofport->odp_port);
 -    }
 -}
 -
 -static void
 -ofport_free(struct ofport *ofport)
 -{
 -    if (ofport) {
 -        netdev_close(ofport->netdev);
 -        free(ofport);
 -    }
 -}
 -
 -static struct ofport *
 -get_port(const struct ofproto *ofproto, uint16_t odp_port)
 -{
 -    struct ofport *port;
 -
 -    HMAP_FOR_EACH_IN_BUCKET (port, hmap_node,
 -                             hash_int(odp_port, 0), &ofproto->ports) {
 -        if (port->odp_port == odp_port) {
 -            return port;
 -        }
 -    }
 -    return NULL;
 -}
 -
 -static void
 -update_port(struct ofproto *p, const char *devname)
 -{
 -    struct odp_port odp_port;
 -    struct ofport *old_ofport;
 -    struct ofport *new_ofport;
 -    int error;
 -
 -    COVERAGE_INC(ofproto_update_port);
 -
 -    /* Query the datapath for port information. */
 -    error = dpif_port_query_by_name(p->dpif, devname, &odp_port);
 -
 -    /* Find the old ofport. */
 -    old_ofport = shash_find_data(&p->port_by_name, devname);
 -    if (!error) {
 -        if (!old_ofport) {
 -            /* There's no port named 'devname' but there might be a port with
 -             * the same port number.  This could happen if a port is deleted
 -             * and then a new one added in its place very quickly, or if a port
 -             * is renamed.  In the former case we want to send an OFPPR_DELETE
 -             * and an OFPPR_ADD, and in the latter case we want to send a
 -             * single OFPPR_MODIFY.  We can distinguish the cases by comparing
 -             * the old port's ifindex against the new port, or perhaps less
 -             * reliably but more portably by comparing the old port's MAC
 -             * against the new port's MAC.  However, this code isn't that smart
 -             * and always sends an OFPPR_MODIFY (XXX). */
 -            old_ofport = get_port(p, odp_port.port);
 -        }
 -    } else if (error != ENOENT && error != ENODEV) {
 -        VLOG_WARN_RL(&rl, "dpif_port_query_by_name returned unexpected error "
 -                     "%s", strerror(error));
 -        return;
 -    }
 -
 -    /* Create a new ofport. */
 -    new_ofport = !error ? make_ofport(&odp_port) : NULL;
 -
 -    /* Eliminate a few pathological cases. */
 -    if (!old_ofport && !new_ofport) {
 -        return;
 -    } else if (old_ofport && new_ofport) {
 -        /* Most of the 'config' bits are OpenFlow soft state, but
 -         * OFPPC_PORT_DOWN is maintained the kernel.  So transfer the OpenFlow
 -         * bits from old_ofport.  (make_ofport() only sets OFPPC_PORT_DOWN and
 -         * leaves the other bits 0.)  */
 -        new_ofport->opp.config |= old_ofport->opp.config & ~OFPPC_PORT_DOWN;
 -
 -        if (ofport_equal(old_ofport, new_ofport)) {
 -            /* False alarm--no change. */
 -            ofport_free(new_ofport);
 -            return;
 -        }
 -    }
 -
 -    /* Now deal with the normal cases. */
 -    if (old_ofport) {
 -        ofport_remove(p, old_ofport);
 -    }
 -    if (new_ofport) {
 -        ofport_install(p, new_ofport);
 -    }
 -    send_port_status(p, new_ofport ? new_ofport : old_ofport,
 -                     (!old_ofport ? OFPPR_ADD
 -                      : !new_ofport ? OFPPR_DELETE
 -                      : OFPPR_MODIFY));
 -    ofport_free(old_ofport);
 -
 -    /* Update port groups. */
 -    refresh_port_groups(p);
 -}
 -
 -static int
 -init_ports(struct ofproto *p)
 -{
 -    struct odp_port *ports;
 -    size_t n_ports;
 -    size_t i;
 -    int error;
 -
 -    error = dpif_port_list(p->dpif, &ports, &n_ports);
 -    if (error) {
 -        return error;
 -    }
 -
 -    for (i = 0; i < n_ports; i++) {
 -        const struct odp_port *odp_port = &ports[i];
 -        if (!ofport_conflicts(p, odp_port)) {
 -            struct ofport *ofport = make_ofport(odp_port);
 -            if (ofport) {
 -                ofport_install(p, ofport);
 -            }
 -        }
 -    }
 -    free(ports);
 -    refresh_port_groups(p);
 -    return 0;
 -}
 -\f
  static struct ofconn *
  ofconn_create(struct ofproto *p, struct rconn *rconn, enum ofconn_type type)
  {
@@@ -1263,7 -1769,7 +1258,7 @@@ ofconn_receives_async_msgs(const struc
  static char *
  ofconn_make_name(const struct ofproto *ofproto, const char *target)
  {
 -    return xasprintf("%s<->%s", dpif_base_name(ofproto->dpif), target);
 +    return xasprintf("%s<->%s", wdp_base_name(ofproto->wdp), target);
  }
  
  static void
@@@ -1336,8 -1842,8 +1331,8 @@@ ofservice_lookup(struct ofproto *ofprot
  {
      struct ofservice *ofservice;
  
-     HMAP_FOR_EACH_WITH_HASH (ofservice, struct ofservice, node,
-                              hash_string(target, 0), &ofproto->services) {
+     HMAP_FOR_EACH_WITH_HASH (ofservice, node, hash_string(target, 0),
+                              &ofproto->services) {
          if (!strcmp(pvconn_get_name(ofservice->pvconn), target)) {
              return ofservice;
          }
      return NULL;
  }
  \f
 -/* Caller is responsible for initializing the 'cr' member of the returned
 - * rule. */
 -static struct rule *
 -rule_create(struct ofproto *ofproto, struct rule *super,
 -            const union ofp_action *actions, size_t n_actions,
 -            uint16_t idle_timeout, uint16_t hard_timeout,
 -            uint64_t flow_cookie, bool send_flow_removed)
 -{
 -    struct rule *rule = xzalloc(sizeof *rule);
 -    rule->idle_timeout = idle_timeout;
 -    rule->hard_timeout = hard_timeout;
 -    rule->flow_cookie = flow_cookie;
 -    rule->used = rule->created = time_msec();
 -    rule->send_flow_removed = send_flow_removed;
 -    rule->super = super;
 -    if (super) {
 -        list_push_back(&super->list, &rule->list);
 -    } else {
 -        list_init(&rule->list);
 -    }
 -    rule->n_actions = n_actions;
 -    rule->actions = xmemdup(actions, n_actions * sizeof *actions);
 -    netflow_flow_clear(&rule->nf_flow);
 -    netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->created);
 -
 -    return rule;
 -}
 -
 -static struct rule *
 -rule_from_cls_rule(const struct cls_rule *cls_rule)
 -{
 -    return cls_rule ? CONTAINER_OF(cls_rule, struct rule, cr) : NULL;
 -}
 -
 -static void
 -rule_free(struct rule *rule)
 -{
 -    free(rule->actions);
 -    free(rule->odp_actions);
 -    free(rule);
 -}
 -
 -/* Destroys 'rule'.  If 'rule' is a subrule, also removes it from its
 - * super-rule's list of subrules.  If 'rule' is a super-rule, also iterates
 - * through all of its subrules and revalidates them, destroying any that no
 - * longer has a super-rule (which is probably all of them).
 - *
 - * Before calling this function, the caller must make have removed 'rule' from
 - * the classifier.  If 'rule' is an exact-match rule, the caller is also
 - * responsible for ensuring that it has been uninstalled from the datapath. */
 -static void
 -rule_destroy(struct ofproto *ofproto, struct rule *rule)
 -{
 -    if (!rule->super) {
 -        struct rule *subrule, *next;
 -        LIST_FOR_EACH_SAFE (subrule, next, list, &rule->list) {
 -            revalidate_rule(ofproto, subrule);
 -        }
 -    } else {
 -        list_remove(&rule->list);
 -    }
 -    rule_free(rule);
 -}
 -
  static bool
 -rule_has_out_port(const struct rule *rule, uint16_t out_port)
 +rule_has_out_port(const struct wdp_rule *rule, uint16_t out_port)
  {
      const union ofp_action *oa;
      struct actions_iterator i;
      }
      return false;
  }
 -
 -/* Executes, within 'ofproto', the 'n_actions' actions in 'actions' on
 - * 'packet', which arrived on 'in_port'.
 - *
 - * Takes ownership of 'packet'. */
 -static bool
 -execute_odp_actions(struct ofproto *ofproto, uint16_t in_port,
 -                    const union odp_action *actions, size_t n_actions,
 -                    struct ofpbuf *packet)
 -{
 -    if (n_actions == 1 && actions[0].type == ODPAT_CONTROLLER) {
 -        /* As an optimization, avoid a round-trip from userspace to kernel to
 -         * userspace.  This also avoids possibly filling up kernel packet
 -         * buffers along the way. */
 -        struct odp_msg *msg;
 -
 -        msg = ofpbuf_push_uninit(packet, sizeof *msg);
 -        msg->type = _ODPL_ACTION_NR;
 -        msg->length = sizeof(struct odp_msg) + packet->size;
 -        msg->port = in_port;
 -        msg->reserved = 0;
 -        msg->arg = actions[0].controller.arg;
 -
 -        send_packet_in(ofproto, packet);
 -
 -        return true;
 -    } else {
 -        int error;
 -
 -        error = dpif_execute(ofproto->dpif, in_port,
 -                             actions, n_actions, packet);
 -        ofpbuf_delete(packet);
 -        return !error;
 -    }
 -}
 -
 -/* Executes the actions indicated by 'rule' on 'packet', which is in flow
 - * 'flow' and is considered to have arrived on ODP port 'in_port'.  'packet'
 - * must have at least sizeof(struct ofp_packet_in) bytes of headroom.
 - *
 - * The flow that 'packet' actually contains does not need to actually match
 - * 'rule'; the actions in 'rule' will be applied to it either way.  Likewise,
 - * the packet and byte counters for 'rule' will be credited for the packet sent
 - * out whether or not the packet actually matches 'rule'.
 - *
 - * If 'rule' is an exact-match rule and 'flow' actually equals the rule's flow,
 - * the caller must already have accurately composed ODP actions for it given
 - * 'packet' using rule_make_actions().  If 'rule' is a wildcard rule, or if
 - * 'rule' is an exact-match rule but 'flow' is not the rule's flow, then this
 - * function will compose a set of ODP actions based on 'rule''s OpenFlow
 - * actions and apply them to 'packet'.
 - *
 - * Takes ownership of 'packet'. */
 -static void
 -rule_execute(struct ofproto *ofproto, struct rule *rule,
 -             struct ofpbuf *packet, const flow_t *flow)
 -{
 -    const union odp_action *actions;
 -    struct odp_flow_stats stats;
 -    size_t n_actions;
 -    struct odp_actions a;
 -
 -    assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
 -
 -    /* Grab or compose the ODP actions.
 -     *
 -     * The special case for an exact-match 'rule' where 'flow' is not the
 -     * rule's flow is important to avoid, e.g., sending a packet out its input
 -     * port simply because the ODP actions were composed for the wrong
 -     * scenario. */
 -    if (rule->cr.wc.wildcards || !flow_equal(flow, &rule->cr.flow)) {
 -        struct rule *super = rule->super ? rule->super : rule;
 -        if (xlate_actions(super->actions, super->n_actions, flow, ofproto,
 -                          packet, &a, NULL, 0, NULL)) {
 -            ofpbuf_delete(packet);
 -            return;
 -        }
 -        actions = a.actions;
 -        n_actions = a.n_actions;
 -    } else {
 -        actions = rule->odp_actions;
 -        n_actions = rule->n_odp_actions;
 -    }
 -
 -    /* Execute the ODP actions. */
 -    flow_extract_stats(flow, packet, &stats);
 -    if (execute_odp_actions(ofproto, flow->in_port,
 -                            actions, n_actions, packet)) {
 -        update_stats(ofproto, rule, &stats);
 -        rule->used = time_msec();
 -        netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->used);
 -    }
 -}
 -
 -/* Inserts 'rule' into 'p''s flow table.
 - *
 - * If 'packet' is nonnull, takes ownership of 'packet', executes 'rule''s
 - * actions on it and credits the statistics for sending the packet to 'rule'.
 - * 'packet' must have at least sizeof(struct ofp_packet_in) bytes of
 - * headroom. */
 -static void
 -rule_insert(struct ofproto *p, struct rule *rule, struct ofpbuf *packet,
 -            uint16_t in_port)
 -{
 -    struct rule *displaced_rule;
 -
 -    /* Insert the rule in the classifier. */
 -    displaced_rule = rule_from_cls_rule(classifier_insert(&p->cls, &rule->cr));
 -    if (!rule->cr.wc.wildcards) {
 -        rule_make_actions(p, rule, packet);
 -    }
 -
 -    /* Send the packet and credit it to the rule. */
 -    if (packet) {
 -        flow_t flow;
 -        flow_extract(packet, 0, in_port, &flow);
 -        rule_execute(p, rule, packet, &flow);
 -    }
 -
 -    /* Install the rule in the datapath only after sending the packet, to
 -     * avoid packet reordering.  */
 -    if (rule->cr.wc.wildcards) {
 -        COVERAGE_INC(ofproto_add_wc_flow);
 -        p->need_revalidate = true;
 -    } else {
 -        rule_install(p, rule, displaced_rule);
 -    }
 -
 -    /* Free the rule that was displaced, if any. */
 -    if (displaced_rule) {
 -        rule_destroy(p, displaced_rule);
 -    }
 -}
 -
 -static struct rule *
 -rule_create_subrule(struct ofproto *ofproto, struct rule *rule,
 -                    const flow_t *flow)
 -{
 -    struct rule *subrule = rule_create(ofproto, rule, NULL, 0,
 -                                       rule->idle_timeout, rule->hard_timeout,
 -                                       0, false);
 -    COVERAGE_INC(ofproto_subrule_create);
 -    cls_rule_from_flow(flow, 0, (rule->cr.priority <= UINT16_MAX ? UINT16_MAX
 -                        : rule->cr.priority), &subrule->cr);
 -    classifier_insert_exact(&ofproto->cls, &subrule->cr);
 -
 -    return subrule;
 -}
 -
 -static void
 -rule_remove(struct ofproto *ofproto, struct rule *rule)
 -{
 -    if (rule->cr.wc.wildcards) {
 -        COVERAGE_INC(ofproto_del_wc_flow);
 -        ofproto->need_revalidate = true;
 -    } else {
 -        rule_uninstall(ofproto, rule);
 -    }
 -    classifier_remove(&ofproto->cls, &rule->cr);
 -    rule_destroy(ofproto, rule);
 -}
 -
 -/* Returns true if the actions changed, false otherwise. */
 -static bool
 -rule_make_actions(struct ofproto *p, struct rule *rule,
 -                  const struct ofpbuf *packet)
 -{
 -    const struct rule *super;
 -    struct odp_actions a;
 -    size_t actions_len;
 -
 -    assert(!rule->cr.wc.wildcards);
 -
 -    super = rule->super ? rule->super : rule;
 -    rule->tags = 0;
 -    xlate_actions(super->actions, super->n_actions, &rule->cr.flow, p,
 -                  packet, &a, &rule->tags, &rule->may_install,
 -                  &rule->nf_flow.output_iface);
 -
 -    actions_len = a.n_actions * sizeof *a.actions;
 -    if (rule->n_odp_actions != a.n_actions
 -        || memcmp(rule->odp_actions, a.actions, actions_len)) {
 -        COVERAGE_INC(ofproto_odp_unchanged);
 -        free(rule->odp_actions);
 -        rule->n_odp_actions = a.n_actions;
 -        rule->odp_actions = xmemdup(a.actions, actions_len);
 -        return true;
 -    } else {
 -        return false;
 -    }
 -}
 -
 -static int
 -do_put_flow(struct ofproto *ofproto, struct rule *rule, int flags,
 -            struct odp_flow_put *put)
 -{
 -    memset(&put->flow.stats, 0, sizeof put->flow.stats);
 -    put->flow.key = rule->cr.flow;
 -    put->flow.actions = rule->odp_actions;
 -    put->flow.n_actions = rule->n_odp_actions;
 -    put->flow.flags = 0;
 -    put->flags = flags;
 -    return dpif_flow_put(ofproto->dpif, put);
 -}
 -
 -static void
 -rule_install(struct ofproto *p, struct rule *rule, struct rule *displaced_rule)
 -{
 -    assert(!rule->cr.wc.wildcards);
 -
 -    if (rule->may_install) {
 -        struct odp_flow_put put;
 -        if (!do_put_flow(p, rule,
 -                         ODPPF_CREATE | ODPPF_MODIFY | ODPPF_ZERO_STATS,
 -                         &put)) {
 -            rule->installed = true;
 -            if (displaced_rule) {
 -                update_stats(p, displaced_rule, &put.flow.stats);
 -                rule_post_uninstall(p, displaced_rule);
 -            }
 -        }
 -    } else if (displaced_rule) {
 -        rule_uninstall(p, displaced_rule);
 -    }
 -}
 -
 -static void
 -rule_reinstall(struct ofproto *ofproto, struct rule *rule)
 -{
 -    if (rule->installed) {
 -        struct odp_flow_put put;
 -        COVERAGE_INC(ofproto_dp_missed);
 -        do_put_flow(ofproto, rule, ODPPF_CREATE | ODPPF_MODIFY, &put);
 -    } else {
 -        rule_install(ofproto, rule, NULL);
 -    }
 -}
 -
 -static void
 -rule_update_actions(struct ofproto *ofproto, struct rule *rule)
 -{
 -    bool actions_changed;
 -    uint16_t new_out_iface, old_out_iface;
 -
 -    old_out_iface = rule->nf_flow.output_iface;
 -    actions_changed = rule_make_actions(ofproto, rule, NULL);
 -
 -    if (rule->may_install) {
 -        if (rule->installed) {
 -            if (actions_changed) {
 -                struct odp_flow_put put;
 -                do_put_flow(ofproto, rule, ODPPF_CREATE | ODPPF_MODIFY
 -                                           | ODPPF_ZERO_STATS, &put);
 -                update_stats(ofproto, rule, &put.flow.stats);
 -
 -                /* Temporarily set the old output iface so that NetFlow
 -                 * messages have the correct output interface for the old
 -                 * stats. */
 -                new_out_iface = rule->nf_flow.output_iface;
 -                rule->nf_flow.output_iface = old_out_iface;
 -                rule_post_uninstall(ofproto, rule);
 -                rule->nf_flow.output_iface = new_out_iface;
 -            }
 -        } else {
 -            rule_install(ofproto, rule, NULL);
 -        }
 -    } else {
 -        rule_uninstall(ofproto, rule);
 -    }
 -}
 -
 -static void
 -rule_account(struct ofproto *ofproto, struct rule *rule, uint64_t extra_bytes)
 -{
 -    uint64_t total_bytes = rule->byte_count + extra_bytes;
 -
 -    if (ofproto->ofhooks->account_flow_cb
 -        && total_bytes > rule->accounted_bytes)
 -    {
 -        ofproto->ofhooks->account_flow_cb(
 -            &rule->cr.flow, rule->tags, rule->odp_actions, rule->n_odp_actions,
 -            total_bytes - rule->accounted_bytes, ofproto->aux);
 -        rule->accounted_bytes = total_bytes;
 -    }
 -}
 -
 -static void
 -rule_uninstall(struct ofproto *p, struct rule *rule)
 -{
 -    assert(!rule->cr.wc.wildcards);
 -    if (rule->installed) {
 -        struct odp_flow odp_flow;
 -
 -        odp_flow.key = rule->cr.flow;
 -        odp_flow.actions = NULL;
 -        odp_flow.n_actions = 0;
 -        odp_flow.flags = 0;
 -        if (!dpif_flow_del(p->dpif, &odp_flow)) {
 -            update_stats(p, rule, &odp_flow.stats);
 -        }
 -        rule->installed = false;
 -
 -        rule_post_uninstall(p, rule);
 -    }
 -}
 -
 -static bool
 -is_controller_rule(struct rule *rule)
 -{
 -    /* If the only action is send to the controller then don't report
 -     * NetFlow expiration messages since it is just part of the control
 -     * logic for the network and not real traffic. */
 -
 -    return (rule
 -            && rule->super
 -            && rule->super->n_actions == 1
 -            && action_outputs_to_port(&rule->super->actions[0],
 -                                      htons(OFPP_CONTROLLER)));
 -}
 -
 -static void
 -rule_post_uninstall(struct ofproto *ofproto, struct rule *rule)
 -{
 -    struct rule *super = rule->super;
 -
 -    rule_account(ofproto, rule, 0);
 -
 -    if (ofproto->netflow && !is_controller_rule(rule)) {
 -        struct ofexpired expired;
 -        expired.flow = rule->cr.flow;
 -        expired.packet_count = rule->packet_count;
 -        expired.byte_count = rule->byte_count;
 -        expired.used = rule->used;
 -        netflow_expire(ofproto->netflow, &rule->nf_flow, &expired);
 -    }
 -    if (super) {
 -        super->packet_count += rule->packet_count;
 -        super->byte_count += rule->byte_count;
 -
 -        /* Reset counters to prevent double counting if the rule ever gets
 -         * reinstalled. */
 -        rule->packet_count = 0;
 -        rule->byte_count = 0;
 -        rule->accounted_bytes = 0;
 -
 -        netflow_flow_clear(&rule->nf_flow);
 -    }
 -}
  \f
  static void
  queue_tx(struct ofpbuf *msg, const struct ofconn *ofconn,
      }
  }
  
 -static void
 -send_error(const struct ofconn *ofconn, const struct ofp_header *oh,
 -           int error, const void *data, size_t len)
 -{
 -    struct ofpbuf *buf;
 -    struct ofp_error_msg *oem;
 -
 -    if (!(error >> 16)) {
 -        VLOG_WARN_RL(&rl, "not sending bad error code %d to controller",
 -                     error);
 -        return;
 -    }
 -
 -    COVERAGE_INC(ofproto_error);
 -    oem = make_openflow_xid(len + sizeof *oem, OFPT_ERROR,
 -                            oh ? oh->xid : 0, &buf);
 -    oem->type = htons((unsigned int) error >> 16);
 -    oem->code = htons(error & 0xffff);
 -    memcpy(oem->data, data, len);
 -    queue_tx(buf, ofconn, ofconn->reply_counter);
 -}
 -
  static void
  send_error_oh(const struct ofconn *ofconn, const struct ofp_header *oh,
                int error)
  {
 -    size_t oh_length = ntohs(oh->length);
 -    send_error(ofconn, oh, error, oh, MIN(oh_length, 64));
 -}
 -
 -static void
 -hton_ofp_phy_port(struct ofp_phy_port *opp)
 -{
 -    opp->port_no = htons(opp->port_no);
 -    opp->config = htonl(opp->config);
 -    opp->state = htonl(opp->state);
 -    opp->curr = htonl(opp->curr);
 -    opp->advertised = htonl(opp->advertised);
 -    opp->supported = htonl(opp->supported);
 -    opp->peer = htonl(opp->peer);
 +    struct ofpbuf *buf = make_ofp_error_msg(error, oh);
 +    if (buf) {
 +        COVERAGE_INC(ofproto_error);
 +        queue_tx(buf, ofconn, ofconn->reply_counter);
 +    }
  }
  
  static int
@@@ -1396,29 -2345,35 +1391,29 @@@ static in
  handle_features_request(struct ofproto *p, struct ofconn *ofconn,
                          struct ofp_header *oh)
  {
 -    struct ofp_switch_features *osf;
 -    struct ofpbuf *buf;
 -    struct ofport *port;
 -
 -    osf = make_openflow_xid(sizeof *osf, OFPT_FEATURES_REPLY, oh->xid, &buf);
 -    osf->datapath_id = htonll(p->datapath_id);
 -    osf->n_buffers = htonl(pktbuf_capacity());
 -    osf->n_tables = 2;
 -    osf->capabilities = htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
 -                              OFPC_PORT_STATS | OFPC_ARP_MATCH_IP);
 -    osf->actions = htonl((1u << OFPAT_OUTPUT) |
 -                         (1u << OFPAT_SET_VLAN_VID) |
 -                         (1u << OFPAT_SET_VLAN_PCP) |
 -                         (1u << OFPAT_STRIP_VLAN) |
 -                         (1u << OFPAT_SET_DL_SRC) |
 -                         (1u << OFPAT_SET_DL_DST) |
 -                         (1u << OFPAT_SET_NW_SRC) |
 -                         (1u << OFPAT_SET_NW_DST) |
 -                         (1u << OFPAT_SET_NW_TOS) |
 -                         (1u << OFPAT_SET_TP_SRC) |
 -                         (1u << OFPAT_SET_TP_DST) |
 -                         (1u << OFPAT_ENQUEUE));
 -
 -    HMAP_FOR_EACH (port, hmap_node, &p->ports) {
 -        hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
 -    }
 +    struct ofpbuf *features;
 +    int error;
  
 -    queue_tx(buf, ofconn, ofconn->reply_counter);
 -    return 0;
 +    error = wdp_get_features(p->wdp, &features);
 +    if (!error) {
 +        struct ofp_switch_features *osf = features->data;
 +
 +        update_openflow_length(features);
 +        osf->header.version = OFP_VERSION;
 +        osf->header.type = OFPT_FEATURES_REPLY;
 +        osf->header.xid = oh->xid;
 +
 +        osf->datapath_id = htonll(p->datapath_id);
 +        osf->n_buffers = htonl(pktbuf_capacity());
 +        memset(osf->pad, 0, sizeof osf->pad);
 +
 +        /* Turn on capabilities implemented by ofproto. */
 +        osf->capabilities |= htonl(OFPC_FLOW_STATS | OFPC_TABLE_STATS |
 +                                   OFPC_PORT_STATS);
 +
 +        queue_tx(features, ofconn, ofconn->reply_counter);
 +    }
 +    return error;
  }
  
  static int
@@@ -1431,7 -2386,7 +1426,7 @@@ handle_get_config_request(struct ofprot
      bool drop_frags;
  
      /* Figure out flags. */
 -    dpif_get_drop_frags(p->dpif, &drop_frags);
 +    wdp_get_drop_frags(p->wdp, &drop_frags);
      flags = drop_frags ? OFPC_FRAG_DROP : OFPC_FRAG_NORMAL;
  
      /* Send reply. */
@@@ -1459,10 -2414,10 +1454,10 @@@ handle_set_config(struct ofproto *p, st
      if (ofconn->type == OFCONN_PRIMARY && ofconn->role != NX_ROLE_SLAVE) {
          switch (flags & OFPC_FRAG_MASK) {
          case OFPC_FRAG_NORMAL:
 -            dpif_set_drop_frags(p->dpif, false);
 +            wdp_set_drop_frags(p->wdp, false);
              break;
          case OFPC_FRAG_DROP:
 -            dpif_set_drop_frags(p->dpif, true);
 +            wdp_set_drop_frags(p->wdp, true);
              break;
          default:
              VLOG_WARN_RL(&rl, "requested bad fragment mode (flags=%"PRIx16")",
      return 0;
  }
  
 -static void
 -add_output_group_action(struct odp_actions *actions, uint16_t group,
 -                        uint16_t *nf_output_iface)
 -{
 -    odp_actions_add(actions, ODPAT_OUTPUT_GROUP)->output_group.group = group;
 -
 -    if (group == DP_GROUP_ALL || group == DP_GROUP_FLOOD) {
 -        *nf_output_iface = NF_OUT_FLOOD;
 -    }
 -}
 -
 -static void
 -add_controller_action(struct odp_actions *actions, uint16_t max_len)
 -{
 -    union odp_action *a = odp_actions_add(actions, ODPAT_CONTROLLER);
 -    a->controller.arg = max_len;
 -}
 -
 -struct action_xlate_ctx {
 -    /* Input. */
 -    flow_t 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
 -
 -static void do_xlate_actions(const union ofp_action *in, size_t n_in,
 -                             struct action_xlate_ctx *ctx);
 -
 -static void
 -add_output_action(struct action_xlate_ctx *ctx, uint16_t port)
 -{
 -    const struct ofport *ofport = get_port(ctx->ofproto, port);
 -
 -    if (ofport) {
 -        if (ofport->opp.config & OFPPC_NO_FWD) {
 -            /* Forwarding disabled on port. */
 -            return;
 -        }
 -    } else {
 -        /*
 -         * We don't have an ofport record for this port, but it doesn't hurt to
 -         * allow forwarding to it anyhow.  Maybe such a port will appear later
 -         * and we're pre-populating the flow table.
 -         */
 -    }
 -
 -    odp_actions_add(ctx->out, ODPAT_OUTPUT)->output.port = port;
 -    ctx->nf_output_iface = port;
 -}
 -
 -static struct rule *
 -lookup_valid_rule(struct ofproto *ofproto, const flow_t *flow)
 -{
 -    struct rule *rule;
 -    rule = rule_from_cls_rule(classifier_lookup(&ofproto->cls, flow));
 -
 -    /* The rule we found might not be valid, since we could be in need of
 -     * revalidation.  If it is not valid, don't return it. */
 -    if (rule
 -        && rule->super
 -        && ofproto->need_revalidate
 -        && !revalidate_rule(ofproto, rule)) {
 -        COVERAGE_INC(ofproto_invalidated);
 -        return NULL;
 -    }
 -
 -    return rule;
 -}
 -
 -static void
 -xlate_table_action(struct action_xlate_ctx *ctx, uint16_t in_port)
 -{
 -    if (ctx->recurse < MAX_RESUBMIT_RECURSION) {
 -        uint16_t old_in_port;
 -        struct rule *rule;
 -
 -        /* Look up a flow with 'in_port' as the input port.  Then restore the
 -         * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
 -         * have surprising behavior). */
 -        old_in_port = ctx->flow.in_port;
 -        ctx->flow.in_port = in_port;
 -        rule = lookup_valid_rule(ctx->ofproto, &ctx->flow);
 -        ctx->flow.in_port = old_in_port;
 -
 -        if (rule) {
 -            if (rule->super) {
 -                rule = rule->super;
 -            }
 -
 -            ctx->recurse++;
 -            do_xlate_actions(rule->actions, rule->n_actions, ctx);
 -            ctx->recurse--;
 -        }
 -    } else {
 -        struct vlog_rate_limit recurse_rl = VLOG_RATE_LIMIT_INIT(1, 1);
 -
 -        VLOG_ERR_RL(&recurse_rl, "NXAST_RESUBMIT recursed over %d times",
 -                    MAX_RESUBMIT_RECURSION);
 -    }
 -}
 -
 -static void
 -xlate_output_action__(struct action_xlate_ctx *ctx,
 -                      uint16_t port, uint16_t max_len)
 -{
 -    uint16_t odp_port;
 -    uint16_t prev_nf_output_iface = ctx->nf_output_iface;
 -
 -    ctx->nf_output_iface = NF_OUT_DROP;
 -
 -    switch (port) {
 -    case OFPP_IN_PORT:
 -        add_output_action(ctx, ctx->flow.in_port);
 -        break;
 -    case OFPP_TABLE:
 -        xlate_table_action(ctx, ctx->flow.in_port);
 -        break;
 -    case OFPP_NORMAL:
 -        if (!ctx->ofproto->ofhooks->normal_cb(&ctx->flow, ctx->packet,
 -                                              ctx->out, ctx->tags,
 -                                              &ctx->nf_output_iface,
 -                                              ctx->ofproto->aux)) {
 -            COVERAGE_INC(ofproto_uninstallable);
 -            ctx->may_set_up_flow = false;
 -        }
 -        break;
 -    case OFPP_FLOOD:
 -        add_output_group_action(ctx->out, DP_GROUP_FLOOD,
 -                                &ctx->nf_output_iface);
 -        break;
 -    case OFPP_ALL:
 -        add_output_group_action(ctx->out, DP_GROUP_ALL, &ctx->nf_output_iface);
 -        break;
 -    case OFPP_CONTROLLER:
 -        add_controller_action(ctx->out, max_len);
 -        break;
 -    case OFPP_LOCAL:
 -        add_output_action(ctx, ODPP_LOCAL);
 -        break;
 -    default:
 -        odp_port = ofp_port_to_odp_port(port);
 -        if (odp_port != ctx->flow.in_port) {
 -            add_output_action(ctx, odp_port);
 -        }
 -        break;
 -    }
 -
 -    if (prev_nf_output_iface == NF_OUT_FLOOD) {
 -        ctx->nf_output_iface = NF_OUT_FLOOD;
 -    } else if (ctx->nf_output_iface == NF_OUT_DROP) {
 -        ctx->nf_output_iface = prev_nf_output_iface;
 -    } else if (prev_nf_output_iface != NF_OUT_DROP &&
 -               ctx->nf_output_iface != NF_OUT_FLOOD) {
 -        ctx->nf_output_iface = NF_OUT_MULTI;
 -    }
 -}
 -
 -static void
 -xlate_output_action(struct action_xlate_ctx *ctx,
 -                    const struct ofp_action_output *oao)
 -{
 -    xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
 -}
 -
 -/* If the final ODP action in 'ctx' is "pop priority", drop it, as an
 - * optimization, because we're going to add another action that sets the
 - * priority immediately after, or because there are no actions following the
 - * pop.  */
 -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--;
 -    }
 -}
 -
 -static void
 -xlate_enqueue_action(struct action_xlate_ctx *ctx,
 -                     const struct ofp_action_enqueue *oae)
 -{
 -    uint16_t ofp_port, odp_port;
 -    uint32_t priority;
 -    int error;
 -
 -    error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(oae->queue_id),
 -                                   &priority);
 -    if (error) {
 -        /* Fall back to ordinary output action. */
 -        xlate_output_action__(ctx, ntohs(oae->port), 0);
 -        return;
 -    }
 -
 -    /* Figure out ODP output port. */
 -    ofp_port = ntohs(oae->port);
 -    if (ofp_port != OFPP_IN_PORT) {
 -        odp_port = ofp_port_to_odp_port(ofp_port);
 -    } else {
 -        odp_port = ctx->flow.in_port;
 -    }
 -
 -    /* Add ODP actions. */
 -    remove_pop_action(ctx);
 -    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);
 -
 -    /* Update NetFlow output port. */
 -    if (ctx->nf_output_iface == NF_OUT_DROP) {
 -        ctx->nf_output_iface = odp_port;
 -    } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
 -        ctx->nf_output_iface = NF_OUT_MULTI;
 -    }
 -}
 -
 -static void
 -xlate_set_queue_action(struct action_xlate_ctx *ctx,
 -                       const struct nx_action_set_queue *nasq)
 -{
 -    uint32_t priority;
 -    int error;
 -
 -    error = dpif_queue_to_priority(ctx->ofproto->dpif, ntohl(nasq->queue_id),
 -                                   &priority);
 -    if (error) {
 -        /* Couldn't translate queue to a priority, so ignore.  A warning
 -         * has already been logged. */
 -        return;
 -    }
 -
 -    remove_pop_action(ctx);
 -    odp_actions_add(ctx->out, ODPAT_SET_PRIORITY)->priority.priority
 -        = priority;
 -}
 -
 -static void
 -xlate_nicira_action(struct action_xlate_ctx *ctx,
 -                    const struct nx_action_header *nah)
 -{
 -    const struct nx_action_resubmit *nar;
 -    const struct nx_action_set_tunnel *nast;
 -    const struct nx_action_set_queue *nasq;
 -    union odp_action *oa;
 -    int subtype = ntohs(nah->subtype);
 -
 -    assert(nah->vendor == htonl(NX_VENDOR_ID));
 -    switch (subtype) {
 -    case NXAST_RESUBMIT:
 -        nar = (const struct nx_action_resubmit *) nah;
 -        xlate_table_action(ctx, ofp_port_to_odp_port(ntohs(nar->in_port)));
 -        break;
 -
 -    case NXAST_SET_TUNNEL:
 -        nast = (const struct nx_action_set_tunnel *) nah;
 -        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);
 -        }
 -        break;
 -
 -    case NXAST_SET_QUEUE:
 -        nasq = (const struct nx_action_set_queue *) nah;
 -        xlate_set_queue_action(ctx, nasq);
 -        break;
 -
 -    case NXAST_POP_QUEUE:
 -        odp_actions_add(ctx->out, ODPAT_POP_PRIORITY);
 -        break;
 -
 -    /* 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. */
 -
 -    default:
 -        VLOG_DBG_RL(&rl, "unknown Nicira action type %"PRIu16, subtype);
 -        break;
 -    }
 -}
 -
 -static void
 -do_xlate_actions(const union ofp_action *in, size_t n_in,
 -                 struct action_xlate_ctx *ctx)
 -{
 -    struct actions_iterator iter;
 -    const union ofp_action *ia;
 -    const struct ofport *port;
 -
 -    port = get_port(ctx->ofproto, ctx->flow.in_port);
 -    if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
 -        port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
 -                            ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
 -        /* Drop this flow. */
 -        return;
 -    }
 -
 -    for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
 -        uint16_t type = ntohs(ia->type);
 -        union odp_action *oa;
 -
 -        switch (type) {
 -        case OFPAT_OUTPUT:
 -            xlate_output_action(ctx, &ia->output);
 -            break;
 -
 -        case OFPAT_SET_VLAN_VID:
 -            oa = odp_actions_add(ctx->out, ODPAT_SET_VLAN_VID);
 -            ctx->flow.dl_vlan = oa->vlan_vid.vlan_vid = ia->vlan_vid.vlan_vid;
 -            break;
 -
 -        case OFPAT_SET_VLAN_PCP:
 -            oa = odp_actions_add(ctx->out, ODPAT_SET_VLAN_PCP);
 -            ctx->flow.dl_vlan_pcp = oa->vlan_pcp.vlan_pcp = ia->vlan_pcp.vlan_pcp;
 -            break;
 -
 -        case OFPAT_STRIP_VLAN:
 -            odp_actions_add(ctx->out, ODPAT_STRIP_VLAN);
 -            ctx->flow.dl_vlan = htons(OFP_VLAN_NONE);
 -            ctx->flow.dl_vlan_pcp = 0;
 -            break;
 -
 -        case OFPAT_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,
 -                   ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
 -            break;
 -
 -        case OFPAT_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,
 -                   ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
 -            break;
 -
 -        case OFPAT_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);
 -            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);
 -            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);
 -            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);
 -            ctx->flow.tp_dst = oa->tp_port.tp_port = ia->tp_port.tp_port;
 -            break;
 -
 -        case OFPAT_VENDOR:
 -            xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
 -            break;
 -
 -        case OFPAT_ENQUEUE:
 -            xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
 -            break;
 -
 -        default:
 -            VLOG_DBG_RL(&rl, "unknown action type %"PRIu16, type);
 -            break;
 -        }
 -    }
 -}
 -
 -static int
 -xlate_actions(const union ofp_action *in, size_t n_in,
 -              const flow_t *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)
 -{
 -    tag_type no_tags = 0;
 -    struct action_xlate_ctx ctx;
 -    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 = tags ? tags : &no_tags;
 -    ctx.may_set_up_flow = true;
 -    ctx.nf_output_iface = NF_OUT_DROP;
 -    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 (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)) {
 -        COVERAGE_INC(odp_overflow);
 -        odp_actions_init(out);
 -        return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_TOO_MANY);
 -    }
 -    return 0;
 -}
 -
  /* Checks whether 'ofconn' is a slave controller.  If so, returns an OpenFlow
   * error message code (composed with ofp_mkerr()) for the caller to propagate
   * upward.  Otherwise, returns 0.
@@@ -1505,7 -2896,7 +1500,7 @@@ handle_packet_out(struct ofproto *p, st
  {
      struct ofp_packet_out *opo;
      struct ofpbuf payload, *buffer;
 -    struct odp_actions actions;
 +    struct ofp_action_header *actions;
      int n_actions;
      uint16_t in_port;
      flow_t flow;
          return error;
      }
      opo = (struct ofp_packet_out *) oh;
 +    actions = opo->actions;
  
      COVERAGE_INC(ofproto_packet_out);
      if (opo->buffer_id != htonl(UINT32_MAX)) {
          buffer = NULL;
      }
  
 -    flow_extract(&payload, 0, ofp_port_to_odp_port(ntohs(opo->in_port)), &flow);
 -    error = xlate_actions((const union ofp_action *) opo->actions, n_actions,
 -                          &flow, p, &payload, &actions, NULL, NULL, NULL);
 -    if (error) {
 -        return error;
 -    }
 -
 -    dpif_execute(p->dpif, flow.in_port, actions.actions, actions.n_actions,
 -                 &payload);
 +    flow_extract(&payload, 0, ntohs(opo->in_port), &flow);
 +    wdp_execute(p->wdp, flow.in_port, (const union ofp_action *) actions,
 +                n_actions, &payload);
      ofpbuf_delete(buffer);
  
      return 0;
  }
  
 -static void
 -update_port_config(struct ofproto *p, struct ofport *port,
 -                   uint32_t config, uint32_t mask)
 -{
 -    mask &= config ^ port->opp.config;
 -    if (mask & OFPPC_PORT_DOWN) {
 -        if (config & OFPPC_PORT_DOWN) {
 -            netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
 -        } else {
 -            netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
 -        }
 -    }
 -#define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | OFPPC_NO_FWD)
 -    if (mask & REVALIDATE_BITS) {
 -        COVERAGE_INC(ofproto_costly_flags);
 -        port->opp.config ^= mask & REVALIDATE_BITS;
 -        p->need_revalidate = true;
 -    }
 -#undef REVALIDATE_BITS
 -    if (mask & OFPPC_NO_FLOOD) {
 -        port->opp.config ^= OFPPC_NO_FLOOD;
 -        refresh_port_groups(p);
 -    }
 -    if (mask & OFPPC_NO_PACKET_IN) {
 -        port->opp.config ^= OFPPC_NO_PACKET_IN;
 -    }
 -}
 -
  static int
  handle_port_mod(struct ofproto *p, struct ofconn *ofconn,
                  struct ofp_header *oh)
  {
      const struct ofp_port_mod *opm;
 -    struct ofport *port;
 +    struct wdp_port port;
      int error;
  
      error = reject_slave_controller(ofconn, oh);
      }
      opm = (struct ofp_port_mod *) oh;
  
 -    port = get_port(p, ofp_port_to_odp_port(ntohs(opm->port_no)));
 -    if (!port) {
 -        return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
 -    } else if (memcmp(port->opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
 -        return ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
 +    if (wdp_port_query_by_number(p->wdp, ntohs(opm->port_no), &port)) {
 +        error = ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT);
 +    } else if (memcmp(port.opp.hw_addr, opm->hw_addr, OFP_ETH_ALEN)) {
 +        error = ofp_mkerr(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR);
      } else {
 -        update_port_config(p, port, ntohl(opm->config), ntohl(opm->mask));
 +        uint32_t mask, new_config;
 +
 +        mask = ntohl(opm->mask) & (OFPPC_PORT_DOWN | OFPPC_NO_STP
 +                                   | OFPPC_NO_RECV | OFPPC_NO_RECV_STP
 +                                   | OFPPC_NO_FLOOD | OFPPC_NO_FWD
 +                                   | OFPPC_NO_PACKET_IN);
 +        new_config = (port.opp.config & ~mask) | (ntohl(opm->config) & mask);
 +        if (new_config != port.opp.config) {
 +            error = wdp_port_set_config(p->wdp, ntohs(opm->port_no),
 +                                        new_config);
 +        }
          if (opm->advertise) {
 -            netdev_set_advertisements(port->netdev, ntohl(opm->advertise));
 +            netdev_set_advertisements(port.netdev, ntohl(opm->advertise));
          }
      }
 -    return 0;
 +    wdp_port_free(&port);
 +
 +    return error;
  }
  
  static struct ofpbuf *
@@@ -1639,25 -3052,65 +1634,25 @@@ handle_desc_stats_request(struct ofprot
      return 0;
  }
  
 -static void
 -count_subrules(struct cls_rule *cls_rule, void *n_subrules_)
 -{
 -    struct rule *rule = rule_from_cls_rule(cls_rule);
 -    int *n_subrules = n_subrules_;
 -
 -    if (rule->super) {
 -        (*n_subrules)++;
 -    }
 -}
 -
  static int
  handle_table_stats_request(struct ofproto *p, struct ofconn *ofconn,
                             struct ofp_stats_request *request)
  {
 -    struct ofp_table_stats *ots;
      struct ofpbuf *msg;
 -    struct odp_stats dpstats;
 -    int n_exact, n_subrules, n_wild;
 -
 -    msg = start_stats_reply(request, sizeof *ots * 2);
 -
 -    /* Count rules of various kinds. */
 -    n_subrules = 0;
 -    classifier_for_each(&p->cls, CLS_INC_EXACT, count_subrules, &n_subrules);
 -    n_exact = classifier_count_exact(&p->cls) - n_subrules;
 -    n_wild = classifier_count(&p->cls) - classifier_count_exact(&p->cls);
 -
 -    /* Hash table. */
 -    dpif_get_dp_stats(p->dpif, &dpstats);
 -    ots = append_stats_reply(sizeof *ots, ofconn, &msg);
 -    memset(ots, 0, sizeof *ots);
 -    ots->table_id = TABLEID_HASH;
 -    strcpy(ots->name, "hash");
 -    ots->wildcards = htonl(0);
 -    ots->max_entries = htonl(dpstats.max_capacity);
 -    ots->active_count = htonl(n_exact);
 -    ots->lookup_count = htonll(dpstats.n_frags + dpstats.n_hit +
 -                               dpstats.n_missed);
 -    ots->matched_count = htonll(dpstats.n_hit); /* XXX */
 -
 -    /* Classifier table. */
 -    ots = append_stats_reply(sizeof *ots, ofconn, &msg);
 -    memset(ots, 0, sizeof *ots);
 -    ots->table_id = TABLEID_CLASSIFIER;
 -    strcpy(ots->name, "classifier");
 -    ots->wildcards = p->tun_id_from_cookie ? htonl(OVSFW_ALL)
 -                                           : htonl(OFPFW_ALL);
 -    ots->max_entries = htonl(65536);
 -    ots->active_count = htonl(n_wild);
 -    ots->lookup_count = htonll(0);              /* XXX */
 -    ots->matched_count = htonll(0);             /* XXX */
 +    int error;
  
 -    queue_tx(msg, ofconn, ofconn->reply_counter);
 -    return 0;
 +    msg = start_stats_reply(request, sizeof(struct ofp_table_stats) * 3);
 +    error = wdp_get_table_stats(p->wdp, msg);
 +    if (!error) {
 +        queue_tx(msg, ofconn, ofconn->reply_counter);
 +    } else {
 +        ofpbuf_delete(msg);
 +    }
 +    return error;
  }
  
  static void
 -append_port_stat(struct ofport *port, struct ofconn *ofconn,
 +append_port_stat(struct wdp_port *port, struct ofconn *ofconn,
                   struct ofpbuf **msgp)
  {
      struct netdev_stats stats;
@@@ -1693,6 -3146,7 +1688,6 @@@ handle_port_stats_request(struct ofprot
      struct ofp_port_stats_request *psr;
      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);
  
      msg = start_stats_reply(osr, 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) {
 -            append_port_stat(port, ofconn, &msg);
 +        struct wdp_port port;
 +
 +        if (!wdp_port_query_by_number(p->wdp, ntohs(psr->port_no), &port)) {
 +            append_port_stat(&port, ofconn, &msg);
 +            wdp_port_free(&port);
          }
      } else {
 -        HMAP_FOR_EACH (port, hmap_node, &p->ports) {
 -            append_port_stat(port, ofconn, &msg);
 +        struct wdp_port *ports;
 +        size_t n_ports;
 +        size_t i;
 +
 +        wdp_port_list(p->wdp, &ports, &n_ports);
 +        for (i = 0; i < n_ports; i++) {
 +            append_port_stat(&ports[i], ofconn, &msg);
          }
 +        wdp_port_array_free(ports, n_ports);
      }
  
      queue_tx(msg, ofconn, ofconn->reply_counter);
@@@ -1734,23 -3180,60 +1729,23 @@@ struct flow_stats_cbdata 
   * '*packet_countp' and '*byte_countp'.  If 'rule' is a wildcarded rule, the
   * returned statistic include statistics for all of 'rule''s subrules. */
  static void
 -query_stats(struct ofproto *p, struct rule *rule,
 +query_stats(struct ofproto *p, struct wdp_rule *rule,
              uint64_t *packet_countp, uint64_t *byte_countp)
  {
 -    uint64_t packet_count, byte_count;
 -    struct rule *subrule;
 -    struct odp_flow *odp_flows;
 -    size_t n_odp_flows;
 -
 -    /* Start from historical data for 'rule' itself that are no longer tracked
 -     * by the datapath.  This counts, for example, subrules that have
 -     * expired. */
 -    packet_count = rule->packet_count;
 -    byte_count = rule->byte_count;
 -
 -    /* Prepare to ask the datapath for statistics on 'rule', or if it is
 -     * wildcarded then on all of its subrules.
 -     *
 -     * Also, add any statistics that are not tracked by the datapath for each
 -     * subrule.  This includes, for example, statistics for packets that were
 -     * executed "by hand" by ofproto via dpif_execute() but must be accounted
 -     * to a flow. */
 -    n_odp_flows = rule->cr.wc.wildcards ? list_size(&rule->list) : 1;
 -    odp_flows = xzalloc(n_odp_flows * sizeof *odp_flows);
 -    if (rule->cr.wc.wildcards) {
 -        size_t i = 0;
 -        LIST_FOR_EACH (subrule, list, &rule->list) {
 -            odp_flows[i++].key = subrule->cr.flow;
 -            packet_count += subrule->packet_count;
 -            byte_count += subrule->byte_count;
 -        }
 -    } else {
 -        odp_flows[0].key = rule->cr.flow;
 -    }
 +    struct wdp_flow_stats stats;
  
 -    /* Fetch up-to-date statistics from the datapath and add them in. */
 -    if (!dpif_flow_get_multiple(p->dpif, odp_flows, n_odp_flows)) {
 -        size_t i;
 -        for (i = 0; i < n_odp_flows; i++) {
 -            struct odp_flow *odp_flow = &odp_flows[i];
 -            packet_count += odp_flow->stats.n_packets;
 -            byte_count += odp_flow->stats.n_bytes;
 -        }
 +    if (!wdp_flow_get_stats(p->wdp, rule, &stats)) {
 +        *packet_countp = stats.n_packets;
 +        *byte_countp = stats.n_bytes;
 +    } else {
 +        *packet_countp = 0;
 +        *byte_countp = 0;
      }
 -    free(odp_flows);
 -
 -    /* Return the stats to the caller. */
 -    *packet_countp = packet_count;
 -    *byte_countp = byte_count;
  }
  
 -static void
 -flow_stats_cb(struct cls_rule *rule_, void *cbdata_)
 +static int
 +flow_stats_cb(struct wdp_rule *rule, void *cbdata_)
  {
 -    struct rule *rule = rule_from_cls_rule(rule_);
      struct flow_stats_cbdata *cbdata = cbdata_;
      struct ofp_flow_stats *ofs;
      uint64_t packet_count, byte_count;
      uint32_t sec = tdiff / 1000;
      uint32_t msec = tdiff - (sec * 1000);
  
 -    if (rule_is_hidden(rule) || !rule_has_out_port(rule, cbdata->out_port)) {
 -        return;
 +    if (rule_is_hidden(rule)
 +        || !rule_has_out_port(rule, cbdata->out_port)) {
 +        return 0;
      }
  
      act_len = sizeof *rule->actions * rule->n_actions;
  
      ofs = append_stats_reply(len, cbdata->ofconn, &cbdata->msg);
      ofs->length = htons(len);
 -    ofs->table_id = rule->cr.wc.wildcards ? TABLEID_CLASSIFIER : TABLEID_HASH;
 +    ofs->table_id = rule->ofp_table_id;
      ofs->pad = 0;
 -    flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards,
 -                  cbdata->ofproto->tun_id_from_cookie, &ofs->match);
 +    flow_to_match(&rule->cr.flow, cbdata->ofproto->tun_id_from_cookie,
 +                  &ofs->match);
      ofs->duration_sec = htonl(sec);
      ofs->duration_nsec = htonl(msec * 1000000);
 -    ofs->cookie = rule->flow_cookie;
 -    ofs->priority = htons(rule->cr.priority);
 +    ofs->cookie = ofproto_rule_cast(rule)->flow_cookie;
 +    ofs->priority = htons(rule->cr.flow.priority);
      ofs->idle_timeout = htons(rule->idle_timeout);
      ofs->hard_timeout = htons(rule->hard_timeout);
      memset(ofs->pad2, 0, sizeof ofs->pad2);
      ofs->packet_count = htonll(packet_count);
      ofs->byte_count = htonll(byte_count);
      memcpy(ofs->actions, rule->actions, act_len);
 +
 +    return 0;
  }
  
 -static int
 +static unsigned int
  table_id_to_include(uint8_t table_id)
  {
 -    return (table_id == TABLEID_HASH ? CLS_INC_EXACT
 -            : table_id == TABLEID_CLASSIFIER ? CLS_INC_WILD
 -            : table_id == 0xff ? CLS_INC_ALL
 +    return (table_id == 0xff ? UINT_MAX
 +            : table_id < 32 ? 1u << table_id
              : 0);
  }
  
 +static unsigned int
 +flow_mod_table_id(const struct ofconn *ofconn, const struct ofp_flow_mod *ofm)
 +{
 +    return ofconn->flow_mod_table_id ? ntohs(ofm->command) >> 8 : 0xff;
 +}
 +
  static int
  handle_flow_stats_request(struct ofproto *p, struct ofconn *ofconn,
                            const struct ofp_stats_request *osr,
  {
      struct ofp_flow_stats_request *fsr;
      struct flow_stats_cbdata cbdata;
 -    struct cls_rule target;
 +    flow_t target;
  
      if (arg_size != sizeof *fsr) {
          return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
      cbdata.ofconn = ofconn;
      cbdata.out_port = fsr->out_port;
      cbdata.msg = start_stats_reply(osr, 1024);
 -    cls_rule_from_match(&fsr->match, 0, false, 0, &target);
 -    classifier_for_each_match(&p->cls, &target,
 -                              table_id_to_include(fsr->table_id),
 -                              flow_stats_cb, &cbdata);
 +    flow_from_match(&fsr->match, 0, false, 0, &target);
 +    wdp_flow_for_each_match(p->wdp, &target,
 +                            table_id_to_include(fsr->table_id),
 +                            flow_stats_cb, &cbdata);
      queue_tx(cbdata.msg, ofconn, ofconn->reply_counter);
      return 0;
  }
@@@ -1835,29 -3310,33 +1830,29 @@@ struct flow_stats_ds_cbdata 
      struct ds *results;
  };
  
 -static void
 -flow_stats_ds_cb(struct cls_rule *rule_, void *cbdata_)
 +static int
 +flow_stats_ds_cb(struct wdp_rule *rule, void *cbdata_)
  {
 -    struct rule *rule = rule_from_cls_rule(rule_);
      struct flow_stats_ds_cbdata *cbdata = cbdata_;
      struct ds *results = cbdata->results;
      struct ofp_match match;
      uint64_t packet_count, byte_count;
      size_t act_len = sizeof *rule->actions * rule->n_actions;
  
 -    /* Don't report on subrules. */
 -    if (rule->super != NULL) {
 -        return;
 -    }
 -
      query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
 -    flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards,
 -                  cbdata->ofproto->tun_id_from_cookie, &match);
 +    flow_to_match(&rule->cr.flow, cbdata->ofproto->tun_id_from_cookie,
 +                  &match);
  
      ds_put_format(results, "duration=%llds, ",
                    (time_msec() - rule->created) / 1000);
 -    ds_put_format(results, "priority=%u, ", rule->cr.priority);
 +    ds_put_format(results, "priority=%u, ", rule->cr.flow.priority);
      ds_put_format(results, "n_packets=%"PRIu64", ", packet_count);
      ds_put_format(results, "n_bytes=%"PRIu64", ", byte_count);
      ofp_print_match(results, &match, true);
      ofp_print_actions(results, &rule->actions->header, act_len);
      ds_put_cstr(results, "\n");
 +
 +    return 0;
  }
  
  /* Adds a pretty-printed description of all flows to 'results', including
  void
  ofproto_get_all_flows(struct ofproto *p, struct ds *results)
  {
 -    struct ofp_match match;
 -    struct cls_rule target;
      struct flow_stats_ds_cbdata cbdata;
 +    struct ofp_match match;
 +    flow_t target;
  
      memset(&match, 0, sizeof match);
      match.wildcards = htonl(OVSFW_ALL);
      cbdata.ofproto = p;
      cbdata.results = results;
  
 -    cls_rule_from_match(&match, 0, false, 0, &target);
 -    classifier_for_each_match(&p->cls, &target, CLS_INC_ALL,
 -                              flow_stats_ds_cb, &cbdata);
 +    flow_from_match(&match, 0, false, 0, &target);
 +    wdp_flow_for_each_match(p->wdp, &target, UINT_MAX,
 +                            flow_stats_ds_cb, &cbdata);
  }
  
  struct aggregate_stats_cbdata {
      uint32_t n_flows;
  };
  
 -static void
 -aggregate_stats_cb(struct cls_rule *rule_, void *cbdata_)
 +static int
 +aggregate_stats_cb(struct wdp_rule *rule, void *cbdata_)
  {
 -    struct rule *rule = rule_from_cls_rule(rule_);
      struct aggregate_stats_cbdata *cbdata = cbdata_;
      uint64_t packet_count, byte_count;
  
      if (rule_is_hidden(rule) || !rule_has_out_port(rule, cbdata->out_port)) {
 -        return;
 +        return 0;
      }
  
      query_stats(cbdata->ofproto, rule, &packet_count, &byte_count);
      cbdata->packet_count += packet_count;
      cbdata->byte_count += byte_count;
      cbdata->n_flows++;
 +
 +    return 0;
  }
  
  static int
@@@ -1915,8 -3393,8 +1910,8 @@@ handle_aggregate_stats_request(struct o
      struct ofp_aggregate_stats_request *asr;
      struct ofp_aggregate_stats_reply *reply;
      struct aggregate_stats_cbdata cbdata;
 -    struct cls_rule target;
      struct ofpbuf *msg;
 +    flow_t target;
  
      if (arg_size != sizeof *asr) {
          return ofp_mkerr(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
      cbdata.packet_count = 0;
      cbdata.byte_count = 0;
      cbdata.n_flows = 0;
 -    cls_rule_from_match(&asr->match, 0, false, 0, &target);
 -    classifier_for_each_match(&p->cls, &target,
 -                              table_id_to_include(asr->table_id),
 -                              aggregate_stats_cb, &cbdata);
 +    flow_from_match(&asr->match, 0, false, 0, &target);
 +    wdp_flow_for_each_match(p->wdp, &target,
 +                            table_id_to_include(asr->table_id),
 +                            aggregate_stats_cb, &cbdata);
  
      msg = start_stats_reply(osr, sizeof *reply);
      reply = append_stats_reply(sizeof *reply, ofconn, &msg);
  
  struct queue_stats_cbdata {
      struct ofconn *ofconn;
 -    struct ofport *ofport;
++    struct wdp_port *wdp_port;
      struct ofpbuf *msg;
-     uint16_t port_no;
  };
  
  static void
@@@ -1956,7 -3434,7 +1951,7 @@@ put_queue_stats(struct queue_stats_cbda
      struct ofp_queue_stats *reply;
  
      reply = append_stats_reply(sizeof *reply, cbdata->ofconn, &cbdata->msg);
-     reply->port_no = htons(cbdata->port_no);
 -    reply->port_no = htons(cbdata->ofport->opp.port_no);
++    reply->port_no = htons(cbdata->wdp_port->opp.port_no);
      memset(reply->pad, 0, sizeof reply->pad);
      reply->queue_id = htonl(queue_id);
      reply->tx_bytes = htonll(stats->tx_bytes);
@@@ -1975,18 -3453,19 +1970,19 @@@ handle_queue_stats_dump_cb(uint32_t que
  }
  
  static void
 -handle_queue_stats_for_port(struct ofport *port, uint32_t queue_id,
 +handle_queue_stats_for_port(struct wdp_port *port, uint32_t queue_id,
                              struct queue_stats_cbdata *cbdata)
  {
-     cbdata->port_no = port->opp.port_no;
 -    cbdata->ofport = port;
++    cbdata->wdp_port = port;
      if (queue_id == OFPQ_ALL) {
          netdev_dump_queue_stats(port->netdev,
                                  handle_queue_stats_dump_cb, cbdata);
      } else {
          struct netdev_queue_stats stats;
  
-         netdev_get_queue_stats(port->netdev, queue_id, &stats);
-         put_queue_stats(cbdata, queue_id, &stats);
+         if (!netdev_get_queue_stats(port->netdev, queue_id, &stats)) {
+             put_queue_stats(cbdata, queue_id, &stats);
+         }
      }
  }
  
@@@ -1997,6 -3476,7 +1993,6 @@@ handle_queue_stats_request(struct ofpro
  {
      struct ofp_queue_stats_request *qsr;
      struct queue_stats_cbdata cbdata;
 -    struct ofport *port;
      unsigned int port_no;
      uint32_t queue_id;
  
      port_no = ntohs(qsr->port_no);
      queue_id = ntohl(qsr->queue_id);
      if (port_no == OFPP_ALL) {
 -        HMAP_FOR_EACH (port, hmap_node, &ofproto->ports) {
 -            handle_queue_stats_for_port(port, queue_id, &cbdata);
 +        struct wdp_port *ports;
 +        size_t n_ports, i;
 +
 +        wdp_port_list(ofproto->wdp, &ports, &n_ports);
 +        /* XXX deal with wdp_port_list() errors */
 +        for (i = 0; i < n_ports; i++) {
 +            handle_queue_stats_for_port(&ports[i], queue_id, &cbdata);
          }
 +        wdp_port_array_free(ports, n_ports);
      } else if (port_no < ofproto->max_ports) {
 -        port = get_port(ofproto, ofp_port_to_odp_port(port_no));
 -        if (port) {
 -            handle_queue_stats_for_port(port, queue_id, &cbdata);
 +        struct wdp_port port;
 +        int error;
 +
 +        error = wdp_port_query_by_number(ofproto->wdp, port_no, &port);
 +        if (!error) {
 +            handle_queue_stats_for_port(&port, queue_id, &cbdata);
 +        } else {
 +            /* XXX deal with wdp_port_query_by_number() errors */
          }
 +        wdp_port_free(&port);
      } else {
          ofpbuf_delete(cbdata.msg);
          return ofp_mkerr(OFPET_QUEUE_OP_FAILED, OFPQOFC_BAD_PORT);
@@@ -2084,6 -3552,38 +2080,6 @@@ handle_stats_request(struct ofproto *p
      }
  }
  
 -static long long int
 -msec_from_nsec(uint64_t sec, uint32_t nsec)
 -{
 -    return !sec ? 0 : sec * 1000 + nsec / 1000000;
 -}
 -
 -static void
 -update_time(struct ofproto *ofproto, struct rule *rule,
 -            const struct odp_flow_stats *stats)
 -{
 -    long long int used = msec_from_nsec(stats->used_sec, stats->used_nsec);
 -    if (used > rule->used) {
 -        rule->used = used;
 -        if (rule->super && used > rule->super->used) {
 -            rule->super->used = used;
 -        }
 -        netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, used);
 -    }
 -}
 -
 -static void
 -update_stats(struct ofproto *ofproto, struct rule *rule,
 -             const struct odp_flow_stats *stats)
 -{
 -    if (stats->n_packets) {
 -        update_time(ofproto, rule, stats);
 -        rule->packet_count += stats->n_packets;
 -        rule->byte_count += stats->n_bytes;
 -        netflow_flow_update_flags(&rule->nf_flow, stats->tcp_flags);
 -    }
 -}
 -
  /* Implements OFPFC_ADD and the cases for OFPFC_MODIFY and OFPFC_MODIFY_STRICT
   * in which no matching flow already exists in the flow table.
   *
@@@ -2097,67 -3597,63 +2093,67 @@@ static in
  add_flow(struct ofproto *p, struct ofconn *ofconn,
           const struct ofp_flow_mod *ofm, size_t n_actions)
  {
 +    struct wdp_rule *rule;
 +    struct wdp_flow_put put;
      struct ofpbuf *packet;
 -    struct rule *rule;
      uint16_t in_port;
 +    flow_t flow;
      int error;
  
 -    if (ofm->flags & htons(OFPFF_CHECK_OVERLAP)) {
 -        flow_t flow;
 -        uint32_t wildcards;
 -
 -        flow_from_match(&ofm->match, p->tun_id_from_cookie, ofm->cookie,
 -                        &flow, &wildcards);
 -        if (classifier_rule_overlaps(&p->cls, &flow, wildcards,
 -                                     ntohs(ofm->priority))) {
 -            return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
 -        }
 +    flow_from_match(&ofm->match, ntohs(ofm->priority), p->tun_id_from_cookie,
 +                    ofm->cookie, &flow);
 +    if (ofm->flags & htons(OFPFF_CHECK_OVERLAP)
 +        && wdp_flow_overlaps(p->wdp, &flow)) {
 +        return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP);
 +    }
 +
 +    put.flags = WDP_PUT_CREATE | WDP_PUT_MODIFY | WDP_PUT_ALL;
 +    put.flow = &flow;
 +    put.actions = (const union ofp_action *) ofm->actions;
 +    put.n_actions = n_actions;
 +    put.idle_timeout = ntohs(ofm->idle_timeout);
 +    put.hard_timeout = ntohs(ofm->hard_timeout);
 +    put.ofp_table_id = flow_mod_table_id(ofconn, ofm);
 +    put.cookie = ofm->cookie;
 +    put.xid = ofm->header.xid;
 +    error = wdp_flow_put(p->wdp, &put, NULL, &rule);
 +    if (error) {
 +        /* XXX wdp_flow_put should return OpenFlow error code. */
 +        return error;
      }
 +    ofproto_rule_init(rule);
  
 -    rule = rule_create(p, NULL, (const union ofp_action *) ofm->actions,
 -                       n_actions, ntohs(ofm->idle_timeout),
 -                       ntohs(ofm->hard_timeout),  ofm->cookie,
 -                       ofm->flags & htons(OFPFF_SEND_FLOW_REM));
 -    cls_rule_from_match(&ofm->match, ntohs(ofm->priority),
 -                        p->tun_id_from_cookie, ofm->cookie, &rule->cr);
 -
 -    error = 0;
      if (ofm->buffer_id != htonl(UINT32_MAX)) {
          error = pktbuf_retrieve(ofconn->pktbuf, ntohl(ofm->buffer_id),
                                  &packet, &in_port);
 -    } else {
 -        packet = NULL;
 -        in_port = UINT16_MAX;
 +        if (!error) {
 +            error = wdp_flow_inject(p->wdp, rule, in_port, packet);
 +            ofpbuf_delete(packet);
 +        }
      }
  
 -    rule_insert(p, rule, packet, in_port);
      return error;
  }
  
 -static struct rule *
 -find_flow_strict(struct ofproto *p, const struct ofp_flow_mod *ofm)
 +static struct wdp_rule *
 +find_flow_strict(struct ofproto *p, const struct ofconn *ofconn,
 +                 const struct ofp_flow_mod *ofm)
  {
 -    uint32_t wildcards;
 +    uint8_t table_id;
      flow_t flow;
  
 -    flow_from_match(&ofm->match, p->tun_id_from_cookie, ofm->cookie,
 -                    &flow, &wildcards);
 -    return rule_from_cls_rule(classifier_find_rule_exactly(
 -                                  &p->cls, &flow, wildcards,
 -                                  ntohs(ofm->priority)));
 +    flow_from_match(&ofm->match, ntohs(ofm->priority),
 +                    p->tun_id_from_cookie, ofm->cookie, &flow);
 +    table_id = flow_mod_table_id(ofconn, ofm);
 +    return wdp_flow_get(p->wdp, &flow, table_id_to_include(table_id));
  }
  
  static int
  send_buffered_packet(struct ofproto *ofproto, struct ofconn *ofconn,
 -                     struct rule *rule, const struct ofp_flow_mod *ofm)
 +                     struct wdp_rule *rule, const struct ofp_flow_mod *ofm)
  {
      struct ofpbuf *packet;
      uint16_t in_port;
 -    flow_t flow;
      int error;
  
      if (ofm->buffer_id == htonl(UINT32_MAX)) {
          return error;
      }
  
 -    flow_extract(packet, 0, in_port, &flow);
 -    rule_execute(ofproto, rule, packet, &flow);
 +    error = wdp_flow_inject(ofproto->wdp, rule, in_port, packet);
 +    ofpbuf_delete(packet);
  
 -    return 0;
 +    return error;
  }
  \f
  /* OFPFC_MODIFY and OFPFC_MODIFY_STRICT. */
@@@ -2182,16 -3678,15 +2178,16 @@@ struct modify_flows_cbdata 
      struct ofproto *ofproto;
      const struct ofp_flow_mod *ofm;
      size_t n_actions;
 -    struct rule *match;
 +    struct wdp_rule *match;
  };
  
  static int modify_flow(struct ofproto *, const struct ofp_flow_mod *,
 -                       size_t n_actions, struct rule *);
 -static void modify_flows_cb(struct cls_rule *, void *cbdata_);
 +                       size_t n_actions, struct wdp_rule *)
 +    WARN_UNUSED_RESULT;
 +static int modify_flows_cb(struct wdp_rule *, void *cbdata_);
  
 -/* Implements OFPFC_MODIFY.  Returns 0 on success or an OpenFlow error code as
 - * encoded by ofp_mkerr() on failure.
 +/* Implements OFPFC_ADD and OFPFC_MODIFY.  Returns 0 on success or an OpenFlow
 + * error code as encoded by ofp_mkerr() on failure.
   *
   * 'ofconn' is used to retrieve the packet buffer specified in ofm->buffer_id,
   * if any. */
@@@ -2200,31 -3695,24 +2196,31 @@@ modify_flows_loose(struct ofproto *p, s
                     const struct ofp_flow_mod *ofm, size_t n_actions)
  {
      struct modify_flows_cbdata cbdata;
 -    struct cls_rule target;
 +    uint8_t table_id;
 +    flow_t target;
 +    int error;
  
      cbdata.ofproto = p;
      cbdata.ofm = ofm;
      cbdata.n_actions = n_actions;
      cbdata.match = NULL;
  
 -    cls_rule_from_match(&ofm->match, 0, p->tun_id_from_cookie, ofm->cookie,
 -                        &target);
 +    flow_from_match(&ofm->match, 0, p->tun_id_from_cookie, ofm->cookie,
 +                    &target);
 +
 +    table_id = flow_mod_table_id(ofconn, ofm);
 +    error = wdp_flow_for_each_match(p->wdp, &target,
 +                                    table_id_to_include(table_id),
 +                                    modify_flows_cb, &cbdata);
 +    if (error) {
 +        return error;
 +    }
  
 -    classifier_for_each_match(&p->cls, &target, CLS_INC_ALL,
 -                              modify_flows_cb, &cbdata);
      if (cbdata.match) {
 -        /* This credits the packet to whichever flow happened to happened to
 -         * match last.  That's weird.  Maybe we should do a lookup for the
 -         * flow that actually matches the packet?  Who knows. */
 -        send_buffered_packet(p, ofconn, cbdata.match, ofm);
 -        return 0;
 +        /* This credits the packet to whichever flow happened to match last.
 +         * That's weird.  Maybe we should do a lookup for the flow that
 +         * actually matches the packet?  Who knows. */
 +        return send_buffered_packet(p, ofconn, cbdata.match, ofm);
      } else {
          return add_flow(p, ofconn, ofm, n_actions);
      }
@@@ -2239,27 -3727,26 +2235,27 @@@ static in
  modify_flow_strict(struct ofproto *p, struct ofconn *ofconn,
                     struct ofp_flow_mod *ofm, size_t n_actions)
  {
 -    struct rule *rule = find_flow_strict(p, ofm);
 +    struct wdp_rule *rule = find_flow_strict(p, ofconn, ofm);
      if (rule && !rule_is_hidden(rule)) {
 -        modify_flow(p, ofm, n_actions, rule);
 -        return send_buffered_packet(p, ofconn, rule, ofm);
 +        int error = modify_flow(p, ofm, n_actions, rule);
 +        return error ? error : send_buffered_packet(p, ofconn, rule, ofm);
      } else {
          return add_flow(p, ofconn, ofm, n_actions);
      }
  }
  
  /* Callback for modify_flows_loose(). */
 -static void
 -modify_flows_cb(struct cls_rule *rule_, void *cbdata_)
 +static int
 +modify_flows_cb(struct wdp_rule *rule, void *cbdata_)
  {
 -    struct rule *rule = rule_from_cls_rule(rule_);
      struct modify_flows_cbdata *cbdata = cbdata_;
  
      if (!rule_is_hidden(rule)) {
          cbdata->match = rule;
 -        modify_flow(cbdata->ofproto, cbdata->ofm, cbdata->n_actions, rule);
 +        return modify_flow(cbdata->ofproto, cbdata->ofm, cbdata->n_actions,
 +                           rule);
      }
 +    return 0;
  }
  
  /* Implements core of OFPFC_MODIFY and OFPFC_MODIFY_STRICT where 'rule' has
   * ofp_action[] structures). */
  static int
  modify_flow(struct ofproto *p, const struct ofp_flow_mod *ofm,
 -            size_t n_actions, struct rule *rule)
 +            size_t n_actions, struct wdp_rule *rule)
  {
 -    size_t actions_len = n_actions * sizeof *rule->actions;
 +    const struct ofp_action_header *actions = ofm->actions;
 +    struct ofproto_rule *ofproto_rule = ofproto_rule_cast(rule);
 +    struct wdp_flow_put put;
  
 -    rule->flow_cookie = ofm->cookie;
 +    ofproto_rule->flow_cookie = ofm->cookie;
  
      /* If the actions are the same, do nothing. */
      if (n_actions == rule->n_actions
 -        && !memcmp(ofm->actions, rule->actions, actions_len))
 +        && !memcmp(ofm->actions, rule->actions, sizeof *actions * n_actions))
      {
          return 0;
      }
  
 -    /* Replace actions. */
 -    free(rule->actions);
 -    rule->actions = xmemdup(ofm->actions, actions_len);
 -    rule->n_actions = n_actions;
 -
 -    /* Make sure that the datapath gets updated properly. */
 -    if (rule->cr.wc.wildcards) {
 -        COVERAGE_INC(ofproto_mod_wc_flow);
 -        p->need_revalidate = true;
 -    } else {
 -        rule_update_actions(p, rule);
 -    }
 -
 -    return 0;
 +    put.flags = WDP_PUT_MODIFY | WDP_PUT_ACTIONS;
 +    put.flow = &rule->cr.flow;
 +    put.actions = (const union ofp_action *) actions;
 +    put.n_actions = n_actions;
 +    put.idle_timeout = put.hard_timeout = 0;
 +    put.ofp_table_id = rule->ofp_table_id;
 +    put.cookie = ofm->cookie;
 +    put.xid = ofm->header.xid;
 +    return wdp_flow_put(p->wdp, &put, NULL, NULL);
  }
  \f
  /* OFPFC_DELETE implementation. */
@@@ -2301,50 -3791,44 +2297,50 @@@ struct delete_flows_cbdata 
      uint16_t out_port;
  };
  
 -static void delete_flows_cb(struct cls_rule *, void *cbdata_);
 -static void delete_flow(struct ofproto *, struct rule *, uint16_t out_port);
 +static int delete_flows_cb(struct wdp_rule *, void *cbdata_);
 +static int delete_flow_core(struct ofproto *, struct wdp_rule *,
 +                             uint16_t out_port);
  
  /* Implements OFPFC_DELETE. */
 -static void
 -delete_flows_loose(struct ofproto *p, const struct ofp_flow_mod *ofm)
 +static int
 +delete_flows_loose(struct ofproto *p, const struct ofconn *ofconn,
 +                   const struct ofp_flow_mod *ofm)
  {
      struct delete_flows_cbdata cbdata;
 -    struct cls_rule target;
 +    uint8_t table_id;
 +    flow_t target;
  
      cbdata.ofproto = p;
      cbdata.out_port = ofm->out_port;
  
 -    cls_rule_from_match(&ofm->match, 0, p->tun_id_from_cookie, ofm->cookie,
 -                        &target);
 +    flow_from_match(&ofm->match, 0, p->tun_id_from_cookie, ofm->cookie,
 +                    &target);
 +    table_id = flow_mod_table_id(ofconn, ofm);
  
 -    classifier_for_each_match(&p->cls, &target, CLS_INC_ALL,
 -                              delete_flows_cb, &cbdata);
 +    return wdp_flow_for_each_match(p->wdp, &target,
 +                                   table_id_to_include(table_id),
 +                                   delete_flows_cb, &cbdata);
  }
  
  /* Implements OFPFC_DELETE_STRICT. */
 -static void
 -delete_flow_strict(struct ofproto *p, struct ofp_flow_mod *ofm)
 +static int
 +delete_flow_strict(struct ofproto *p, const struct ofconn *ofconn,
 +                   struct ofp_flow_mod *ofm)
  {
 -    struct rule *rule = find_flow_strict(p, ofm);
 +    struct wdp_rule *rule = find_flow_strict(p, ofconn, ofm);
      if (rule) {
 -        delete_flow(p, rule, ofm->out_port);
 +        return delete_flow_core(p, rule, ofm->out_port);
      }
 +    return 0;
  }
  
  /* Callback for delete_flows_loose(). */
 -static void
 -delete_flows_cb(struct cls_rule *rule_, void *cbdata_)
 +static int
 +delete_flows_cb(struct wdp_rule *rule, void *cbdata_)
  {
 -    struct rule *rule = rule_from_cls_rule(rule_);
      struct delete_flows_cbdata *cbdata = cbdata_;
  
 -    delete_flow(cbdata->ofproto, rule, cbdata->out_port);
 +    return delete_flow_core(cbdata->ofproto, rule, cbdata->out_port);
  }
  
  /* Implements core of OFPFC_DELETE and OFPFC_DELETE_STRICT where 'rule' has
   * Will not delete 'rule' if it is hidden.  Will delete 'rule' only if
   * 'out_port' is htons(OFPP_NONE) or if 'rule' actually outputs to the
   * specified 'out_port'. */
 -static void
 -delete_flow(struct ofproto *p, struct rule *rule, uint16_t out_port)
 +static int
 +delete_flow_core(struct ofproto *p, struct wdp_rule *rule, uint16_t out_port)
  {
      if (rule_is_hidden(rule)) {
 -        return;
 +        return 0;
      }
  
      if (out_port != htons(OFPP_NONE) && !rule_has_out_port(rule, out_port)) {
 -        return;
 +        return 0;
      }
  
 -    send_flow_removed(p, rule, time_msec(), OFPRR_DELETE);
 -    rule_remove(p, rule);
 +    return delete_flow(p, rule, OFPRR_DELETE);
  }
  \f
  static int
@@@ -2424,15 -3909,7 +2420,15 @@@ handle_flow_mod(struct ofproto *p, stru
          return error;
      }
  
 -    switch (ntohs(ofm->command)) {
 +    if (!ofconn->flow_mod_table_id && ofm->command & htons(0xff00)) {
 +        static struct vlog_rate_limit table_id_rl = VLOG_RATE_LIMIT_INIT(1, 1);
 +        VLOG_WARN_RL(&table_id_rl, "%s: flow_mod table_id feature must be "
 +                     "enabled with NXT_FLOW_MOD_TABLE_ID",
 +                     rconn_get_name(ofconn->rconn));
 +        return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
 +    }
 +
 +    switch (ntohs(ofm->command) & 0xff) {
      case OFPFC_ADD:
          return add_flow(p, ofconn, ofm, n_actions);
  
          return modify_flow_strict(p, ofconn, ofm, n_actions);
  
      case OFPFC_DELETE:
 -        delete_flows_loose(p, ofm);
 -        return 0;
 +        return delete_flows_loose(p, ofconn, ofm);
  
      case OFPFC_DELETE_STRICT:
 -        delete_flow_strict(p, ofm);
 -        return 0;
 +        return delete_flow_strict(p, ofconn, ofm);
  
      default:
          return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND);
@@@ -2467,21 -3946,6 +2463,21 @@@ handle_tun_id_from_cookie(struct ofprot
      return 0;
  }
  
 +static int
 +handle_flow_mod_table_id(struct ofconn *ofconn,
 +                         struct nxt_flow_mod_table_id *msg)
 +{
 +    int error;
 +
 +    error = check_ofp_message(&msg->header, OFPT_VENDOR, sizeof *msg);
 +    if (error) {
 +        return error;
 +    }
 +
 +    ofconn->flow_mod_table_id = !!msg->set;
 +    return 0;
 +}
 +
  static int
  handle_role_request(struct ofproto *ofproto,
                      struct ofconn *ofconn, struct nicira_header *msg)
      if (role == NX_ROLE_MASTER) {
          struct ofconn *other;
  
-         HMAP_FOR_EACH (other, struct ofconn, hmap_node,
-                        &ofproto->controllers) {
+         HMAP_FOR_EACH (other, hmap_node, &ofproto->controllers) {
              if (other->role == NX_ROLE_MASTER) {
                  other->role = NX_ROLE_SLAVE;
              }
@@@ -2566,9 -4029,6 +2561,9 @@@ handle_vendor(struct ofproto *p, struc
      case NXT_TUN_ID_FROM_COOKIE:
          return handle_tun_id_from_cookie(p, msg);
  
 +    case NXT_FLOW_MOD_TABLE_ID:
 +        return handle_flow_mod_table_id(ofconn, msg);
 +
      case NXT_ROLE_REQUEST:
          return handle_role_request(p, ofconn, msg);
      }
@@@ -2658,28 -4118,41 +2653,28 @@@ handle_openflow(struct ofconn *ofconn, 
  }
  \f
  static void
 -handle_odp_miss_msg(struct ofproto *p, struct ofpbuf *packet)
 +handle_flow_miss(struct ofproto *p, struct wdp_packet *packet)
  {
 -    struct odp_msg *msg = packet->data;
 -    struct rule *rule;
 -    struct ofpbuf payload;
 +    struct wdp_rule *rule;
      flow_t flow;
  
 -    payload.data = msg + 1;
 -    payload.size = msg->length - sizeof *msg;
 -    flow_extract(&payload, msg->arg, msg->port, &flow);
 -
 -    /* Check with in-band control to see if this packet should be sent
 -     * to the local port regardless of the flow table. */
 -    if (in_band_msg_in_hook(p->in_band, &flow, &payload)) {
 -        union odp_action action;
 -
 -        memset(&action, 0, sizeof(action));
 -        action.output.type = ODPAT_OUTPUT;
 -        action.output.port = ODPP_LOCAL;
 -        dpif_execute(p->dpif, flow.in_port, &action, 1, &payload);
 -    }
 -
 -    rule = lookup_valid_rule(p, &flow);
 +    flow_extract(packet->payload, packet->tun_id, packet->in_port, &flow);
 +    rule = wdp_flow_match(p->wdp, &flow);
      if (!rule) {
          /* Don't send a packet-in if OFPPC_NO_PACKET_IN asserted. */
 -        struct ofport *port = get_port(p, msg->port);
 -        if (port) {
 -            if (port->opp.config & OFPPC_NO_PACKET_IN) {
 +        struct wdp_port port;
 +
 +        if (!wdp_port_query_by_number(p->wdp, packet->in_port, &port)) {
 +            bool no_packet_in = (port.opp.config & OFPPC_NO_PACKET_IN) != 0;
 +            wdp_port_free(&port);
 +            if (no_packet_in) {
                  COVERAGE_INC(ofproto_no_packet_in);
 -                /* XXX install 'drop' flow entry */
 -                ofpbuf_delete(packet);
 +                wdp_packet_destroy(packet);
                  return;
              }
          } else {
 -            VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16, msg->port);
 +            VLOG_WARN_RL(&rl, "packet-in on unknown port %"PRIu16,
 +                         packet->in_port);
          }
  
          COVERAGE_INC(ofproto_packet_in);
          return;
      }
  
 -    if (rule->cr.wc.wildcards) {
 -        rule = rule_create_subrule(p, rule, &flow);
 -        rule_make_actions(p, rule, packet);
 -    } else {
 -        if (!rule->may_install) {
 -            /* The rule is not installable, that is, we need to process every
 -             * packet, so process the current packet and set its actions into
 -             * 'subrule'. */
 -            rule_make_actions(p, rule, packet);
 -        } else {
 -            /* XXX revalidate rule if it needs it */
 -        }
 -    }
 +    wdp_flow_inject(p->wdp, rule, packet->in_port, packet->payload);
  
 -    if (rule->super && rule->super->cr.priority == FAIL_OPEN_PRIORITY) {
 +    if (rule->cr.flow.priority == FAIL_OPEN_PRIORITY) {
          /*
           * Extra-special case for fail-open mode.
           *
           *
           * See the top-level comment in fail-open.c for more information.
           */
 -        send_packet_in(p, ofpbuf_clone_with_headroom(packet,
 -                                                     DPIF_RECV_MSG_PADDING));
 +        send_packet_in(p, packet);
 +    } else {
 +        wdp_packet_destroy(packet);
      }
 -
 -    ofpbuf_pull(packet, sizeof *msg);
 -    rule_execute(p, rule, packet, &flow);
 -    rule_reinstall(p, rule);
  }
  
  static void
 -handle_odp_msg(struct ofproto *p, struct ofpbuf *packet)
 +handle_wdp_packet(struct ofproto *p, struct wdp_packet *packet)
  {
 -    struct odp_msg *msg = packet->data;
 -
 -    switch (msg->type) {
 -    case _ODPL_ACTION_NR:
 +    switch (packet->channel) {
 +    case WDP_CHAN_ACTION:
          COVERAGE_INC(ofproto_ctlr_action);
          send_packet_in(p, packet);
          break;
  
 -    case _ODPL_SFLOW_NR:
 -        if (p->sflow) {
 -            ofproto_sflow_received(p->sflow, msg);
 -        }
 -        ofpbuf_delete(packet);
 +    case WDP_CHAN_SFLOW:
 +        /* XXX */
 +        wdp_packet_destroy(packet);
          break;
  
 -    case _ODPL_MISS_NR:
 -        handle_odp_miss_msg(p, packet);
 +    case WDP_CHAN_MISS:
 +        handle_flow_miss(p, packet);
          break;
  
 +    case WDP_N_CHANS:
      default:
 -        VLOG_WARN_RL(&rl, "received ODP message of unexpected type %"PRIu32,
 -                     msg->type);
 +        wdp_packet_destroy(packet);
 +        VLOG_WARN_RL(&rl, "received message on unexpected channel %d",
 +                     (int) packet->channel);
          break;
      }
  }
  \f
 -static void
 -revalidate_cb(struct cls_rule *sub_, void *cbdata_)
 -{
 -    struct rule *sub = rule_from_cls_rule(sub_);
 -    struct revalidate_cbdata *cbdata = cbdata_;
 -
 -    if (cbdata->revalidate_all
 -        || (cbdata->revalidate_subrules && sub->super)
 -        || (tag_set_intersects(&cbdata->revalidate_set, sub->tags))) {
 -        revalidate_rule(cbdata->ofproto, sub);
 -    }
 -}
 -
 -static bool
 -revalidate_rule(struct ofproto *p, struct rule *rule)
 -{
 -    const flow_t *flow = &rule->cr.flow;
 -
 -    COVERAGE_INC(ofproto_revalidate_rule);
 -    if (rule->super) {
 -        struct rule *super;
 -        super = rule_from_cls_rule(classifier_lookup_wild(&p->cls, flow));
 -        if (!super) {
 -            rule_remove(p, rule);
 -            return false;
 -        } else if (super != rule->super) {
 -            COVERAGE_INC(ofproto_revalidate_moved);
 -            list_remove(&rule->list);
 -            list_push_back(&super->list, &rule->list);
 -            rule->super = super;
 -            rule->hard_timeout = super->hard_timeout;
 -            rule->idle_timeout = super->idle_timeout;
 -            rule->created = super->created;
 -            rule->used = 0;
 -        }
 -    }
 -
 -    rule_update_actions(p, rule);
 -    return true;
 -}
 -
  static struct ofpbuf *
 -compose_flow_removed(struct ofproto *p, const struct rule *rule,
 -                     long long int now, uint8_t reason)
 +compose_flow_removed(struct ofproto *p, const struct wdp_rule *rule,
 +                     uint8_t reason)
  {
 -    struct ofp_flow_removed *ofr;
 -    struct ofpbuf *buf;
 -    long long int tdiff = now - rule->created;
 +    long long int tdiff = time_msec() - rule->created;
      uint32_t sec = tdiff / 1000;
      uint32_t msec = tdiff - (sec * 1000);
 +    struct ofp_flow_removed *ofr;
 +    struct ofpbuf *buf;
  
      ofr = make_openflow(sizeof *ofr, OFPT_FLOW_REMOVED, &buf);
 -    flow_to_match(&rule->cr.flow, rule->cr.wc.wildcards, p->tun_id_from_cookie,
 -                  &ofr->match);
 -    ofr->cookie = rule->flow_cookie;
 -    ofr->priority = htons(rule->cr.priority);
 +    flow_to_match(&rule->cr.flow, p->tun_id_from_cookie, &ofr->match);
 +    ofr->cookie = ofproto_rule_cast(rule)->flow_cookie;
 +    ofr->priority = htons(rule->cr.flow.priority);
      ofr->reason = reason;
      ofr->duration_sec = htonl(sec);
      ofr->duration_nsec = htonl(msec * 1000000);
      ofr->idle_timeout = htons(rule->idle_timeout);
 -    ofr->packet_count = htonll(rule->packet_count);
 -    ofr->byte_count = htonll(rule->byte_count);
  
      return buf;
  }
  
 -static void
 -uninstall_idle_flow(struct ofproto *ofproto, struct rule *rule)
 -{
 -    assert(rule->installed);
 -    assert(!rule->cr.wc.wildcards);
 -
 -    if (rule->super) {
 -        rule_remove(ofproto, rule);
 -    } else {
 -        rule_uninstall(ofproto, rule);
 -    }
 -}
 -
 -static void
 -send_flow_removed(struct ofproto *p, struct rule *rule,
 -                  long long int now, uint8_t reason)
 +static int
 +delete_flow(struct ofproto *p, struct wdp_rule *rule, uint8_t reason)
  {
 -    struct ofconn *ofconn;
 -    struct ofconn *prev;
 -    struct ofpbuf *buf = NULL;
 -
      /* We limit the maximum number of queued flow expirations it by accounting
       * them under the counter for replies.  That works because preventing
       * OpenFlow requests from being processed also prevents new flows from
       * being added (and expiring).  (It also prevents processing OpenFlow
       * requests that would not add new flows, so it is imperfect.) */
  
 -    prev = NULL;
 -    LIST_FOR_EACH (ofconn, node, &p->all_conns) {
 -        if (rule->send_flow_removed && rconn_is_connected(ofconn->rconn)
 -            && ofconn_receives_async_msgs(ofconn)) {
 -            if (prev) {
 -                queue_tx(ofpbuf_clone(buf), prev, prev->reply_counter);
 -            } else {
 -                buf = compose_flow_removed(p, rule, now, reason);
 -            }
 -            prev = ofconn;
 -        }
 -    }
 -    if (prev) {
 -        queue_tx(buf, prev, prev->reply_counter);
 -    }
 -}
 -
 -
 -static void
 -expire_rule(struct cls_rule *cls_rule, void *p_)
 -{
 -    struct ofproto *p = p_;
 -    struct rule *rule = rule_from_cls_rule(cls_rule);
 -    long long int hard_expire, idle_expire, expire, now;
 -
 -    hard_expire = (rule->hard_timeout
 -                   ? rule->created + rule->hard_timeout * 1000
 -                   : LLONG_MAX);
 -    idle_expire = (rule->idle_timeout
 -                   && (rule->super || list_is_empty(&rule->list))
 -                   ? rule->used + rule->idle_timeout * 1000
 -                   : LLONG_MAX);
 -    expire = MIN(hard_expire, idle_expire);
 -
 -    now = time_msec();
 -    if (now < expire) {
 -        if (rule->installed && now >= rule->used + 5000) {
 -            uninstall_idle_flow(p, rule);
 -        } else if (!rule->cr.wc.wildcards) {
 -            active_timeout(p, rule);
 -        }
 -
 -        return;
 -    }
 -
 -    COVERAGE_INC(ofproto_expired);
 +    struct ofproto_rule *ofproto_rule = ofproto_rule_cast(rule);
 +    struct wdp_flow_stats stats;
 +    struct ofpbuf *buf;
 +    int error;
  
 -    /* Update stats.  This code will be a no-op if the rule expired
 -     * due to an idle timeout. */
 -    if (rule->cr.wc.wildcards) {
 -        struct rule *subrule, *next;
 -        LIST_FOR_EACH_SAFE (subrule, next, list, &rule->list) {
 -            rule_remove(p, subrule);
 -        }
 +    if (ofproto_rule->send_flow_removed) {
 +        /* Compose most of the ofp_flow_removed before 'rule' is destroyed. */
 +        buf = compose_flow_removed(p, rule, reason);
      } else {
 -        rule_uninstall(p, rule);
 -    }
 -
 -    if (!rule_is_hidden(rule)) {
 -        send_flow_removed(p, rule, now,
 -                          (now >= hard_expire
 -                           ? OFPRR_HARD_TIMEOUT : OFPRR_IDLE_TIMEOUT));
 -    }
 -    rule_remove(p, rule);
 -}
 -
 -static void
 -active_timeout(struct ofproto *ofproto, struct rule *rule)
 -{
 -    if (ofproto->netflow && !is_controller_rule(rule) &&
 -        netflow_active_timeout_expired(ofproto->netflow, &rule->nf_flow)) {
 -        struct ofexpired expired;
 -        struct odp_flow odp_flow;
 -
 -        /* Get updated flow stats. */
 -        memset(&odp_flow, 0, sizeof odp_flow);
 -        if (rule->installed) {
 -            odp_flow.key = rule->cr.flow;
 -            odp_flow.flags = ODPFF_ZERO_TCP_FLAGS;
 -            dpif_flow_get(ofproto->dpif, &odp_flow);
 -
 -            if (odp_flow.stats.n_packets) {
 -                update_time(ofproto, rule, &odp_flow.stats);
 -                netflow_flow_update_flags(&rule->nf_flow,
 -                                          odp_flow.stats.tcp_flags);
 -            }
 -        }
 -
 -        expired.flow = rule->cr.flow;
 -        expired.packet_count = rule->packet_count +
 -                               odp_flow.stats.n_packets;
 -        expired.byte_count = rule->byte_count + odp_flow.stats.n_bytes;
 -        expired.used = rule->used;
 -
 -        netflow_expire(ofproto->netflow, &rule->nf_flow, &expired);
 -
 -        /* Schedule us to send the accumulated records once we have
 -         * collected all of them. */
 -        poll_immediate_wake();
 +        buf = NULL;
      }
 -}
 -
 -static void
 -update_used(struct ofproto *p)
 -{
 -    struct odp_flow *flows;
 -    size_t n_flows;
 -    size_t i;
 -    int error;
  
 -    error = dpif_flow_list_all(p->dpif, &flows, &n_flows);
 +    error = wdp_flow_delete(p->wdp, rule, &stats);
      if (error) {
 -        return;
 +        return error;
      }
  
 -    for (i = 0; i < n_flows; i++) {
 -        struct odp_flow *f = &flows[i];
 -        struct rule *rule;
 +    if (buf) {
 +        struct ofp_flow_removed *ofr;
 +        struct ofconn *prev = NULL;
 +        struct ofconn *ofconn;
  
 -        rule = rule_from_cls_rule(
 -            classifier_find_rule_exactly(&p->cls, &f->key, 0, UINT16_MAX));
 -        if (!rule || !rule->installed) {
 -            COVERAGE_INC(ofproto_unexpected_rule);
 -            dpif_flow_del(p->dpif, f);
 -            continue;
 -        }
 +        /* Compose the parts of the ofp_flow_removed that require stats. */
 +        ofr = buf->data;
 +        ofr->packet_count = htonll(stats.n_packets);
 +        ofr->byte_count = htonll(stats.n_bytes);
  
-         LIST_FOR_EACH (ofconn, struct ofconn, node, &p->all_conns) {
 -        update_time(p, rule, &f->stats);
 -        rule_account(p, rule, f->stats.n_bytes);
++        LIST_FOR_EACH (ofconn, node, &p->all_conns) {
 +            if (rconn_is_connected(ofconn->rconn)) {
 +                if (prev) {
 +                    queue_tx(ofpbuf_clone(buf), prev, prev->reply_counter);
 +                }
 +                prev = ofconn;
 +            }
 +        }
 +        if (prev) {
 +            queue_tx(buf, prev, prev->reply_counter);
 +        } else {
 +            ofpbuf_delete(buf);
 +        }
      }
 -    free(flows);
 +    free(ofproto_rule);
 +
 +    return 0;
  }
  
  /* pinsched callback for sending 'packet' on 'ofconn'. */
  static void
 -do_send_packet_in(struct ofpbuf *packet, void *ofconn_)
 +do_send_packet_in(struct wdp_packet *packet, void *ofconn_)
  {
      struct ofconn *ofconn = ofconn_;
  
 -    rconn_send_with_limit(ofconn->rconn, packet,
 +    rconn_send_with_limit(ofconn->rconn, packet->payload,
                            ofconn->packet_in_counter, 100);
 +    packet->payload = NULL;
 +    wdp_packet_destroy(packet);
  }
  
  /* Takes 'packet', which has been converted with do_convert_to_packet_in(), and
   * If 'clone' is true, the caller retains ownership of 'packet'.  Otherwise,
   * ownership is transferred to this function. */
  static void
 -schedule_packet_in(struct ofconn *ofconn, struct ofpbuf *packet, int max_len,
 -                   bool clone)
 +schedule_packet_in(struct ofconn *ofconn, struct wdp_packet *packet,
 +                   int max_len, bool clone)
  {
      struct ofproto *ofproto = ofconn->ofproto;
 -    struct ofp_packet_in *opi = packet->data;
 -    uint16_t in_port = ofp_port_to_odp_port(ntohs(opi->in_port));
 +    struct ofp_packet_in *opi = packet->payload->data;
      int send_len, trim_size;
      uint32_t buffer_id;
  
      } else {
          struct ofpbuf payload;
          payload.data = opi->data;
 -        payload.size = packet->size - offsetof(struct ofp_packet_in, data);
 -        buffer_id = pktbuf_save(ofconn->pktbuf, &payload, in_port);
 +        payload.size = (packet->payload->size
 +                        - offsetof(struct ofp_packet_in, data));
 +        buffer_id = pktbuf_save(ofconn->pktbuf, &payload, packet->in_port);
      }
  
      /* Figure out how much of the packet to send. */
      /* Adjust packet length and clone if necessary. */
      trim_size = offsetof(struct ofp_packet_in, data) + send_len;
      if (clone) {
 -        packet = ofpbuf_clone_data(packet->data, trim_size);
 -        opi = packet->data;
 +        packet = wdp_packet_clone(packet, trim_size);
 +        opi = packet->payload->data;
      } else {
 -        packet->size = trim_size;
 +        packet->payload->size = trim_size;
      }
  
      /* Update packet headers. */
      opi->buffer_id = htonl(buffer_id);
 -    update_openflow_length(packet);
 +    update_openflow_length(packet->payload);
  
      /* Hand over to packet scheduler.  It might immediately call into
       * do_send_packet_in() or it might buffer it for a while (until a later
       * call to pinsched_run()). */
 -    pinsched_send(ofconn->schedulers[opi->reason], in_port,
 +    pinsched_send(ofconn->schedulers[opi->reason], packet->in_port,
                    packet, do_send_packet_in, ofconn);
  }
  
 -/* Replace struct odp_msg header in 'packet' by equivalent struct
 - * ofp_packet_in.  The odp_msg must have sufficient headroom to do so (e.g. as
 - * returned by dpif_recv()).
 +/* Converts 'packet->payload' to a struct ofp_packet_in.  It must have
 + * sufficient headroom to do so (e.g. as returned by xfif_recv()).
   *
   * The conversion is not complete: the caller still needs to trim any unneeded
   * payload off the end of the buffer, set the length in the OpenFlow header,
   * Returns the maximum number of bytes of the packet that should be sent to
   * the controller (INT_MAX if no limit). */
  static int
 -do_convert_to_packet_in(struct ofpbuf *packet)
 +do_convert_to_packet_in(struct wdp_packet *packet)
  {
 -    struct odp_msg *msg = packet->data;
 +    uint16_t total_len = packet->payload->size;
      struct ofp_packet_in *opi;
 -    uint8_t reason;
 -    uint16_t total_len;
 -    uint16_t in_port;
 -    int max_len;
 -
 -    /* Extract relevant header fields */
 -    if (msg->type == _ODPL_ACTION_NR) {
 -        reason = OFPR_ACTION;
 -        max_len = msg->arg;
 -    } else {
 -        reason = OFPR_NO_MATCH;
 -        max_len = INT_MAX;
 -    }
 -    total_len = msg->length - sizeof *msg;
 -    in_port = odp_port_to_ofp_port(msg->port);
  
      /* Repurpose packet buffer by overwriting header. */
 -    ofpbuf_pull(packet, sizeof(struct odp_msg));
 -    opi = ofpbuf_push_zeros(packet, offsetof(struct ofp_packet_in, data));
 +    opi = ofpbuf_push_zeros(packet->payload,
 +                            offsetof(struct ofp_packet_in, data));
      opi->header.version = OFP_VERSION;
      opi->header.type = OFPT_PACKET_IN;
      opi->total_len = htons(total_len);
 -    opi->in_port = htons(in_port);
 -    opi->reason = reason;
 -
 -    return max_len;
 +    opi->in_port = htons(packet->in_port);
 +    if (packet->channel == WDP_CHAN_MISS) {
 +        opi->reason = OFPR_NO_MATCH;
 +        return INT_MAX;
 +    } else {
 +        opi->reason = OFPR_ACTION;
 +        return packet->send_len;
 +    }
  }
  
 -/* Given 'packet' containing an odp_msg of type _ODPL_ACTION_NR or
 - * _ODPL_MISS_NR, sends an OFPT_PACKET_IN message to each OpenFlow controller
 - * as necessary according to their individual configurations.
 +/* Given 'packet' with channel WDP_CHAN_ACTION or WDP_CHAN_MISS, sends an
 + * OFPT_PACKET_IN message to each OpenFlow controller as necessary according to
 + * their individual configurations.
   *
 - * 'packet' must have sufficient headroom to convert it into a struct
 + * 'packet->payload' must have sufficient headroom to convert it into a struct
   * ofp_packet_in (e.g. as returned by dpif_recv()).
   *
   * Takes ownership of 'packet'. */
  static void
 -send_packet_in(struct ofproto *ofproto, struct ofpbuf *packet)
 +send_packet_in(struct ofproto *ofproto, struct wdp_packet *packet)
  {
      struct ofconn *ofconn, *prev;
      int max_len;
      max_len = do_convert_to_packet_in(packet);
  
      prev = NULL;
-     LIST_FOR_EACH (ofconn, struct ofconn, node, &ofproto->all_conns) {
+     LIST_FOR_EACH (ofconn, node, &ofproto->all_conns) {
          if (ofconn_receives_async_msgs(ofconn)) {
              if (prev) {
                  schedule_packet_in(prev, packet, max_len, true);
      if (prev) {
          schedule_packet_in(prev, packet, max_len, false);
      } else {
 -        ofpbuf_delete(packet);
 +        wdp_packet_destroy(packet);
      }
  }
  
  static uint64_t
  pick_datapath_id(const struct ofproto *ofproto)
  {
 -    const struct ofport *port;
 +    struct wdp_port port;
  
 -    port = get_port(ofproto, ODPP_LOCAL);
 -    if (port) {
 +    if (!wdp_port_query_by_number(ofproto->wdp, OFPP_LOCAL, &port)) {
          uint8_t ea[ETH_ADDR_LEN];
          int error;
  
 -        error = netdev_get_etheraddr(port->netdev, ea);
 +        error = netdev_get_etheraddr(port.netdev, ea);
          if (!error) {
 +            wdp_port_free(&port);
              return eth_addr_to_uint64(ea);
          }
          VLOG_WARN("could not get MAC address for %s (%s)",
 -                  netdev_get_name(port->netdev), strerror(error));
 +                  netdev_get_name(port.netdev), strerror(error));
 +        wdp_port_free(&port);
      }
 +
      return ofproto->fallback_dpid;
  }
  
@@@ -2975,3 -4622,53 +2970,3 @@@ pick_fallback_dpid(void
      eth_addr_nicira_random(ea);
      return eth_addr_to_uint64(ea);
  }
 -\f
 -static bool
 -default_normal_ofhook_cb(const flow_t *flow, const struct ofpbuf *packet,
 -                         struct odp_actions *actions, tag_type *tags,
 -                         uint16_t *nf_output_iface, void *ofproto_)
 -{
 -    struct ofproto *ofproto = ofproto_;
 -    int out_port;
 -
 -    /* Drop frames for reserved multicast addresses. */
 -    if (eth_addr_is_reserved(flow->dl_dst)) {
 -        return true;
 -    }
 -
 -    /* Learn source MAC (but don't try to learn from revalidation). */
 -    if (packet != NULL) {
 -        tag_type rev_tag = mac_learning_learn(ofproto->ml, flow->dl_src,
 -                                              0, flow->in_port,
 -                                              GRAT_ARP_LOCK_NONE);
 -        if (rev_tag) {
 -            /* The log messages here could actually be useful in debugging,
 -             * so keep the rate limit relatively high. */
 -            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
 -            VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
 -                        ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
 -            ofproto_revalidate(ofproto, rev_tag);
 -        }
 -    }
 -
 -    /* Determine output port. */
 -    out_port = mac_learning_lookup_tag(ofproto->ml, flow->dl_dst, 0, tags,
 -                                       NULL);
 -    if (out_port < 0) {
 -        add_output_group_action(actions, DP_GROUP_FLOOD, nf_output_iface);
 -    } else if (out_port != flow->in_port) {
 -        odp_actions_add(actions, ODPAT_OUTPUT)->output.port = out_port;
 -        *nf_output_iface = out_port;
 -    } else {
 -        /* Drop. */
 -    }
 -
 -    return true;
 -}
 -
 -static const struct ofhooks default_ofhooks = {
 -    NULL,
 -    default_normal_ofhook_cb,
 -    NULL,
 -    NULL
 -};
diff --combined ofproto/pktbuf.c
@@@ -21,7 -21,6 +21,7 @@@
  #include "coverage.h"
  #include "ofp-util.h"
  #include "ofpbuf.h"
 +#include "openflow/openflow.h"
  #include "timeval.h"
  #include "util.h"
  #include "vconn.h"
@@@ -113,8 -112,8 +113,8 @@@ pktbuf_save(struct pktbuf *pb, struct o
      if (++p->cookie >= COOKIE_MAX) {
          p->cookie = 0;
      }
-     p->buffer = ofpbuf_new(sizeof(struct ofp_packet_in) + buffer->size);
-     ofpbuf_reserve(p->buffer, sizeof(struct ofp_packet_in));
+     p->buffer = ofpbuf_new_with_headroom(buffer->size,
+                                          sizeof(struct ofp_packet_in));
      ofpbuf_put(p->buffer, buffer->data, buffer->size);
      p->timeout = time_msec() + OVERWRITE_MSECS;
      p->in_port = in_port;
@@@ -155,7 -154,7 +155,7 @@@ pktbuf_get_null(void
   * datapath port number on which the packet was received in '*in_port'.  The
   * caller becomes responsible for freeing the buffer.  However, if 'id'
   * identifies a "null" packet buffer (created with pktbuf_get_null()), stores
 - * NULL in '*bufferp' and UINT16_max in '*in_port'.
 + * NULL in '*bufferp' and OFPP_NONE in '*in_port'.
   *
   * A returned packet will have at least sizeof(struct ofp_packet_in) bytes of
   * headroom.
@@@ -201,7 -200,7 +201,7 @@@ pktbuf_retrieve(struct pktbuf *pb, uint
          error = 0;
      }
      *bufferp = NULL;
 -    *in_port = UINT16_MAX;
 +    *in_port = OFPP_NONE;
      return error;
  }
  
diff --combined ofproto/wdp-xflow.c
index ce7ff2f,0000000..26830e6
mode 100644,000000..100644
--- /dev/null
@@@ -1,2659 -1,0 +1,2724 @@@
-     struct port_array ports;    /* Index is xflow port nr;
-                                  * wdp_port->opp.port_no is OFP port nr. */
 +/*
 + * Copyright (c) 2010 Nicira Networks.
 + *
 + * Licensed under the Apache License, Version 2.0 (the "License");
 + * you may not use this file except in compliance with the License.
 + * You may obtain a copy of the License at:
 + *
 + *     http://www.apache.org/licenses/LICENSE-2.0
 + *
 + * Unless required by applicable law or agreed to in writing, software
 + * distributed under the License is distributed on an "AS IS" BASIS,
 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 + * See the License for the specific language governing permissions and
 + * limitations under the License.
 + */
 +
 +#include <config.h>
 +
 +#include "wdp-xflow.h"
 +
 +#include <errno.h>
 +#include <inttypes.h>
 +
 +#include "coverage.h"
 +#include "dhcp.h"
 +#include "mac-learning.h"
 +#include "netdev.h"
 +#include "netflow.h"
 +#include "ofp-util.h"
 +#include "ofpbuf.h"
 +#include "ofproto.h"
 +#include "openflow/nicira-ext.h"
 +#include "openflow/openflow.h"
 +#include "packets.h"
 +#include "poll-loop.h"
 +#include "port-array.h"
 +#include "queue.h"
 +#include "shash.h"
 +#include "svec.h"
 +#include "timeval.h"
 +#include "util.h"
 +#include "vconn.h"
 +#include "wdp-provider.h"
 +#include "xfif.h"
 +#include "xflow-util.h"
 +#include "vlog.h"
 +#include "xtoxll.h"
 +
 +VLOG_DEFINE_THIS_MODULE(wdp_xflow)
 +
 +enum {
 +    TABLEID_HASH = 0,
 +    TABLEID_CLASSIFIER = 1
 +};
 +
 +static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
 +\f
 +/* Maximum numbers of rules. */
 +#define WX_MAX_WILD     65536   /* Wildcarded rules. */
 +#define WX_MAX_EXACT    1048576 /* Exact-match rules. */
 +
++struct wx_port {
++    struct hmap_node hmap_node;
++    struct wdp_port wdp_port;
++    uint16_t xflow_port;
++};
++
 +struct wx {
 +    struct list list_node;
 +    struct wdp wdp;
 +    struct xfif *xfif;
 +    struct classifier cls;
 +    struct netdev_monitor *netdev_monitor;
-         LIST_FOR_EACH_SAFE (subrule, next, struct wx_rule, list, &rule->list) {
++    struct hmap ports;          /* Contains "struct wx_port"s. */
 +    struct shash port_by_name;
 +    long long int next_expiration;
 +    int wdp_listen_mask;
 +
 +    /* Rules that might need to be revalidated. */
 +    bool need_revalidate;      /* Revalidate all subrules? */
 +    bool revalidate_all;       /* Revalidate all subrules and other rules? */
 +    struct tag_set revalidate_set; /* Tag set of (sub)rules to revalidate. */
 +
 +    /* Hooks for ovs-vswitchd. */
 +    const struct ofhooks *ofhooks;
 +    void *aux;
 +
 +    /* Used by default ofhooks. */
 +    struct mac_learning *ml;
 +
 +    /* List of "struct wdp_packets" queued for the controller by
 +     * execute_xflow_actions(). */
 +#define MAX_CTL_PACKETS 50
 +    struct list ctl_packets;
 +    int n_ctl_packets;
 +};
 +
 +static const struct ofhooks default_ofhooks;
 +
 +static struct list all_wx = LIST_INITIALIZER(&all_wx);
 +
 +static int wx_port_init(struct wx *);
++static struct wx_port *wx_port_get(const struct wx *, uint16_t xflow_port);
 +static void wx_port_process_change(struct wx *wx, int error, char *devname,
 +                                   wdp_port_poll_cb_func *cb, void *aux);
 +static void wx_port_refresh_groups(struct wx *);
 +
 +static void wx_purge_ctl_packets__(struct wx *);
 +
 +enum {
 +    WX_GROUP_FLOOD = 0,
 +    WX_GROUP_ALL = 1
 +};
 +
 +static struct wx *
 +wx_cast(const struct wdp *wdp)
 +{
 +    return CONTAINER_OF(wdp, struct wx, wdp);
 +}
 +
 +static int
 +wx_xlate_actions(struct wx *, const union ofp_action *, size_t n,
 +                 const flow_t *flow, const struct ofpbuf *packet,
 +                 tag_type *tags, struct xflow_actions *out,
 +                 bool *may_set_up_flow);
 +\f
 +struct wx_rule {
 +    struct wdp_rule wr;
 +
 +    uint64_t packet_count;      /* Number of packets received. */
 +    uint64_t byte_count;        /* Number of bytes received. */
 +    uint64_t accounted_bytes;   /* Number of bytes passed to account_cb. */
 +    long long int used;         /* Last-used time (0 if never used). */
 +    tag_type tags;              /* Tags (set only by hooks). */
 +
 +    /* If 'super' is non-NULL, this rule is a subrule, that is, it is an
 +     * exact-match rule (having cr.wc.wildcards of 0) generated from the
 +     * wildcard rule 'super'.  In this case, 'list' is an element of the
 +     * super-rule's list.
 +     *
 +     * If 'super' is NULL, this rule is a super-rule, and 'list' is the head of
 +     * a list of subrules.  A super-rule with no wildcards (where
 +     * cr.wc.wildcards is 0) will never have any subrules. */
 +    struct wx_rule *super;
 +    struct list list;
 +
 +    /* Datapath actions.
 +     *
 +     * A super-rule with wildcard fields never has xflow actions (since the
 +     * datapath only supports exact-match flows). */
 +    bool installed;             /* Installed in datapath? */
 +    bool may_install;           /* True ordinarily; false if actions must
 +                                 * be reassessed for every packet. */
 +    int n_xflow_actions;
 +    union xflow_action *xflow_actions;
 +};
 +
 +static void wx_rule_destroy(struct wx *, struct wx_rule *);
 +static void wx_rule_update_actions(struct wx *, struct wx_rule *);
 +static void wx_rule_execute(struct wx *, struct wx_rule *,
 +                            struct ofpbuf *packet, const flow_t *);
 +static bool wx_rule_make_actions(struct wx *, struct wx_rule *,
 +                                 const struct ofpbuf *packet);
 +static void wx_rule_install(struct wx *, struct wx_rule *,
 +                            struct wx_rule *displaced_rule);
 +
 +static struct wx_rule *
 +wx_rule_cast(const struct cls_rule *cls_rule)
 +{
 +    return cls_rule ? CONTAINER_OF(cls_rule, struct wx_rule, wr.cr) : NULL;
 +}
 +
 +/* Returns true if 'rule' is merely an implementation detail that should be
 + * hidden from the client. */
 +static inline bool
 +wx_rule_is_hidden(const struct wx_rule *rule)
 +{
 +    return rule->super != NULL;
 +}
 +
 +static void
 +wx_rule_free(struct wx_rule *rule)
 +{
 +    wdp_rule_uninit(&rule->wr);
 +    free(rule->xflow_actions);
 +    free(rule);
 +}
 +
 +static void
 +wx_rule_account(struct wx *wx OVS_UNUSED, struct wx_rule *rule OVS_UNUSED,
 +                uint64_t extra_bytes OVS_UNUSED)
 +{
 +    /* XXX call account_cb hook */
 +}
 +
 +static void
 +wx_rule_post_uninstall(struct wx *wx, struct wx_rule *rule)
 +{
 +    struct wx_rule *super = rule->super;
 +
 +    wx_rule_account(wx, rule, 0);
 +
 +    /* XXX netflow expiration */
 +
 +    if (super) {
 +        super->packet_count += rule->packet_count;
 +        super->byte_count += rule->byte_count;
 +
 +        /* Reset counters to prevent double counting if the rule ever gets
 +         * reinstalled. */
 +        rule->packet_count = 0;
 +        rule->byte_count = 0;
 +        rule->accounted_bytes = 0;
 +
 +        //XXX netflow_flow_clear(&rule->nf_flow);
 +    }
 +}
 +
 +static long long int
 +xflow_flow_stats_to_msec(const struct xflow_flow_stats *stats)
 +{
 +    return (stats->used_sec
 +            ? stats->used_sec * 1000 + stats->used_nsec / 1000000
 +            : 0);
 +}
 +
 +static void
 +wx_rule_update_time(struct wx *wx OVS_UNUSED, struct wx_rule *rule,
 +                    const struct xflow_flow_stats *stats)
 +{
 +    long long int used = xflow_flow_stats_to_msec(stats);
 +    if (used > rule->used) {
 +        rule->used = used;
 +        if (rule->super && used > rule->super->used) {
 +            rule->super->used = used;
 +        }
 +        //XXX netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, used);
 +    }
 +}
 +
 +static void
 +wx_rule_update_stats(struct wx *wx, struct wx_rule *rule,
 +                     const struct xflow_flow_stats *stats)
 +{
 +    if (stats->n_packets) {
 +        wx_rule_update_time(wx, rule, stats);
 +        rule->packet_count += stats->n_packets;
 +        rule->byte_count += stats->n_bytes;
 +        /* XXX netflow_flow_update_flags(&rule->nf_flow, stats->tcp_flags); */
 +    }
 +}
 +
 +static void
 +wx_rule_uninstall(struct wx *wx, struct wx_rule *rule)
 +{
 +    assert(!rule->wr.cr.flow.wildcards);
 +    if (rule->installed) {
 +        struct xflow_flow xflow_flow;
 +
 +        xflow_key_from_flow(&xflow_flow.key, &rule->wr.cr.flow);
 +        xflow_flow.actions = NULL;
 +        xflow_flow.n_actions = 0;
 +        xflow_flow.flags = 0;
 +        if (!xfif_flow_del(wx->xfif, &xflow_flow)) {
 +            wx_rule_update_stats(wx, rule, &xflow_flow.stats);
 +        }
 +        rule->installed = false;
 +
 +        wx_rule_post_uninstall(wx, rule);
 +    }
 +}
 +
 +#if 0
 +static bool
 +is_controller_rule(struct wx_rule *rule)
 +{
 +    /* If the only action is send to the controller then don't report
 +     * NetFlow expiration messages since it is just part of the control
 +     * logic for the network and not real traffic. */
 +
 +    return (rule
 +            && rule->super
 +            && rule->super->n_actions == 1
 +            && action_outputs_to_port(&rule->super->actions[0],
 +                                      htons(OFPP_CONTROLLER)));
 +}
 +#endif
 +
 +static void
 +wx_rule_remove(struct wx *wx, struct wx_rule *rule)
 +{
 +    if (rule->wr.cr.flow.wildcards) {
 +        COVERAGE_INC(wx_del_wc_flow);
 +        wx->need_revalidate = true;
 +    } else {
 +        wx_rule_uninstall(wx, rule);
 +    }
 +    classifier_remove(&wx->cls, &rule->wr.cr);
 +    wx_rule_destroy(wx, rule);
 +}
 +
 +static bool
 +wx_rule_revalidate(struct wx *wx, struct wx_rule *rule)
 +{
 +    const flow_t *flow = &rule->wr.cr.flow;
 +
 +    COVERAGE_INC(wx_rule_revalidate);
 +    if (rule->super) {
 +        struct wx_rule *super;
 +        super = wx_rule_cast(classifier_lookup_wild(&wx->cls, flow));
 +        if (!super) {
 +            wx_rule_remove(wx, rule);
 +            return false;
 +        } else if (super != rule->super) {
 +            COVERAGE_INC(wx_revalidate_moved);
 +            list_remove(&rule->list);
 +            list_push_back(&super->list, &rule->list);
 +            rule->super = super;
 +            rule->wr.hard_timeout = super->wr.hard_timeout;
 +            rule->wr.idle_timeout = super->wr.idle_timeout;
 +            rule->wr.created = super->wr.created;
 +            rule->used = 0;
 +        }
 +    }
 +
 +    wx_rule_update_actions(wx, rule);
 +    return true;
 +}
 +
 +/* Destroys 'rule'.  If 'rule' is a subrule, also removes it from its
 + * super-rule's list of subrules.  If 'rule' is a super-rule, also iterates
 + * through all of its subrules and revalidates them, destroying any that no
 + * longer has a super-rule (which is probably all of them).
 + *
 + * Before calling this function, the caller must make have removed 'rule' from
 + * the classifier.  If 'rule' is an exact-match rule, the caller is also
 + * responsible for ensuring that it has been uninstalled from the datapath. */
 +static void
 +wx_rule_destroy(struct wx *wx, struct wx_rule *rule)
 +{
 +    if (!rule->super) {
 +        struct wx_rule *subrule, *next;
-     const struct wdp_port *wdp_port = port_array_get(&ctx->wx->ports, port);
++        LIST_FOR_EACH_SAFE (subrule, next, list, &rule->list) {
 +            wx_rule_revalidate(wx, subrule);
 +        }
 +    } else {
 +        list_remove(&rule->list);
 +    }
 +    wx_rule_free(rule);
 +}
 +
 +#if 0
 +static bool
 +wx_rule_has_out_port(const struct wx_rule *rule, uint16_t out_port)
 +{
 +    const union ofp_action *oa;
 +    struct actions_iterator i;
 +
 +    if (out_port == htons(OFPP_NONE)) {
 +        return true;
 +    }
 +    for (oa = actions_first(&i, rule->wr.actions,
 +                            rule->wr.n_actions);
 +         oa;
 +         oa = actions_next(&i)) {
 +        if (oa->type == htons(OFPAT_OUTPUT) && oa->output.port == out_port) {
 +            return true;
 +        }
 +    }
 +    return false;
 +}
 +#endif
 +
 +/* Caller is responsible for initializing the 'cr' and ofp_table_id members of
 + * the returned rule. */
 +static struct wx_rule *
 +wx_rule_create(struct wx_rule *super,
 +               const union ofp_action *actions, size_t n_actions,
 +               uint16_t idle_timeout, uint16_t hard_timeout)
 +{
 +    struct wx_rule *rule = xzalloc(sizeof *rule);
 +    wdp_rule_init(&rule->wr, actions, n_actions);
 +    rule->wr.idle_timeout = idle_timeout;
 +    rule->wr.hard_timeout = hard_timeout;
 +    rule->used = rule->wr.created;
 +    rule->super = super;
 +    if (super) {
 +        list_push_back(&super->list, &rule->list);
 +    } else {
 +        list_init(&rule->list);
 +    }
 +#if 0
 +    netflow_flow_clear(&rule->nf_flow);
 +    netflow_flow_update_time(ofproto->netflow, &rule->nf_flow, rule->created);
 +#endif
 +
 +    return rule;
 +}
 +
 +/* Executes, within 'wx', the 'n_actions' actions in 'actions' on 'packet',
 + * which arrived on 'in_port'.
 + *
 + * Takes ownership of 'packet'. */
 +static bool
 +execute_xflow_actions(struct wx *wx, uint16_t in_port,
 +                      const union xflow_action *actions, size_t n_actions,
 +                      struct ofpbuf *packet)
 +{
 +    if (n_actions == 1 && actions[0].type == XFLOWAT_CONTROLLER
 +        && wx->n_ctl_packets < MAX_CTL_PACKETS) {
 +        /* As an optimization, avoid a round-trip from userspace to kernel to
 +         * userspace.  This also avoids possibly filling up kernel packet
 +         * buffers along the way. */
 +        struct wdp_packet *wdp_packet;
 +
 +        if (!(wx->wdp_listen_mask & WDP_CHAN_ACTION)) {
 +            return true;
 +        }
 +
 +        wdp_packet = xmalloc(sizeof *wdp_packet);
 +        wdp_packet->channel = WDP_CHAN_ACTION;
 +        wdp_packet->tun_id = 0;
 +        wdp_packet->in_port = in_port;
 +        wdp_packet->send_len = actions[0].controller.arg;
 +        wdp_packet->payload = packet;
 +
 +        list_push_back(&wx->ctl_packets, &wdp_packet->list);
 +
 +        return true;
 +    } else {
 +        int error;
 +
 +        error = xfif_execute(wx->xfif, in_port, actions, n_actions, packet);
 +        ofpbuf_delete(packet);
 +        return !error;
 +    }
 +}
 +
 +/* Executes the actions indicated by 'rule' on 'packet', which is in flow
 + * 'flow' and is considered to have arrived on xflow port 'in_port'.  'packet'
 + * must have at least sizeof(struct ofp_packet_in) bytes of headroom.
 + *
 + * The flow that 'packet' actually contains does not need to actually match
 + * 'rule'; the actions in 'rule' will be applied to it either way.  Likewise,
 + * the packet and byte counters for 'rule' will be credited for the packet sent
 + * out whether or not the packet actually matches 'rule'.
 + *
 + * If 'rule' is an exact-match rule and 'flow' actually equals the rule's flow,
 + * the caller must already have accurately composed xflow actions for it given
 + * 'packet' using rule_make_actions().  If 'rule' is a wildcard rule, or if
 + * 'rule' is an exact-match rule but 'flow' is not the rule's flow, then this
 + * function will compose a set of xflow actions based on 'rule''s OpenFlow
 + * actions and apply them to 'packet'.
 + *
 + * Takes ownership of 'packet'. */
 +static void
 +wx_rule_execute(struct wx *wx, struct wx_rule *rule,
 +                struct ofpbuf *packet, const flow_t *flow)
 +{
 +    const union xflow_action *actions;
 +    struct xflow_flow_stats stats;
 +    size_t n_actions;
 +    struct xflow_actions a;
 +
 +    assert(ofpbuf_headroom(packet) >= sizeof(struct ofp_packet_in));
 +
 +    /* Grab or compose the xflow actions.
 +     *
 +     * The special case for an exact-match 'rule' where 'flow' is not the
 +     * rule's flow is important to avoid, e.g., sending a packet out its input
 +     * port simply because the xflow actions were composed for the wrong
 +     * scenario. */
 +    if (rule->wr.cr.flow.wildcards
 +        || !flow_equal_headers(flow, &rule->wr.cr.flow))
 +    {
 +        struct wx_rule *super = rule->super ? rule->super : rule;
 +        if (wx_xlate_actions(wx, super->wr.actions, super->wr.n_actions, flow,
 +                             packet, NULL, &a, NULL)) {
 +            ofpbuf_delete(packet);
 +            return;
 +        }
 +        actions = a.actions;
 +        n_actions = a.n_actions;
 +    } else {
 +        actions = rule->xflow_actions;
 +        n_actions = rule->n_xflow_actions;
 +    }
 +
 +    /* Execute the xflow actions. */
 +    flow_extract_stats(flow, packet, &stats);
 +    if (!execute_xflow_actions(wx, flow->in_port,
 +                               actions, n_actions, packet)) {
 +        wx_rule_update_stats(wx, rule, &stats);
 +        rule->used = time_msec();
 +        //XXX netflow_flow_update_time(wx->netflow, &rule->nf_flow, rule->used);
 +    }
 +}
 +
 +/* Inserts 'rule' into 'p''s flow table.
 + *
 + * If 'packet' is nonnull, takes ownership of 'packet', executes 'rule''s
 + * actions on it and credits the statistics for sending the packet to 'rule'.
 + * 'packet' must have at least sizeof(struct ofp_packet_in) bytes of
 + * headroom. */
 +static void
 +wx_rule_insert(struct wx *wx, struct wx_rule *rule, struct ofpbuf *packet,
 +               uint16_t in_port)
 +{
 +    struct wx_rule *displaced_rule;
 +
 +    /* Insert the rule in the classifier. */
 +    displaced_rule = wx_rule_cast(classifier_insert(&wx->cls, &rule->wr.cr));
 +    if (!rule->wr.cr.flow.wildcards) {
 +        wx_rule_make_actions(wx, rule, packet);
 +    }
 +
 +    /* Send the packet and credit it to the rule. */
 +    if (packet) {
 +        flow_t flow;
 +        flow_extract(packet, 0, in_port, &flow);
 +        wx_rule_execute(wx, rule, packet, &flow);
 +    }
 +
 +    /* Install the rule in the datapath only after sending the packet, to
 +     * avoid packet reordering.  */
 +    if (rule->wr.cr.flow.wildcards) {
 +        COVERAGE_INC(wx_add_wc_flow);
 +        wx->need_revalidate = true;
 +    } else {
 +        wx_rule_install(wx, rule, displaced_rule);
 +    }
 +
 +    /* Free the rule that was displaced, if any. */
 +    if (displaced_rule) {
 +        rule->wr.client_data = displaced_rule->wr.client_data;
 +        wx_rule_destroy(wx, displaced_rule);
 +    }
 +}
 +
 +static struct wx_rule *
 +wx_rule_create_subrule(struct wx *wx, struct wx_rule *rule, const flow_t *flow)
 +{
 +    struct wx_rule *subrule;
 +
 +    subrule = wx_rule_create(rule, NULL, 0,
 +                             rule->wr.idle_timeout,
 +                             rule->wr.hard_timeout);
 +    /* Subrules aren't really in any OpenFlow table, so don't bother with
 +     * subrule->wr.ofp_table_id. */
 +    COVERAGE_INC(wx_subrule_create);
 +    cls_rule_from_flow(flow, &subrule->wr.cr);
 +    classifier_insert_exact(&wx->cls, &subrule->wr.cr);
 +
 +    return subrule;
 +}
 +
 +/* Returns true if the actions changed, false otherwise. */
 +static bool
 +wx_rule_make_actions(struct wx *wx, struct wx_rule *rule,
 +                     const struct ofpbuf *packet)
 +{
 +    const struct wx_rule *super;
 +    struct xflow_actions a;
 +    size_t actions_len;
 +
 +    assert(!rule->wr.cr.flow.wildcards);
 +
 +    super = rule->super ? rule->super : rule;
 +    wx_xlate_actions(wx, super->wr.actions, super->wr.n_actions,
 +                     &rule->wr.cr.flow, packet,
 +                     &rule->tags, &a, &rule->may_install);
 +
 +    actions_len = a.n_actions * sizeof *a.actions;
 +    if (rule->n_xflow_actions != a.n_actions
 +        || memcmp(rule->xflow_actions, a.actions, actions_len)) {
 +        COVERAGE_INC(wx_xflow_unchanged);
 +        free(rule->xflow_actions);
 +        rule->n_xflow_actions = a.n_actions;
 +        rule->xflow_actions = xmemdup(a.actions, actions_len);
 +        return true;
 +    } else {
 +        return false;
 +    }
 +}
 +
 +static int
 +do_put_flow(struct wx *wx, struct wx_rule *rule, int flags,
 +            struct xflow_flow_put *put)
 +{
 +    memset(&put->flow.stats, 0, sizeof put->flow.stats);
 +    xflow_key_from_flow(&put->flow.key, &rule->wr.cr.flow);
 +    put->flow.actions = rule->xflow_actions;
 +    put->flow.n_actions = rule->n_xflow_actions;
 +    put->flow.flags = 0;
 +    put->flags = flags;
 +    return xfif_flow_put(wx->xfif, put);
 +}
 +
 +static void
 +wx_rule_install(struct wx *wx, struct wx_rule *rule, struct wx_rule *displaced_rule)
 +{
 +    assert(!rule->wr.cr.flow.wildcards);
 +
 +    if (rule->may_install) {
 +        struct xflow_flow_put put;
 +        if (!do_put_flow(wx, rule,
 +                         XFLOWPF_CREATE | XFLOWPF_MODIFY | XFLOWPF_ZERO_STATS,
 +                         &put)) {
 +            rule->installed = true;
 +            if (displaced_rule) {
 +                wx_rule_update_stats(wx, displaced_rule, &put.flow.stats);
 +                wx_rule_post_uninstall(wx, displaced_rule);
 +            }
 +        }
 +    } else if (displaced_rule) {
 +        wx_rule_uninstall(wx, displaced_rule);
 +    }
 +}
 +
 +static void
 +wx_rule_reinstall(struct wx *wx, struct wx_rule *rule)
 +{
 +    if (rule->installed) {
 +        struct xflow_flow_put put;
 +        COVERAGE_INC(wx_dp_missed);
 +        do_put_flow(wx, rule, XFLOWPF_CREATE | XFLOWPF_MODIFY, &put);
 +    } else {
 +        wx_rule_install(wx, rule, NULL);
 +    }
 +}
 +
 +static void
 +wx_rule_update_actions(struct wx *wx, struct wx_rule *rule)
 +{
 +    bool actions_changed;
 +#if 0
 +    uint16_t new_out_iface, old_out_iface;
 +
 +    old_out_iface = rule->nf_flow.output_iface;
 +#endif
 +    actions_changed = wx_rule_make_actions(wx, rule, NULL);
 +
 +    if (rule->may_install) {
 +        if (rule->installed) {
 +            if (actions_changed) {
 +                struct xflow_flow_put put;
 +                do_put_flow(wx, rule, XFLOWPF_CREATE | XFLOWPF_MODIFY
 +                            | XFLOWPF_ZERO_STATS, &put);
 +                wx_rule_update_stats(wx, rule, &put.flow.stats);
 +#if 0
 +                /* Temporarily set the old output iface so that NetFlow
 +                 * messages have the correct output interface for the old
 +                 * stats. */
 +                new_out_iface = rule->nf_flow.output_iface;
 +                rule->nf_flow.output_iface = old_out_iface;
 +#endif
 +                wx_rule_post_uninstall(wx, rule);
 +                //rule->nf_flow.output_iface = new_out_iface;
 +            }
 +        } else {
 +            wx_rule_install(wx, rule, NULL);
 +        }
 +    } else {
 +        wx_rule_uninstall(wx, rule);
 +    }
 +}
 +\f
 +static void
 +add_output_group_action(struct xflow_actions *actions, uint16_t group,
 +                        uint16_t *nf_output_iface)
 +{
 +    xflow_actions_add(actions, XFLOWAT_OUTPUT_GROUP)->output_group.group = group;
 +
 +    if (group == WX_GROUP_ALL || group == WX_GROUP_FLOOD) {
 +        *nf_output_iface = NF_OUT_FLOOD;
 +    }
 +}
 +
 +static void
 +add_controller_action(struct xflow_actions *actions, uint16_t max_len)
 +{
 +    union xflow_action *a = xflow_actions_add(actions, XFLOWAT_CONTROLLER);
 +    a->controller.arg = max_len;
 +}
 +
 +struct wx_xlate_ctx {
 +    /* Input. */
 +    flow_t flow;                /* Flow to which these actions correspond. */
 +    int recurse;                /* Recursion level, via xlate_table_action. */
 +    struct wx *wx;
 +    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 xflow_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. */
 +};
 +
 +static void do_xlate_actions(const union ofp_action *in, size_t n_in,
 +                             struct wx_xlate_ctx *ctx);
 +
 +static void
 +add_output_action(struct wx_xlate_ctx *ctx, uint16_t port)
 +{
-     if (wdp_port) {
-         if (wdp_port->opp.config & OFPPC_NO_FWD) {
++    const struct wx_port *wx_port = wx_port_get(ctx->wx, port);
 +
-     const struct wdp_port *port;
++    if (wx_port) {
++        if (wx_port->wdp_port.opp.config & OFPPC_NO_FWD) {
 +            /* Forwarding disabled on port. */
 +            return;
 +        }
 +    } else {
 +        /*
 +         * We don't have an ofport record for this port, but it doesn't hurt to
 +         * allow forwarding to it anyhow.  Maybe such a port will appear later
 +         * and we're pre-populating the flow table.
 +         */
 +    }
 +
 +    xflow_actions_add(ctx->out, XFLOWAT_OUTPUT)->output.port = port;
 +    //ctx->nf_output_iface = port;
 +}
 +
 +static struct wx_rule *
 +wx_rule_lookup_valid(struct wx *wx, const flow_t *flow)
 +{
 +    struct wx_rule *rule = wx_rule_cast(classifier_lookup(&wx->cls, flow));
 +
 +    /* The rule we found might not be valid, since we could be in need of
 +     * revalidation.  If it is not valid, don't return it. */
 +    if (rule
 +        && rule->super
 +        && wx->need_revalidate
 +        && !wx_rule_revalidate(wx, rule)) {
 +        COVERAGE_INC(wx_invalidated);
 +        return NULL;
 +    }
 +
 +    return rule;
 +}
 +
 +static void
 +xlate_table_action(struct wx_xlate_ctx *ctx, uint16_t in_port)
 +{
 +    if (!ctx->recurse) {
 +        uint16_t old_in_port;
 +        struct wx_rule *rule;
 +
 +        /* Look up a flow with 'in_port' as the input port.  Then restore the
 +         * original input port (otherwise OFPP_NORMAL and OFPP_IN_PORT will
 +         * have surprising behavior). */
 +        old_in_port = ctx->flow.in_port;
 +        ctx->flow.in_port = in_port;
 +        rule = wx_rule_lookup_valid(ctx->wx, &ctx->flow);
 +        ctx->flow.in_port = old_in_port;
 +
 +        if (rule) {
 +            if (rule->super) {
 +                rule = rule->super;
 +            }
 +
 +            ctx->recurse++;
 +            do_xlate_actions(rule->wr.actions, rule->wr.n_actions, ctx);
 +            ctx->recurse--;
 +        }
 +    }
 +}
 +
 +static void
 +xlate_output_action__(struct wx_xlate_ctx *ctx,
 +                      uint16_t port, uint16_t max_len)
 +{
 +    uint16_t xflow_port;
 +    uint16_t prev_nf_output_iface = ctx->nf_output_iface;
 +
 +    ctx->nf_output_iface = NF_OUT_DROP;
 +
 +    switch (port) {
 +    case OFPP_IN_PORT:
 +        add_output_action(ctx, ctx->flow.in_port);
 +        break;
 +    case OFPP_TABLE:
 +        xlate_table_action(ctx, ctx->flow.in_port);
 +        break;
 +    case OFPP_NORMAL:
 +        if (!ctx->wx->ofhooks->normal_cb(&ctx->flow, ctx->packet,
 +                                         ctx->out, ctx->tags,
 +                                         &ctx->nf_output_iface,
 +                                         ctx->wx->aux)) {
 +            COVERAGE_INC(wx_uninstallable);
 +            ctx->may_set_up_flow = false;
 +        }
 +        break;
 +
 +    case OFPP_FLOOD:
 +        add_output_group_action(ctx->out, WX_GROUP_FLOOD,
 +                                &ctx->nf_output_iface);
 +        break;
 +    case OFPP_ALL:
 +        add_output_group_action(ctx->out, WX_GROUP_ALL, &ctx->nf_output_iface);
 +        break;
 +    case OFPP_CONTROLLER:
 +        add_controller_action(ctx->out, max_len);
 +        break;
 +    case OFPP_LOCAL:
 +        add_output_action(ctx, XFLOWP_LOCAL);
 +        break;
 +    default:
 +        xflow_port = ofp_port_to_xflow_port(port);
 +        if (xflow_port != ctx->flow.in_port) {
 +            add_output_action(ctx, xflow_port);
 +        }
 +        break;
 +    }
 +
 +    if (prev_nf_output_iface == NF_OUT_FLOOD) {
 +        ctx->nf_output_iface = NF_OUT_FLOOD;
 +    } else if (ctx->nf_output_iface == NF_OUT_DROP) {
 +        ctx->nf_output_iface = prev_nf_output_iface;
 +    } else if (prev_nf_output_iface != NF_OUT_DROP &&
 +               ctx->nf_output_iface != NF_OUT_FLOOD) {
 +        ctx->nf_output_iface = NF_OUT_MULTI;
 +    }
 +}
 +
 +static void
 +xlate_output_action(struct wx_xlate_ctx *ctx,
 +                    const struct ofp_action_output *oao)
 +{
 +    xlate_output_action__(ctx, ntohs(oao->port), ntohs(oao->max_len));
 +}
 +
 +/* If the final xflow action in 'ctx' is "pop priority", drop it, as an
 + * optimization, because we're going to add another action that sets the
 + * priority immediately after, or because there are no actions following the
 + * pop.  */
 +static void
 +remove_pop_action(struct wx_xlate_ctx *ctx)
 +{
 +    size_t n = ctx->out->n_actions;
 +    if (n > 0 && ctx->out->actions[n - 1].type == XFLOWAT_POP_PRIORITY) {
 +        ctx->out->n_actions--;
 +    }
 +}
 +
 +static void
 +xlate_enqueue_action(struct wx_xlate_ctx *ctx,
 +                     const struct ofp_action_enqueue *oae)
 +{
 +    uint16_t ofp_port, xflow_port;
 +    uint32_t priority;
 +    int error;
 +
 +    error = xfif_queue_to_priority(ctx->wx->xfif, ntohl(oae->queue_id),
 +                                   &priority);
 +    if (error) {
 +        /* Fall back to ordinary output action. */
 +        xlate_output_action__(ctx, ntohs(oae->port), 0);
 +        return;
 +    }
 +
 +    /* Figure out xflow output port. */
 +    ofp_port = ntohs(oae->port);
 +    if (ofp_port != OFPP_IN_PORT) {
 +        xflow_port = ofp_port_to_xflow_port(ofp_port);
 +    } else {
 +        xflow_port = ctx->flow.in_port;
 +    }
 +
 +    /* Add xflow actions. */
 +    remove_pop_action(ctx);
 +    xflow_actions_add(ctx->out, XFLOWAT_SET_PRIORITY)->priority.priority
 +        = priority;
 +    add_output_action(ctx, xflow_port);
 +    xflow_actions_add(ctx->out, XFLOWAT_POP_PRIORITY);
 +
 +    /* Update NetFlow output port. */
 +    if (ctx->nf_output_iface == NF_OUT_DROP) {
 +        ctx->nf_output_iface = xflow_port;
 +    } else if (ctx->nf_output_iface != NF_OUT_FLOOD) {
 +        ctx->nf_output_iface = NF_OUT_MULTI;
 +    }
 +}
 +
++static void
++xlate_set_queue_action(struct wx_xlate_ctx *ctx,
++                       const struct nx_action_set_queue *nasq)
++{
++    uint32_t priority;
++    int error;
++
++    error = xfif_queue_to_priority(ctx->wx->xfif, ntohl(nasq->queue_id),
++                                   &priority);
++    if (error) {
++        /* Couldn't translate queue to a priority, so ignore.  A warning
++         * has already been logged. */
++        return;
++    }
++
++    remove_pop_action(ctx);
++    xflow_actions_add(ctx->out, XFLOWAT_SET_PRIORITY)->priority.priority
++        = priority;
++}
++
 +static void
 +xlate_nicira_action(struct wx_xlate_ctx *ctx,
 +                    const struct nx_action_header *nah)
 +{
 +    const struct nx_action_resubmit *nar;
 +    const struct nx_action_set_tunnel *nast;
++    const struct nx_action_set_queue *nasq;
 +    union xflow_action *oa;
 +    int subtype = ntohs(nah->subtype);
 +
 +    assert(nah->vendor == htonl(NX_VENDOR_ID));
 +    switch (subtype) {
 +    case NXAST_RESUBMIT:
 +        nar = (const struct nx_action_resubmit *) nah;
 +        xlate_table_action(ctx, ofp_port_to_xflow_port(ntohs(nar->in_port)));
 +        break;
 +
 +    case NXAST_SET_TUNNEL:
 +        nast = (const struct nx_action_set_tunnel *) nah;
 +        oa = xflow_actions_add(ctx->out, XFLOWAT_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)) {
 +            xflow_actions_add(ctx->out, XFLOWAT_DROP_SPOOFED_ARP);
 +        }
 +        break;
 +
++    case NXAST_SET_QUEUE:
++        nasq = (const struct nx_action_set_queue *) nah;
++        xlate_set_queue_action(ctx, nasq);
++        break;
++
++    case NXAST_POP_QUEUE:
++        xflow_actions_add(ctx->out, XFLOWAT_POP_PRIORITY);
++        break;
++
 +    /* 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. */
 +
 +    default:
 +        VLOG_DBG_RL(&rl, "unknown Nicira action type %"PRIu16, subtype);
 +        break;
 +    }
 +}
 +
 +static void
 +do_xlate_actions(const union ofp_action *in, size_t n_in,
 +                 struct wx_xlate_ctx *ctx)
 +{
 +    struct actions_iterator iter;
 +    const union ofp_action *ia;
-     port = port_array_get(&ctx->wx->ports, ctx->flow.in_port);
-     if (port && port->opp.config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
-         port->opp.config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
-                             ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
-         /* Drop this flow. */
-         return;
++    const struct wx_port *port;
 +
-         LIST_FOR_EACH_SAFE (subrule, next, struct wx_rule, list, &rule->list) {
++    port = wx_port_get(ctx->wx, ctx->flow.in_port);
++    if (port) {
++        const struct ofp_phy_port *opp = &port->wdp_port.opp;
++        if (opp->config & (OFPPC_NO_RECV | OFPPC_NO_RECV_STP) &&
++            opp->config & (eth_addr_equals(ctx->flow.dl_dst, eth_addr_stp)
++                           ? OFPPC_NO_RECV_STP : OFPPC_NO_RECV)) {
++            /* Drop this flow. */
++            return;
++        }
 +    }
 +
 +    for (ia = actions_first(&iter, in, n_in); ia; ia = actions_next(&iter)) {
 +        uint16_t type = ntohs(ia->type);
 +        union xflow_action *oa;
 +
 +        switch (type) {
 +        case OFPAT_OUTPUT:
 +            xlate_output_action(ctx, &ia->output);
 +            break;
 +
 +        case OFPAT_SET_VLAN_VID:
 +            oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_TCI);
 +            oa->dl_tci.tci = ia->vlan_vid.vlan_vid & htons(VLAN_VID_MASK);
 +            oa->dl_tci.mask = htons(VLAN_VID_MASK);
 +            ctx->flow.dl_vlan = ia->vlan_vid.vlan_vid;
 +            break;
 +
 +        case OFPAT_SET_VLAN_PCP:
 +            oa = xflow_actions_add(ctx->out, XFLOWAT_SET_DL_TCI);
 +            oa->dl_tci.tci = htons((ia->vlan_pcp.vlan_pcp << VLAN_PCP_SHIFT)
 +                                   & VLAN_PCP_MASK);
 +            oa->dl_tci.mask = htons(VLAN_PCP_MASK);
 +
 +            if (ctx->flow.dl_vlan == htons(OFP_VLAN_NONE)) {
 +                ctx->flow.dl_vlan = htons(0);
 +            }
 +            ctx->flow.dl_vlan_pcp = ia->vlan_pcp.vlan_pcp;
 +            break;
 +
 +        case OFPAT_STRIP_VLAN:
 +            xflow_actions_add(ctx->out, XFLOWAT_STRIP_VLAN);
 +            ctx->flow.dl_vlan = htons(OFP_VLAN_NONE);
 +            ctx->flow.dl_vlan_pcp = 0;
 +            break;
 +
 +        case OFPAT_SET_DL_SRC:
 +            oa = xflow_actions_add(ctx->out, XFLOWAT_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,
 +                   ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
 +            break;
 +
 +        case OFPAT_SET_DL_DST:
 +            oa = xflow_actions_add(ctx->out, XFLOWAT_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,
 +                   ((struct ofp_action_dl_addr *) ia)->dl_addr, ETH_ADDR_LEN);
 +            break;
 +
 +        case OFPAT_SET_NW_SRC:
 +            oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_SRC);
 +            ctx->flow.nw_src = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
 +            break;
 +
 +        case OFPAT_SET_NW_DST:
 +            oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_DST);
 +            ctx->flow.nw_dst = oa->nw_addr.nw_addr = ia->nw_addr.nw_addr;
 +            break;
 +
 +        case OFPAT_SET_NW_TOS:
 +            oa = xflow_actions_add(ctx->out, XFLOWAT_SET_NW_TOS);
 +            ctx->flow.nw_tos = oa->nw_tos.nw_tos = ia->nw_tos.nw_tos;
 +            break;
 +
 +        case OFPAT_SET_TP_SRC:
 +            oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TP_SRC);
 +            ctx->flow.tp_src = oa->tp_port.tp_port = ia->tp_port.tp_port;
 +            break;
 +
 +        case OFPAT_SET_TP_DST:
 +            oa = xflow_actions_add(ctx->out, XFLOWAT_SET_TP_DST);
 +            ctx->flow.tp_dst = oa->tp_port.tp_port = ia->tp_port.tp_port;
 +            break;
 +
 +        case OFPAT_ENQUEUE:
 +            xlate_enqueue_action(ctx, (const struct ofp_action_enqueue *) ia);
 +            break;
 +
 +        case OFPAT_VENDOR:
 +            xlate_nicira_action(ctx, (const struct nx_action_header *) ia);
 +            break;
 +
 +        default:
 +            VLOG_DBG_RL(&rl, "unknown action type %"PRIu16, type);
 +            break;
 +        }
 +    }
 +}
 +
 +/* Returns true if 'flow' and 'actions' may be set up as a flow in the kernel.
 + * This is true most of the time, but we don't allow flows that would prevent
 + * DHCP replies from being seen by the local port to be set up in the
 + * kernel.
 + *
 + * We only need this, strictly speaking, when in-band control is turned on. */
 +static bool
 +wx_may_set_up(const flow_t *flow, const struct xflow_actions *actions)
 +{
 +    if (flow->dl_type == htons(ETH_TYPE_IP)
 +        && flow->nw_proto == IP_TYPE_UDP
 +        && flow->tp_src == htons(DHCP_SERVER_PORT)
 +        && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
 +        int i;
 +
 +        for (i = 0; i < actions->n_actions; i++) {
 +            const struct xflow_action_output *oao = &actions->actions[i].output;
 +            if (oao->type == XFLOWAT_OUTPUT && oao->port == XFLOWP_LOCAL) {
 +                return true;
 +            }
 +        }
 +        return false;
 +    }
 +
 +    return true;
 +}
 +
 +static int
 +wx_xlate_actions(struct wx *wx, const union ofp_action *in, size_t n_in,
 +                 const flow_t *flow, const struct ofpbuf *packet,
 +                 tag_type *tags, struct xflow_actions *out,
 +                 bool *may_set_up_flow)
 +{
 +    tag_type no_tags = 0;
 +    struct wx_xlate_ctx ctx;
 +    COVERAGE_INC(wx_ofp2xflow);
 +    xflow_actions_init(out);
 +    ctx.flow = *flow;
 +    ctx.recurse = 0;
 +    ctx.wx = wx;
 +    ctx.packet = packet;
 +    ctx.out = out;
 +    ctx.tags = tags ? tags : &no_tags;
 +    ctx.may_set_up_flow = true;
 +    ctx.nf_output_iface = NF_OUT_DROP;
 +    do_xlate_actions(in, n_in, &ctx);
 +    remove_pop_action(&ctx);
 +
 +    if (may_set_up_flow) {
 +        *may_set_up_flow = ctx.may_set_up_flow && wx_may_set_up(flow, out);
 +    }
 +#if 0
 +    if (nf_output_iface) {
 +        *nf_output_iface = ctx.nf_output_iface;
 +    }
 +#endif
 +    if (xflow_actions_overflow(out)) {
 +        COVERAGE_INC(xflow_overflow);
 +        xflow_actions_init(out);
 +        return ofp_mkerr(OFPET_BAD_ACTION, OFPBAC_TOO_MANY);
 +    }
 +    return 0;
 +}
 +\f
 +static void
 +update_used(struct wx *wx)
 +{
 +    struct xflow_flow *flows;
 +    size_t n_flows;
 +    size_t i;
 +    int error;
 +
 +    error = xfif_flow_list_all(wx->xfif, &flows, &n_flows);
 +    if (error) {
 +        return;
 +    }
 +
 +    for (i = 0; i < n_flows; i++) {
 +        struct xflow_flow *f = &flows[i];
 +        struct wx_rule *rule;
 +        flow_t flow;
 +
 +        xflow_key_to_flow(&f->key, &flow);
 +        rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, &flow));
 +        if (!rule || !rule->installed) {
 +            COVERAGE_INC(wx_unexpected_rule);
 +            xfif_flow_del(wx->xfif, f);
 +            continue;
 +        }
 +
 +        wx_rule_update_time(wx, rule, &f->stats);
 +        wx_rule_account(wx, rule, f->stats.n_bytes);
 +    }
 +    free(flows);
 +}
 +
 +static void
 +uninstall_idle_flow(struct wx *wx, struct wx_rule *rule)
 +{
 +    assert(rule->installed);
 +    assert(!rule->wr.cr.flow.wildcards);
 +
 +    if (rule->super) {
 +        wx_rule_remove(wx, rule);
 +    } else {
 +        wx_rule_uninstall(wx, rule);
 +    }
 +}
 +
 +static int
 +expire_rule(struct cls_rule *cls_rule, void *wx_)
 +{
 +    struct wx *wx = wx_;
 +    struct wx_rule *rule = wx_rule_cast(cls_rule);
 +    long long int hard_expire, idle_expire, expire, now;
 +
 +    hard_expire = (rule->wr.hard_timeout
 +                   ? rule->wr.created + rule->wr.hard_timeout * 1000
 +                   : LLONG_MAX);
 +    idle_expire = (rule->wr.idle_timeout
 +                   && (rule->super || list_is_empty(&rule->list))
 +                   ? rule->used + rule->wr.idle_timeout * 1000
 +                   : LLONG_MAX);
 +    expire = MIN(hard_expire, idle_expire);
 +
 +    now = time_msec();
 +    if (now < expire) {
 +        if (rule->installed && now >= rule->used + 5000) {
 +            uninstall_idle_flow(wx, rule);
 +        } else if (!rule->wr.cr.flow.wildcards) {
 +            //XXX active_timeout(wx, rule);
 +        }
 +
 +        return 0;
 +    }
 +
 +    COVERAGE_INC(wx_expired);
 +
 +    /* Update stats.  This code will be a no-op if the rule expired
 +     * due to an idle timeout. */
 +    if (rule->wr.cr.flow.wildcards) {
 +        struct wx_rule *subrule, *next;
-     LIST_FOR_EACH (wx, struct wx, list_node, &all_wx) {
++        LIST_FOR_EACH_SAFE (subrule, next, list, &rule->list) {
 +            wx_rule_remove(wx, subrule);
 +        }
 +    } else {
 +        wx_rule_uninstall(wx, rule);
 +    }
 +
 +#if 0                           /* XXX */
 +    if (!wx_rule_is_hidden(rule)) {
 +        send_flow_removed(wx, rule, now,
 +                          (now >= hard_expire
 +                           ? OFPRR_HARD_TIMEOUT : OFPRR_IDLE_TIMEOUT));
 +    }
 +#endif
 +    wx_rule_remove(wx, rule);
 +
 +    return 0;
 +}
 +
 +struct revalidate_cbdata {
 +    struct wx *wx;
 +    bool revalidate_all;        /* Revalidate all exact-match rules? */
 +    bool revalidate_subrules;   /* Revalidate all exact-match subrules? */
 +    struct tag_set revalidate_set; /* Set of tags to revalidate. */
 +};
 +
 +static bool
 +revalidate_rule(struct wx *wx, struct wx_rule *rule)
 +{
 +    const flow_t *flow = &rule->wr.cr.flow;
 +
 +    COVERAGE_INC(wx_revalidate_rule);
 +    if (rule->super) {
 +        struct wx_rule *super;
 +        super = wx_rule_cast(classifier_lookup_wild(&wx->cls, flow));
 +        if (!super) {
 +            wx_rule_remove(wx, rule);
 +            return false;
 +        } else if (super != rule->super) {
 +            COVERAGE_INC(wx_revalidate_moved);
 +            list_remove(&rule->list);
 +            list_push_back(&super->list, &rule->list);
 +            rule->super = super;
 +            rule->wr.hard_timeout = super->wr.hard_timeout;
 +            rule->wr.idle_timeout = super->wr.idle_timeout;
 +            rule->wr.created = super->wr.created;
 +            rule->used = 0;
 +        }
 +    }
 +
 +    wx_rule_update_actions(wx, rule);
 +    return true;
 +}
 +
 +static int
 +revalidate_cb(struct cls_rule *sub_, void *cbdata_)
 +{
 +    struct wx_rule *sub = wx_rule_cast(sub_);
 +    struct revalidate_cbdata *cbdata = cbdata_;
 +
 +    if (cbdata->revalidate_all
 +        || (cbdata->revalidate_subrules && sub->super)
 +        || tag_set_intersects(&cbdata->revalidate_set, sub->tags)) {
 +        revalidate_rule(cbdata->wx, sub);
 +    }
 +    return 0;
 +}
 +
 +static void
 +wx_run_one(struct wx *wx)
 +{
 +    if (time_msec() >= wx->next_expiration) {
 +        COVERAGE_INC(wx_expiration);
 +        wx->next_expiration = time_msec() + 1000;
 +        update_used(wx);
 +
 +        classifier_for_each(&wx->cls, CLS_INC_ALL, expire_rule, wx);
 +
 +        /* XXX account_checkpoint_cb */
 +    }
 +
 +    if (wx->need_revalidate || !tag_set_is_empty(&wx->revalidate_set)) {
 +        struct revalidate_cbdata cbdata;
 +        cbdata.wx = wx;
 +        cbdata.revalidate_all = wx->revalidate_all;
 +        cbdata.revalidate_subrules = wx->need_revalidate;
 +        cbdata.revalidate_set = wx->revalidate_set;
 +        tag_set_init(&wx->revalidate_set);
 +        COVERAGE_INC(wx_revalidate);
 +        classifier_for_each(&wx->cls, CLS_INC_EXACT, revalidate_cb, &cbdata);
 +        wx->need_revalidate = false;
 +    }
 +}
 +
 +static void
 +wx_run(void)
 +{
 +    struct wx *wx;
 +
-     LIST_FOR_EACH (wx, struct wx, list_node, &all_wx) {
++    LIST_FOR_EACH (wx, list_node, &all_wx) {
 +        wx_run_one(wx);
 +    }
 +    xf_run();
 +}
 +
 +static void
 +wx_wait_one(struct wx *wx)
 +{
 +    if (wx->need_revalidate || !tag_set_is_empty(&wx->revalidate_set)) {
 +        poll_immediate_wake();
 +    } else if (wx->next_expiration != LLONG_MAX) {
 +        poll_timer_wait_until(wx->next_expiration);
 +    }
 +}
 +
 +static void
 +wx_wait(void)
 +{
 +    struct wx *wx;
 +
-         port_array_init(&wx->ports);
++    LIST_FOR_EACH (wx, list_node, &all_wx) {
 +        wx_wait_one(wx);
 +    }
 +    xf_wait();
 +}
 +\f
 +static int wx_flow_flush(struct wdp *);
 +
 +static int
 +wx_enumerate(const struct wdp_class *wdp_class, struct svec *all_wdps)
 +{
 +    struct svec names = SVEC_EMPTY_INITIALIZER;
 +    int error = xf_enumerate_names(wdp_class->type, &names);
 +    svec_move(all_wdps, &names);
 +    return error;
 +}
 +
 +static int
 +wx_open(const struct wdp_class *wdp_class, const char *name, bool create,
 +        struct wdp **wdpp)
 +{
 +    struct xfif *xfif;
 +    int error;
 +
 +    error = (create
 +             ? xfif_create_and_open(name, wdp_class->type, &xfif)
 +             : xfif_open(name, wdp_class->type, &xfif));
 +    if (!error) {
 +        struct wx *wx;
 +
 +        wx = xzalloc(sizeof *wx);
 +        list_push_back(&all_wx, &wx->list_node);
 +        wdp_init(&wx->wdp, wdp_class, name, 0, 0);
 +        wx->xfif = xfif;
 +        classifier_init(&wx->cls);
 +        wx->netdev_monitor = netdev_monitor_create();
-     unsigned int port_no;
-     struct wdp_port *port;
++        hmap_init(&wx->ports);
 +        shash_init(&wx->port_by_name);
 +        wx->next_expiration = time_msec() + 1000;
 +        tag_set_init(&wx->revalidate_set);
 +
 +        wx_port_init(wx);
 +
 +        wx->ofhooks = &default_ofhooks;
 +        wx->aux = wx;
 +        wx->ml = mac_learning_create();
 +
 +        list_init(&wx->ctl_packets);
 +
 +        *wdpp = &wx->wdp;
 +    }
 +
 +    return error;
 +}
 +
 +static void
 +wx_close(struct wdp *wdp)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    wx_flow_flush(wdp);
 +    xfif_close(wx->xfif);
 +    classifier_destroy(&wx->cls);
 +    netdev_monitor_destroy(wx->netdev_monitor);
 +    list_remove(&wx->list_node);
 +    mac_learning_destroy(wx->ml);
++    hmap_destroy(&wx->ports);
++    shash_destroy(&wx->port_by_name);
 +    free(wx);
 +}
 +
 +static int
 +wx_get_all_names(const struct wdp *wdp, struct svec *all_names)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    return xfif_get_all_names(wx->xfif, all_names);
 +}
 +
 +static int
 +wx_destroy(struct wdp *wdp)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    return xfif_delete(wx->xfif);
 +}
 +
 +static int
 +wx_get_features(const struct wdp *wdp, struct ofpbuf **featuresp)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    struct ofp_switch_features *osf;
 +    struct ofpbuf *buf;
-     PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
-         hton_ofp_phy_port(ofpbuf_put(buf, &port->opp, sizeof port->opp));
++    struct wx_port *port;
 +
 +    buf = ofpbuf_new(sizeof *osf);
 +    osf = ofpbuf_put_zeros(buf, sizeof *osf);
 +    osf->n_tables = 2;
 +    osf->capabilities = htonl(OFPC_ARP_MATCH_IP);
 +    osf->actions = htonl((1u << OFPAT_OUTPUT) |
 +                         (1u << OFPAT_SET_VLAN_VID) |
 +                         (1u << OFPAT_SET_VLAN_PCP) |
 +                         (1u << OFPAT_STRIP_VLAN) |
 +                         (1u << OFPAT_SET_DL_SRC) |
 +                         (1u << OFPAT_SET_DL_DST) |
 +                         (1u << OFPAT_SET_NW_SRC) |
 +                         (1u << OFPAT_SET_NW_DST) |
 +                         (1u << OFPAT_SET_NW_TOS) |
 +                         (1u << OFPAT_SET_TP_SRC) |
 +                         (1u << OFPAT_SET_TP_DST) |
 +                         (1u << OFPAT_ENQUEUE));
 +
- wx_answer_port_query(const struct wdp_port *port, struct wdp_port *portp)
++    HMAP_FOR_EACH (port, hmap_node, &wx->ports) {
++        const struct ofp_phy_port *opp = &port->wdp_port.opp;
++        hton_ofp_phy_port(ofpbuf_put(buf, opp, sizeof *opp));
 +    }
 +
 +    *featuresp = buf;
 +    return 0;
 +}
 +
 +static int
 +count_subrules(struct cls_rule *cls_rule, void *n_subrules_)
 +{
 +    struct wx_rule *rule = wx_rule_cast(cls_rule);
 +    int *n_subrules = n_subrules_;
 +
 +    if (rule->super) {
 +        (*n_subrules)++;
 +    }
 +    return 0;
 +}
 +
 +static int
 +wx_get_stats(const struct wdp *wdp, struct wdp_stats *stats)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    struct xflow_stats xflow_stats;
 +    int error;
 +
 +    error = xfif_get_xf_stats(wx->xfif, &xflow_stats);
 +    stats->max_ports = xflow_stats.max_ports;
 +    return error;
 +}
 +
 +static int
 +wx_get_table_stats(const struct wdp *wdp, struct ofpbuf *stats)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    struct xflow_stats xflow_stats;
 +    struct ofp_table_stats *exact, *wild;
 +    int n_subrules;
 +
 +    xfif_get_xf_stats(wx->xfif, &xflow_stats);
 +    /* XXX should pass up errors, but there are no appropriate OpenFlow error
 +     * codes. */
 +
 +    n_subrules = 0;
 +    classifier_for_each(&wx->cls, CLS_INC_EXACT, count_subrules, &n_subrules);
 +
 +    exact = ofpbuf_put_zeros(stats, sizeof *exact);
 +    exact->table_id = TABLEID_HASH;
 +    strcpy(exact->name, "exact");
 +    exact->wildcards = htonl(0);
 +    exact->max_entries = htonl(MIN(WX_MAX_EXACT, xflow_stats.max_capacity));
 +    exact->active_count = htonl(classifier_count_exact(&wx->cls) - n_subrules);
 +    exact->lookup_count = htonll(xflow_stats.n_hit + xflow_stats.n_missed);
 +    exact->matched_count = htonll(xflow_stats.n_hit);
 +
 +    wild = ofpbuf_put_zeros(stats, sizeof *exact);
 +    wild->table_id = TABLEID_CLASSIFIER;
 +    strcpy(wild->name, "classifier");
 +    wild->wildcards = htonl(OVSFW_ALL);
 +    wild->max_entries = htonl(WX_MAX_WILD);
 +    wild->active_count = htonl(classifier_count_wild(&wx->cls));
 +    wild->lookup_count = htonll(0);  /* XXX */
 +    wild->matched_count = htonll(0); /* XXX */
 +
 +    return 0;
 +}
 +
 +static int
 +wx_get_drop_frags(const struct wdp *wdp, bool *drop_frags)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    return xfif_get_drop_frags(wx->xfif, drop_frags);
 +}
 +
 +static int
 +wx_set_drop_frags(struct wdp *wdp, bool drop_frags)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    return xfif_set_drop_frags(wx->xfif, drop_frags);
 +}
 +
 +static int
 +wx_port_add(struct wdp *wdp, const char *devname,
 +            bool internal, uint16_t *port_no)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    uint16_t xflow_flags = internal ? XFLOW_PORT_INTERNAL : 0;
 +    return xfif_port_add(wx->xfif, devname, xflow_flags, port_no);
 +}
 +
 +static int
 +wx_port_del(struct wdp *wdp, uint16_t port_no)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    return xfif_port_del(wx->xfif, port_no);
 +}
 +
 +static int
-         wdp_port_copy(portp, port);
++wx_answer_port_query(const struct wx_port *port, struct wdp_port *portp)
 +{
 +    if (port) {
-     const struct wdp_port *port;
++        wdp_port_copy(portp, &port->wdp_port);
 +        return 0;
 +    } else {
 +        return ENOENT;
 +    }
 +}
 +
 +static int
 +wx_port_query_by_number(const struct wdp *wdp, uint16_t port_no,
 +                        struct wdp_port *portp)
 +{
 +    struct wx *wx = wx_cast(wdp);
-     port = port_array_get(&wx->ports, ofp_port_to_xflow_port(port_no));
-     return wx_answer_port_query(port, portp);
++    struct wx_port *wx_port = wx_port_get(wx, ofp_port_to_xflow_port(port_no));
 +
-     struct wdp_port *port;
++    return wx_answer_port_query(wx_port, portp);
 +}
 +
 +static int
 +wx_port_query_by_name(const struct wdp *wdp, const char *devname,
 +                      struct wdp_port *portp)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    return wx_answer_port_query(shash_find_data(&wx->port_by_name, devname),
 +                                portp);
 +}
 +
 +static int
 +wx_port_set_config(struct wdp *wdp, uint16_t port_no, uint32_t config)
 +{
 +    struct wx *wx = wx_cast(wdp);
-     port = port_array_get(&wx->ports, ofp_port_to_xflow_port(port_no));
++    struct wx_port *port;
++    struct ofp_phy_port *opp;
 +    uint32_t changes;
 +
-     changes = config ^ port->opp.config;
++    port = wx_port_get(wx, ofp_port_to_xflow_port(port_no));
 +    if (!port) {
 +        return ENOENT;
 +    }
-             error = netdev_turn_flags_off(port->netdev, NETDEV_UP, true);
++    opp = &port->wdp_port.opp;
++    changes = config ^ opp->config;
 +
 +    if (changes & OFPPC_PORT_DOWN) {
++        struct netdev *netdev = port->wdp_port.netdev;
 +        int error;
++
 +        if (config & OFPPC_PORT_DOWN) {
-             error = netdev_turn_flags_on(port->netdev, NETDEV_UP, true);
++            error = netdev_turn_flags_off(netdev, NETDEV_UP, true);
 +        } else {
-             port->opp.config ^= OFPPC_PORT_DOWN;
++            error = netdev_turn_flags_on(netdev, NETDEV_UP, true);
 +        }
 +        if (!error) {
-         port->opp.config ^= changes & REVALIDATE_BITS;
++            opp->config ^= OFPPC_PORT_DOWN;
 +        }
 +    }
 +
 +#define REVALIDATE_BITS (OFPPC_NO_RECV | OFPPC_NO_RECV_STP | OFPPC_NO_FWD)
 +    if (changes & REVALIDATE_BITS) {
 +        COVERAGE_INC(wx_costly_flags);
-         port->opp.config ^= OFPPC_NO_FLOOD;
++        opp->config ^= changes & REVALIDATE_BITS;
 +        wx->need_revalidate = true;
 +    }
 +#undef REVALIDATE_BITS
 +
 +    if (changes & OFPPC_NO_FLOOD) {
-         port->opp.config ^= OFPPC_NO_PACKET_IN;
++        opp->config ^= OFPPC_NO_FLOOD;
 +        wx_port_refresh_groups(wx);
 +    }
 +
 +    if (changes & OFPPC_NO_PACKET_IN) {
-     struct wdp_port *ports, *port;
-     unsigned int port_no;
++        opp->config ^= OFPPC_NO_PACKET_IN;
 +    }
 +
 +    return 0;
 +}
 +
 +static int
 +wx_port_list(const struct wdp *wdp, struct wdp_port **portsp, size_t *n_portsp)
 +{
 +    struct wx *wx = wx_cast(wdp);
-     *n_portsp = n_ports = port_array_count(&wx->ports);
++    struct wdp_port *ports;
++    struct wx_port *port;
 +    size_t n_ports, i;
 +
-     PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
-         wdp_port_copy(&ports[i++], port);
++    *n_portsp = n_ports = hmap_count(&wx->ports);
 +    *portsp = ports = xmalloc(n_ports * sizeof *ports);
 +    i = 0;
-         LIST_FOR_EACH (subrule, struct wx_rule, list, &rule->list) {
++    HMAP_FOR_EACH (port, hmap_node, &wx->ports) {
++        wdp_port_copy(&ports[i++], &port->wdp_port);
 +    }
 +    assert(i == n_ports);
 +
 +    return 0;
 +}
 +
 +static int
 +wx_port_poll(struct wdp *wdp, wdp_port_poll_cb_func *cb, void *aux)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    char *devname;
 +    int retval;
 +    int error;
 +
 +    retval = 0;
 +    while ((error = xfif_port_poll(wx->xfif, &devname)) != EAGAIN) {
 +        wx_port_process_change(wx, error, devname, cb, aux);
 +        if (error && error != ENOBUFS) {
 +            retval = error;
 +        }
 +    }
 +    while ((error = netdev_monitor_poll(wx->netdev_monitor,
 +                                        &devname)) != EAGAIN) {
 +        wx_port_process_change(wx, error, devname, cb, aux);
 +        if (error && error != ENOBUFS) {
 +            retval = error;
 +        }
 +    }
 +    return retval;
 +}
 +
 +static int
 +wx_port_poll_wait(const struct wdp *wdp)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    xfif_port_poll_wait(wx->xfif);
 +    netdev_monitor_poll_wait(wx->netdev_monitor);
 +    return 0;
 +}
 +
 +static struct wdp_rule *
 +wx_flow_get(const struct wdp *wdp, const flow_t *flow, unsigned int include)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    struct wx_rule *rule;
 +    int table_id;
 +
 +    table_id = flow->wildcards ? TABLEID_CLASSIFIER : TABLEID_HASH;
 +    if (!(include & (1u << table_id))) {
 +        return NULL;
 +    }
 +
 +    rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, flow));
 +    return rule && !wx_rule_is_hidden(rule) ? &rule->wr : NULL;
 +}
 +
 +static struct wdp_rule *
 +wx_flow_match(const struct wdp *wdp, const flow_t *flow)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    struct wx_rule *rule;
 +
 +    rule = wx_rule_cast(classifier_lookup(&wx->cls, flow));
 +    if (rule) {
 +        if (wx_rule_is_hidden(rule)) {
 +            rule = rule->super;
 +        }
 +        return &rule->wr;
 +    } else {
 +        return NULL;
 +    }
 +}
 +
 +struct wx_for_each_thunk_aux {
 +    wdp_flow_cb_func *client_callback;
 +    void *client_aux;
 +};
 +
 +static int
 +wx_for_each_thunk(struct cls_rule *cls_rule, void *aux_)
 +{
 +    struct wx_for_each_thunk_aux *aux = aux_;
 +    struct wx_rule *rule = wx_rule_cast(cls_rule);
 +
 +    if (!wx_rule_is_hidden(rule)) {
 +        return aux->client_callback(&rule->wr, aux->client_aux);
 +    }
 +    return 0;
 +}
 +
 +static int
 +wx_flow_for_each_match(const struct wdp *wdp, const flow_t *target,
 +                       unsigned int include,
 +                       wdp_flow_cb_func *client_callback, void *client_aux)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    struct wx_for_each_thunk_aux aux;
 +    int cls_include;
 +
 +    cls_include = 0;
 +    if (include & (1u << TABLEID_HASH)) {
 +        cls_include |= CLS_INC_EXACT;
 +    }
 +    if (include & (1u << TABLEID_CLASSIFIER)) {
 +        cls_include |= CLS_INC_WILD;
 +    }
 +
 +    aux.client_callback = client_callback;
 +    aux.client_aux = client_aux;
 +    return classifier_for_each_match(&wx->cls, target, cls_include,
 +                                     wx_for_each_thunk, &aux);
 +}
 +
 +/* Obtains statistic counters for 'rule' within 'wx' and stores them into
 + * '*stats'.  If 'rule' is a wildcarded rule, the returned statistic include
 + * statistics for all of 'rule''s subrules. */
 +static void
 +query_stats(struct wx *wx, struct wx_rule *rule, struct wdp_flow_stats *stats)
 +{
 +    struct wx_rule *subrule;
 +    struct xflow_flow *xflow_flows;
 +    size_t n_xflow_flows;
 +
 +    /* Start from historical data for 'rule' itself that are no longer tracked
 +     * by the datapath.  This counts, for example, subrules that have
 +     * expired. */
 +    stats->n_packets = rule->packet_count;
 +    stats->n_bytes = rule->byte_count;
 +    stats->inserted = rule->wr.created;
 +    stats->used = LLONG_MIN;
 +    stats->tcp_flags = 0;
 +    stats->ip_tos = 0;
 +
 +    /* Prepare to ask the datapath for statistics on 'rule', or if it is
 +     * wildcarded then on all of its subrules.
 +     *
 +     * Also, add any statistics that are not tracked by the datapath for each
 +     * subrule.  This includes, for example, statistics for packets that were
 +     * executed "by hand" by ofproto via xfif_execute() but must be accounted
 +     * to a flow. */
 +    n_xflow_flows = rule->wr.cr.flow.wildcards ? list_size(&rule->list) : 1;
 +    xflow_flows = xzalloc(n_xflow_flows * sizeof *xflow_flows);
 +    if (rule->wr.cr.flow.wildcards) {
 +        size_t i = 0;
-     const struct wdp_port *port = port_array_get(&wx->ports, XFLOWP_LOCAL);
-     return port ? port->opp.hw_addr : NULL;
++        LIST_FOR_EACH (subrule, list, &rule->list) {
 +            xflow_key_from_flow(&xflow_flows[i++].key, &subrule->wr.cr.flow);
 +            stats->n_packets += subrule->packet_count;
 +            stats->n_bytes += subrule->byte_count;
 +        }
 +    } else {
 +        xflow_key_from_flow(&xflow_flows[0].key, &rule->wr.cr.flow);
 +    }
 +
 +    /* Fetch up-to-date statistics from the datapath and add them in. */
 +    if (!xfif_flow_get_multiple(wx->xfif, xflow_flows, n_xflow_flows)) {
 +        size_t i;
 +        for (i = 0; i < n_xflow_flows; i++) {
 +            struct xflow_flow *xflow_flow = &xflow_flows[i];
 +            long long int used;
 +
 +            stats->n_packets += xflow_flow->stats.n_packets;
 +            stats->n_bytes += xflow_flow->stats.n_bytes;
 +            used = xflow_flow_stats_to_msec(&xflow_flow->stats);
 +            if (used > stats->used) {
 +                stats->used = used;
 +            }
 +            stats->tcp_flags |= xflow_flow->stats.tcp_flags;
 +        }
 +    }
 +    free(xflow_flows);
 +}
 +
 +static int
 +wx_flow_get_stats(const struct wdp *wdp,
 +                  const struct wdp_rule *wdp_rule,
 +                  struct wdp_flow_stats *stats)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
 +
 +    query_stats(wx, rule, stats);
 +    return 0;
 +}
 +
 +static bool
 +wx_flow_overlaps(const struct wdp *wdp, const flow_t *flow)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    /* XXX overlap with a subrule? */
 +    return classifier_rule_overlaps(&wx->cls, flow);
 +}
 +
 +static int
 +wx_flow_put(struct wdp *wdp, const struct wdp_flow_put *put,
 +            struct wdp_flow_stats *old_stats, struct wdp_rule **rulep)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    struct wx_rule *rule;
 +    uint8_t ofp_table_id;
 +
 +    ofp_table_id = put->flow->wildcards ? TABLEID_CLASSIFIER : TABLEID_HASH;
 +    if (put->ofp_table_id != 0xff && put->ofp_table_id != ofp_table_id) {
 +        return ofp_mkerr_nicira(OFPET_FLOW_MOD_FAILED, NXFMFC_BAD_TABLE_ID);
 +    }
 +
 +    rule = wx_rule_cast(classifier_find_rule_exactly(&wx->cls, put->flow));
 +    if (rule && wx_rule_is_hidden(rule)) {
 +        rule = NULL;
 +    }
 +
 +    if (rule) {
 +        if (!(put->flags & WDP_PUT_MODIFY)) {
 +            return EEXIST;
 +        }
 +    } else {
 +        if (!(put->flags & WDP_PUT_CREATE)) {
 +            return EINVAL;
 +        }
 +        if ((put->flow->wildcards
 +             ? classifier_count_wild(&wx->cls) >= WX_MAX_WILD
 +             : classifier_count_exact(&wx->cls) >= WX_MAX_EXACT)) {
 +            /* XXX subrules should not count against exact-match limit */
 +            return ofp_mkerr(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL);
 +        }
 +    }
 +
 +    rule = wx_rule_create(NULL, put->actions, put->n_actions,
 +                          put->idle_timeout, put->hard_timeout);
 +    cls_rule_from_flow(put->flow, &rule->wr.cr);
 +    rule->wr.ofp_table_id = ofp_table_id;
 +    wx_rule_insert(wx, rule, NULL, 0);
 +
 +    if (old_stats) {
 +        /* XXX */
 +        memset(old_stats, 0, sizeof *old_stats);
 +    }
 +    if (rulep) {
 +        *rulep = &rule->wr;
 +    }
 +
 +    return 0;
 +}
 +
 +static int
 +wx_flow_delete(struct wdp *wdp, struct wdp_rule *wdp_rule,
 +               struct wdp_flow_stats *final_stats)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
 +
 +    wx_rule_remove(wx, rule);
 +    if (final_stats) {
 +        memset(final_stats, 0, sizeof *final_stats); /* XXX */
 +    }
 +    return 0;
 +}
 +
 +static int
 +wx_flush_rule(struct cls_rule *cls_rule, void *wx_)
 +{
 +    struct wx_rule *rule = wx_rule_cast(cls_rule);
 +    struct wx *wx = wx_;
 +
 +    /* Mark the flow as not installed, even though it might really be
 +     * installed, so that wx_rule_remove() doesn't bother trying to uninstall
 +     * it.  There is no point in uninstalling it individually since we are
 +     * about to blow away all the flows with xfif_flow_flush(). */
 +    rule->installed = false;
 +
 +    wx_rule_remove(wx, rule);
 +
 +    return 0;
 +}
 +
 +static int
 +wx_flow_flush(struct wdp *wdp)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    COVERAGE_INC(wx_flow_flush);
 +    classifier_for_each(&wx->cls, CLS_INC_ALL, wx_flush_rule, wx);
 +    xfif_flow_flush(wx->xfif);
 +    return 0;
 +}
 +
 +static int
 +wx_execute(struct wdp *wdp, uint16_t in_port,
 +           const union ofp_action actions[], int n_actions,
 +           const struct ofpbuf *packet)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    struct xflow_actions xflow_actions;
 +    flow_t flow;
 +    int error;
 +
 +    flow_extract((struct ofpbuf *) packet, 0, in_port, &flow);
 +    error = wx_xlate_actions(wx, actions, n_actions, &flow, packet,
 +                             NULL, &xflow_actions, NULL);
 +    if (error) {
 +        return error;
 +    }
 +    return xfif_execute(wx->xfif, ofp_port_to_xflow_port(in_port),
 +                        xflow_actions.actions, xflow_actions.n_actions,
 +                        packet);
 +}
 +
 +static int
 +wx_flow_inject(struct wdp *wdp, struct wdp_rule *wdp_rule,
 +               uint16_t in_port, const struct ofpbuf *packet)
 +{
 +    struct wx_rule *rule = wx_rule_cast(&wdp_rule->cr);
 +    int error;
 +
 +    error = wx_execute(wdp, in_port, rule->wr.actions, rule->wr.n_actions,
 +                       packet);
 +    if (!error) {
 +        rule->packet_count++;
 +        rule->byte_count += packet->size;
 +        rule->used = time_msec();
 +    }
 +    return error;
 +}
 +
 +static int
 +wx_recv_get_mask(const struct wdp *wdp, int *listen_mask)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    int xflow_listen_mask;
 +    int error;
 +
 +    error = xfif_recv_get_mask(wx->xfif, &xflow_listen_mask);
 +    if (!error) {
 +        *listen_mask = 0;
 +        if (xflow_listen_mask & XFLOWL_MISS) {
 +            *listen_mask |= 1 << WDP_CHAN_MISS;
 +        }
 +        if (xflow_listen_mask & XFLOWL_ACTION) {
 +            *listen_mask |= 1 << WDP_CHAN_ACTION;
 +        }
 +        if (xflow_listen_mask & XFLOWL_SFLOW) {
 +            *listen_mask |= 1 << WDP_CHAN_SFLOW;
 +        }
 +    }
 +    return error;
 +}
 +
 +static int
 +wx_recv_set_mask(struct wdp *wdp, int listen_mask)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    int xflow_listen_mask;
 +
 +    wx->wdp_listen_mask = listen_mask;
 +
 +    xflow_listen_mask = 0;
 +    if (listen_mask & (1 << WDP_CHAN_MISS)) {
 +        xflow_listen_mask |= XFLOWL_MISS;
 +    }
 +    if (listen_mask & (1 << WDP_CHAN_ACTION)) {
 +        xflow_listen_mask |= XFLOWL_ACTION;
 +    } else {
 +        wx_purge_ctl_packets__(wx);
 +    }
 +    if (listen_mask & (1 << WDP_CHAN_SFLOW)) {
 +        xflow_listen_mask |= XFLOWL_SFLOW;
 +    }
 +
 +    return xfif_recv_set_mask(wx->xfif, xflow_listen_mask);
 +}
 +
 +static int
 +wx_get_sflow_probability(const struct wdp *wdp, uint32_t *probability)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    return xfif_get_sflow_probability(wx->xfif, probability);
 +}
 +
 +static int
 +wx_set_sflow_probability(struct wdp *wdp, uint32_t probability)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    return xfif_set_sflow_probability(wx->xfif, probability);
 +}
 +
 +static int
 +wx_translate_xflow_msg(struct xflow_msg *msg, struct ofpbuf *payload,
 +                       struct wdp_packet *packet)
 +{
 +    packet->in_port = xflow_port_to_ofp_port(msg->port);
 +    packet->send_len = 0;
 +    packet->tun_id = 0;
 +
 +    switch (msg->type) {
 +    case _XFLOWL_MISS_NR:
 +        packet->channel = WDP_CHAN_MISS;
 +        packet->payload = payload;
 +        packet->tun_id = msg->arg;
 +        return 0;
 +
 +    case _XFLOWL_ACTION_NR:
 +        packet->channel = WDP_CHAN_ACTION;
 +        packet->payload = payload;
 +        packet->send_len = msg->arg;
 +        return 0;
 +
 +    case _XFLOWL_SFLOW_NR:
 +        /* XXX */
 +        ofpbuf_delete(payload);
 +        return ENOSYS;
 +
 +    default:
 +        VLOG_WARN_RL(&rl, "received XFLOW message of unexpected type %"PRIu32,
 +                     msg->type);
 +        ofpbuf_delete(payload);
 +        return ENOSYS;
 +    }
 +}
 +
 +static const uint8_t *
 +get_local_mac(const struct wx *wx)
 +{
-     LIST_FOR_EACH_SAFE (this, next, struct wdp_packet, list,
-                         &wx->ctl_packets) {
++    const struct wx_port *port = wx_port_get(wx, XFLOWP_LOCAL);
++    return port ? port->wdp_port.opp.hw_addr : NULL;
 +}
 +
 +/* Returns true if 'packet' is a DHCP reply to the local port.  Such a reply
 + * should be sent to the local port regardless of the flow table.
 + *
 + * We only need this, strictly speaking, when in-band control is turned on. */
 +static bool
 +wx_is_local_dhcp_reply(const struct wx *wx,
 +                       const flow_t *flow, const struct ofpbuf *packet)
 +{
 +    if (flow->dl_type == htons(ETH_TYPE_IP)
 +        && flow->nw_proto == IP_TYPE_UDP
 +        && flow->tp_src == htons(DHCP_SERVER_PORT)
 +        && flow->tp_dst == htons(DHCP_CLIENT_PORT)
 +        && packet->l7)
 +    {
 +        const uint8_t *local_mac = get_local_mac(wx);
 +        struct dhcp_header *dhcp = ofpbuf_at(
 +            packet, (char *)packet->l7 - (char *)packet->data, sizeof *dhcp);
 +        return dhcp && local_mac && eth_addr_equals(dhcp->chaddr, local_mac);
 +    }
 +
 +    return false;
 +}
 +
 +/* Determines whether 'payload' that arrived on 'in_port' is included in any of
 + * the flows in 'wx''s OpenFlow flow table.  If so, then it adds a
 + * corresponding flow to the xfif's exact-match flow table, taking ownership of
 + * 'payload', and returns true.  If not, it returns false and the caller
 + * retains ownership of 'payload'. */
 +static bool
 +wx_explode_rule(struct wx *wx, uint16_t in_port, struct ofpbuf *payload)
 +{
 +    struct wx_rule *rule;
 +    flow_t flow;
 +
 +    flow_extract(payload, 0, xflow_port_to_ofp_port(in_port), &flow);
 +
 +    if (wx_is_local_dhcp_reply(wx, &flow, payload)) {
 +        union xflow_action action;
 +
 +        memset(&action, 0, sizeof(action));
 +        action.output.type = XFLOWAT_OUTPUT;
 +        action.output.port = XFLOWP_LOCAL;
 +        xfif_execute(wx->xfif, in_port, &action, 1, payload);
 +    }
 +
 +    rule = wx_rule_lookup_valid(wx, &flow);
 +    if (!rule) {
 +        return false;
 +    }
 +
 +    if (rule->wr.cr.flow.wildcards) {
 +        rule = wx_rule_create_subrule(wx, rule, &flow);
 +        wx_rule_make_actions(wx, rule, payload);
 +    } else {
 +        if (!rule->may_install) {
 +            /* The rule is not installable, that is, we need to process every
 +             * packet, so process the current packet and set its actions into
 +             * 'subrule'. */
 +            wx_rule_make_actions(wx, rule, payload);
 +        } else {
 +            /* XXX revalidate rule if it needs it */
 +        }
 +    }
 +
 +    wx_rule_execute(wx, rule, payload, &flow);
 +    wx_rule_reinstall(wx, rule);
 +
 +    return true;
 +}
 +
 +static int
 +wx_recv(struct wdp *wdp, struct wdp_packet *packet)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    int i;
 +
 +    if (wx->n_ctl_packets) {
 +        struct wdp_packet *wdp_packet;
 +
 +        wdp_packet = CONTAINER_OF(list_pop_front(&wx->ctl_packets),
 +                                  struct wdp_packet, list);
 +        wx->n_ctl_packets--;
 +
 +        *packet = *wdp_packet;
 +        free(wdp_packet);
 +
 +        return 0;
 +    }
 +
 +    /* XXX need to avoid 50*50 potential cost for caller. */
 +    for (i = 0; i < 50; i++) {
 +        struct xflow_msg *msg;
 +        struct ofpbuf *buf;
 +        int error;
 +
 +        error = xfif_recv(wx->xfif, &buf);
 +        if (error) {
 +            return error;
 +        }
 +
 +        msg = ofpbuf_pull(buf, sizeof *msg);
 +        if (msg->type != _XFLOWL_MISS_NR
 +            || !wx_explode_rule(wx, msg->port, buf)) {
 +            return wx_translate_xflow_msg(msg, buf, packet);
 +        }
 +    }
 +    return EAGAIN;
 +}
 +
 +static void
 +wx_recv_purge_queue__(struct wx *wx, int max, int xflow_listen_mask,
 +                      int *errorp)
 +{
 +    int error;
 +
 +    error = xfif_recv_set_mask(wx->xfif, xflow_listen_mask);
 +    if (!error) {
 +        struct ofpbuf *buf;
 +
 +        while (max > 0 && (error = xfif_recv(wx->xfif, &buf)) == 0) {
 +            ofpbuf_delete(buf);
 +            max--;
 +        }
 +    }
 +    if (error && error != EAGAIN) {
 +        *errorp = error;
 +    }
 +}
 +
 +static void
 +wx_purge_ctl_packets__(struct wx *wx)
 +{
 +    struct wdp_packet *this, *next;
 +
-     struct wdp_port *port;
-     unsigned int port_no;
++    LIST_FOR_EACH_SAFE (this, next, list, &wx->ctl_packets) {
 +        list_remove(&this->list);
 +        ofpbuf_delete(this->payload);
 +        free(this);
 +    }
 +    wx->n_ctl_packets = 0;
 +}
 +
 +static int
 +wx_recv_purge(struct wdp *wdp)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +    struct xflow_stats xflow_stats;
 +    int xflow_listen_mask;
 +    int retval, error;
 +
 +    xfif_get_xf_stats(wx->xfif, &xflow_stats);
 +
 +    error = xfif_recv_get_mask(wx->xfif, &xflow_listen_mask);
 +    if (error || !(xflow_listen_mask & XFLOWL_ALL)) {
 +        return error;
 +    }
 +
 +    if (xflow_listen_mask & XFLOWL_MISS) {
 +        wx_recv_purge_queue__(wx, xflow_stats.max_miss_queue, XFLOWL_MISS,
 +                              &error);
 +    }
 +    if (xflow_listen_mask & XFLOWL_ACTION) {
 +        wx_recv_purge_queue__(wx, xflow_stats.max_action_queue, XFLOWL_ACTION,
 +                              &error);
 +        wx_purge_ctl_packets__(wx);
 +    }
 +    if (xflow_listen_mask & XFLOWL_SFLOW) {
 +        wx_recv_purge_queue__(wx, xflow_stats.max_sflow_queue, XFLOWL_SFLOW,
 +                              &error);
 +    }
 +
 +    retval = xfif_recv_set_mask(wx->xfif, xflow_listen_mask);
 +    return retval ? retval : error;
 +}
 +
 +
 +static void
 +wx_recv_wait(struct wdp *wdp)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    if (wx->n_ctl_packets) {
 +        poll_immediate_wake();
 +    } else {
 +        xfif_recv_wait(wx->xfif);
 +    }
 +}
 +
 +static int
 +wx_set_ofhooks(struct wdp *wdp, const struct ofhooks *ofhooks, void *aux)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    if (wx->ofhooks == &default_ofhooks) {
 +        mac_learning_destroy(wx->ml);
 +        wx->ml = NULL;
 +    }
 +
 +    wx->ofhooks = ofhooks;
 +    wx->aux = aux;
 +    return 0;
 +}
 +
 +static void
 +wx_revalidate(struct wdp *wdp, tag_type tag)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    tag_set_add(&wx->revalidate_set, tag);
 +}
 +
 +static void
 +wx_revalidate_all(struct wdp *wdp)
 +{
 +    struct wx *wx = wx_cast(wdp);
 +
 +    wx->revalidate_all = true;
 +}
 +\f
 +static void wx_port_update(struct wx *, const char *devname,
 +                           wdp_port_poll_cb_func *cb, void *aux);
 +static void wx_port_reinit(struct wx *, wdp_port_poll_cb_func *cb, void *aux);
 +
 +static void
 +wx_port_process_change(struct wx *wx, int error, char *devname,
 +                       wdp_port_poll_cb_func *cb, void *aux)
 +{
 +    if (error == ENOBUFS) {
 +        wx_port_reinit(wx, cb, aux);
 +    } else if (!error) {
 +        wx_port_update(wx, devname, cb, aux);
 +        free(devname);
 +    }
 +}
 +
 +static size_t
 +wx_port_refresh_group(struct wx *wx, unsigned int group)
 +{
 +    uint16_t *ports;
 +    size_t n_ports;
-     ports = xmalloc(port_array_count(&wx->ports) * sizeof *ports);
++    struct wx_port *port;
 +
 +    assert(group == WX_GROUP_ALL || group == WX_GROUP_FLOOD);
 +
-     PORT_ARRAY_FOR_EACH (port, &wx->ports, port_no) {
-         if (group == WX_GROUP_ALL || !(port->opp.config & OFPPC_NO_FLOOD)) {
-             ports[n_ports++] = port_no;
++    ports = xmalloc(hmap_count(&wx->ports) * sizeof *ports);
 +    n_ports = 0;
-     struct wdp_port *wdp_port;
-     unsigned int port_no;
++    HMAP_FOR_EACH (port, hmap_node, &wx->ports) {
++        const struct ofp_phy_port *opp = &port->wdp_port.opp;
++        if (group == WX_GROUP_ALL || !(opp->config & OFPPC_NO_FLOOD)) {
++            ports[n_ports++] = port->xflow_port;
 +        }
 +    }
 +    xfif_port_group_set(wx->xfif, group, ports, n_ports);
 +    free(ports);
 +
 +    return n_ports;
 +}
 +
 +static void
 +wx_port_refresh_groups(struct wx *wx)
 +{
 +    wx_port_refresh_group(wx, WX_GROUP_FLOOD);
 +    wx_port_refresh_group(wx, WX_GROUP_ALL);
 +}
 +
 +static void
 +wx_port_reinit(struct wx *wx, wdp_port_poll_cb_func *cb, void *aux)
 +{
 +    struct svec devnames;
-     PORT_ARRAY_FOR_EACH (wdp_port, &wx->ports, port_no) {
-         svec_add (&devnames, (char *) wdp_port->opp.name);
++    struct wx_port *wx_port;
 +    struct xflow_port *xflow_ports;
 +    size_t n_xflow_ports;
 +    size_t i;
 +
 +    svec_init(&devnames);
- static struct wdp_port *
- make_wdp_port(const struct xflow_port *xflow_port)
++    HMAP_FOR_EACH (wx_port, hmap_node, &wx->ports) {
++        svec_add (&devnames, (char *) wx_port->wdp_port.opp.name);
 +    }
 +    xfif_port_list(wx->xfif, &xflow_ports, &n_xflow_ports);
 +    for (i = 0; i < n_xflow_ports; i++) {
 +        svec_add(&devnames, xflow_ports[i].devname);
 +    }
 +    free(xflow_ports);
 +
 +    svec_sort_unique(&devnames);
 +    for (i = 0; i < devnames.n; i++) {
 +        wx_port_update(wx, devnames.names[i], cb, aux);
 +    }
 +    svec_destroy(&devnames);
 +
 +    wx_port_refresh_groups(wx);
 +}
 +
-     wdp_port = xmalloc(sizeof *wdp_port);
++static struct wx_port *
++make_wx_port(const struct xflow_port *xflow_port)
 +{
 +    struct netdev_options netdev_options;
 +    enum netdev_flags flags;
++    struct wx_port *wx_port;
 +    struct wdp_port *wdp_port;
 +    struct netdev *netdev;
 +    bool carrier;
 +    int error;
 +
 +    memset(&netdev_options, 0, sizeof netdev_options);
 +    netdev_options.name = xflow_port->devname;
 +    netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
 +
 +    error = netdev_open(&netdev_options, &netdev);
 +    if (error) {
 +        VLOG_WARN_RL(&rl, "ignoring port %s (%"PRIu16") because netdev %s "
 +                     "cannot be opened (%s)",
 +                     xflow_port->devname, xflow_port->port,
 +                     xflow_port->devname, strerror(error));
 +        return NULL;
 +    }
 +
-     return wdp_port;
++    wx_port = xmalloc(sizeof *wx_port);
++    wx_port->xflow_port = xflow_port->port;
++    wdp_port = &wx_port->wdp_port;
 +    wdp_port->netdev = netdev;
 +    wdp_port->opp.port_no = xflow_port_to_ofp_port(xflow_port->port);
 +    netdev_get_etheraddr(netdev, wdp_port->opp.hw_addr);
 +    strncpy((char *) wdp_port->opp.name, xflow_port->devname,
 +            sizeof wdp_port->opp.name);
 +    wdp_port->opp.name[sizeof wdp_port->opp.name - 1] = '\0';
 +
 +    netdev_get_flags(netdev, &flags);
 +    wdp_port->opp.config = flags & NETDEV_UP ? 0 : OFPPC_PORT_DOWN;
 +
 +    netdev_get_carrier(netdev, &carrier);
 +    wdp_port->opp.state = carrier ? 0 : OFPPS_LINK_DOWN;
 +
 +    netdev_get_features(netdev,
 +                        &wdp_port->opp.curr, &wdp_port->opp.advertised,
 +                        &wdp_port->opp.supported, &wdp_port->opp.peer);
 +
 +    wdp_port->devname = xstrdup(xflow_port->devname);
 +    wdp_port->internal = (xflow_port->flags & XFLOW_PORT_INTERNAL) != 0;
-     if (port_array_get(&wx->ports, xflow_port->port)) {
++    return wx_port;
 +}
 +
 +static bool
 +wx_port_conflicts(const struct wx *wx, const struct xflow_port *xflow_port)
 +{
- wdp_port_equal(const struct wdp_port *a_, const struct wdp_port *b_)
++    if (wx_port_get(wx, xflow_port->port)) {
 +        VLOG_WARN_RL(&rl, "ignoring duplicate port %"PRIu16" in datapath",
 +                     xflow_port->port);
 +        return true;
 +    } else if (shash_find(&wx->port_by_name, xflow_port->devname)) {
 +        VLOG_WARN_RL(&rl, "ignoring duplicate device %s in datapath",
 +                     xflow_port->devname);
 +        return true;
 +    } else {
 +        return false;
 +    }
 +}
 +
 +static int
-     const struct ofp_phy_port *a = &a_->opp;
-     const struct ofp_phy_port *b = &b_->opp;
++wx_port_equal(const struct wx_port *a_, const struct wx_port *b_)
 +{
- wx_port_install(struct wx *wx, struct wdp_port *wdp_port)
++    const struct ofp_phy_port *a = &a_->wdp_port.opp;
++    const struct ofp_phy_port *b = &b_->wdp_port.opp;
 +
 +    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)
 +            && a->state == b->state
 +            && a->config == b->config
 +            && a->curr == b->curr
 +            && a->advertised == b->advertised
 +            && a->supported == b->supported
 +            && a->peer == b->peer);
 +}
 +
 +static void
-     uint16_t xflow_port = ofp_port_to_xflow_port(wdp_port->opp.port_no);
-     const char *netdev_name = (const char *) wdp_port->opp.name;
++wx_port_install(struct wx *wx, struct wx_port *wx_port)
 +{
-     netdev_monitor_add(wx->netdev_monitor, wdp_port->netdev);
-     port_array_set(&wx->ports, xflow_port, wdp_port);
-     shash_add(&wx->port_by_name, netdev_name, wdp_port);
++    const struct ofp_phy_port *opp = &wx_port->wdp_port.opp;
++    uint16_t xflow_port = ofp_port_to_xflow_port(opp->port_no);
++    const char *name = (const char *) opp->name;
 +
- wx_port_remove(struct wx *wx, struct wdp_port *wdp_port)
++    netdev_monitor_add(wx->netdev_monitor, wx_port->wdp_port.netdev);
++    hmap_insert(&wx->ports, &wx_port->hmap_node, hash_int(xflow_port, 0));
++    shash_add(&wx->port_by_name, name, wx_port);
 +}
 +
 +static void
-     uint16_t xflow_port = ofp_port_to_xflow_port(wdp_port->opp.port_no);
++wx_port_remove(struct wx *wx, struct wx_port *wx_port)
 +{
-     netdev_monitor_remove(wx->netdev_monitor, wdp_port->netdev);
-     port_array_delete(&wx->ports, xflow_port);
-     shash_delete(&wx->port_by_name,
-                  shash_find(&wx->port_by_name, (char *) wdp_port->opp.name));
++    const struct ofp_phy_port *opp = &wx_port->wdp_port.opp;
++    const char *name = (const char *) opp->name;
 +
- wx_port_free(struct wdp_port *wdp_port)
++    netdev_monitor_remove(wx->netdev_monitor, wx_port->wdp_port.netdev);
++    hmap_remove(&wx->ports, &wx_port->hmap_node);
++    shash_delete(&wx->port_by_name, shash_find(&wx->port_by_name, name));
 +}
 +
 +static void
-     wdp_port_free(wdp_port);
-     free(wdp_port);
++wx_port_free(struct wx_port *wx_port)
 +{
-     struct wdp_port *old_wdp_port;
-     struct wdp_port *new_wdp_port;
++    if (wx_port) {
++        wdp_port_free(&wx_port->wdp_port);
++        free(wx_port);
++    }
 +}
 +
 +static void
 +wx_port_update(struct wx *wx, const char *devname,
 +               wdp_port_poll_cb_func *cb, void *aux)
 +{
 +    struct xflow_port xflow_port;
-     /* Find the old wdp_port. */
-     old_wdp_port = shash_find_data(&wx->port_by_name, devname);
++    struct wx_port *old_wx_port;
++    struct wx_port *new_wx_port;
 +    int error;
 +
 +    COVERAGE_INC(wx_update_port);
 +
 +    /* Query the datapath for port information. */
 +    error = xfif_port_query_by_name(wx->xfif, devname, &xflow_port);
 +
-         if (!old_wdp_port) {
++    /* Find the old wx_port. */
++    old_wx_port = shash_find_data(&wx->port_by_name, devname);
 +    if (!error) {
-             old_wdp_port = port_array_get(&wx->ports, xflow_port.port);
++        if (!old_wx_port) {
 +            /* There's no port named 'devname' but there might be a port with
 +             * the same port number.  This could happen if a port is deleted
 +             * and then a new one added in its place very quickly, or if a port
 +             * is renamed.  In the former case we want to send an OFPPR_DELETE
 +             * and an OFPPR_ADD, and in the latter case we want to send a
 +             * single OFPPR_MODIFY.  We can distinguish the cases by comparing
 +             * the old port's ifindex against the new port, or perhaps less
 +             * reliably but more portably by comparing the old port's MAC
 +             * against the new port's MAC.  However, this code isn't that smart
 +             * and always sends an OFPPR_MODIFY (XXX). */
-     /* Create a new wdp_port. */
-     new_wdp_port = !error ? make_wdp_port(&xflow_port) : NULL;
++            old_wx_port = wx_port_get(wx, xflow_port.port);
 +        }
 +    } else if (error != ENOENT && error != ENODEV) {
 +        VLOG_WARN_RL(&rl, "xfif_port_query_by_name returned unexpected error "
 +                     "%s", strerror(error));
 +        return;
 +    }
 +
-     if (!old_wdp_port && !new_wdp_port) {
++    /* Create a new wx_port. */
++    new_wx_port = !error ? make_wx_port(&xflow_port) : NULL;
 +
 +    /* Eliminate a few pathological cases. */
-     } else if (old_wdp_port && new_wdp_port) {
++    if (!old_wx_port && !new_wx_port) {
 +        return;
-          * OpenFlow bits from old_wdp_port.  (make_wdp_port() only sets
++    } else if (old_wx_port && new_wx_port) {
 +        /* Most of the 'config' bits are OpenFlow soft state, but
 +         * OFPPC_PORT_DOWN is maintained by the kernel.  So transfer the
-         new_wdp_port->opp.config |= old_wdp_port->opp.config & ~OFPPC_PORT_DOWN;
++         * OpenFlow bits from old_wx_port.  (make_wx_port() only sets
 +         * OFPPC_PORT_DOWN and leaves the other bits 0.)  */
-         if (wdp_port_equal(old_wdp_port, new_wdp_port)) {
++        struct ofp_phy_port *new_opp = &new_wx_port->wdp_port.opp;
++        struct ofp_phy_port *old_opp = &old_wx_port->wdp_port.opp;
++        new_opp->config |= old_opp->config & ~OFPPC_PORT_DOWN;
 +
-             wx_port_free(new_wdp_port);
++        if (wx_port_equal(old_wx_port, new_wx_port)) {
 +            /* False alarm--no change. */
-     if (old_wdp_port) {
-         wx_port_remove(wx, old_wdp_port);
++            wx_port_free(new_wx_port);
 +            return;
 +        }
 +    }
 +
 +    /* Now deal with the normal cases. */
-     if (new_wdp_port) {
-         wx_port_install(wx, new_wdp_port);
++    if (old_wx_port) {
++        wx_port_remove(wx, old_wx_port);
 +    }
-     if (!old_wdp_port) {
-         (*cb)(&new_wdp_port->opp, OFPPR_ADD, aux);
-     } else if (!new_wdp_port) {
-         (*cb)(&old_wdp_port->opp, OFPPR_DELETE, aux);
++    if (new_wx_port) {
++        wx_port_install(wx, new_wx_port);
 +    }
 +
 +    /* Call back. */
-         (*cb)(&new_wdp_port->opp, OFPPR_MODIFY, aux);
++    if (!old_wx_port) {
++        (*cb)(&new_wx_port->wdp_port.opp, OFPPR_ADD, aux);
++    } else if (!new_wx_port) {
++        (*cb)(&old_wx_port->wdp_port.opp, OFPPR_DELETE, aux);
 +    } else {
-     wx_port_free(old_wdp_port);
++        (*cb)(&new_wx_port->wdp_port.opp, OFPPR_MODIFY, aux);
 +    }
 +
 +    /* Update port groups. */
 +    wx_port_refresh_groups(wx);
 +
 +    /* Clean up. */
-             struct wdp_port *wdp_port = make_wdp_port(xflow_port);
-             if (wdp_port) {
-                 wx_port_install(wx, wdp_port);
++    wx_port_free(old_wx_port);
 +}
 +
 +static int
 +wx_port_init(struct wx *wx)
 +{
 +    struct xflow_port *ports;
 +    size_t n_ports;
 +    size_t i;
 +    int error;
 +
 +    error = xfif_port_list(wx->xfif, &ports, &n_ports);
 +    if (error) {
 +        return error;
 +    }
 +
 +    for (i = 0; i < n_ports; i++) {
 +        const struct xflow_port *xflow_port = &ports[i];
 +        if (!wx_port_conflicts(wx, xflow_port)) {
++            struct wx_port *wx_port = make_wx_port(xflow_port);
++            if (wx_port) {
++                wx_port_install(wx, wx_port);
 +            }
 +        }
 +    }
 +    free(ports);
 +    wx_port_refresh_groups(wx);
 +    return 0;
 +}
++
++/* Returns the port in 'wx' with xflow port number 'xflow_port'. */
++static struct wx_port *
++wx_port_get(const struct wx *wx, uint16_t xflow_port)
++{
++    struct wx_port *port;
++
++    HMAP_FOR_EACH_IN_BUCKET (port, hmap_node, hash_int(xflow_port, 0),
++                             &wx->ports) {
++        if (port->xflow_port == xflow_port) {
++            return port;
++        }
++    }
++    return NULL;
++}
 +\f
 +void
 +wdp_xflow_register(void)
 +{
 +    static const struct wdp_class wdp_xflow_class = {
 +        NULL,                   /* name */
 +        wx_run,
 +        wx_wait,
 +        wx_enumerate,
 +        wx_open,
 +        wx_close,
 +        wx_get_all_names,
 +        wx_destroy,
 +        wx_get_features,
 +        wx_get_stats,
 +        wx_get_table_stats,
 +        wx_get_drop_frags,
 +        wx_set_drop_frags,
 +        wx_port_add,
 +        wx_port_del,
 +        wx_port_query_by_number,
 +        wx_port_query_by_name,
 +        wx_port_list,
 +        wx_port_set_config,
 +        wx_port_poll,
 +        wx_port_poll_wait,
 +        wx_flow_get,
 +        wx_flow_match,
 +        wx_flow_for_each_match,
 +        wx_flow_get_stats,
 +        wx_flow_overlaps,
 +        wx_flow_put,
 +        wx_flow_delete,
 +        wx_flow_flush,
 +        wx_flow_inject,
 +        wx_execute,
 +        wx_recv_get_mask,
 +        wx_recv_set_mask,
 +        wx_get_sflow_probability,
 +        wx_set_sflow_probability,
 +        wx_recv,
 +        wx_recv_purge,
 +        wx_recv_wait,
 +        wx_set_ofhooks,
 +        wx_revalidate,
 +        wx_revalidate_all,
 +    };
 +
 +    static bool inited = false;
 +
 +    struct svec types;
 +    const char *type;
 +    bool registered;
 +    int i;
 +
 +    if (inited) {
 +        return;
 +    }
 +    inited = true;
 +
 +    svec_init(&types);
 +    xf_enumerate_types(&types);
 +
 +    registered = false;
 +    SVEC_FOR_EACH (i, type, &types) {
 +        struct wdp_class *class;
 +
 +        class = xmalloc(sizeof *class);
 +        *class = wdp_xflow_class;
 +        class->type = xstrdup(type);
 +        if (registered) {
 +            class->run = NULL;
 +            class->wait = NULL;
 +        }
 +        if (!wdp_register_provider(class)) {
 +            registered = true;
 +        }
 +    }
 +
 +    svec_destroy(&types);
 +}
 +\f
 +static bool
 +default_normal_ofhook_cb(const flow_t *flow, const struct ofpbuf *packet,
 +                         struct xflow_actions *actions, tag_type *tags,
 +                         uint16_t *nf_output_iface, void *wx_)
 +{
 +    struct wx *wx = wx_;
 +    int out_port;
 +
 +    /* Drop frames for reserved multicast addresses. */
 +    if (eth_addr_is_reserved(flow->dl_dst)) {
 +        return true;
 +    }
 +
 +    /* Learn source MAC (but don't try to learn from revalidation). */
 +    if (packet != NULL) {
 +        tag_type rev_tag = mac_learning_learn(wx->ml, flow->dl_src,
 +                                              0, flow->in_port,
 +                                              GRAT_ARP_LOCK_NONE);
 +        if (rev_tag) {
 +            /* The log messages here could actually be useful in debugging,
 +             * so keep the rate limit relatively high. */
 +            static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(30, 300);
 +            VLOG_DBG_RL(&rl, "learned that "ETH_ADDR_FMT" is on port %"PRIu16,
 +                        ETH_ADDR_ARGS(flow->dl_src), flow->in_port);
 +            tag_set_add(&wx->revalidate_set, rev_tag);
 +        }
 +    }
 +
 +    /* Determine output port. */
 +    out_port = mac_learning_lookup_tag(wx->ml, flow->dl_dst, 0, tags,
 +                                       NULL);
 +    if (out_port < 0) {
 +        add_output_group_action(actions, WX_GROUP_FLOOD, nf_output_iface);
 +    } else if (out_port != flow->in_port) {
 +        xflow_actions_add(actions, XFLOWAT_OUTPUT)->output.port = out_port;
 +        *nf_output_iface = out_port;
 +    } else {
 +        /* Drop. */
 +    }
 +
 +    return true;
 +}
 +
 +static const struct ofhooks default_ofhooks = {
 +    NULL,
 +    default_normal_ofhook_cb,
 +    NULL,
 +    NULL
 +};
diff --combined tests/automake.mk
@@@ -12,6 -12,7 +12,7 @@@ TESTSUITE_AT = 
        tests/check-structs.at \
        tests/daemon.at \
        tests/daemon-py.at \
+       tests/ovs-ofctl.at \
        tests/vconn.at \
        tests/dir_name.at \
        tests/aes128.at \
@@@ -171,10 -172,7 +172,10 @@@ tests_test_aes128_SOURCES = tests/test-
  tests_test_aes128_LDADD = lib/libopenvswitch.a
  
  noinst_PROGRAMS += tests/test-classifier
 -tests_test_classifier_SOURCES = tests/test-classifier.c
 +tests_test_classifier_SOURCES = \
 +      tests/test-classifier.c \
 +      tests/test-command-line.c \
 +      tests/test-command-line.h
  tests_test_classifier_LDADD = lib/libopenvswitch.a
  
  noinst_PROGRAMS += tests/test-csum
diff --combined tests/test-classifier.c
@@@ -32,7 -32,6 +32,7 @@@
  #include "command-line.h"
  #include "flow.h"
  #include "packets.h"
 +#include "test-command-line.h"
  
  #undef NDEBUG
  #include <assert.h>
@@@ -84,7 -83,7 +84,7 @@@ tcls_count_exact(const struct tcls *tcl
  
      n_exact = 0;
      for (i = 0; i < tcls->n_rules; i++) {
 -        n_exact += tcls->rules[i]->cls_rule.wc.wildcards == 0;
 +        n_exact += tcls->rules[i]->cls_rule.flow.wildcards == 0;
      }
      return n_exact;
  }
@@@ -95,28 -94,23 +95,28 @@@ tcls_is_empty(const struct tcls *tcls
      return tcls->n_rules == 0;
  }
  
 +static unsigned int
 +effective_priority(const flow_t *flow)
 +{
 +    return flow->wildcards ? flow->priority : MAX(flow->priority, UINT16_MAX);
 +}
 +
  static struct test_rule *
  tcls_insert(struct tcls *tcls, const struct test_rule *rule)
  {
 +    unsigned int priority = effective_priority(&rule->cls_rule.flow);
      size_t i;
  
 -    assert(rule->cls_rule.wc.wildcards || rule->cls_rule.priority == UINT_MAX);
      for (i = 0; i < tcls->n_rules; i++) {
          const struct cls_rule *pos = &tcls->rules[i]->cls_rule;
 -        if (pos->priority == rule->cls_rule.priority
 -            && pos->wc.wildcards == rule->cls_rule.wc.wildcards
 -            && flow_equal(&pos->flow, &rule->cls_rule.flow)) {
 -            /* Exact match.
 -             * XXX flow_equal should ignore wildcarded fields */
 +        if (pos->flow.priority == priority
 +            && pos->flow.wildcards == rule->cls_rule.flow.wildcards
 +            && flow_equal_headers(&pos->flow, &rule->cls_rule.flow)) {
 +            /* Exact match. */
              free(tcls->rules[i]);
              tcls->rules[i] = xmemdup(rule, sizeof *rule);
              return tcls->rules[i];
 -        } else if (pos->priority < rule->cls_rule.priority) {
 +        } else if (pos->flow.priority < priority) {
              break;
          }
      }
@@@ -170,18 -164,18 +170,18 @@@ match(const struct cls_rule *wild, cons
          void *wild_field = (char *) &wild->flow + f->ofs;
          void *fixed_field = (char *) fixed + f->ofs;
  
 -        if ((wild->wc.wildcards & f->wildcards) == f->wildcards ||
 +        if ((wild->flow.wildcards & f->wildcards) == f->wildcards ||
              !memcmp(wild_field, fixed_field, f->len)) {
              /* Definite match. */
              continue;
          }
  
 -        if (wild->wc.wildcards & f->wildcards) {
 +        if (wild->flow.wildcards & f->wildcards) {
              uint32_t test = read_uint32(wild_field);
              uint32_t ip = read_uint32(fixed_field);
              int shift = (f_idx == CLS_F_IDX_NW_SRC
                           ? OFPFW_NW_SRC_SHIFT : OFPFW_NW_DST_SHIFT);
 -            uint32_t mask = flow_nw_bits_to_mask(wild->wc.wildcards, shift);
 +            uint32_t mask = flow_nw_bits_to_mask(wild->flow.wildcards, shift);
              if (!((test ^ ip) & mask)) {
                  continue;
              }
@@@ -199,7 -193,7 +199,7 @@@ tcls_lookup(const struct tcls *cls, con
  
      for (i = 0; i < cls->n_rules; i++) {
          struct test_rule *pos = cls->rules[i];
 -        uint32_t wildcards = pos->cls_rule.wc.wildcards;
 +        uint32_t wildcards = pos->cls_rule.flow.wildcards;
          if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
              && match(&pos->cls_rule, flow)) {
              return &pos->cls_rule;
@@@ -217,7 -211,7 +217,7 @@@ tcls_delete_matches(struct tcls *cls
  
      for (i = 0; i < cls->n_rules; ) {
          struct test_rule *pos = cls->rules[i];
 -        uint32_t wildcards = pos->cls_rule.wc.wildcards;
 +        uint32_t wildcards = pos->cls_rule.flow.wildcards;
          if (include & (wildcards ? CLS_INC_WILD : CLS_INC_EXACT)
              && match(target, &pos->cls_rule.flow)) {
              tcls_remove(cls, pos);
@@@ -386,6 -380,7 +386,6 @@@ compare_classifiers(struct classifier *
                 ETH_ADDR_LEN);
          flow.nw_proto = nw_proto_values[get_value(&x, N_NW_PROTO_VALUES)];
          flow.nw_tos = nw_tos_values[get_value(&x, N_NW_TOS_VALUES)];
 -        memset(flow.reserved, 0, sizeof flow.reserved);
  
          for (include = 1; include <= 3; include++) {
              cr0 = lookup_with_include_bits(cls, &flow, include);
                  const struct test_rule *tr0 = test_rule_from_cls_rule(cr0);
                  const struct test_rule *tr1 = test_rule_from_cls_rule(cr1);
  
 -                assert(flow_equal(&cr0->flow, &cr1->flow));
 -                assert(cr0->wc.wildcards == cr1->wc.wildcards);
 -                assert(cr0->priority == cr1->priority);
 -                /* Skip nw_src_mask and nw_dst_mask, because they are derived
 -                 * members whose values are used only for optimization. */
 +                assert(flow_equal_headers(&cr0->flow, &cr1->flow));
 +                assert(cr0->flow.wildcards == cr1->flow.wildcards);
 +                assert(cr0->flow.priority == cr1->flow.priority);
 +                /* Skip nw_src_mask, nw_dst_mask, and dl_tci_mask, because they
 +                 * are derived members used only for optimization. */
                  assert(tr0->aux == tr1->aux);
              }
          }
      }
  }
  
 -static void
 +static int
  free_rule(struct cls_rule *cls_rule, void *cls)
  {
      classifier_remove(cls, cls_rule);
      free(test_rule_from_cls_rule(cls_rule));
 +    return 0;
  }
  
  static void
@@@ -436,7 -430,7 +436,7 @@@ check_tables(const struct classifier *c
          if (!hmap_is_empty(&cls->tables[i])) {
              found_tables++;
          }
-         HMAP_FOR_EACH (bucket, struct cls_bucket, hmap_node, &cls->tables[i]) {
+         HMAP_FOR_EACH (bucket, hmap_node, &cls->tables[i]) {
              found_buckets++;
              assert(!list_is_empty(&bucket->rules));
              found_rules += list_size(&bucket->rules);
@@@ -459,14 -453,15 +459,14 @@@ make_rule(int wc_fields, unsigned int p
  {
      const struct cls_field *f;
      struct test_rule *rule;
 -    uint32_t wildcards;
      flow_t flow;
  
 -    wildcards = 0;
      memset(&flow, 0, sizeof flow);
 +    flow.priority = priority;
      for (f = &cls_fields[0]; f < &cls_fields[CLS_N_FIELDS]; f++) {
          int f_idx = f - cls_fields;
          if (wc_fields & (1u << f_idx)) {
 -            wildcards |= f->wildcards;
 +            flow.wildcards |= f->wildcards;
          } else {
              int value_idx = (value_pat & (1u << f_idx)) != 0;
              memcpy((char *) &flow + f->ofs, values[f_idx][value_idx], f->len);
      }
  
      rule = xzalloc(sizeof *rule);
 -    cls_rule_from_flow(&flow, wildcards, !wildcards ? UINT_MAX : priority,
 -                       &rule->cls_rule);
 +    cls_rule_from_flow(&flow, &rule->cls_rule);
      return rule;
  }
  
@@@ -963,9 -959,9 +963,9 @@@ test_many_rules_in_different_tables(in
              struct test_rule *rule = xmemdup(tcls.rules[rand() % tcls.n_rules],
                                               sizeof(struct test_rule));
              int include = rand() % 2 ? CLS_INC_WILD : CLS_INC_EXACT;
 -            include |= (rule->cls_rule.wc.wildcards
 +            include |= (rule->cls_rule.flow.wildcards
                          ? CLS_INC_WILD : CLS_INC_EXACT);
 -            classifier_for_each_match(&cls, &rule->cls_rule, include,
 +            classifier_for_each_match(&cls, &rule->cls_rule.flow, include,
                                        free_rule, &cls);
              tcls_delete_matches(&tcls, &rule->cls_rule, include);
              compare_classifiers(&cls, &tcls);
      }
  }
  \f
 -static const struct command commands[] = {
 -    {"empty", 0, 0, test_empty},
 -    {"destroy-null", 0, 0, test_destroy_null},
 -    {"single-rule", 0, 0, test_single_rule},
 -    {"rule-replacement", 0, 0, test_rule_replacement},
 -    {"two-rules-in-one-bucket", 0, 0, test_two_rules_in_one_bucket},
 -    {"two-rules-in-one-table", 0, 0, test_two_rules_in_one_table},
 -    {"two-rules-in-different-tables", 0, 0,
 -     test_two_rules_in_different_tables},
 -    {"many-rules-in-one-bucket", 0, 0, test_many_rules_in_one_bucket},
 -    {"many-rules-in-one-table", 0, 0, test_many_rules_in_one_table},
 -    {"many-rules-in-different-tables", 0, 0,
 -     test_many_rules_in_different_tables},
 -    {NULL, 0, 0, NULL},
 -};
 -
  int
  main(int argc, char *argv[])
  {
 +    static const struct command all_commands[] = {
 +        { "empty", 0, 0, test_empty },
 +        { "destroy-null", 0, 0, test_destroy_null },
 +        { "single-rule", 0, 0, test_single_rule },
 +        { "rule-replacement", 0, 0, test_rule_replacement },
 +        { "two-rules-in-one-bucket", 0, 0, test_two_rules_in_one_bucket },
 +        { "two-rules-in-one-table", 0, 0, test_two_rules_in_one_table },
 +        { "two-rules-in-different-tables", 0, 0,
 +          test_two_rules_in_different_tables },
 +        { "many-rules-in-one-bucket", 0, 0,
 +          test_many_rules_in_one_bucket },
 +        { "many-rules-in-one-table", 0, 0, test_many_rules_in_one_table },
 +        { "many-rules-in-different-tables", 0, 0,
 +          test_many_rules_in_different_tables },
 +        { NULL, 0, 0, NULL },
 +    };
 +
 +    set_program_name(argv[0]);
      init_values();
 -    run_command(argc - 1, argv + 1, commands);
 +    parse_test_options(argc, argv, all_commands);
 +    run_command(argc - 1, argv + 1, all_commands);
      return 0;
  }
  #include "compiler.h"
  #include "daemon.h"
  #include "learning-switch.h"
+ #include "ofp-parse.h"
++#include "ofp-util.h"
  #include "ofpbuf.h"
  #include "openflow/openflow.h"
  #include "poll-loop.h"
  #include "rconn.h"
+ #include "shash.h"
  #include "stream-ssl.h"
  #include "timeval.h"
  #include "unixctl.h"
@@@ -49,10 -51,11 +52,11 @@@ struct switch_ 
      struct rconn *rconn;
  };
  
- /* Learn the ports on which MAC addresses appear? */
+ /* -H, --hub: Learn the ports on which MAC addresses appear? */
  static bool learn_macs = true;
  
- /* Set up flows?  (If not, every packet is processed at the controller.) */
+ /* -n, --noflow: Set up flows?  (If not, every packet is processed at the
+  * controller.) */
  static bool set_up_flows = true;
  
  /* -N, --normal: Use "NORMAL" action instead of explicit port? */
@@@ -68,12 -71,15 +72,15 @@@ static int max_idle = 60
   * of their messages (for debugging fail-open mode). */
  static bool mute = false;
  
- /* -q, --queue: OpenFlow queue to use, or the default queue if UINT32_MAX. */
- static uint32_t queue_id = UINT32_MAX;
+ /* -q, --queue: default OpenFlow queue, none if UINT32_MAX. */
+ static uint32_t default_queue = UINT32_MAX;
+ /* -Q, --port-queue: map from port name to port number (cast to void *). */
+ static struct shash port_queues = SHASH_INITIALIZER(&port_queues);
  
  /* --with-flows: File with flows to send to switch, or null to not load
   * any default flows. */
- static FILE *flow_file = NULL;
+ static struct ovs_queue default_flows = OVS_QUEUE_INITIALIZER;
  
  /* --unixctl: Name of unixctl socket, or null to use the default. */
  static char *unixctl_path = NULL;
@@@ -107,7 -113,6 +114,6 @@@ main(int argc, char *argv[]
      for (i = optind; i < argc; i++) {
          const char *name = argv[i];
          struct vconn *vconn;
-         int retval;
  
          retval = vconn_open(name, OFP_VERSION, &vconn);
          if (!retval) {
  
      while (n_switches > 0 || n_listeners > 0) {
          int iteration;
-         int i;
  
          /* Accept connections on listening vconns. */
          for (i = 0; i < n_listeners && n_switches < MAX_SWITCHES; ) {
              struct vconn *new_vconn;
-             int retval;
  
              retval = pvconn_accept(listeners[i], OFP_VERSION, &new_vconn);
              if (!retval || retval == EAGAIN) {
              bool progress = false;
              for (i = 0; i < n_switches; ) {
                  struct switch_ *this = &switches[i];
-                 int retval = do_switching(this);
+                 retval = do_switching(this);
                  if (!retval || retval == EAGAIN) {
                      if (!retval) {
                          progress = true;
  static void
  new_switch(struct switch_ *sw, struct vconn *vconn)
  {
+     struct lswitch_config cfg;
      sw->rconn = rconn_create(60, 0);
      rconn_connect_unreliably(sw->rconn, vconn, NULL);
  
-     /* If it was set, rewind 'flow_file' to the beginning, since a
-      * previous call to lswitch_create() will leave the stream at the
-      * end. */
-     if (flow_file) {
-         rewind(flow_file);
-     }
-     sw->lswitch = lswitch_create(sw->rconn, learn_macs, exact_flows,
-                                  set_up_flows ? max_idle : -1,
-                                  action_normal, flow_file);
-     lswitch_set_queue(sw->lswitch, queue_id);
+     cfg.mode = (action_normal ? LSW_NORMAL
+                 : learn_macs ? LSW_LEARN
+                 : LSW_FLOOD);
+     cfg.max_idle = set_up_flows ? max_idle : -1;
+     cfg.default_flows = default_flows.head;
+     cfg.default_queue = default_queue;
+     cfg.port_queues = &port_queues;
+     sw->lswitch = lswitch_create(sw->rconn, &cfg);
  }
  
  static int
@@@ -254,6 -257,45 +258,52 @@@ do_switching(struct switch_ *sw
              : EAGAIN);
  }
  
 -    while ((b = parse_ofp_add_flow_file(stream)) != NULL) {
+ static void
+ read_flow_file(const char *name)
+ {
++    bool table_id_enabled = false;
++    uint8_t table_idx;
+     struct ofpbuf *b;
+     FILE *stream;
+     stream = fopen(optarg, "r");
+     if (!stream) {
+         ovs_fatal(errno, "%s: open", name);
+     }
++    while ((b = parse_ofp_add_flow_file(stream, &table_idx)) != NULL) {
++        if ((table_idx != 0xff) != table_id_enabled) {
++            table_id_enabled = table_idx != 0xff;
++            queue_push_tail(&default_flows,
++                            make_nxt_flow_mod_table_id(table_id_enabled));
++        }
+         queue_push_tail(&default_flows, b);
+     }
+     fclose(stream);
+ }
+ static void
+ add_port_queue(char *s)
+ {
+     char *save_ptr = NULL;
+     char *port_name;
+     char *queue_id;
+     port_name = strtok_r(s, ":", &save_ptr);
+     queue_id = strtok_r(NULL, "", &save_ptr);
+     if (!queue_id) {
+         ovs_fatal(0, "argument to -Q or --port-queue should take the form "
+                   "\"<port-name>:<queue-id>\"");
+     }
+     if (!shash_add_once(&port_queues, port_name,
+                         (void *) (uintptr_t) atoi(queue_id))) {
+         ovs_fatal(0, "<port-name> arguments for -Q or --port-queue must "
+                   "be unique");
+     }
+ }
  static void
  parse_options(int argc, char *argv[])
  {
          {"max-idle",    required_argument, 0, OPT_MAX_IDLE},
          {"mute",        no_argument, 0, OPT_MUTE},
          {"queue",       required_argument, 0, 'q'},
+         {"port-queue",  required_argument, 0, 'Q'},
          {"with-flows",  required_argument, 0, OPT_WITH_FLOWS},
          {"unixctl",     required_argument, 0, OPT_UNIXCTL},
          {"help",        no_argument, 0, 'h'},
              break;
  
          case 'q':
-             queue_id = atoi(optarg);
+             default_queue = atoi(optarg);
+             break;
+         case 'Q':
+             add_port_queue(optarg);
              break;
  
          case OPT_WITH_FLOWS:
-             flow_file = fopen(optarg, "r");
-             if (flow_file == NULL) {
-                 ovs_fatal(errno, "%s: open", optarg);
-             }
+             read_flow_file(optarg);
              break;
  
          case OPT_UNIXCTL:
          }
      }
      free(short_options);
+     if (!shash_is_empty(&port_queues) || default_queue != UINT32_MAX) {
+         if (action_normal) {
+             ovs_error(0, "queue IDs are incompatible with -N or --normal; "
+                       "not using OFPP_NORMAL");
+             action_normal = false;
+         }
+         if (!learn_macs) {
+             ovs_error(0, "queue IDs are incompatible with -H or --hub; "
+                       "not acting as hub");
+             learn_macs = true;
+         }
+     }
  }
  
  static void
@@@ -386,9 -444,10 +452,10 @@@ usage(void
             "  -H, --hub               act as hub instead of learning switch\n"
             "  -n, --noflow            pass traffic, but don't add flows\n"
             "  --max-idle=SECS         max idle time for new flows\n"
-            "  -N, --normal            use OFPAT_NORMAL action\n"
+            "  -N, --normal            use OFPP_NORMAL action\n"
             "  -w, --wildcard          use wildcards, not exact-match rules\n"
-            "  -q, --queue=QUEUE       OpenFlow queue ID to use for output\n"
+            "  -q, --queue=QUEUE-ID    OpenFlow queue ID to use for output\n"
+            "  -Q PORT-NAME:QUEUE-ID   use QUEUE-ID for frames from PORT-NAME\n"
             "  --with-flows FILE       use the flows from FILE\n"
             "  --unixctl=SOCKET        override default control socket name\n"
             "  -h, --help              display this help message\n"
diff --combined utilities/ovs-ofctl.8.in
@@@ -115,6 -115,15 +115,15 @@@ the statistics are aggregated across al
  tables.  See \fBFlow Syntax\fR, below, for the syntax of \fIflows\fR.
  The output format is descrbed in \fBTable Entry Output\fR.
  .
+ .IP "\fBqueue\-stats \fIswitch \fR[\fIport \fR[\fIqueue\fR]]"
+ Prints to the console statistics for the specified \fIqueue\fR on
+ \fIport\fR within \fIswitch\fR.  Either of \fIport\fR or \fIqueue\fR
+ or both may be omitted (or equivalently specified as \fBALL\fR).  If
+ both are omitted, statistics are printed for all queues on all ports.
+ If only \fIqueue\fR is omitted, then statistics are printed for all
+ queues on \fIport\fR; if only \fIport\fR is omitted, then statistics
+ are printed for \fIqueue\fR on every port where it exists.
+ .
  .TP
  \fBadd\-flow \fIswitch flow\fR
  Add the flow entry as described by \fIflow\fR to the \fIswitch\fR's 
@@@ -329,22 -338,6 +338,22 @@@ as a decimal number between 0 and 255, 
  When \fBdl_type\fR and \fBnw_proto\fR take other values, the values of
  these settings are ignored (see \fBFlow Syntax\fR above).
  .
 +.IP \fBtable=\fInumber\fR
 +If specified, limits the flow manipulation and flow dump commands to 
 +only apply to the table with the given \fInumber\fR.  
 +\fInumber\fR is a number between 0 and 31, inclusive.
 +.
 +If this field is not specified, for \fBadd-flow\fR, \fBadd-flows\fR, 
 +\fBmod-flows\fR and \fBdel-flows\fR commands, the switch will choose
 +the table for these commands to operate on; for \fBdump-flows\fR and
 +\fBdump-aggregate\fR commands, statistics are gathered about flows 
 +from all tables.
 +.IP
 +When this field is specified in \fBadd-flow\fR, \fBadd-flows\fR, 
 +\fBmod-flows\fR and \fBdel-flows\fR commands, it will activate a 
 +Nicira extension to OpenFlow, which as of this writing is only 
 +known to be implemented by Open vSwitch.
 +.
  .PP
  The following shorthand notations are also available:
  .
@@@ -467,6 -460,25 +476,25 @@@ addition to any other actions in this f
  If outputting to a port that encapsulates the packet in a tunnel and supports
  an identifier (such as GRE), sets the identifier to \fBid\fR.
  .
+ .IP \fBdrop_spoofed_arp\fR
+ Stops processing further actions, if the packet being processed is an
+ Ethernet+IPv4 ARP packet for which the source Ethernet address inside
+ the ARP packet differs from the source Ethernet address in the
+ Ethernet header.
+ .
+ This is useful because OpenFlow does not provide a way to match on the
+ Ethernet addresses inside ARP packets, so there is no other way to
+ drop spoofed ARPs other than sending every ARP packet to a controller.
+ .
+ .IP \fBset_queue\fB:\fIqueue\fR
+ Sets the queue that should be used to \fIqueue\fR when packets are
+ output.  The number of supported queues depends on the switch; some
+ OpenFlow implementations do not support queuing at all.
+ .
+ .IP \fBpop_queue\fR
+ Restores the queue to the value it was before any \fBset_queue\fR
+ actions were applied.
+ .
  .RE
  .
  .IP
@@@ -502,8 -514,8 +530,8 @@@ optional fields
  .TP
  \fBidle_timeout=\fIseconds\fR
  Causes the flow to expire after the given number of seconds of
- inactivity.  A value of 0 prevents a flow from expiring due to
- inactivity.  The default is 60 seconds.
+ inactivity.  A value of 0 (the default) prevents a flow from expiring due to
+ inactivity.
  .
  .IP \fBhard_timeout=\fIseconds\fR
  Causes the flow to expire after the given number of seconds,
@@@ -518,6 -530,17 +546,6 @@@ and \fBdel\-flows\fR commands support o
  \fBout_port=\fIport\fR
  If set, a matching flow must include an output action to \fIport\fR.
  .
 -.PP
 -The \fBdump\-flows\fR and \fBdump\-aggregate\fR commands support an
 -additional optional field:
 -.
 -.IP \fBtable=\fInumber\fR
 -If specified, limits the flows about which statistics are gathered to
 -those in the table with the given \fInumber\fR.  Tables are numbered
 -as shown by the \fBdump\-tables\fR command.
 -.
 -If this field is not specified, or if \fInumber\fR is given as
 -\fB255\fR, statistics are gathered about flows from all tables.
  .
  .SS "Table Entry Output"
  .
diff --combined utilities/ovs-ofctl.c
@@@ -29,9 -29,9 +29,9 @@@
  #include "command-line.h"
  #include "compiler.h"
  #include "dirs.h"
 -#include "dpif.h"
 +#include "dynamic-string.h"
 +#include "netdev.h"
  #include "netlink.h"
 -#include "odp-util.h"
  #include "ofp-parse.h"
  #include "ofp-print.h"
  #include "ofp-util.h"
@@@ -44,8 -44,6 +44,8 @@@
  #include "util.h"
  #include "vconn.h"
  #include "vlog.h"
 +#include "xfif.h"
 +#include "xflow-util.h"
  #include "xtoxll.h"
  
  VLOG_DEFINE_THIS_MODULE(ofctl)
@@@ -152,6 -150,7 +152,7 @@@ usage(void
             "  dump-flows SWITCH FLOW      print matching FLOWs\n"
             "  dump-aggregate SWITCH       print aggregate flow statistics\n"
             "  dump-aggregate SWITCH FLOW  print aggregate stats for FLOWs\n"
+            "  queue-stats SWITCH [PORT [QUEUE]]  dump queue stats\n"
             "  add-flow SWITCH FLOW        add flow described by FLOW\n"
             "  add-flows SWITCH FILE       add flows from FILE\n"
             "  mod-flows SWITCH FLOW       modify actions of matching FLOWs\n"
@@@ -211,12 -210,12 +212,12 @@@ static voi
  open_vconn__(const char *name, const char *default_suffix,
               struct vconn **vconnp)
  {
 -    struct dpif *dpif;
 +    struct xfif *xfif;
      struct stat s;
      char *bridge_path, *datapath_name, *datapath_type;
  
      bridge_path = xasprintf("%s/%s.%s", ovs_rundir, name, default_suffix);
 -    dp_parse_name(name, &datapath_name, &datapath_type);
 +    xf_parse_name(name, &datapath_name, &datapath_type);
  
      if (strstr(name, ":")) {
          run(vconn_open_block(name, OFP_VERSION, vconnp),
          open_vconn_socket(name, vconnp);
      } else if (!stat(bridge_path, &s) && S_ISSOCK(s.st_mode)) {
          open_vconn_socket(bridge_path, vconnp);
 -    } else if (!dpif_open(datapath_name, datapath_type, &dpif)) {
 -        char dpif_name[IF_NAMESIZE + 1];
 +    } else if (!xfif_open(datapath_name, datapath_type, &xfif)) {
 +        char xfif_name[IF_NAMESIZE + 1];
          char *socket_name;
  
 -        run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
 -            "obtaining name of %s", dpif_name);
 -        dpif_close(dpif);
 -        if (strcmp(dpif_name, name)) {
 -            VLOG_INFO("datapath %s is named %s", name, dpif_name);
 +        run(xfif_port_get_name(xfif, XFLOWP_LOCAL, xfif_name, sizeof xfif_name),
 +            "obtaining name of %s", xfif_name);
 +        xfif_close(xfif);
 +        if (strcmp(xfif_name, name)) {
 +            VLOG_INFO("datapath %s is named %s", name, xfif_name);
          }
  
          socket_name = xasprintf("%s/%s.%s",
 -                                ovs_rundir, dpif_name, default_suffix);
 +                                ovs_rundir, xfif_name, default_suffix);
          if (stat(socket_name, &s)) {
              ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
                        name, socket_name);
@@@ -466,19 -465,28 +467,34 @@@ do_dump_aggregate(int argc, char *argv[
      dump_stats_transaction(argv[1], request);
  }
  
- enable_flow_mod_table_id_ext(struct vconn *vconn, uint8_t enable)
 +static void
-     struct nxt_flow_mod_table_id *flow_mod_table_id;
-     struct ofpbuf *buffer;
++enable_flow_mod_table_id_ext(struct vconn *vconn, bool enable)
 +{
-     flow_mod_table_id = make_openflow(sizeof *flow_mod_table_id, OFPT_VENDOR, &buffer);
++    send_openflow_buffer(vconn, make_nxt_flow_mod_table_id(enable));
++}
 +
+ static void
+ do_queue_stats(int argc, char *argv[])
+ {
+     struct ofp_queue_stats_request *req;
+     struct ofpbuf *request;
  
-     flow_mod_table_id->vendor = htonl(NX_VENDOR_ID);
-     flow_mod_table_id->subtype = htonl(NXT_FLOW_MOD_TABLE_ID);
-     flow_mod_table_id->set = enable;
+     req = alloc_stats_request(sizeof *req, OFPST_QUEUE, &request);
  
-     send_openflow_buffer(vconn, buffer);
+     if (argc > 2 && argv[2][0] && strcasecmp(argv[2], "all")) {
+         req->port_no = htons(str_to_port_no(argv[1], argv[2]));
+     } else {
+         req->port_no = htons(OFPP_ALL);
+     }
+     if (argc > 3 && argv[3][0] && strcasecmp(argv[3], "all")) {
+         req->queue_id = htonl(atoi(argv[3]));
+     } else {
+         req->queue_id = htonl(OFPQ_ALL);
+     }
+     memset(req->pad, 0, sizeof req->pad);
+     dump_stats_transaction(argv[1], request);
  }
  
  static void
@@@ -490,14 -498,13 +506,14 @@@ do_add_flow(int argc OVS_UNUSED, char *
      uint16_t priority, idle_timeout, hard_timeout;
      uint64_t cookie;
      struct ofp_match match;
 +    uint8_t table_idx;
  
      /* Parse and send.  parse_ofp_str() will expand and reallocate the
       * data in 'buffer', so we can't keep pointers to across the
       * parse_ofp_str() call. */
      make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
      parse_ofp_str(argv[2], &match, buffer,
 -                  NULL, NULL, &priority, &idle_timeout, &hard_timeout,
 +                  &table_idx, NULL, &priority, &idle_timeout, &hard_timeout,
                    &cookie);
      ofm = buffer->data;
      ofm->match = match;
      ofm->priority = htons(priority);
  
      open_vconn(argv[1], &vconn);
 +    if (table_idx != 0xff) {
 +        enable_flow_mod_table_id_ext(vconn, 1);
 +        ofm->command = htons(ntohs(ofm->command) | (table_idx << 8));
 +    }
      send_openflow_buffer(vconn, buffer);
      vconn_close(vconn);
  }
@@@ -521,10 -524,8 +537,10 @@@ static voi
  do_add_flows(int argc OVS_UNUSED, char *argv[])
  {
      struct vconn *vconn;
+     struct ofpbuf *b;
      FILE *file;
-     char line[1024];
++    bool table_id_enabled = false;
 +    uint8_t table_idx;
-     int table_id_enabled = 0;
  
      file = fopen(argv[2], "r");
      if (file == NULL) {
      }
  
      open_vconn(argv[1], &vconn);
-     while (fgets(line, sizeof line, file)) {
-         struct ofpbuf *buffer;
-         struct ofp_flow_mod *ofm;
-         uint16_t priority, idle_timeout, hard_timeout;
-         uint64_t cookie;
-         struct ofp_match match;
-         char *comment;
-         /* Delete comments. */
-         comment = strchr(line, '#');
-         if (comment) {
-             *comment = '\0';
-         }
-         /* Drop empty lines. */
-         if (line[strspn(line, " \t\n")] == '\0') {
-             continue;
-         }
-         /* Parse and send.  parse_ofp_str() will expand and reallocate
-          * the data in 'buffer', so we can't keep pointers to across the
-          * parse_ofp_str() call. */
-         make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
-         parse_ofp_str(line, &match, buffer, &table_idx, NULL, &priority,
-                       &idle_timeout, &hard_timeout, &cookie);
-         ofm = buffer->data;
-         ofm->match = match;
-         ofm->command = htons(OFPFC_ADD);
-         ofm->cookie = htonll(cookie);
-         ofm->idle_timeout = htons(idle_timeout);
-         ofm->hard_timeout = htons(hard_timeout);
-         ofm->buffer_id = htonl(UINT32_MAX);
-         ofm->priority = htons(priority);
-         if (table_idx != 0xff) {
-             if (!table_id_enabled) {
-                 enable_flow_mod_table_id_ext(vconn, 1);
-                 table_id_enabled = 1;
-             }
-             ofm->command = htons(ntohs(ofm->command) | (table_idx << 8));
-         } else {
-             if (table_id_enabled) {
-                 enable_flow_mod_table_id_ext(vconn, 0);
-                 table_id_enabled = 0;
-             }
 -    while ((b = parse_ofp_add_flow_file(file)) != NULL) {
++    while ((b = parse_ofp_add_flow_file(file, &table_idx)) != NULL) {
++        if ((table_idx != 0xff) != table_id_enabled) {
++            table_id_enabled = table_idx != 0xff;
++            enable_flow_mod_table_id_ext(vconn, table_id_enabled);
 +        }
-         send_openflow_buffer(vconn, buffer);
+         send_openflow_buffer(vconn, b);
      }
      vconn_close(vconn);
      fclose(file);
@@@ -594,14 -549,13 +568,14 @@@ do_mod_flows(int argc OVS_UNUSED, char 
      struct ofpbuf *buffer;
      struct ofp_flow_mod *ofm;
      struct ofp_match match;
 +    uint8_t table_idx;
  
      /* Parse and send.  parse_ofp_str() will expand and reallocate the
       * data in 'buffer', so we can't keep pointers to across the
       * parse_ofp_str() call. */
      make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
      parse_ofp_str(argv[2], &match, buffer,
 -                  NULL, NULL, &priority, &idle_timeout, &hard_timeout,
 +                  &table_idx, NULL, &priority, &idle_timeout, &hard_timeout,
                    &cookie);
      ofm = buffer->data;
      ofm->match = match;
      ofm->priority = htons(priority);
  
      open_vconn(argv[1], &vconn);
 +    if (table_idx != 0xff) {
 +        enable_flow_mod_table_id_ext(vconn, 1);
 +        ofm->command = htons(ntohs(ofm->command) | (table_idx << 8));
 +    }
      send_openflow_buffer(vconn, buffer);
      vconn_close(vconn);
  }
@@@ -632,11 -582,10 +606,11 @@@ static void do_del_flows(int argc, cha
      uint16_t out_port;
      struct ofpbuf *buffer;
      struct ofp_flow_mod *ofm;
 +    uint8_t table_idx;
  
      /* Parse and send. */
      ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
 -    parse_ofp_str(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL,
 +    parse_ofp_str(argc > 2 ? argv[2] : "", &ofm->match, NULL, &table_idx,
                    &out_port, &priority, NULL, NULL, NULL);
      if (strict) {
          ofm->command = htons(OFPFC_DELETE_STRICT);
      ofm->priority = htons(priority);
  
      open_vconn(argv[1], &vconn);
 +    if (table_idx != 0xff) {
 +        enable_flow_mod_table_id_ext(vconn, 1);
 +        ofm->command = htons(ntohs(ofm->command) | (table_idx << 8));
 +    }
      send_openflow_buffer(vconn, buffer);
      vconn_close(vconn);
  }
@@@ -914,6 -859,26 +888,27 @@@ do_benchmark(int argc OVS_UNUSED, char 
             count * message_size / (duration / 1000.0));
  }
  
 -    while ((b = parse_ofp_add_flow_file(file)) != NULL) {
+ /* This command is really only useful for testing the flow parser (ofp_parse),
+  * so it is undocumented. */
+ static void
+ do_parse_flows(int argc OVS_UNUSED, char *argv[])
+ {
++    uint8_t table_idx;
+     struct ofpbuf *b;
+     FILE *file;
+     file = fopen(argv[1], "r");
+     if (file == NULL) {
+         ovs_fatal(errno, "%s: open", argv[2]);
+     }
++    while ((b = parse_ofp_add_flow_file(file, &table_idx)) != NULL) {
+         ofp_print(stdout, b->data, b->size, 0);
+         ofpbuf_delete(b);
+     }
+     fclose(file);
+ }
  static void
  do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
  {
@@@ -929,6 -894,7 +924,7 @@@ static const struct command all_command
      { "dump-tables", 1, 1, do_dump_tables },
      { "dump-flows", 1, 2, do_dump_flows },
      { "dump-aggregate", 1, 2, do_dump_aggregate },
+     { "queue-stats", 1, 3, do_queue_stats },
      { "add-flow", 2, 2, do_add_flow },
      { "add-flows", 2, 2, do_add_flows },
      { "mod-flows", 2, 2, do_mod_flows },
      { "probe", 1, 1, do_probe },
      { "ping", 1, 2, do_ping },
      { "benchmark", 3, 3, do_benchmark },
+     { "parse-flows", 1, 1, do_parse_flows },
      { "help", 0, INT_MAX, do_help },
      { NULL, 0, 0, NULL },
  };
  #include "compiler.h"
  #include "daemon.h"
  #include "dirs.h"
 -#include "dpif.h"
  #include "leak-checker.h"
  #include "list.h"
  #include "netdev.h"
  #include "ofpbuf.h"
  #include "ofproto/ofproto.h"
 +#include "ofproto/wdp.h"
  #include "openflow/openflow.h"
  #include "packets.h"
  #include "poll-loop.h"
@@@ -45,7 -45,6 +45,7 @@@
  #include "util.h"
  #include "vconn.h"
  #include "vlog.h"
 +#include "xfif.h"
  
  VLOG_DEFINE_THIS_MODULE(openflowd)
  
@@@ -89,7 -88,7 +89,7 @@@ main(int argc, char *argv[]
      struct ofproto *ofproto;
      struct ofsettings s;
      int error;
 -    struct dpif *dpif;
 +    struct xfif *xfif;
      struct netflow_options nf_options;
  
      proctitle_init(argc, argv);
      VLOG_INFO("Open vSwitch version %s", VERSION BUILDNR);
      VLOG_INFO("OpenFlow protocol version 0x%02x", OFP_VERSION);
  
 -    error = dpif_create_and_open(s.dp_name, s.dp_type, &dpif);
 +    error = xfif_create_and_open(s.dp_name, s.dp_type, &xfif);
      if (error) {
          ovs_fatal(error, "could not create datapath");
      }
          size_t i;
  
          SVEC_FOR_EACH (i, port, &s.ports) {
 -            error = dpif_port_add(dpif, port, 0, NULL);
 +            error = xfif_port_add(xfif, port, 0, NULL);
              if (error) {
                  ovs_fatal(error, "failed to add %s as a port", port);
              }
              ovs_fatal(error, "unrecoverable datapath error");
          }
          unixctl_server_run(unixctl);
 -        dp_run();
 +        wdp_run();
          netdev_run();
  
          ofproto_wait(ofproto);
          unixctl_server_wait(unixctl);
 -        dp_wait();
 +        wdp_wait();
          netdev_wait();
          poll_block();
      }
  
 -    dpif_close(dpif);
 +    xfif_close(xfif);
  
      return 0;
  }
@@@ -441,7 -440,7 +441,7 @@@ parse_options(int argc, char *argv[], s
      }
  
      /* Local vconns. */
 -    dp_parse_name(argv[0], &s->dp_name, &s->dp_type);
 +    xf_parse_name(argv[0], &s->dp_name, &s->dp_type);
  
      /* Figure out controller names. */
      if (!controllers.n) {
      s->n_controllers = controllers.n;
      s->controllers = xmalloc(s->n_controllers * sizeof *s->controllers);
      if (argc > 1) {
-         size_t i;
          for (i = 0; i < s->n_controllers; i++) {
              s->controllers[i] = controller_opts;
              s->controllers[i].target = controllers.names[i];
  
      /* Sanity check. */
      if (controller_opts.band == OFPROTO_OUT_OF_BAND) {
-         size_t i;
          for (i = 0; i < s->n_controllers; i++) {
              if (!strcmp(s->controllers[i].target, "discover")) {
                  ovs_fatal(0, "Cannot perform discovery with out-of-band "
diff --combined vswitchd/bridge.c
  #include "bitmap.h"
  #include "coverage.h"
  #include "dirs.h"
 -#include "dpif.h"
  #include "dynamic-string.h"
  #include "flow.h"
  #include "hash.h"
+ #include "hmap.h"
  #include "jsonrpc.h"
  #include "list.h"
  #include "mac-learning.h"
  #include "netdev.h"
 -#include "odp-util.h"
 +#include "xflow-util.h"
  #include "ofp-print.h"
  #include "ofpbuf.h"
  #include "ofproto/netflow.h"
@@@ -48,7 -50,6 +49,6 @@@
  #include "ovsdb-data.h"
  #include "packets.h"
  #include "poll-loop.h"
- #include "port-array.h"
  #include "proc-net-compat.h"
  #include "process.h"
  #include "sha1.h"
@@@ -56,6 -57,7 +56,7 @@@
  #include "socket-util.h"
  #include "stream-ssl.h"
  #include "svec.h"
+ #include "system-stats.h"
  #include "timeval.h"
  #include "util.h"
  #include "unixctl.h"
@@@ -63,7 -65,6 +64,7 @@@
  #include "vswitchd/vswitch-idl.h"
  #include "xenserver.h"
  #include "vlog.h"
 +#include "xfif.h"
  #include "xtoxll.h"
  #include "sflow_api.h"
  
@@@ -71,7 -72,7 +72,7 @@@ VLOG_DEFINE_THIS_MODULE(bridge
  
  struct dst {
      uint16_t vlan;
 -    uint16_t dp_ifidx;
 +    uint16_t xf_ifidx;
  };
  
  struct iface {
@@@ -84,7 -85,8 +85,8 @@@
  
      /* These members are valid only after bridge_reconfigure() causes them to
       * be initialized. */
 -    struct hmap_node dp_ifidx_node; /* In struct bridge's "ifaces" hmap. */
 -    int dp_ifidx;               /* Index within kernel datapath. */
++    struct hmap_node xf_ifidx_node; /* In struct bridge's "ifaces" hmap. */
 +    int xf_ifidx;               /* Index within kernel datapath. */
      struct netdev *netdev;      /* Network device. */
      bool enabled;               /* May be chosen for flows? */
      const struct ovsrec_interface *cfg;
@@@ -163,8 -165,8 +165,8 @@@ struct bridge 
      struct ofproto *ofproto;    /* OpenFlow switch. */
  
      /* Kernel datapath information. */
 -    struct dpif *dpif;          /* Datapath. */
 +    struct xfif *xfif;          /* Datapath. */
-     struct port_array ifaces;   /* Indexed by kernel datapath port number. */
+     struct hmap ifaces;         /* Contains "struct iface"s. */
  
      /* Bridge ports. */
      struct port **ports;
@@@ -188,10 -190,10 +190,10 @@@ static struct list all_bridges = LIST_I
  /* OVSDB IDL used to obtain configuration. */
  static struct ovsdb_idl *idl;
  
- /* Each time this timer expires, the bridge fetches statistics for every
-  * interface and pushes them into the database. */
- #define IFACE_STATS_INTERVAL (5 * 1000) /* In milliseconds. */
- static long long int iface_stats_timer = LLONG_MIN;
+ /* Each time this timer expires, the bridge fetches systems and interface
+  * statistics and pushes them into the database. */
+ #define STATS_INTERVAL (5 * 1000) /* In milliseconds. */
+ static long long int stats_timer = LLONG_MIN;
  
  static struct bridge *bridge_create(const struct ovsrec_bridge *br_cfg);
  static void bridge_destroy(struct bridge *);
@@@ -232,8 -234,8 +234,8 @@@ static void port_del_ifaces(struct por
  static void port_destroy(struct port *);
  static struct port *port_lookup(const struct bridge *, const char *name);
  static struct iface *port_lookup_iface(const struct port *, const char *name);
 -static struct port *port_from_dp_ifidx(const struct bridge *,
 -                                       uint16_t dp_ifidx);
 +static struct port *port_from_xf_ifidx(const struct bridge *,
 +                                       uint16_t xf_ifidx);
  static void port_update_bond_compat(struct port *);
  static void port_update_vlan_compat(struct port *);
  static void port_update_bonding(struct port *);
@@@ -248,8 -250,8 +250,8 @@@ static struct iface *iface_create(struc
                                    const struct ovsrec_interface *if_cfg);
  static void iface_destroy(struct iface *);
  static struct iface *iface_lookup(const struct bridge *, const char *name);
 -static struct iface *iface_from_dp_ifidx(const struct bridge *,
 -                                         uint16_t dp_ifidx);
 +static struct iface *iface_from_xf_ifidx(const struct bridge *,
 +                                         uint16_t xf_ifidx);
  static bool iface_is_internal(const struct bridge *, const char *name);
  static void iface_set_mac(struct iface *);
  static void iface_update_qos(struct iface *, const struct ovsrec_qos *);
@@@ -297,7 -299,7 +299,7 @@@ bridge_configure_once(const struct ovsr
  {
      static bool already_configured_once;
      struct svec bridge_names;
 -    struct svec dpif_names, dpif_types;
 +    struct svec xfif_names, xfif_types;
      size_t i;
  
      /* Only do this once per ovs-vswitchd run. */
      }
      already_configured_once = true;
  
-     iface_stats_timer = time_msec() + IFACE_STATS_INTERVAL;
+     stats_timer = time_msec() + STATS_INTERVAL;
  
      /* Get all the configured bridges' names from 'cfg' into 'bridge_names'. */
      svec_init(&bridge_names);
      }
      svec_sort(&bridge_names);
  
 -    /* Iterate over all system dpifs and delete any of them that do not appear
 +    /* Iterate over all system xfifs and delete any of them that do not appear
       * in 'cfg'. */
 -    svec_init(&dpif_names);
 -    svec_init(&dpif_types);
 -    dp_enumerate_types(&dpif_types);
 -    for (i = 0; i < dpif_types.n; i++) {
 -        struct dpif *dpif;
 +    svec_init(&xfif_names);
 +    svec_init(&xfif_types);
 +    xf_enumerate_types(&xfif_types);
 +    for (i = 0; i < xfif_types.n; i++) {
 +        struct xfif *xfif;
          int retval;
          size_t j;
  
 -        dp_enumerate_names(dpif_types.names[i], &dpif_names);
 +        xf_enumerate_names(xfif_types.names[i], &xfif_names);
  
 -        /* For each dpif... */
 -        for (j = 0; j < dpif_names.n; j++) {
 -            retval = dpif_open(dpif_names.names[j], dpif_types.names[i], &dpif);
 +        /* For each xfif... */
 +        for (j = 0; j < xfif_names.n; j++) {
 +            retval = xfif_open(xfif_names.names[j], xfif_types.names[i], &xfif);
              if (!retval) {
                  struct svec all_names;
                  size_t k;
  
                  /* ...check whether any of its names is in 'bridge_names'. */
                  svec_init(&all_names);
 -                dpif_get_all_names(dpif, &all_names);
 +                xfif_get_all_names(xfif, &all_names);
                  for (k = 0; k < all_names.n; k++) {
                      if (svec_contains(&bridge_names, all_names.names[k])) {
                          goto found;
                      }
                  }
  
 -                /* No.  Delete the dpif. */
 -                dpif_delete(dpif);
 +                /* No.  Delete the xfif. */
 +                xfif_delete(xfif);
  
              found:
                  svec_destroy(&all_names);
 -                dpif_close(dpif);
 +                xfif_close(xfif);
              }
          }
      }
      svec_destroy(&bridge_names);
 -    svec_destroy(&dpif_names);
 -    svec_destroy(&dpif_types);
 +    svec_destroy(&xfif_names);
 +    svec_destroy(&xfif_types);
  }
  
  /* Attempt to create the network device 'iface_name' through the netdev
@@@ -373,6 -375,20 +375,20 @@@ set_up_iface(const struct ovsrec_interf
                    xstrdup(iface_cfg->value_options[i]));
      }
  
+     /* Include 'other_config' keys in hash of netdev options.  The
+      * namespace of 'other_config' and 'options' must be disjoint.
+      * Prefer 'options' keys over 'other_config' keys. */
+     for (i = 0; i < iface_cfg->n_other_config; i++) {
+         char *value = xstrdup(iface_cfg->value_other_config[i]);
+         if (!shash_add_once(&options, iface_cfg->key_other_config[i],
+                             value)) {
+             VLOG_WARN("%s: \"other_config\" key %s conflicts with existing "
+                       "\"other_config\" or \"options\" entry...ignoring",
+                       iface_cfg->name, iface_cfg->key_other_config[i]);
+             free(value);
+         }
+     }
      if (create) {
          struct netdev_options netdev_options;
  
          }
          netdev_options.args = &options;
          netdev_options.ethertype = NETDEV_ETH_TYPE_NONE;
 -
          error = netdev_open(&netdev_options, &iface->netdev);
  
          if (iface->netdev) {
@@@ -437,17 -454,17 +453,17 @@@ check_iface_netdev(struct bridge *br OV
  }
  
  static bool
 -check_iface_dp_ifidx(struct bridge *br, struct iface *iface,
 +check_iface_xf_ifidx(struct bridge *br, struct iface *iface,
                       void *aux OVS_UNUSED)
  {
 -    if (iface->dp_ifidx >= 0) {
 +    if (iface->xf_ifidx >= 0) {
          VLOG_DBG("%s has interface %s on port %d",
 -                 dpif_name(br->dpif),
 -                 iface->name, iface->dp_ifidx);
 +                 xfif_name(br->xfif),
 +                 iface->name, iface->xf_ifidx);
          return true;
      } else {
          VLOG_ERR("%s interface not in %s, dropping",
 -                 iface->name, dpif_name(br->dpif));
 +                 iface->name, xfif_name(br->xfif));
          return false;
      }
  }
@@@ -463,7 -480,7 +479,7 @@@ set_iface_properties(struct bridge *br 
  
      /* Set MAC address of internal interfaces other than the local
       * interface. */
 -    if (iface->dp_ifidx != ODPP_LOCAL
 +    if (iface->xf_ifidx != XFLOWP_LOCAL
          && iface_is_internal(br, iface->name)) {
          iface_set_mac(iface);
      }
@@@ -558,7 -575,7 +574,7 @@@ bridge_reconfigure(const struct ovsrec_
      /* Collect old and new bridges. */
      shash_init(&old_br);
      shash_init(&new_br);
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
          shash_add(&old_br, br->name, br);
      }
      for (i = 0; i < ovs_cfg->n_bridges; i++) {
      }
  
      /* Get rid of deleted bridges and add new bridges. */
-     LIST_FOR_EACH_SAFE (br, next, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH_SAFE (br, next, node, &all_bridges) {
          struct ovsrec_bridge *br_cfg = shash_find_data(&new_br, br->name);
          if (br_cfg) {
              br->cfg = br_cfg;
      shash_destroy(&new_br);
  
      /* Reconfigure all bridges. */
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
          bridge_reconfigure_one(br);
      }
  
       * The kernel will reject any attempt to add a given port to a datapath if
       * that port already belongs to a different datapath, so we must do all
       * port deletions before any port additions. */
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
 -        struct odp_port *dpif_ports;
 -        size_t n_dpif_ports;
 +        struct xflow_port *xfif_ports;
 +        size_t n_xfif_ports;
          struct shash want_ifaces;
  
 -        dpif_port_list(br->dpif, &dpif_ports, &n_dpif_ports);
 +        xfif_port_list(br->xfif, &xfif_ports, &n_xfif_ports);
          bridge_get_all_ifaces(br, &want_ifaces);
 -        for (i = 0; i < n_dpif_ports; i++) {
 -            const struct odp_port *p = &dpif_ports[i];
 +        for (i = 0; i < n_xfif_ports; i++) {
 +            const struct xflow_port *p = &xfif_ports[i];
              if (!shash_find(&want_ifaces, p->devname)
                  && strcmp(p->devname, br->name)) {
 -                int retval = dpif_port_del(br->dpif, p->port);
 +                int retval = xfif_port_del(br->xfif, p->port);
                  if (retval) {
                      VLOG_ERR("failed to remove %s interface from %s: %s",
 -                             p->devname, dpif_name(br->dpif),
 +                             p->devname, xfif_name(br->xfif),
                               strerror(retval));
                  }
              }
          }
          shash_destroy(&want_ifaces);
 -        free(dpif_ports);
 +        free(xfif_ports);
      }
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
 -        struct odp_port *dpif_ports;
 -        size_t n_dpif_ports;
 +        struct xflow_port *xfif_ports;
 +        size_t n_xfif_ports;
          struct shash cur_ifaces, want_ifaces;
-         struct shash_node *node;
  
          /* Get the set of interfaces currently in this datapath. */
 -        dpif_port_list(br->dpif, &dpif_ports, &n_dpif_ports);
 +        xfif_port_list(br->xfif, &xfif_ports, &n_xfif_ports);
          shash_init(&cur_ifaces);
 -        for (i = 0; i < n_dpif_ports; i++) {
 -            const char *name = dpif_ports[i].devname;
 +        for (i = 0; i < n_xfif_ports; i++) {
 +            const char *name = xfif_ports[i].devname;
              shash_add_once(&cur_ifaces, name, NULL);
          }
 -        free(dpif_ports);
 +        free(xfif_ports);
  
          /* Get the set of interfaces we want on this datapath. */
          bridge_get_all_ifaces(br, &want_ifaces);
  
                  /* Add to datapath. */
                  internal = iface_is_internal(br, if_name);
 -                error = dpif_port_add(br->dpif, if_name,
 -                                      internal ? ODP_PORT_INTERNAL : 0, NULL);
 +                error = xfif_port_add(br->xfif, if_name,
 +                                      internal ? XFLOW_PORT_INTERNAL : 0, NULL);
                  if (error == EFBIG) {
                      VLOG_ERR("ran out of valid port numbers on %s",
 -                             dpif_name(br->dpif));
 +                             xfif_name(br->xfif));
                      break;
                  } else if (error) {
                      VLOG_ERR("failed to add %s interface to %s: %s",
 -                             if_name, dpif_name(br->dpif), strerror(error));
 +                             if_name, xfif_name(br->xfif), strerror(error));
                  }
              }
          }
          shash_destroy(&want_ifaces);
      }
      sflow_bridge_number = 0;
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
          uint8_t ea[8];
          uint64_t dpid;
          struct iface *local_iface;
          bridge_fetch_dp_ifaces(br);
  
          iterate_and_prune_ifaces(br, check_iface_netdev, NULL);
 -        iterate_and_prune_ifaces(br, check_iface_dp_ifidx, NULL);
 +        iterate_and_prune_ifaces(br, check_iface_xf_ifidx, NULL);
  
          /* Pick local port hardware address, datapath ID. */
          bridge_pick_local_hw_addr(br, ea, &hw_addr_iface);
  
              memset(&opts, 0, sizeof opts);
  
 -            dpif_get_netflow_ids(br->dpif, &opts.engine_type, &opts.engine_id);
 +            xfif_get_netflow_ids(br->xfif, &opts.engine_type, &opts.engine_id);
              if (nf_cfg->engine_type) {
                  opts.engine_type = *nf_cfg->engine_type;
              }
              struct ovsrec_controller **controllers;
              struct ofproto_sflow_options oso;
              size_t n_controllers;
-             size_t i;
  
              memset(&oso, 0, sizeof oso);
  
          /* Update the controller and related settings.  It would be more
           * straightforward to call this from bridge_reconfigure_one(), but we
           * can't do it there for two reasons.  First, and most importantly, at
 -         * that point we don't know the dp_ifidx of any interfaces that have
 +         * that point we don't know the xf_ifidx of any interfaces that have
           * been added to the bridge (because we haven't actually added them to
           * the datapath).  Second, at that point we haven't set the datapath ID
           * yet; when a controller is configured, resetting the datapath ID will
           * the datapath ID before the controller. */
          bridge_reconfigure_remotes(br, managers, n_managers);
      }
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
          for (i = 0; i < br->n_ports; i++) {
              struct port *port = br->ports[i];
              int j;
              }
          }
      }
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
          iterate_and_prune_ifaces(br, set_iface_properties, NULL);
      }
  
@@@ -923,7 -938,7 +937,7 @@@ bridge_pick_local_hw_addr(struct bridg
  
              /* The local port doesn't count (since we're trying to choose its
               * MAC address anyway). */
 -            if (iface->dp_ifidx == ODPP_LOCAL) {
 +            if (iface->xf_ifidx == XFLOWP_LOCAL) {
                  continue;
              }
  
@@@ -1097,6 -1112,20 +1111,20 @@@ iface_refresh_stats(struct iface *iface
      ovsrec_interface_set_statistics(iface->cfg, keys, values, n);
  }
  
+ static void
+ refresh_system_stats(const struct ovsrec_open_vswitch *cfg)
+ {
+     struct ovsdb_datum datum;
+     struct shash stats;
+     shash_init(&stats);
+     get_system_stats(&stats);
+     ovsdb_datum_from_shash(&datum, &stats);
+     ovsdb_idl_txn_write(&cfg->header_, &ovsrec_open_vswitch_col_statistics,
+                         &datum);
+ }
  void
  bridge_run(void)
  {
  
      /* Let each bridge do the work that it needs to do. */
      datapath_destroyed = false;
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
          int error = bridge_run_one(br);
          if (error) {
              static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
      }
  #endif
  
-     /* Refresh interface stats if necessary. */
-     if (time_msec() >= iface_stats_timer) {
-         struct ovsdb_idl_txn *txn;
+     /* Refresh system and interface stats if necessary. */
+     if (time_msec() >= stats_timer) {
+         if (cfg) {
+             struct ovsdb_idl_txn *txn;
  
-         txn = ovsdb_idl_txn_create(idl);
-         LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
-             size_t i;
+             txn = ovsdb_idl_txn_create(idl);
+             LIST_FOR_EACH (br, node, &all_bridges) {
+                 size_t i;
  
-             for (i = 0; i < br->n_ports; i++) {
-                 struct port *port = br->ports[i];
-                 size_t j;
+                 for (i = 0; i < br->n_ports; i++) {
+                     struct port *port = br->ports[i];
+                     size_t j;
  
-                 for (j = 0; j < port->n_ifaces; j++) {
-                     struct iface *iface = port->ifaces[j];
-                     iface_refresh_stats(iface);
+                     for (j = 0; j < port->n_ifaces; j++) {
+                         struct iface *iface = port->ifaces[j];
+                         iface_refresh_stats(iface);
+                     }
                  }
              }
+             refresh_system_stats(cfg);
+             ovsdb_idl_txn_commit(txn);
+             ovsdb_idl_txn_destroy(txn); /* XXX */
          }
-         ovsdb_idl_txn_commit(txn);
-         ovsdb_idl_txn_destroy(txn); /* XXX */
  
-         iface_stats_timer = time_msec() + IFACE_STATS_INTERVAL;
+         stats_timer = time_msec() + STATS_INTERVAL;
      }
  }
  
@@@ -1182,7 -1214,7 +1213,7 @@@ bridge_wait(void
  {
      struct bridge *br;
  
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
          ofproto_wait(br->ofproto);
          if (ofproto_has_primary_controller(br->ofproto)) {
              continue;
          bond_wait(br);
      }
      ovsdb_idl_wait(idl);
-     poll_timer_wait_until(iface_stats_timer);
+     poll_timer_wait_until(stats_timer);
  }
  
  /* Forces 'br' to revalidate all of its flows.  This is appropriate when 'br''s
@@@ -1201,11 -1233,11 +1232,11 @@@ static voi
  bridge_flush(struct bridge *br)
  {
      COVERAGE_INC(bridge_flush);
 -    br->flush = true;
 +    ofproto_revalidate_all(br->ofproto);
      mac_learning_flush(br->ml);
  }
  
 -/* Returns the 'br' interface for the ODPP_LOCAL port, or null if 'br' has no
 +/* Returns the 'br' interface for the XFLOWP_LOCAL port, or null if 'br' has no
   * such interface. */
  static struct iface *
  bridge_get_local_iface(struct bridge *br)
          struct port *port = br->ports[i];
          for (j = 0; j < port->n_ifaces; j++) {
              struct iface *iface = port->ifaces[j];
 -            if (iface->dp_ifidx == ODPP_LOCAL) {
 +            if (iface->xf_ifidx == XFLOWP_LOCAL) {
                  return iface;
              }
          }
@@@ -1241,12 -1273,12 +1272,12 @@@ bridge_unixctl_fdb_show(struct unixctl_
      }
  
      ds_put_cstr(&ds, " port  VLAN  MAC                Age\n");
-     LIST_FOR_EACH (e, struct mac_entry, lru_node, &br->ml->lrus) {
+     LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
          if (e->port < 0 || e->port >= br->n_ports) {
              continue;
          }
          ds_put_format(&ds, "%5d  %4d  "ETH_ADDR_FMT"  %3d\n",
 -                      br->ports[e->port]->ifaces[0]->dp_ifidx,
 +                      br->ports[e->port]->ifaces[0]->xf_ifidx,
                        e->vlan, ETH_ADDR_ARGS(e->mac), mac_entry_age(e));
      }
      unixctl_command_reply(conn, 200, ds_cstr(&ds));
@@@ -1263,21 -1295,21 +1294,21 @@@ bridge_create(const struct ovsrec_bridg
      assert(!bridge_lookup(br_cfg->name));
      br = xzalloc(sizeof *br);
  
 -    error = dpif_create_and_open(br_cfg->name, br_cfg->datapath_type,
 -                                 &br->dpif);
 +    error = xfif_create_and_open(br_cfg->name, br_cfg->datapath_type,
 +                                 &br->xfif);
      if (error) {
          free(br);
          return NULL;
      }
 -    dpif_flow_flush(br->dpif);
 +    xfif_flow_flush(br->xfif);
  
      error = ofproto_create(br_cfg->name, br_cfg->datapath_type, &bridge_ofhooks,
                             br, &br->ofproto);
      if (error) {
          VLOG_ERR("failed to create switch %s: %s", br_cfg->name,
                   strerror(error));
 -        dpif_delete(br->dpif);
 -        dpif_close(br->dpif);
 +        xfif_delete(br->xfif);
 +        xfif_close(br->xfif);
          free(br);
          return NULL;
      }
      br->ml = mac_learning_create();
      eth_addr_nicira_random(br->default_ea);
  
-     port_array_init(&br->ifaces);
+     hmap_init(&br->ifaces);
  
      shash_init(&br->port_by_name);
      shash_init(&br->iface_by_name);
  
 -    br->flush = false;
 -
      list_push_back(&all_bridges, &br->node);
  
 -    VLOG_INFO("created bridge %s on %s", br->name, dpif_name(br->dpif));
 +    VLOG_INFO("created bridge %s on %s", br->name, xfif_name(br->xfif));
  
      return br;
  }
@@@ -1309,15 -1343,15 +1340,15 @@@ bridge_destroy(struct bridge *br
              port_destroy(br->ports[br->n_ports - 1]);
          }
          list_remove(&br->node);
 -        error = dpif_delete(br->dpif);
 +        error = xfif_delete(br->xfif);
          if (error && error != ENOENT) {
              VLOG_ERR("failed to delete %s: %s",
 -                     dpif_name(br->dpif), strerror(error));
 +                     xfif_name(br->xfif), strerror(error));
          }
 -        dpif_close(br->dpif);
 +        xfif_close(br->xfif);
          ofproto_destroy(br->ofproto);
          mac_learning_destroy(br->ml);
-         port_array_destroy(&br->ifaces);
+         hmap_destroy(&br->ifaces);
          shash_destroy(&br->port_by_name);
          shash_destroy(&br->iface_by_name);
          free(br->ports);
@@@ -1331,7 -1365,7 +1362,7 @@@ bridge_lookup(const char *name
  {
      struct bridge *br;
  
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
          if (!strcmp(br->name, name)) {
              return br;
          }
@@@ -1377,7 -1411,7 +1408,7 @@@ bridge_unixctl_reconnect(struct unixctl
          }
          ofproto_reconnect_controllers(br->ofproto);
      } else {
-         LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+         LIST_FOR_EACH (br, node, &all_bridges) {
              ofproto_reconnect_controllers(br->ofproto);
          }
      }
  static int
  bridge_run_one(struct bridge *br)
  {
 -    int error;
 -
 -    error = ofproto_run1(br->ofproto);
 -    if (error) {
 -        return error;
 -    }
 -
 -    mac_learning_run(br->ml, ofproto_get_revalidate_set(br->ofproto));
 +    ofproto_revalidate(br->ofproto, mac_learning_run(br->ml));
      bond_run(br);
 -
 -    error = ofproto_run2(br->ofproto, br->flush);
 -    br->flush = false;
 -
 -    return error;
 +    return ofproto_run(br->ofproto);
  }
  
  static size_t
@@@ -1446,7 -1491,7 +1477,7 @@@ bridge_reconfigure_one(struct bridge *b
          char local_name[IF_NAMESIZE];
          int error;
  
 -        error = dpif_port_get_name(br->dpif, ODPP_LOCAL,
 +        error = xfif_port_get_name(br->xfif, XFLOWP_LOCAL,
                                     local_name, sizeof local_name);
          if (!error && !shash_find(&new_ports, local_name)) {
              VLOG_WARN("bridge %s: controller specified but no local port "
@@@ -1635,7 -1680,7 +1666,7 @@@ bridge_reconfigure_remotes(struct bridg
               * domain sockets and overwriting arbitrary local files. */
              VLOG_ERR_RL(&rl, "%s: not adding Unix domain socket controller "
                          "\"%s\" due to possibility for remote exploit",
 -                        dpif_name(br->dpif), c->target);
 +                        br->name, c->target);
              continue;
          }
  
          action.output.len = htons(sizeof action);
          action.output.port = htons(OFPP_NORMAL);
          memset(&flow, 0, sizeof flow);
 -        ofproto_add_flow(br->ofproto, &flow, OVSFW_ALL, 0, &action, 1, 0);
 +        flow.wildcards = OVSFW_ALL;
 +        ofproto_add_flow(br->ofproto, &flow, &action, 1, 0);
      }
  }
  
@@@ -1692,16 -1736,16 +1723,16 @@@ bridge_get_all_ifaces(const struct brid
  /* For robustness, in case the administrator moves around datapath ports behind
   * our back, we re-check all the datapath port numbers here.
   *
 - * This function will set the 'dp_ifidx' members of interfaces that have
 + * This function will set the 'xf_ifidx' members of interfaces that have
   * disappeared to -1, so only call this function from a context where those
   * 'struct iface's will be removed from the bridge.  Otherwise, the -1
 - * 'dp_ifidx'es will cause trouble later when we try to send them to the
 + * 'xf_ifidx'es will cause trouble later when we try to send them to the
   * datapath, which doesn't support UINT16_MAX+1 ports. */
  static void
  bridge_fetch_dp_ifaces(struct bridge *br)
  {
 -    struct odp_port *dpif_ports;
 -    size_t n_dpif_ports;
 +    struct xflow_port *xfif_ports;
 +    size_t n_xfif_ports;
      size_t i, j;
  
      /* Reset all interface numbers. */
          struct port *port = br->ports[i];
          for (j = 0; j < port->n_ifaces; j++) {
              struct iface *iface = port->ifaces[j];
 -            iface->dp_ifidx = -1;
 +            iface->xf_ifidx = -1;
          }
      }
-     port_array_clear(&br->ifaces);
+     hmap_clear(&br->ifaces);
  
 -    dpif_port_list(br->dpif, &dpif_ports, &n_dpif_ports);
 -    for (i = 0; i < n_dpif_ports; i++) {
 -        struct odp_port *p = &dpif_ports[i];
 +    xfif_port_list(br->xfif, &xfif_ports, &n_xfif_ports);
 +    for (i = 0; i < n_xfif_ports; i++) {
 +        struct xflow_port *p = &xfif_ports[i];
          struct iface *iface = iface_lookup(br, p->devname);
          if (iface) {
 -            if (iface->dp_ifidx >= 0) {
 +            if (iface->xf_ifidx >= 0) {
                  VLOG_WARN("%s reported interface %s twice",
 -                          dpif_name(br->dpif), p->devname);
 -            } else if (iface_from_dp_ifidx(br, p->port)) {
 +                          xfif_name(br->xfif), p->devname);
 +            } else if (iface_from_xf_ifidx(br, p->port)) {
                  VLOG_WARN("%s reported interface %"PRIu16" twice",
 -                          dpif_name(br->dpif), p->port);
 +                          xfif_name(br->xfif), p->port);
              } else {
-                 port_array_set(&br->ifaces, p->port, iface);
 -                iface->dp_ifidx = p->port;
 -                hmap_insert(&br->ifaces, &iface->dp_ifidx_node,
 -                            hash_int(iface->dp_ifidx, 0));
 +                iface->xf_ifidx = p->port;
++                hmap_insert(&br->ifaces, &iface->xf_ifidx_node,
++                            hash_int(iface->xf_ifidx, 0));
              }
  
              if (iface->cfg) {
 -                int64_t ofport = (iface->dp_ifidx >= 0
 -                                  ? odp_port_to_ofp_port(iface->dp_ifidx)
 +                int64_t ofport = (iface->xf_ifidx >= 0
 +                                  ? xflow_port_to_ofp_port(iface->xf_ifidx)
                                    : -1);
                  ovsrec_interface_set_ofport(iface->cfg, &ofport, 1);
              }
          }
      }
 -    free(dpif_ports);
 +    free(xfif_ports);
  }
  \f
  /* Bridge packet processing functions. */
@@@ -1787,7 -1832,7 +1819,7 @@@ bond_choose_iface(const struct port *po
  
  static bool
  choose_output_iface(const struct port *port, const uint8_t *dl_src,
 -                    uint16_t *dp_ifidx, tag_type *tags)
 +                    uint16_t *xf_ifidx, tag_type *tags)
  {
      struct iface *iface;
  
          *tags |= e->iface_tag;
          iface = port->ifaces[e->iface_idx];
      }
 -    *dp_ifidx = iface->dp_ifidx;
 +    *xf_ifidx = iface->xf_ifidx;
      *tags |= iface->tag;        /* Currently only used for bonding. */
      return true;
  }
@@@ -2014,7 -2059,7 +2046,7 @@@ set_dst(struct dst *p, const flow_t *fl
      p->vlan = (out_port->vlan >= 0 ? OFP_VLAN_NONE
                : in_port->vlan >= 0 ? in_port->vlan
                : ntohs(flow->dl_vlan));
 -    return choose_output_iface(out_port, flow->dl_src, &p->dp_ifidx, tags);
 +    return choose_output_iface(out_port, flow->dl_src, &p->xf_ifidx, tags);
  }
  
  static void
@@@ -2075,7 -2120,7 +2107,7 @@@ dst_is_duplicate(const struct dst *dsts
  {
      size_t i;
      for (i = 0; i < n_dsts; i++) {
 -        if (dsts[i].vlan == test->vlan && dsts[i].dp_ifidx == test->dp_ifidx) {
 +        if (dsts[i].vlan == test->vlan && dsts[i].xf_ifidx == test->xf_ifidx) {
              return true;
          }
      }
@@@ -2105,7 -2150,7 +2137,7 @@@ compose_dsts(const struct bridge *br, c
      size_t i;
  
      if (out_port == FLOOD_PORT) {
 -        /* XXX use ODP_FLOOD if no vlans or bonding. */
 +        /* XXX use XFLOW_FLOOD if no vlans or bonding. */
          /* XXX even better, define each VLAN as a datapath port group */
          for (i = 0; i < br->n_ports; i++) {
              struct port *port = br->ports[i];
          }
          *nf_output_iface = NF_OUT_FLOOD;
      } else if (out_port && set_dst(dst, flow, in_port, out_port, tags)) {
 -        *nf_output_iface = dst->dp_ifidx;
 +        *nf_output_iface = dst->xf_ifidx;
          mirrors |= out_port->dst_mirrors;
          dst++;
      }
@@@ -2176,7 -2221,7 +2208,7 @@@ static void OVS_UNUSE
  print_dsts(const struct dst *dsts, size_t n)
  {
      for (; n--; dsts++) {
 -        printf(">p%"PRIu16, dsts->dp_ifidx);
 +        printf(">p%"PRIu16, dsts->xf_ifidx);
          if (dsts->vlan != OFP_VLAN_NONE) {
              printf("v%"PRIu16, dsts->vlan);
          }
  static void
  compose_actions(struct bridge *br, const flow_t *flow, uint16_t vlan,
                  const struct port *in_port, const struct port *out_port,
 -                tag_type *tags, struct odp_actions *actions,
 +                tag_type *tags, struct xflow_actions *actions,
                  uint16_t *nf_output_iface)
  {
      struct dst dsts[DP_MAX_PORTS * (MAX_MIRRORS + 1)];
  
      cur_vlan = ntohs(flow->dl_vlan);
      for (p = dsts; p < &dsts[n_dsts]; p++) {
 -        union odp_action *a;
 +        union xflow_action *a;
          if (p->vlan != cur_vlan) {
              if (p->vlan == OFP_VLAN_NONE) {
 -                odp_actions_add(actions, ODPAT_STRIP_VLAN);
 +                xflow_actions_add(actions, XFLOWAT_STRIP_VLAN);
              } else {
 -                a = odp_actions_add(actions, ODPAT_SET_VLAN_VID);
 -                a->vlan_vid.vlan_vid = htons(p->vlan);
 +                a = xflow_actions_add(actions, XFLOWAT_SET_DL_TCI);
 +                a->dl_tci.tci = htons(p->vlan & VLAN_VID_MASK);
 +                a->dl_tci.mask = htons(VLAN_VID_MASK);
              }
              cur_vlan = p->vlan;
          }
 -        a = odp_actions_add(actions, ODPAT_OUTPUT);
 -        a->output.port = p->dp_ifidx;
 +        a = xflow_actions_add(actions, XFLOWAT_OUTPUT);
 +        a->output.port = p->xf_ifidx;
      }
  }
  
@@@ -2331,7 -2375,7 +2363,7 @@@ is_admissible(struct bridge *br, const 
      int vlan;
  
      /* Find the interface and port structure for the received packet. */
 -    in_iface = iface_from_dp_ifidx(br, flow->in_port);
 +    in_iface = iface_from_xf_ifidx(br, ofp_port_to_xflow_port(flow->in_port));
      if (!in_iface) {
          /* No interface?  Something fishy... */
          if (have_packet) {
   * not at all, if 'packet' was NULL. */
  static bool
  process_flow(struct bridge *br, const flow_t *flow,
 -             const struct ofpbuf *packet, struct odp_actions *actions,
 +             const struct ofpbuf *packet, struct xflow_actions *actions,
               tag_type *tags, uint16_t *nf_output_iface)
  {
      struct port *in_port;
@@@ -2473,7 -2517,7 +2505,7 @@@ bridge_port_changed_ofhook_cb(enum ofp_
      struct iface *iface;
      struct port *port;
  
 -    iface = iface_from_dp_ifidx(br, ofp_port_to_odp_port(opp->port_no));
 +    iface = iface_from_xf_ifidx(br, ofp_port_to_xflow_port(opp->port_no));
      if (!iface) {
          return;
      }
  
  static bool
  bridge_normal_ofhook_cb(const flow_t *flow, const struct ofpbuf *packet,
 -                        struct odp_actions *actions, tag_type *tags,
 +                        struct xflow_actions *actions, tag_type *tags,
                          uint16_t *nf_output_iface, void *br_)
  {
      struct bridge *br = br_;
  
  static void
  bridge_account_flow_ofhook_cb(const flow_t *flow, tag_type tags,
 -                              const union odp_action *actions,
 +                              const union xflow_action *actions,
                                size_t n_actions, unsigned long long int n_bytes,
                                void *br_)
  {
      struct bridge *br = br_;
 -    const union odp_action *a;
 +    const union xflow_action *a;
      struct port *in_port;
      tag_type dummy = 0;
      int vlan;
          return;
      }
      for (a = actions; a < &actions[n_actions]; a++) {
 -        if (a->type == ODPAT_OUTPUT) {
 -            struct port *out_port = port_from_dp_ifidx(br, a->output.port);
 +        if (a->type == XFLOWAT_OUTPUT) {
 +            struct port *out_port = port_from_xf_ifidx(br, a->output.port);
              if (out_port && out_port->n_ifaces >= 2) {
                  struct bond_entry *e = lookup_bond_entry(out_port,
                                                           flow->dl_src);
@@@ -2810,7 -2854,6 +2842,6 @@@ bond_rebalance_port(struct port *port
               * smallest hashes instead of the biggest ones.  There is little
               * reason behind this decision; we could use the opposite sort
               * order to shift away big hashes ahead of small ones. */
-             size_t i;
              bool order_swapped;
  
              for (i = 0; i < from->n_hashes; i++) {
@@@ -2891,15 -2934,15 +2922,15 @@@ bond_send_learning_packets(struct port 
  
      ofpbuf_init(&packet, 128);
      error = n_packets = n_errors = 0;
-     LIST_FOR_EACH (e, struct mac_entry, lru_node, &br->ml->lrus) {
+     LIST_FOR_EACH (e, lru_node, &br->ml->lrus) {
          union ofp_action actions[2], *a;
 -        uint16_t dp_ifidx;
 +        uint16_t xf_ifidx;
          tag_type tags = 0;
          flow_t flow;
          int retval;
  
          if (e->port == port->port_idx
 -            || !choose_output_iface(port, e->mac, &dp_ifidx, &tags)) {
 +            || !choose_output_iface(port, e->mac, &xf_ifidx, &tags)) {
              continue;
          }
  
          }
          a->output.type = htons(OFPAT_OUTPUT);
          a->output.len = htons(sizeof *a);
 -        a->output.port = htons(odp_port_to_ofp_port(dp_ifidx));
 +        a->output.port = htons(xflow_port_to_ofp_port(xf_ifidx));
          a++;
  
          /* Send packet. */
          n_packets++;
          compose_benign_packet(&packet, "Open vSwitch Bond Failover", 0xf177,
                                e->mac);
 -        flow_extract(&packet, 0, ODPP_NONE, &flow);
 +        flow_extract(&packet, 0, OFPP_NONE, &flow);
          retval = ofproto_send_packet(br->ofproto, &flow, actions, a - actions,
                                       &packet);
          if (retval) {
@@@ -2953,7 -2996,7 +2984,7 @@@ bond_unixctl_list(struct unixctl_conn *
  
      ds_put_cstr(&ds, "bridge\tbond\tslaves\n");
  
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
          size_t i;
  
          for (i = 0; i < br->n_ports; i++) {
@@@ -2982,7 -3025,7 +3013,7 @@@ bond_find(const char *name
  {
      const struct bridge *br;
  
-     LIST_FOR_EACH (br, struct bridge, node, &all_bridges) {
+     LIST_FOR_EACH (br, node, &all_bridges) {
          size_t i;
  
          for (i = 0; i < br->n_ports; i++) {
@@@ -3042,14 -3085,13 +3073,13 @@@ bond_unixctl_show(struct unixctl_conn *
                            hash, be->tx_bytes / 1024);
  
              /* MACs. */
-             LIST_FOR_EACH (me, struct mac_entry, lru_node,
-                            &port->bridge->ml->lrus) {
+             LIST_FOR_EACH (me, lru_node, &port->bridge->ml->lrus) {
 -                uint16_t dp_ifidx;
 +                uint16_t xf_ifidx;
                  tag_type tags = 0;
                  if (bond_hash(me->mac) == hash
                      && me->port != port->port_idx
 -                    && choose_output_iface(port, me->mac, &dp_ifidx, &tags)
 -                    && dp_ifidx == iface->dp_ifidx)
 +                    && choose_output_iface(port, me->mac, &xf_ifidx, &tags)
 +                    && xf_ifidx == iface->xf_ifidx)
                  {
                      ds_put_format(&ds, "\t\t"ETH_ADDR_FMT"\n",
                                    ETH_ADDR_ARGS(me->mac));
@@@ -3395,7 -3437,6 +3425,6 @@@ port_reconfigure(struct port *port, con
      trunks = NULL;
      if (vlan < 0 && cfg->n_trunks) {
          size_t n_errors;
-         size_t i;
  
          trunks = bitmap_allocate(4096);
          n_errors = 0;
@@@ -3466,9 -3507,9 +3495,9 @@@ port_destroy(struct port *port
  }
  
  static struct port *
 -port_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
 +port_from_xf_ifidx(const struct bridge *br, uint16_t xf_ifidx)
  {
 -    struct iface *iface = iface_from_dp_ifidx(br, dp_ifidx);
 +    struct iface *iface = iface_from_xf_ifidx(br, xf_ifidx);
      return iface ? iface->port : NULL;
  }
  
@@@ -3644,7 -3685,7 +3673,7 @@@ iface_create(struct port *port, const s
      iface->port = port;
      iface->port_ifidx = port->n_ifaces;
      iface->name = xstrdup(name);
 -    iface->dp_ifidx = -1;
 +    iface->xf_ifidx = -1;
      iface->tag = tag_create_random();
      iface->delay_expires = LLONG_MAX;
      iface->netdev = NULL;
@@@ -3693,8 -3734,8 +3722,8 @@@ iface_destroy(struct iface *iface
  
          shash_find_and_delete_assert(&br->iface_by_name, iface->name);
  
 -        if (iface->dp_ifidx >= 0) {
 -            hmap_remove(&br->ifaces, &iface->dp_ifidx_node);
 +        if (iface->xf_ifidx >= 0) {
-             port_array_set(&br->ifaces, iface->xf_ifidx, NULL);
++            hmap_remove(&br->ifaces, &iface->xf_ifidx_node);
          }
  
          del = port->ifaces[iface->port_ifidx] = port->ifaces[--port->n_ifaces];
@@@ -3722,14 -3763,22 +3751,22 @@@ iface_lookup(const struct bridge *br, c
  }
  
  static struct iface *
 -iface_from_dp_ifidx(const struct bridge *br, uint16_t dp_ifidx)
 +iface_from_xf_ifidx(const struct bridge *br, uint16_t xf_ifidx)
  {
-     return port_array_get(&br->ifaces, xf_ifidx);
+     struct iface *iface;
 -    HMAP_FOR_EACH_IN_BUCKET (iface, dp_ifidx_node,
 -                             hash_int(dp_ifidx, 0), &br->ifaces) {
 -        if (iface->dp_ifidx == dp_ifidx) {
++    HMAP_FOR_EACH_IN_BUCKET (iface, xf_ifidx_node,
++                             hash_int(xf_ifidx, 0), &br->ifaces) {
++        if (iface->xf_ifidx == xf_ifidx) {
+             return iface;
+         }
+     }
+     return NULL;
  }
  
  /* Returns true if 'iface' is the name of an "internal" interface on bridge
   * 'br', that is, an interface that is entirely simulated within the datapath.
 - * The local port (ODPP_LOCAL) is always an internal interface.  Other local
 + * The local port (XFLOWP_LOCAL) is always an internal interface.  Other local
   * interfaces are created by setting "iface.<iface>.internal = true".
   *
   * In addition, we have a kluge-y feature that creates an internal port with
@@@ -3770,7 -3819,7 +3807,7 @@@ iface_set_mac(struct iface *iface
          if (eth_addr_is_multicast(ea)) {
              VLOG_ERR("interface %s: cannot set MAC to multicast address",
                       iface->name);
 -        } else if (iface->dp_ifidx == ODPP_LOCAL) {
 +        } else if (iface->xf_ifidx == XFLOWP_LOCAL) {
              VLOG_ERR("ignoring iface.%s.mac; use bridge.%s.mac instead",
                       iface->name, iface->name);
          } else {