Fix problem with identifying SNAP frames when extracting flows.
[sliver-openvswitch.git] / datapath / datapath.c
index 639a483..3853195 100644 (file)
@@ -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 {
@@ -76,79 +77,125 @@ 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 *);
 
-
-/* nla_unreserve - reduce amount of space reserved by nla_reserve  
+/* nla_shrink - reduce amount of space reserved by nla_reserve
  * @skb: socket buffer from which to recover room
  * @nla: netlink attribute to adjust
- * @len: amount by which to reduce attribute payload
+ * @len: new length of attribute payload
  *
  * Reduces amount of space reserved by a call to nla_reserve.
  *
  * No other attributes may be added between calling nla_reserve and this
  * function, since it will create a hole in the message.
  */
-void nla_unreserve(struct sk_buff *skb, struct nlattr *nla, int len)
+void nla_shrink(struct sk_buff *skb, struct nlattr *nla, int len)
 {
-       skb->tail -= len;
-       skb->len  -= len;
-
-       nla->nla_len -= len;
+       int delta = nla_total_size(len) - nla_total_size(nla_len(nla));
+       BUG_ON(delta > 0);
+       skb->tail += delta;
+       skb->len  += delta;
+       nla->nla_len = nla_attr_size(len);
 }
 
+/* Puts a set of openflow headers for a message of the given 'type' into 'skb'.
+ * If 'sender' is nonnull, then it is used as the message's destination.  'dp'
+ * must specify the datapath to use.
+ *
+ * '*max_openflow_len' receives the maximum number of bytes that are available
+ * for the embedded OpenFlow message.  The caller must call
+ * resize_openflow_skb() to set the actual size of the message to this number
+ * of bytes or less.
+ *
+ * Returns the openflow header if successful, otherwise (if 'skb' is too small)
+ * an error code. */
 static void *
-alloc_openflow_skb(struct datapath *dp, size_t openflow_len, uint8_t type,
-                  const struct sender *sender, struct sk_buff **pskb) 
+put_openflow_headers(struct datapath *dp, struct sk_buff *skb, uint8_t type,
+                    const struct sender *sender, int *max_openflow_len)
 {
-       size_t genl_len;
-       struct sk_buff *skb;
-       struct nlattr *attr;
        struct ofp_header *oh;
-
-       genl_len = nla_total_size(sizeof(uint32_t)); /* DP_GENL_A_DP_IDX */
-       genl_len += nla_total_size(openflow_len);    /* DP_GENL_A_OPENFLOW */
-       skb = *pskb = genlmsg_new(genl_len, GFP_ATOMIC);
-       if (!skb) {
-               if (net_ratelimit())
-                       printk("alloc_openflow_skb: genlmsg_new failed\n");
-               return NULL;
-       }
+       struct nlattr *attr;
+       int openflow_len;
 
        /* Assemble the Generic Netlink wrapper. */
        if (!genlmsg_put(skb,
                         sender ? sender->pid : 0,
                         sender ? sender->seq : 0,
                         &dp_genl_family, 0, DP_GENL_C_OPENFLOW))
-               BUG();
+               return ERR_PTR(-ENOBUFS);
        if (nla_put_u32(skb, DP_GENL_A_DP_IDX, dp->dp_idx) < 0)
-               BUG();
+               return ERR_PTR(-ENOBUFS);
+       openflow_len = (skb_tailroom(skb) - NLA_HDRLEN) & ~(NLA_ALIGNTO - 1);
+       if (openflow_len < sizeof *oh)
+               return ERR_PTR(-ENOBUFS);
+       *max_openflow_len = openflow_len;
        attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, openflow_len);
        BUG_ON(!attr);
-       nlmsg_end(skb, (struct nlmsghdr *) skb->data);
 
-       /* Fill in the header. */
+       /* Fill in the header.  The caller is responsible for the length. */
        oh = nla_data(attr);
        oh->version = OFP_VERSION;
        oh->type = type;
