Fix "make dist".
[sliver-openvswitch.git] / datapath / datapath.c
index 7d2a44c..a4c6a2d 100644 (file)
@@ -33,9 +33,9 @@
 #include "datapath.h"
 #include "table.h"
 #include "chain.h"
+#include "dp_dev.h"
 #include "forward.h"
 #include "flow.h"
-#include "datapath_t.h"
 
 #include "compat.h"
 
@@ -46,6 +46,7 @@
 #define BRIDGE_PORT_NO_FLOOD   0x00000001 
 
 #define UINT32_MAX                       4294967295U
+#define UINT16_MAX                       65535
 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
 
 struct net_bridge_port {
@@ -59,8 +60,6 @@ struct net_bridge_port {
 static struct genl_family dp_genl_family;
 static struct genl_multicast_group mc_group;
 
-int dp_dev_setup(struct net_device *dev);  
-
 /* It's hard to imagine wanting more than one datapath, but... */
 #define DP_MAX 32
 
@@ -76,7 +75,10 @@ static DEFINE_MUTEX(dp_mutex);
 
 static int dp_maint_func(void *data);
 static int send_port_status(struct net_bridge_port *p, uint8_t status);
-
+static int dp_genl_openflow_done(struct netlink_callback *);
+static struct net_bridge_port *new_nbp(struct datapath *,
+                                      struct net_device *, int port_no);
+static int del_switch_port(struct net_bridge_port *);
 
 /* nla_shrink - reduce amount of space reserved by nla_reserve
  * @skb: socket buffer from which to recover room
@@ -169,6 +171,13 @@ alloc_openflow_skb(struct datapath *dp, size_t openflow_len, uint8_t type,
        struct sk_buff *skb;
        int max_openflow_len;
 
+       if ((openflow_len + sizeof(struct ofp_header)) > UINT16_MAX) {
+               if (net_ratelimit())
+                       printk("alloc_openflow_skb: openflow message too large: %zu\n", 
+                                       openflow_len);
+               return NULL;
+       }
+
        genl_len = nlmsg_total_size(GENL_HDRLEN + dp_genl_family.hdrsize);
        genl_len += nla_total_size(sizeof(uint32_t)); /* DP_GENL_A_DP_IDX */
        genl_len += nla_total_size(openflow_len);    /* DP_GENL_A_OPENFLOW */
@@ -191,13 +200,9 @@ alloc_openflow_skb(struct datapath *dp, size_t openflow_len, uint8_t type,
 static int
 send_openflow_skb(struct sk_buff *skb, const struct sender *sender) 
 {
-       int err = (sender
-                  ? genlmsg_unicast(skb, sender->pid)
-                  : genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC));
-       if (err && net_ratelimit())
-               printk(KERN_WARNING "send_openflow_skb: send failed: %d\n",
-                      err);
-       return err;
+       return (sender
+               ? genlmsg_unicast(skb, sender->pid)
+               : genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC));
 }
 
 /* Generates a unique datapath id.  It incorporates the datapath index
@@ -262,35 +267,42 @@ static int new_dp(int dp_idx)
        if (dp == NULL)
                goto err_unlock;
 
+       /* Setup our "of" device */
+       err = dp_dev_setup(dp);
+       if (err)
+               goto err_free_dp;
+
        dp->dp_idx = dp_idx;
        dp->id = gen_datapath_id(dp_idx);
        dp->chain = chain_create(dp);
        if (dp->chain == NULL)
-               goto err_free_dp;
+               goto err_destroy_dp_dev;
        INIT_LIST_HEAD(&dp->port_list);
 
-#if 0
-       /* Setup our "of" device */
-       dp->dev.priv = dp;
-       rtnl_lock();
-       err = dp_dev_setup(&dp->dev);
-       rtnl_unlock();
-       if (err != 0) 
-               printk("datapath: problem setting up 'of' device\n");
-#endif
+       dp->local_port = new_nbp(dp, dp->netdev, OFPP_LOCAL);
+       if (IS_ERR(dp->local_port)) {
+               err = PTR_ERR(dp->local_port);
+               goto err_destroy_local_port;
+       }
 
        dp->flags = 0;
        dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
 
        dp->dp_task = kthread_run(dp_maint_func, dp, "dp%d", dp_idx);
        if (IS_ERR(dp->dp_task))
-               goto err_free_dp;
+               goto err_destroy_chain;
 
        rcu_assign_pointer(dps[dp_idx], dp);
        mutex_unlock(&dp_mutex);
 
        return 0;
 
+err_destroy_local_port:
+       del_switch_port(dp->local_port);
+err_destroy_chain:
+       chain_destroy(dp->chain);
+err_destroy_dp_dev:
+       dp_dev_destroy(dp);
 err_free_dp:
        kfree(dp);
 err_unlock:
@@ -310,23 +322,29 @@ static int find_portno(struct datapath *dp)
 }
 
 static struct net_bridge_port *new_nbp(struct datapath *dp,
-                                                                          struct net_device *dev)
+                                      struct net_device *dev, int port_no)
 {
        struct net_bridge_port *p;
-       int port_no;
 
-       port_no = find_portno(dp);
-       if (port_no < 0)
-               return ERR_PTR(port_no);
+       if (dev->br_port != NULL)
+               return ERR_PTR(-EBUSY);
 
        p = kzalloc(sizeof(*p), GFP_KERNEL);
        if (p == NULL)
                return ERR_PTR(-ENOMEM);
 
-       p->dp = dp;
+       rtnl_lock();
+       dev_set_promiscuity(dev, 1);
+       rtnl_unlock();
        dev_hold(dev);
+       p->dp = dp;
        p->dev = dev;
        p->port_no = port_no;
+       if (port_no != OFPP_LOCAL)
+               rcu_assign_pointer(dev->br_port, p);
+       if (port_no < OFPP_MAX)
+               rcu_assign_pointer(dp->ports[port_no], p); 
+       list_add_rcu(&p->node, &dp->port_list);
 
        return p;
 }
