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

@@@ -553,31 -548,39 +548,39 @@@ void dp_process_received_packet(struct 
  
        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()];
Simple merge
diff --cc 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);
@@@ -238,14 -267,16 +266,15 @@@ static __be16 parse_ethertype(struct sk
   * 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
diff --cc datapath/flow.h
@@@ -33,9 -33,12 +33,12 @@@ 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)
  {
@@@ -514,98 -688,283 +688,283 @@@ 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;
        }
  }
  
Simple merge
Simple merge
Simple merge
Simple merge
@@@ -333,24 -326,24 +332,24 @@@ classifier_find_rule_exactly(const stru
      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;
                  }
              }
@@@ -378,13 -373,12 +377,12 @@@ classifier_rule_overlaps(const struct c
      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;
                  }
              }
@@@ -433,14 -419,10 +430,13 @@@ classifier_for_each_match(const struct 
                   * 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 (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 {
@@@ -515,13 -481,9 +510,12 @@@ classifier_for_each(const struct classi
                   * 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 (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,9 -634,9 +674,9 @@@ 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);
@@@ -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 --cc lib/hmap.h
Simple merge
Simple merge
diff --cc lib/list.h
@@@ -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 */
Simple merge
Simple merge
Simple merge
diff --cc lib/netdev.c
Simple merge
diff --cc lib/ofp-parse.c
@@@ -501,3 -514,65 +518,68 @@@ parse_ofp_str(char *string, struct ofp_
          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 --cc lib/ofp-parse.h
@@@ -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 --cc lib/ofp-print.c
@@@ -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');
diff --cc lib/ofp-util.c
@@@ -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)
diff --cc 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;
  };
diff --cc lib/rtnetlink.c
Simple merge
@@@ -76,18 -76,16 +76,19 @@@ VLOG_MODULE(stream_unix
  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
@@@ -328,18 -328,17 +328,17 @@@ xfif_linux_port_list(const struct xfif 
  }
  
  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;
@@@ -478,9 -477,8 +477,8 @@@ xfif_linux_recv(struct xfif *xfif_, str
      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) {
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,
 +};
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;
 +}
  #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"
  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)
@@@ -266,9 -284,9 +286,9 @@@ 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;
  }
  
@@@ -333,10 -350,11 +352,11 @@@ ofproto_sflow_add_port(struct ofproto_s
      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) {
      }
  }
  
+ 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);
      }
  }
  
@@@ -434,21 -458,21 +460,21 @@@ ofproto_sflow_set_options(struct ofprot
      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;
  }
  
  #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"
@@@ -821,9 -949,9 +820,8 @@@ ofproto_destroy(struct ofproto *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);
@@@ -907,30 -1055,6 +904,30 @@@ add_snooper(struct ofproto *ofproto, st
      }
  }
  
-     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)
  {
@@@ -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++) {
@@@ -1945,8 -3423,8 +1940,8 @@@ handle_aggregate_stats_request(struct o
  
  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,10 -3453,10 +1970,10 @@@ 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);
@@@ -2764,50 -4316,137 +2759,50 @@@ delete_flow(struct ofproto *p, struct w
       * 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'. */
Simple merge
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
 +};
Simple merge
Simple merge
@@@ -28,6 -28,7 +28,8 @@@
  #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"
@@@ -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[])
  {
Simple merge
@@@ -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
@@@ -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);
@@@ -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)
  {
Simple merge
@@@ -84,7 -85,8 +85,8 @@@ struct iface 
  
      /* 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;
@@@ -605,42 -622,41 +621,41 @@@ bridge_reconfigure(const struct ovsrec_
       * 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);
@@@ -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);
@@@ -1709,25 -1753,26 +1740,26 @@@ bridge_fetch_dp_ifaces(struct bridge *b
          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) {
@@@ -2891,9 -2934,9 +2922,9 @@@ 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;
@@@ -3042,9 -3085,8 +3073,8 @@@ 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
@@@ -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,9 -3763,17 +3751,17 @@@ 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