-       oh->length = htons(openflow_len);
        oh->xid = sender ? sender->xid : 0;
 
        return oh;
 }
 
+/* Resizes OpenFlow header 'oh', which must be at the tail end of 'skb', to new
+ * length 'new_length' (in bytes), adjusting pointers and size values as
+ * necessary. */
 static void
 resize_openflow_skb(struct sk_buff *skb,
                    struct ofp_header *oh, size_t new_length)
 {
-       struct nlattr *attr;
-
-       BUG_ON(new_length > ntohs(oh->length));
-       attr = ((void *) oh) - NLA_HDRLEN;
-       nla_unreserve(skb, attr, ntohs(oh->length) - new_length);
+       struct nlattr *attr = ((void *) oh) - NLA_HDRLEN;
+       nla_shrink(skb, attr, new_length);
        oh->length = htons(new_length);
        nlmsg_end(skb, (struct nlmsghdr *) skb->data);
 }
 
+/* Allocates a new skb to contain an OpenFlow message 'openflow_len' bytes in
+ * length.  Returns a null pointer if memory is unavailable, otherwise returns
+ * the OpenFlow header and stores a pointer to the skb in '*pskb'. 
+ *
+ * 'type' is the OpenFlow message type.  If 'sender' is nonnull, then it is
+ * used as the message's destination.  'dp' must specify the datapath to
+ * use.  */
+static void *
+alloc_openflow_skb(struct datapath *dp, size_t openflow_len, uint8_t type,
+                  const struct sender *sender, struct sk_buff **pskb) 
+{
+       struct ofp_header *oh;
+       size_t genl_len;
+       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 */
+       skb = *pskb = genlmsg_new(genl_len, GFP_ATOMIC);
+       if (!skb) {
+               if (net_ratelimit())
+                       printk("alloc_openflow_skb: genlmsg_new failed\n");
+               return NULL;
+       }
+
+       oh = put_openflow_headers(dp, skb, type, sender, &max_openflow_len);
+       BUG_ON(!oh || IS_ERR(oh));
+       resize_openflow_skb(skb, oh, openflow_len);
+
+       return oh;
+}
+
+/* Sends 'skb' to 'sender' if it is nonnull, otherwise multicasts 'skb' to all
+ * listeners. */
 static int
 send_openflow_skb(struct sk_buff *skb, const struct sender *sender) 
 {
@@ -240,8 +287,8 @@ static int new_dp(int dp_idx)
                printk("datapath: problem setting up 'of' device\n");
 #endif
 
-       dp->config.flags = 0;
-       dp->config.miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
+       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))
@@ -460,13 +507,17 @@ 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 */
+/* Don't forward packets to originating port.  If we're flooding,
+ * then don't send out ports with flooding disabled.
+ */
 static inline int should_deliver(const struct net_bridge_port *p,
-                       const struct sk_buff *skb)
+                       const struct sk_buff *skb, int flood)
 {
-       if ((skb->dev == p->dev) || (p->flags & BRIDGE_PORT_NO_FLOOD)) {
+       if (skb->dev == p->dev)
+               return 0;
+
+       if (flood && (p->flags & BRIDGE_PORT_NO_FLOOD))
                return 0;
-       } 
 
        return 1;
 }