@@ -335,26 +353,20 @@ static struct net_bridge_port *new_nbp(struct datapath *dp,
 int add_switch_port(struct datapath *dp, struct net_device *dev)
 {
        struct net_bridge_port *p;
+       int port_no;
 
-       if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER)
+       if (dev->flags & IFF_LOOPBACK || dev->type != ARPHRD_ETHER
+           || is_dp_dev(dev))
                return -EINVAL;
 
-       if (dev->br_port != NULL)
-               return -EBUSY;
+       port_no = find_portno(dp);
+       if (port_no < 0)
+               return port_no;
 
-       p = new_nbp(dp, dev);
+       p = new_nbp(dp, dev, port_no);
        if (IS_ERR(p))
                return PTR_ERR(p);
 
-       dev_hold(dev);
-       rcu_assign_pointer(dev->br_port, p);
-       rtnl_lock();
-       dev_set_promiscuity(dev, 1);
-       rtnl_unlock();
-
-       rcu_assign_pointer(dp->ports[p->port_no], p);
-       list_add_rcu(&p->node, &dp->port_list);
-
        /* Notify the ctlpath that this port has been added */
        send_port_status(p, OFPPR_ADD);
 
@@ -370,7 +382,8 @@ static int del_switch_port(struct net_bridge_port *p)
        dev_set_promiscuity(p->dev, -1);
        rtnl_unlock();
        list_del_rcu(&p->node);
-       rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
+       if (p->port_no != OFPP_LOCAL)
+               rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
        rcu_assign_pointer(p->dev->br_port, NULL);
 
        /* Then wait until no one is still using it, and destroy it. */
@@ -390,13 +403,6 @@ static void del_dp(struct datapath *dp)
 {
        struct net_bridge_port *p, *n;
 
-#if 0
-       /* Unregister the "of" device of this dp */
-       rtnl_lock();
-       unregister_netdevice(&dp->dev);
-       rtnl_unlock();
-#endif
-
        kthread_stop(dp->dp_task);
 
        /* Drop references to DP. */
@@ -404,6 +410,15 @@ static void del_dp(struct datapath *dp)
                del_switch_port(p);
        rcu_assign_pointer(dps[dp->dp_idx], NULL);
 
+       /* Kill off local_port dev references from buffered packets that have
+        * associated dst entries. */
+       synchronize_rcu();
+       fwd_discard_all();
+
+       /* Destroy dp->netdev.  (Must follow deleting switch ports since
+        * dp->local_port has a reference to it.) */
+       dp_dev_destroy(dp);
+
        /* Wait until no longer in use, then destroy it. */
        synchronize_rcu();
        chain_destroy(dp->chain);
@@ -416,80 +431,49 @@ static int dp_maint_func(void *data)
        struct datapath *dp = (struct datapath *) data;
 
        while (!kthread_should_stop()) {
-#if 1
                chain_timeout(dp->chain);
-#else
-               int count = chain_timeout(dp->chain);
-               chain_print_stats(dp->chain);
-               if (count)
-                       printk("%d flows timed out\n", count);
-#endif
                msleep_interruptible(MAINT_SLEEP_MSECS);
        }
                
        return 0;
 }
 
+static void
+do_port_input(struct net_bridge_port *p, struct sk_buff *skb) 
+{
+       /* Push the Ethernet header back on. */
+       skb_push(skb, ETH_HLEN);
+       fwd_port_input(p->dp->chain, skb, p->port_no);
+}
+
 /*
  * Used as br_handle_frame_hook.  (Cannot run bridge at the same time, even on
- * different set of devices!)  Returns 0 if *pskb should be processed further,
- * 1 if *pskb is handled. */
+ * different set of devices!)
+ */
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
 /* Called with rcu_read_lock. */
 static struct sk_buff *dp_frame_hook(struct net_bridge_port *p,
                                         struct sk_buff *skb)
 {
-       struct ethhdr *eh = eth_hdr(skb);
-       struct sk_buff *skb_local = NULL;
-
-
-       if (compare_ether_addr(eh->h_dest, skb->dev->dev_addr) == 0) 
-               return skb;
-
-       if (is_broadcast_ether_addr(eh->h_dest)
-                               || is_multicast_ether_addr(eh->h_dest)
-                               || is_local_ether_addr(eh->h_dest)) 
-               skb_local = skb_clone(skb, GFP_ATOMIC);
-
-       /* Push the Ethernet header back on. */
-       if (skb->protocol == htons(ETH_P_8021Q))
-               skb_push(skb, VLAN_ETH_HLEN);
-       else
-               skb_push(skb, ETH_HLEN);
-
-       fwd_port_input(p->dp->chain, skb, p->port_no);
-
-       return skb_local;
+       do_port_input(p, skb);
+       return NULL;
 }
 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
 static int dp_frame_hook(struct net_bridge_port *p, struct sk_buff **pskb)
 {
-       /* Push the Ethernet header back on. */
-       if ((*pskb)->protocol == htons(ETH_P_8021Q))
-               skb_push(*pskb, VLAN_ETH_HLEN);
-       else
-               skb_push(*pskb, ETH_HLEN);
-
-       fwd_port_input(p->dp->chain, *pskb, p->port_no);
+       do_port_input(p, *pskb);
        return 1;
 }