@@ -479,15 +530,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)
 {
        struct net_bridge_port *p;
        int prev_port;
 
        prev_port = -1;
        list_for_each_entry_rcu (p, &dp->port_list, node) {
-               if (!should_deliver(p, skb))
+               if (!should_deliver(p, skb, flood))
                        continue;
                if (prev_port != -1) {
                        struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
@@ -528,7 +582,9 @@ int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
 
        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);
@@ -593,6 +649,10 @@ dp_output_control(struct datapath *dp, struct sk_buff *skb,
 
        opi_len = offsetof(struct ofp_packet_in, data) + fwd_len;
        opi = alloc_openflow_skb(dp, opi_len, OFPT_PACKET_IN, NULL, &f_skb);
+       if (!opi) {
+               err = -ENOMEM;
+               goto out;
+       }
        opi->buffer_id      = htonl(buffer_id);
        opi->total_len      = htons(skb->len);
        opi->in_port        = htons(skb->dev->br_port->port_no);
@@ -601,8 +661,8 @@ dp_output_control(struct datapath *dp, struct sk_buff *skb,
        memcpy(opi->data, skb_mac_header(skb), fwd_len);
        err = send_openflow_skb(f_skb, NULL);
 
+out:
        kfree_skb(skb);
-
        return err;
 }
 
@@ -653,7 +713,6 @@ fill_features_reply(struct datapath *dp, struct ofp_switch_features *ofr)
        ofr->datapath_id    = cpu_to_be64(dp->id); 
 
        ofr->n_exact        = htonl(2 * TABLE_HASH_MAX_FLOWS);
-       ofr->n_mac_only     = htonl(TABLE_MAC_MAX_FLOWS);
        ofr->n_compression  = 0;                                           /* Not supported */
        ofr->n_general      = htonl(TABLE_LINEAR_MAX_FLOWS);
        ofr->buffer_mb      = htonl(UINT32_MAX);
@@ -699,13 +758,14 @@ dp_send_config_reply(struct datapath *dp, const struct sender *sender)
        struct sk_buff *skb;
        struct ofp_switch_config *osc;
 
-       osc = alloc_openflow_skb(dp, sizeof *osc, OFPT_PORT_STATUS, sender,
+       osc = alloc_openflow_skb(dp, sizeof *osc, OFPT_GET_CONFIG_REPLY, sender,
                                 &skb);
        if (!osc)
                return -ENOMEM;
-       memcpy(((char *)osc) + sizeof osc->header,
-              ((char *)&dp->config) + sizeof dp->config.header,
-              sizeof dp->config - sizeof dp->config.header);
+
+       osc->flags = htons(dp->flags);
+       osc->miss_send_len = htons(dp->miss_send_len);
+
        return send_openflow_skb(skb, sender);
 }
 
@@ -737,6 +797,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);
@@ -754,156 +815,35 @@ 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->table_id        = table_idx;
-       ofs->packet_count    = cpu_to_be64(flow->packet_count);
-       ofs->byte_count      = cpu_to_be64(flow->byte_count);
-}
-
-int
-dp_send_flow_stats(struct datapath *dp, const struct sender *sender,
-                  const struct ofp_match *match)
-{
-       struct sk_buff *skb;
-       struct ofp_flow_stat_reply *fsr;
-       size_t header_size, fudge, flow_size;
-       struct sw_flow_key match_key;
-       int table_idx, n_flows, max_flows;
-
-       header_size = offsetof(struct ofp_flow_stat_reply, flows);
-       fudge = 128;
-       flow_size = sizeof fsr->flows[0];
-       max_flows = (NLMSG_GOODSIZE - header_size - fudge) / flow_size;
-       fsr = alloc_openflow_skb(dp, header_size + max_flows * flow_size,
-                                OFPT_FLOW_STAT_REPLY, sender, &skb);
-       if (!fsr)
-               return -ENOMEM;
 
-       n_flows = 0;
-       flow_extract_match(&match_key, match);
-       for (table_idx = 0; table_idx < dp->chain->n_tables; table_idx++) {
-               struct sw_table *table = dp->chain->tables[table_idx];
-               struct swt_iterator iter;
+       memset(ofe->pad, 0, sizeof ofe->pad);
+       ofe->priority = htons(flow->priority);
 
-               if (n_flows >= max_flows) {
-                       break;
-               }
-
-               if (!table->iterator(table, &iter)) {
-                       if (net_ratelimit())
-                               printk("iterator failed for table %d\n",
-                                      table_idx);
-                       continue;
-               }
-
-               for (; iter.flow; table->iterator_next(&iter)) {
-                       if (flow_matches(&match_key, &iter.flow->key)) {
-                               fill_flow_stats(&fsr->flows[n_flows],
-                                               iter.flow, table_idx);
-                               if (++n_flows >= max_flows) {
-                                       break;
-                               }
-                       }
-               }
-               table->iterator_destroy(&iter);
-       }
-       resize_openflow_skb(skb, &fsr->header,
-                           header_size + flow_size * n_flows);
-       return send_openflow_skb(skb, sender);
-}
-
-static int 
-fill_port_stat_reply(struct datapath *dp, struct ofp_port_stat_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);
 }
 
 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_stat_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_STAT_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_stat_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)
-{
-       struct sk_buff *skb;
-       struct ofp_table_stat_reply *tsr;
-       int i, n_tables;
-
-       n_tables = dp->chain->n_tables;
-       tsr = alloc_openflow_skb(dp, (offsetof(struct ofp_table_stat_reply,
-                                              tables)
-                                     + sizeof tsr->tables[0] * n_tables),
-                                OFPT_TABLE_STAT_REPLY, sender, &skb);
-       if (!tsr)
-               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 */
-       }
        return send_openflow_skb(skb, sender);
 }
 
@@ -1007,7 +947,7 @@ static int dp_genl_query(struct sk_buff *skb, struct genl_info *info)
                err = -ENOENT;
        else {
                void *data;
-               ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
+               ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
                if (!ans_skb) {
                        err = -ENOMEM;
                        goto err;
@@ -1139,12 +1079,432 @@ static struct nla_policy dp_genl_openflow_policy[DP_GENL_A_MAX + 1] = {
        [DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
 };
 
+struct flow_stats_state {
+       int table_idx;
+       struct sw_table_position position;
+       const struct ofp_flow_stats_request *rq;
+
+       void *body;
+       int bytes_used, bytes_allocated;
+};
+
+static int flow_stats_init(struct datapath *dp, const void *body, int body_len,
+                          void **state)
+{
+       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);
+
+       s->bytes_used += length;
+       return 0;
+}
+
+static int flow_stats_dump(struct datapath *dp, void *state,
+                          void *body, int *body_len)
+{
+       struct flow_stats_state *s = state;
+       struct sw_flow_key match_key;
+       int error = 0;
+
+       s->bytes_used = 0;
+       s->bytes_allocated = *body_len;
+       s->body = body;
+
+       flow_extract_match(&match_key, &s->rq->match);
+       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];
+
+               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);
+       }
+       *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++;
+       }
+       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;
+       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_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])
+                       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];
+               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_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]];
+
+               dp = dp_get(dp_idx);
+               if (!dp) {
+                       err = -ENOENT;
+                       goto out;
+               }
+       } else {
+               err = 0;
+               goto out;
+       }
+
+       sender.xid = cb->args[3];
+       sender.pid = NETLINK_CB(cb->skb).pid;
+       sender.seq = cb->nlh->nlmsg_seq;
+
+       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();
+       return err;
+}
+
+static int
+dp_genl_openflow_done(struct netlink_callback *cb)
+{
+       if (cb->args[0]) {
+               const struct stats_type *s = &stats[cb->args[2]];
+               if (s->done)
+                       s->done((void *) cb->args[4]);
+       }
+       return 0;
+}
+
 static struct genl_ops dp_genl_ops_openflow = {
        .cmd = DP_GENL_C_OPENFLOW,
        .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
        .policy = dp_genl_openflow_policy,
        .doit = dp_genl_openflow,
-       .dumpit = NULL,
+       .dumpit = dp_genl_openflow_dumpit,
 };
 
 static struct nla_policy dp_genl_benchmark_policy[DP_GENL_A_MAX + 1] = {