-#else 
+#else
 /* NB: This has only been tested on 2.4.35 */
 
 /* Called without any locks (?) */
 static void dp_frame_hook(struct sk_buff *skb)
 {
        struct net_bridge_port *p = skb->dev->br_port;
-
-       /* Push the Ethernet header back on. */
-       if (skb->protocol == htons(ETH_P_8021Q))
-               skb_push(skb, VLAN_ETH_HLEN);
-       else
-               skb_push(skb, ETH_HLEN);
-
        if (p) {
                rcu_read_lock();
-               fwd_port_input(p->dp->chain, skb, p->port_no);
+               do_port_input(p, skb);
                rcu_read_unlock();
        } else
                kfree_skb(skb);
@@ -499,17 +483,6 @@ static void dp_frame_hook(struct sk_buff *skb)
 /* Forwarding output path.
  * Based on net/bridge/br_forward.c. */
 
-/* Don't forward packets to originating port or with flooding disabled */
-static inline int should_deliver(const struct net_bridge_port *p,
-                       const struct sk_buff *skb)
-{
-       if ((skb->dev == p->dev) || (p->flags & BRIDGE_PORT_NO_FLOOD)) {
-               return 0;
-       } 
-
-       return 1;
-}
-
 static inline unsigned packet_length(const struct sk_buff *skb)
 {
        int length = skb->len - ETH_HLEN;
@@ -518,15 +491,18 @@ static inline unsigned packet_length(const struct sk_buff *skb)
        return length;
 }
 
+/* Send packets out all the ports except the originating one.  If the
+ * "flood" argument is set, only send along the minimum spanning tree.
+ */
 static int
-flood(struct datapath *dp, struct sk_buff *skb)
+output_all(struct datapath *dp, struct sk_buff *skb, int flood)
 {
+       u32 disable = flood ? BRIDGE_PORT_NO_FLOOD : 0;
        struct net_bridge_port *p;
-       int prev_port;
+       int prev_port = -1;
 
-       prev_port = -1;
        list_for_each_entry_rcu (p, &dp->port_list, node) {
-               if (!should_deliver(p, skb))
+               if (skb->dev == p->dev || p->flags & disable)
                        continue;
                if (prev_port != -1) {
                        struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
@@ -551,8 +527,11 @@ flood(struct datapath *dp, struct sk_buff *skb)
 int dp_set_origin(struct datapath *dp, uint16_t in_port,
                           struct sk_buff *skb)
 {
-       if (in_port < OFPP_MAX && dp->ports[in_port]) {
-               skb->dev = dp->ports[in_port]->dev;
+       struct net_bridge_port *p = (in_port < OFPP_MAX ? dp->ports[in_port]
+                                    : in_port == OFPP_LOCAL ? dp->local_port
+                                    : NULL);
+       if (p) {
+               skb->dev = p->dev;
                return 0;
        }
        return -ENOENT;
@@ -562,20 +541,20 @@ int dp_set_origin(struct datapath *dp, uint16_t in_port,
  */
 int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
 {
-       struct net_bridge_port *p;
-       int len = skb->len;
-
        BUG_ON(!skb);
        if (out_port == OFPP_FLOOD)
-               return flood(dp, skb);
+               return output_all(dp, skb, 1);
+       else if (out_port == OFPP_ALL)
+               return output_all(dp, skb, 0);
        else if (out_port == OFPP_CONTROLLER)
                return dp_output_control(dp, skb, fwd_save_skb(skb), 0,
                                                  OFPR_ACTION);
        else if (out_port == OFPP_TABLE) {
+               struct net_bridge_port *p = skb->dev->br_port;
                struct sw_flow_key key;
                struct sw_flow *flow;
 
-               flow_extract(skb, skb->dev->br_port->port_no, &key);
+               flow_extract(skb, p ? p->port_no : OFPP_LOCAL, &key);
                flow = chain_lookup(dp->chain, &key);
                if (likely(flow != NULL)) {
                        flow_used(flow, skb);
@@ -583,25 +562,27 @@ int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
                        return 0;
                }
                return -ESRCH;
-       } else if (out_port >= OFPP_MAX)
-               goto bad_port;
+       } else if (out_port == OFPP_LOCAL) {
+               struct net_device *dev = dp->netdev;
+               return dev ? dp_dev_recv(dev, skb) : -ESRCH;
+       } else if (out_port >= 0 && out_port < OFPP_MAX) {
+               struct net_bridge_port *p = dp->ports[out_port];
+               int len = skb->len;
+               if (p == NULL)
+                       goto bad_port;
+               skb->dev = p->dev; 
+               if (packet_length(skb) > skb->dev->mtu) {
+                       printk("dropped over-mtu packet: %d > %d\n",
+                              packet_length(skb), skb->dev->mtu);
+                       kfree_skb(skb);
+                       return -E2BIG;
+               }
 
-       p = dp->ports[out_port];
-       if (p == NULL)
-               goto bad_port;
+               dev_queue_xmit(skb);
 
-       skb->dev = p->dev;
-       if (packet_length(skb) > skb->dev->mtu) {
-               printk("dropped over-mtu packet: %d > %d\n",
-                                       packet_length(skb), skb->dev->mtu);
-               kfree_skb(skb);
-               return -E2BIG;
+               return len;
        }
 
-       dev_queue_xmit(skb);
-
-       return len;
-
 bad_port:
        kfree_skb(skb);
        if (net_ratelimit())
@@ -623,6 +604,7 @@ dp_output_control(struct datapath *dp, struct sk_buff *skb,
         * forward the whole packet? */
        struct sk_buff *f_skb;
        struct ofp_packet_in *opi;
+       struct net_bridge_port *p;
        size_t fwd_len, opi_len;
        int err;
 
@@ -638,7 +620,8 @@ dp_output_control(struct datapath *dp, struct sk_buff *skb,
        }
        opi->buffer_id      = htonl(buffer_id);
        opi->total_len      = htons(skb->len);
-       opi->in_port        = htons(skb->dev->br_port->port_no);
+       p = skb->dev->br_port;
+       opi->in_port        = htons(p ? p->port_no : OFPP_LOCAL);
        opi->reason         = reason;
        opi->pad            = 0;
        memcpy(opi->data, skb_mac_header(skb), fwd_len);
@@ -755,16 +738,14 @@ dp_send_config_reply(struct datapath *dp, const struct sender *sender)
 int
 dp_update_port_flags(struct datapath *dp, const struct ofp_phy_port *opp)
 {
-       struct net_bridge_port *p;
-
-       p = dp->ports[htons(opp->port_no)];
-
+       int port_no = ntohs(opp->port_no);
+       struct net_bridge_port *p = (port_no < OFPP_MAX ? dp->ports[port_no]
+                                    : port_no == OFPP_LOCAL ? dp->local_port
+                                    : NULL);
        /* Make sure the port id hasn't changed since this was sent */
-       if (!p || memcmp(opp->hw_addr, p->dev->dev_addr, ETH_ALEN) != 0) 
+       if (!p || memcmp(opp->hw_addr, p->dev->dev_addr, ETH_ALEN))
                return -1;
-       
        p->flags = htonl(opp->flags);
-
        return 0;
 }
 
@@ -780,6 +761,7 @@ send_port_status(struct net_bridge_port *p, uint8_t status)
        if (!ops)
                return -ENOMEM;
        ops->reason = status;
+       memset(ops->pad, 0, sizeof ops->pad);
        fill_port_desc(p, &ops->desc);
 
        return send_openflow_skb(skb, NULL);
@@ -797,105 +779,52 @@ dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow)
                return -ENOMEM;
 
        flow_fill_match(&ofe->match, &flow->key);
-       duration_j = (flow->timeout - HZ * flow->max_idle) - flow->init_time;
-       ofe->duration   = htonl(duration_j / HZ);
-       ofe->packet_count   = cpu_to_be64(flow->packet_count);
-       ofe->byte_count     = cpu_to_be64(flow->byte_count);
-       return send_openflow_skb(skb, NULL);
-}
 
-static void
-fill_flow_stats(struct ofp_flow_stats *ofs, struct sw_flow *flow,
-               int table_idx)
-{
-       ofs->match.wildcards = htons(flow->key.wildcards);
-       ofs->match.in_port   = flow->key.in_port;
-       memcpy(ofs->match.dl_src, flow->key.dl_src, ETH_ALEN);
-       memcpy(ofs->match.dl_dst, flow->key.dl_dst, ETH_ALEN);
-       ofs->match.dl_vlan   = flow->key.dl_vlan;
-       ofs->match.dl_type   = flow->key.dl_type;
-       ofs->match.nw_src    = flow->key.nw_src;
-       ofs->match.nw_dst    = flow->key.nw_dst;
-       ofs->match.nw_proto  = flow->key.nw_proto;
-       memset(ofs->match.pad, 0, sizeof ofs->match.pad);
-       ofs->match.tp_src    = flow->key.tp_src;
-       ofs->match.tp_dst    = flow->key.tp_dst;
-       ofs->duration        = htonl((jiffies - flow->init_time) / HZ);
-       ofs->priority        = htons(flow->priority);
-       ofs->table_id        = table_idx;
-       ofs->packet_count    = cpu_to_be64(flow->packet_count);
-       ofs->byte_count      = cpu_to_be64(flow->byte_count);
-}
+       memset(ofe->pad, 0, sizeof ofe->pad);
+       ofe->priority = htons(flow->priority);
 
-static int 
-fill_port_stats_reply(struct datapath *dp, struct ofp_port_stats_reply *psr)
-{
-       struct net_bridge_port *p;
-       int port_count = 0;
-
-       list_for_each_entry_rcu (p, &dp->port_list, node) {
-               struct ofp_port_stats *ps = &psr->ports[port_count++];
-               struct net_device_stats *stats = p->dev->get_stats(p->dev);
-               ps->port_no = htons(p->port_no);
-               memset(ps->pad, 0, sizeof ps->pad);
-               ps->rx_count = cpu_to_be64(stats->rx_packets);
-               ps->tx_count = cpu_to_be64(stats->tx_packets);
-               ps->drop_count = cpu_to_be64(stats->rx_dropped
-                                            + stats->tx_dropped);
-       }
+       duration_j = (flow->timeout - HZ * flow->max_idle) - flow->init_time;
+       ofe->duration     = htonl(duration_j / HZ);
+       ofe->packet_count = cpu_to_be64(flow->packet_count);
+       ofe->byte_count   = cpu_to_be64(flow->byte_count);
 
-       return port_count;
+       return send_openflow_skb(skb, NULL);
 }
+EXPORT_SYMBOL(dp_send_flow_expired);
 
 int
-dp_send_port_stats(struct datapath *dp, const struct sender *sender)
+dp_send_error_msg(struct datapath *dp, const struct sender *sender, 
+               uint16_t type, uint16_t code, const uint8_t *data, size_t len)
 {
        struct sk_buff *skb;
-       struct ofp_port_stats_reply *psr;
-       size_t psr_len, port_max_len;
-       int port_count;
+       struct ofp_error_msg *oem;
 
-       /* Overallocate. */
-       port_max_len = sizeof(struct ofp_port_stats) * OFPP_MAX;
-       psr = alloc_openflow_skb(dp, sizeof *psr + port_max_len,
-                                OFPT_PORT_STATS_REPLY, sender, &skb);
-       if (!psr)
+
+       oem = alloc_openflow_skb(dp, sizeof(*oem)+len, OFPT_ERROR_MSG, 
+                       sender, &skb);
+       if (!oem)
                return -ENOMEM;
 
-       /* Fill. */
-       port_count = fill_port_stats_reply(dp, psr);
+       oem->type = htons(type);
+       oem->code = htons(code);
+       memcpy(oem->data, data, len);
 
-       /* Shrink to fit. */
-       psr_len = sizeof *psr + sizeof(struct ofp_port_stats) * port_count;
-       resize_openflow_skb(skb, &psr->header, psr_len);
        return send_openflow_skb(skb, sender);
 }
 
 int
-dp_send_table_stats(struct datapath *dp, const struct sender *sender)
+dp_send_echo_reply(struct datapath *dp, const struct sender *sender,
+                  const struct ofp_header *rq)
 {
        struct sk_buff *skb;
-       struct ofp_table_stats_reply *tsr;
-       int i, n_tables;
-
-       n_tables = dp->chain->n_tables;
-       tsr = alloc_openflow_skb(dp, (offsetof(struct ofp_table_stats_reply,
-                                              tables)
-                                     + sizeof tsr->tables[0] * n_tables),
-                                OFPT_TABLE_STATS_REPLY, sender, &skb);
-       if (!tsr)
+       struct ofp_header *reply;
+
+       reply = alloc_openflow_skb(dp, ntohs(rq->length), OFPT_ECHO_REPLY,
+                                  sender, &skb);
+       if (!reply)
                return -ENOMEM;
-       for (i = 0; i < n_tables; i++) {
-               struct ofp_table_stats *ots = &tsr->tables[i];
-               struct sw_table_stats stats;
-               dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
-               strncpy(ots->name, stats.name, sizeof ots->name);
-               ots->table_id = i;
-               ots->pad[0] = ots->pad[1] = 0;
-               ots->max_entries = htonl(stats.max_flows);
-               ots->active_count = htonl(stats.n_flows);
-               ots->matched_count = cpu_to_be64(0); /* FIXME */
-       }
+
+       memcpy(reply + 1, rq + 1, ntohs(rq->length) - sizeof *rq);
        return send_openflow_skb(skb, sender);
 }
 
@@ -1131,134 +1060,409 @@ static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
        [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
 };
 
-struct flow_stats_cb_state {
-       int dp_idx;
+struct flow_stats_state {
        int table_idx;
        struct sw_table_position position;
-       struct ofp_flow_stats_request *rq;
-       int sent_terminator;
+       const struct ofp_flow_stats_request *rq;
 
-       struct ofp_flow_stats *flows;
-       int n_flows, max_flows;
+       void *body;
+       int bytes_used, bytes_allocated;
 };
 
-static int muster_callback(struct sw_flow *flow, void *private)
+static int flow_stats_init(struct datapath *dp, const void *body, int body_len,
+                          void **state)
 {
-       struct flow_stats_cb_state *s = private;
+       const struct ofp_flow_stats_request *fsr = body;
+       struct flow_stats_state *s = kmalloc(sizeof *s, GFP_ATOMIC);
+       if (!s)
+               return -ENOMEM;
+       s->table_idx = fsr->table_id == 0xff ? 0 : fsr->table_id;
+       memset(&s->position, 0, sizeof s->position);
+       s->rq = fsr;
+       *state = s;
+       return 0;
+}
+
+static int flow_stats_dump_callback(struct sw_flow *flow, void *private)
+{
+       struct flow_stats_state *s = private;
+       struct ofp_flow_stats *ofs;
+       int actions_length;
+       int length;
+
+       actions_length = sizeof *ofs->actions * flow->n_actions;
+       length = sizeof *ofs + sizeof *ofs->actions * flow->n_actions;
+       if (length + s->bytes_used > s->bytes_allocated)
+               return 1;
+
+       ofs = s->body + s->bytes_used;
+       ofs->length          = htons(length);
+       ofs->table_id        = s->table_idx;
+       ofs->pad             = 0;
+       ofs->match.wildcards = htons(flow->key.wildcards);
+       ofs->match.in_port   = flow->key.in_port;
+       memcpy(ofs->match.dl_src, flow->key.dl_src, ETH_ALEN);
+       memcpy(ofs->match.dl_dst, flow->key.dl_dst, ETH_ALEN);
+       ofs->match.dl_vlan   = flow->key.dl_vlan;
+       ofs->match.dl_type   = flow->key.dl_type;
+       ofs->match.nw_src    = flow->key.nw_src;
+       ofs->match.nw_dst    = flow->key.nw_dst;
+       ofs->match.nw_proto  = flow->key.nw_proto;
+       memset(ofs->match.pad, 0, sizeof ofs->match.pad);
+       ofs->match.tp_src    = flow->key.tp_src;
+       ofs->match.tp_dst    = flow->key.tp_dst;
+       ofs->duration        = htonl((jiffies - flow->init_time) / HZ);
+       ofs->packet_count    = cpu_to_be64(flow->packet_count);
+       ofs->byte_count      = cpu_to_be64(flow->byte_count);
+       ofs->priority        = htons(flow->priority);
+       ofs->max_idle        = htons(flow->max_idle);
+       memcpy(ofs->actions, flow->actions, actions_length);
 
-       fill_flow_stats(&s->flows[s->n_flows], flow, s->table_idx);
-       return ++s->n_flows >= s->max_flows;
+       s->bytes_used += length;
+       return 0;
 }
 
-int
-muster_flow_stats(struct datapath *dp, struct flow_stats_cb_state *s,
-                 const struct sender *sender, struct sk_buff *skb)
+static int flow_stats_dump(struct datapath *dp, void *state,
+                          void *body, int *body_len)
 {
-       struct ofp_flow_stats_reply *fsr;
-       size_t header_size, flow_size;
+       struct flow_stats_state *s = state;
        struct sw_flow_key match_key;
-       int max_openflow_len;
-       size_t size;
+       int error = 0;
 
-       fsr = put_openflow_headers(dp, skb, OFPT_FLOW_STATS_REPLY, sender,
-                                  &max_openflow_len);
-       if (IS_ERR(fsr))
-               return PTR_ERR(fsr);
-       resize_openflow_skb(skb, &fsr->header, max_openflow_len);
-
-       header_size = offsetof(struct ofp_flow_stats_reply, flows);
-       flow_size = sizeof fsr->flows[0];
-       s->max_flows = (max_openflow_len - header_size) / flow_size;
-       if (s->max_flows <= 0)
-               return -ENOMEM;
-       s->flows = fsr->flows;
+       s->bytes_used = 0;
+       s->bytes_allocated = *body_len;
+       s->body = body;
 
        flow_extract_match(&match_key, &s->rq->match);
-       s->n_flows = 0;
        while (s->table_idx < dp->chain->n_tables
               && (s->rq->table_id == 0xff || s->rq->table_id == s->table_idx))
        {
                struct sw_table *table = dp->chain->tables[s->table_idx];
 
-               if (table->iterate(table, &match_key, &s->position,
-                                  muster_callback, s))
+               error = table->iterate(table, &match_key, &s->position,
+                                      flow_stats_dump_callback, s);
+               if (error)
                        break;
 
                s->table_idx++;
                memset(&s->position, 0, sizeof s->position);
        }
-       if (!s->n_flows) {
-               /* Signal dump completion. */
-               if (s->sent_terminator) {
-                       return 0;
-               }
-               s->sent_terminator = 1;
+       *body_len = s->bytes_used;
+
+       /* If error is 0, we're done.
+        * Otherwise, if some bytes were used, there are more flows to come.
+        * Otherwise, we were not able to fit even a single flow in the body,
+        * which indicates that we have a single flow with too many actions to
+        * fit.  We won't ever make any progress at that rate, so give up. */
+       return !error ? 0 : s->bytes_used ? 1 : -ENOMEM;
+}
+
+static void flow_stats_done(void *state)
+{
+       kfree(state);
+}
+
+static int aggregate_stats_init(struct datapath *dp,
+                               const void *body, int body_len,
+                               void **state)
+{
+       *state = (void *)body;
+       return 0;
+}
+
+static int aggregate_stats_dump_callback(struct sw_flow *flow, void *private)
+{
+       struct ofp_aggregate_stats_reply *rpy = private;
+       rpy->packet_count += flow->packet_count;
+       rpy->byte_count += flow->byte_count;
+       rpy->flow_count++;
+       return 0;
+}
+
+static int aggregate_stats_dump(struct datapath *dp, void *state,
+                               void *body, int *body_len)
+{
+       struct ofp_aggregate_stats_request *rq = state;
+       struct ofp_aggregate_stats_reply *rpy;
+       struct sw_table_position position;
+       struct sw_flow_key match_key;
+       int table_idx;
+
+       if (*body_len < sizeof *rpy)
+               return -ENOBUFS;
+       rpy = body;
+       *body_len = sizeof *rpy;
+
+       memset(rpy, 0, sizeof *rpy);
+
+       flow_extract_match(&match_key, &rq->match);
+       table_idx = rq->table_id == 0xff ? 0 : rq->table_id;
+       memset(&position, 0, sizeof position);
+       while (table_idx < dp->chain->n_tables
+              && (rq->table_id == 0xff || rq->table_id == table_idx))
+       {
+               struct sw_table *table = dp->chain->tables[table_idx];
+               int error;
+
+               error = table->iterate(table, &match_key, &position,
+                                      aggregate_stats_dump_callback, rpy);
+               if (error)
+                       return error;
+
+               table_idx++;
+               memset(&position, 0, sizeof position);
+       }
+
+       rpy->packet_count = cpu_to_be64(rpy->packet_count);
+       rpy->byte_count = cpu_to_be64(rpy->byte_count);
+       rpy->flow_count = htonl(rpy->flow_count);
+       return 0;
+}
+
+static int table_stats_dump(struct datapath *dp, void *state,
+                           void *body, int *body_len)
+{
+       struct ofp_table_stats *ots;
+       int nbytes = dp->chain->n_tables * sizeof *ots;
+       int i;
+       if (nbytes > *body_len)
+               return -ENOBUFS;
+       *body_len = nbytes;
+       for (i = 0, ots = body; i < dp->chain->n_tables; i++, ots++) {
+               struct sw_table_stats stats;
+               dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
+               strncpy(ots->name, stats.name, sizeof ots->name);
+               ots->table_id = i;
+               memset(ots->pad, 0, sizeof ots->pad);
+               ots->max_entries = htonl(stats.max_flows);
+               ots->active_count = htonl(stats.n_flows);
+               ots->matched_count = cpu_to_be64(0); /* FIXME */
+       }
+       return 0;
+}
+
+struct port_stats_state {
+       int port;
+};
+
+static int port_stats_init(struct datapath *dp, const void *body, int body_len,
+                          void **state)
+{
+       struct port_stats_state *s = kmalloc(sizeof *s, GFP_ATOMIC);
+       if (!s)
+               return -ENOMEM;
+       s->port = 0;
+       *state = s;
+       return 0;
+}
+
+static int port_stats_dump(struct datapath *dp, void *state,
+                          void *body, int *body_len)
+{
+       struct port_stats_state *s = state;
+       struct ofp_port_stats *ops;
+       int n_ports, max_ports;
+       int i;
+
+       max_ports = *body_len / sizeof *ops;
+       if (!max_ports)
+               return -ENOMEM;
+       ops = body;
+
+       n_ports = 0;
+       for (i = s->port; i < OFPP_MAX && n_ports < max_ports; i++) {
+               struct net_bridge_port *p = dp->ports[i];
+               struct net_device_stats *stats;
+               if (!p)
+                       continue;
+               stats = p->dev->get_stats(p->dev);
+               ops->port_no = htons(p->port_no);
+               memset(ops->pad, 0, sizeof ops->pad);
+               ops->rx_count = cpu_to_be64(stats->rx_packets);
+               ops->tx_count = cpu_to_be64(stats->tx_packets);
+               ops->drop_count = cpu_to_be64(stats->rx_dropped
+                                             + stats->tx_dropped);
+               n_ports++;
+               ops++;
        }
-       size = header_size + flow_size * s->n_flows;
-       resize_openflow_skb(skb, &fsr->header, size);
-       return skb->len;
+       s->port = i;
+       *body_len = n_ports * sizeof *ops;
+       return n_ports >= max_ports;
 }
 
+static void port_stats_done(void *state)
+{
+       kfree(state);
+}
+
+struct stats_type {
+       /* Minimum and maximum acceptable number of bytes in body member of
+        * struct ofp_stats_request. */
+       size_t min_body, max_body;
+
+       /* Prepares to dump some kind of statistics on 'dp'.  'body' and
+        * 'body_len' are the 'body' member of the struct ofp_stats_request.
+        * Returns zero if successful, otherwise a negative error code.
+        * May initialize '*state' to state information.  May be null if no
+        * initialization is required.*/
+       int (*init)(struct datapath *dp, const void *body, int body_len,
+                   void **state);
+
+       /* Dumps statistics for 'dp' into the '*body_len' bytes at 'body', and
+        * modifies '*body_len' to reflect the number of bytes actually used.
+        * ('body' will be transmitted as the 'body' member of struct
+        * ofp_stats_reply.) */
+       int (*dump)(struct datapath *dp, void *state,
+                   void *body, int *body_len);
+
+       /* Cleans any state created by the init or dump functions.  May be null
+        * if no cleanup is required. */
+       void (*done)(void *state);
+};
+
+static const struct stats_type stats[] = {
+       [OFPST_FLOW] = {
+               sizeof(struct ofp_flow_stats_request),
+               sizeof(struct ofp_flow_stats_request),
+               flow_stats_init,
+               flow_stats_dump,
+               flow_stats_done
+       },
+       [OFPST_AGGREGATE] = {
+               sizeof(struct ofp_aggregate_stats_request),
+               sizeof(struct ofp_aggregate_stats_request),
+               aggregate_stats_init,
+               aggregate_stats_dump,
+               NULL
+       },
+       [OFPST_TABLE] = {
+               0,
+               0,
+               NULL,
+               table_stats_dump,
+               NULL
+       },
+       [OFPST_PORT] = {
+               0,
+               0,
+               port_stats_init,
+               port_stats_dump,
+               port_stats_done
+       },
+};
+
 static int
 dp_genl_openflow_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
 {
        struct datapath *dp;
        struct sender sender;
-       struct flow_stats_cb_state *state;
+       const struct stats_type *s;
+       struct ofp_stats_reply *osr;
+       int dp_idx;
+       int max_openflow_len, body_len;
+       void *body;
        int err;
 
+       /* Set up the cleanup function for this dump.  Linux 2.6.20 and later
+        * support setting up cleanup functions via the .doneit member of
+        * struct genl_ops.  This kluge supports earlier versions also. */
+       cb->done = dp_genl_openflow_done;
+
+       rcu_read_lock();
        if (!cb->args[0]) {
                struct nlattr *attrs[DP_GENL_A_MAX + 1];
-               struct ofp_flow_stats_request *rq;
+               struct ofp_stats_request *rq;
                struct nlattr *va;
+               size_t len, body_len;
+               int type;
 
                err = nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs, DP_GENL_A_MAX,
                                  dp_genl_openflow_policy);
                if (err < 0)
                        return err;
 
+               err = -EINVAL;
+
                if (!attrs[DP_GENL_A_DP_IDX])
-                       return -EINVAL;
+                       goto out;
+               dp_idx = nla_get_u16(attrs[DP_GENL_A_DP_IDX]);
+               dp = dp_get(dp_idx);
+               if (!dp) {
+                       err = -ENOENT;
+                       goto out;
+               }
 
                va = attrs[DP_GENL_A_OPENFLOW];
-               if (!va || nla_len(va) != sizeof *state->rq)
-                       return -EINVAL;
+               len = nla_len(va);
+               if (!va || len < sizeof *rq)
+                       goto out;
 
                rq = nla_data(va);
+               type = ntohs(rq->type);
                if (rq->header.version != OFP_VERSION
-                   || rq->header.type != OFPT_FLOW_STATS_REQUEST
-                   || ntohs(rq->header.length) != sizeof *rq)
-                       return -EINVAL;
-
-               state = kmalloc(sizeof *state, GFP_KERNEL);
-               if (!state)
-                       return -ENOMEM;
-               state->dp_idx = nla_get_u32(attrs[DP_GENL_A_DP_IDX]);
-               state->table_idx = rq->table_id == 0xff ? 0 : rq->table_id;
-               memset(&state->position, 0, sizeof state->position);
-               state->rq = rq;
-               state->sent_terminator = 0;
-
-               cb->args[0] = (long) state;
-       } else {
-               state = (struct flow_stats_cb_state *) cb->args[0];
-       }
-
-       if (state->rq->type != OFPFS_INDIV) {
-               return -ENOTSUPP;
-       }
+                   || rq->header.type != OFPT_STATS_REQUEST
+                   || ntohs(rq->header.length) != len
+                   || type >= ARRAY_SIZE(stats)
+                   || !stats[type].dump)
+                       goto out;
+
+               s = &stats[type];
+               body_len = len - offsetof(struct ofp_stats_request, body);
+               if (body_len < s->min_body || body_len > s->max_body)
+                       goto out;
+
+               cb->args[0] = 1;
+               cb->args[1] = dp_idx;
+               cb->args[2] = type;
+               cb->args[3] = rq->header.xid;
+               if (s->init) {
+                       void *state;
+                       err = s->init(dp, rq->body, body_len, &state);
+                       if (err)
+                               goto out;
+                       cb->args[4] = (long) state;
+               }
+       } else if (cb->args[0] == 1) {
+               dp_idx = cb->args[1];
+               s = &stats[cb->args[2]];
 
-       rcu_read_lock();
-       dp = dp_get(state->dp_idx);
-       if (!dp) {
-               err = -ENOENT;
+               dp = dp_get(dp_idx);
+               if (!dp) {
+                       err = -ENOENT;
+                       goto out;
+               }
+       } else {
+               err = 0;
                goto out;
        }
 
-       sender.xid = state->rq->header.xid;
+       sender.xid = cb->args[3];
        sender.pid = NETLINK_CB(cb->skb).pid;
        sender.seq = cb->nlh->nlmsg_seq;
-       err = muster_flow_stats(dp, state, &sender, skb);
+
+       osr = put_openflow_headers(dp, skb, OFPT_STATS_REPLY, &sender,
+                                  &max_openflow_len);
+       if (IS_ERR(osr)) {
+               err = PTR_ERR(osr);
+               goto out;
+       }
+       osr->type = htons(s - stats);
+       osr->flags = 0;
+       resize_openflow_skb(skb, &osr->header, max_openflow_len);
+       body = osr->body;
+       body_len = max_openflow_len - offsetof(struct ofp_stats_reply, body);
+
+       err = s->dump(dp, (void *) cb->args[4], body, &body_len);
+       if (err >= 0) {
+               if (!err)
+                       cb->args[0] = 2;
+               else
+                       osr->flags = ntohs(OFPSF_REPLY_MORE);
+               resize_openflow_skb(skb, &osr->header,
+                                   (offsetof(struct ofp_stats_reply, body)
+                                    + body_len));
+               err = skb->len;
+       }
 
 out:
        rcu_read_unlock();
@@ -1268,9 +1472,11 @@ out:
 static int
 dp_genl_openflow_done(struct netlink_callback *cb)
 {
-       struct flow_stats_cb_state *state;
-       state = (struct flow_stats_cb_state *) cb->args[0];
-       kfree(state);
+       if (cb->args[0]) {
+               const struct stats_type *s = &stats[cb->args[2]];
+               if (s->done)
+                       s->done((void *) cb->args[4]);
+       }
        return 0;
 }
 
@@ -1280,21 +1486,6 @@ static struct genl_ops dp_genl_ops_openflow = {
        .policy = dp_genl_openflow_policy,
        .doit = dp_genl_openflow,
        .dumpit = dp_genl_openflow_dumpit,
-       .done = dp_genl_openflow_done,
-};
-
-static struct nla_policy dp_genl_benchmark_policy[DP_GENL_A_MAX + 1] = {
-       [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
-       [DP_GENL_A_NPACKETS] = { .type = NLA_U32 },
-       [DP_GENL_A_PSIZE] = { .type = NLA_U32 },
-};
-
-static struct genl_ops dp_genl_ops_benchmark_nl = {
-       .cmd = DP_GENL_C_BENCHMARK_NL,
-       .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
-       .policy = dp_genl_benchmark_policy,
-       .doit = dp_genl_benchmark_nl,
-       .dumpit = NULL,
 };
 
 static struct genl_ops *dp_genl_all_ops[] = {
@@ -1308,7 +1499,6 @@ static struct genl_ops *dp_genl_all_ops[] = {
        &dp_genl_ops_query_dp,
        &dp_genl_ops_add_port,
        &dp_genl_ops_del_port,
-       &dp_genl_ops_benchmark_nl,
 };
 
 static int dp_init_netlink(void)