Switch from "stat" to "stats" everywhere.
[sliver-openvswitch.git] / datapath / datapath.c
index b4c1615..8ab3001 100644 (file)
@@ -1,11 +1,12 @@
 /*
  * Distributed under the terms of the GNU GPL version 2.
- * Copyright (c) 2007 The Board of Trustees of The Leland Stanford Junior Univer
-sity
+ * Copyright (c) 2007, 2008 The Board of Trustees of The Leland 
+ * Stanford Junior University
  */
 
 /* Functions for managing the dp interface/device. */
 
+#include <linux/init.h>
 #include <linux/module.h>
 #include <linux/if_arp.h>
 #include <linux/if_bridge.h>
@@ -13,11 +14,12 @@ sity
 #include <linux/in.h>
 #include <net/genetlink.h>
 #include <linux/ip.h>
+#include <linux/delay.h>
 #include <linux/etherdevice.h>
 #include <linux/kernel.h>
+#include <linux/kthread.h>
 #include <linux/mutex.h>
 #include <linux/rtnetlink.h>
-#include <linux/timer.h>
 #include <linux/rcupdate.h>
 #include <linux/version.h>
 #include <linux/ethtool.h>
@@ -38,12 +40,13 @@ sity
 #include "compat.h"
 
 
-/* Number of seconds between runs of the flow expiration code. */
-#define EXPIRE_SECS 1
+/* Number of milliseconds between runs of the maintenance thread. */
+#define MAINT_SLEEP_MSECS 1000
 
 #define BRIDGE_PORT_NO_FLOOD   0x00000001 
 
 #define UINT32_MAX                       4294967295U
+#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
 
 struct net_bridge_port {
        u16     port_no;
@@ -71,7 +74,7 @@ int dp_dev_setup(struct net_device *dev);
 static struct datapath *dps[DP_MAX];
 static DEFINE_MUTEX(dp_mutex);
 
-static void dp_timer_handler(unsigned long arg);
+static int dp_maint_func(void *data);
 static int send_port_status(struct net_bridge_port *p, uint8_t status);
 
 
@@ -93,6 +96,71 @@ void nla_unreserve(struct sk_buff *skb, struct nlattr *nla, int len)
        nla->nla_len -= len;
 }
 
+static void *
+alloc_openflow_skb(struct datapath *dp, size_t openflow_len, uint8_t type,
+                  const struct sender *sender, struct sk_buff **pskb) 
+{
+       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;
+       }
+
+       /* 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();
+       if (nla_put_u32(skb, DP_GENL_A_DP_IDX, dp->dp_idx) < 0)
+               BUG();
+       attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, openflow_len);
+       BUG_ON(!attr);
+       nlmsg_end(skb, (struct nlmsghdr *) skb->data);
+
+       /* Fill in the header. */
+       oh = nla_data(attr);
+       oh->version = OFP_VERSION;
+       oh->type = type;
+       oh->length = htons(openflow_len);
+       oh->xid = sender ? sender->xid : 0;
+
+       return oh;
+}
+
+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);
+       oh->length = htons(new_length);
+       nlmsg_end(skb, (struct nlmsghdr *) skb->data);
+}
+
+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;
+}
+
 /* Generates a unique datapath id.  It incorporates the datapath index
  * and a hardware address, if available.  If not, it generates a random
  * one.
@@ -172,10 +240,12 @@ static int new_dp(int dp_idx)
                printk("datapath: problem setting up 'of' device\n");
 #endif
 
+       dp->flags = 0;
        dp->miss_send_len = OFP_DEFAULT_MISS_SEND_LEN;
 
-       setup_timer(&dp->timer, dp_timer_handler, (unsigned long) dp);
-       mod_timer(&dp->timer, round_jiffies(jiffies + (EXPIRE_SECS * HZ)));
+       dp->dp_task = kthread_run(dp_maint_func, dp, "dp%d", dp_idx);
+       if (IS_ERR(dp->dp_task))
+               goto err_free_dp;
 
        rcu_assign_pointer(dps[dp_idx], dp);
        mutex_unlock(&dp_mutex);
@@ -288,10 +358,11 @@ static void del_dp(struct datapath *dp)
        rtnl_unlock();
 #endif
 
+       kthread_stop(dp->dp_task);
+
        /* Drop references to DP. */
        list_for_each_entry_safe (p, n, &dp->port_list, node)
                del_switch_port(p);
-       del_timer_sync(&dp->timer);
        rcu_assign_pointer(dps[dp->dp_idx], NULL);
 
        /* Wait until no longer in use, then destroy it. */
@@ -301,18 +372,23 @@ static void del_dp(struct datapath *dp)
        module_put(THIS_MODULE);
 }
 
-static void dp_timer_handler(unsigned long arg)
+static int dp_maint_func(void *data)
 {
-       struct datapath *dp = (struct datapath *) arg;
+       struct datapath *dp = (struct datapath *) data;
+
+       while (!kthread_should_stop()) {
 #if 1
-       chain_timeout(dp->chain);
+               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);
+               int count = chain_timeout(dp->chain);
+               chain_print_stats(dp->chain);
+               if (count)
+                       printk("%d flows timed out\n", count);
 #endif
-       mod_timer(&dp->timer, round_jiffies(jiffies + (EXPIRE_SECS * HZ)));
+               msleep_interruptible(MAINT_SLEEP_MSECS);
+       }
+               
+       return 0;
 }
 
 /*
@@ -456,7 +532,19 @@ int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
        else if (out_port == OFPP_CONTROLLER)
                return dp_output_control(dp, skb, fwd_save_skb(skb), 0,
                                                  OFPR_ACTION);
-       else if (out_port >= OFPP_MAX)
+       else if (out_port == OFPP_TABLE) {
+               struct sw_flow_key key;
+               struct sw_flow *flow;
+
+               flow_extract(skb, skb->dev->br_port->port_no, &key);
+               flow = chain_lookup(dp->chain, &key);
+               if (likely(flow != NULL)) {
+                       flow_used(flow, skb);
+                       execute_actions(dp, skb, &key, flow->actions, flow->n_actions);
+                       return 0;
+               }
+               return -ESRCH;
+       } else if (out_port >= OFPP_MAX)
                goto bad_port;
 
        p = dp->ports[out_port];
@@ -492,73 +580,29 @@ int
 dp_output_control(struct datapath *dp, struct sk_buff *skb,
                           uint32_t buffer_id, size_t max_len, int reason)
 {
-       /* FIXME? packet_rcv_spkt in net/packet/af_packet.c does some stuff
-          that we should possibly be doing here too. */
        /* FIXME?  Can we avoid creating a new skbuff in the case where we
         * forward the whole packet? */
        struct sk_buff *f_skb;
-       struct nlattr *attr;
        struct ofp_packet_in *opi;
-       size_t opi_len;
-       size_t len, fwd_len;
-       void *data;
-       int err = -ENOMEM;
+       size_t fwd_len, opi_len;
+       int err;
 
        fwd_len = skb->len;
        if ((buffer_id != (uint32_t) -1) && max_len)
                fwd_len = min(fwd_len, max_len);
 
-       len = nla_total_size(offsetof(struct ofp_packet_in, data) + fwd_len) 
-                               + nla_total_size(sizeof(uint32_t));
-
-       f_skb = genlmsg_new(len, GFP_ATOMIC); 
-       if (!f_skb)
-               goto error_free_skb;
-
-       data = genlmsg_put(f_skb, 0, 0, &dp_genl_family, 0,
-                               DP_GENL_C_OPENFLOW);
-       if (data == NULL)
-               goto error_free_f_skb;
-
-       NLA_PUT_U32(f_skb, DP_GENL_A_DP_IDX, dp->dp_idx);
-
        opi_len = offsetof(struct ofp_packet_in, data) + fwd_len;
-       attr = nla_reserve(f_skb, DP_GENL_A_OPENFLOW, opi_len);
-       if (!attr)
-               goto error_free_f_skb;
-       opi = nla_data(attr);
-       opi->header.version = OFP_VERSION;
-       opi->header.type    = OFPT_PACKET_IN;
-       opi->header.length  = htons(opi_len);
-       opi->header.xid     = htonl(0);
-
+       opi = alloc_openflow_skb(dp, opi_len, OFPT_PACKET_IN, NULL, &f_skb);
        opi->buffer_id      = htonl(buffer_id);
        opi->total_len      = htons(skb->len);
        opi->in_port        = htons(skb->dev->br_port->port_no);
        opi->reason         = reason;
        opi->pad            = 0;
-       SKB_LINEAR_ASSERT(skb);
        memcpy(opi->data, skb_mac_header(skb), fwd_len);
+       err = send_openflow_skb(f_skb, NULL);
 
-       err = genlmsg_end(f_skb, data);
-       if (err < 0)
-               goto error_free_f_skb;
-
-       err = genlmsg_multicast(f_skb, 0, mc_group.id, GFP_ATOMIC);
-       if (err && net_ratelimit())
-               printk(KERN_WARNING "dp_output_control: genlmsg_multicast failed: %d\n", err);
-
-       kfree_skb(skb);  
-
-       return err;
-
-nla_put_failure:
-error_free_f_skb:
-       nlmsg_free(f_skb);
-error_free_skb:
        kfree_skb(skb);
-       if (net_ratelimit())
-               printk(KERN_ERR "dp_output_control: failed to send: %d\n", err);
+
        return err;
 }
 
@@ -601,28 +645,23 @@ static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
 }
 
 static int 
-fill_data_hello(struct datapath *dp, struct ofp_data_hello *odh)
+fill_features_reply(struct datapath *dp, struct ofp_switch_features *ofr)
 {
        struct net_bridge_port *p;
        int port_count = 0;
 
-       odh->header.version = OFP_VERSION;
-       odh->header.type    = OFPT_DATA_HELLO;
-       odh->header.xid     = htonl(0);
-       odh->datapath_id    = cpu_to_be64(dp->id); 
-
-       odh->n_exact        = htonl(2 * TABLE_HASH_MAX_FLOWS);
-       odh->n_mac_only     = htonl(TABLE_MAC_MAX_FLOWS);
-       odh->n_compression  = 0;                                           /* Not supported */
-       odh->n_general      = htonl(TABLE_LINEAR_MAX_FLOWS);
-       odh->buffer_mb      = htonl(UINT32_MAX);
-       odh->n_buffers      = htonl(N_PKT_BUFFERS);
-       odh->capabilities   = htonl(OFP_SUPPORTED_CAPABILITIES);
-       odh->actions        = htonl(OFP_SUPPORTED_ACTIONS);
-       odh->miss_send_len  = htons(dp->miss_send_len); 
+       ofr->datapath_id    = cpu_to_be64(dp->id); 
+
+       ofr->n_exact        = htonl(2 * TABLE_HASH_MAX_FLOWS);
+       ofr->n_compression  = 0;                                           /* Not supported */
+       ofr->n_general      = htonl(TABLE_LINEAR_MAX_FLOWS);
+       ofr->buffer_mb      = htonl(UINT32_MAX);
+       ofr->n_buffers      = htonl(N_PKT_BUFFERS);
+       ofr->capabilities   = htonl(OFP_SUPPORTED_CAPABILITIES);
+       ofr->actions        = htonl(OFP_SUPPORTED_ACTIONS);
 
        list_for_each_entry_rcu (p, &dp->port_list, node) {
-               fill_port_desc(p, &odh->ports[port_count]);
+               fill_port_desc(p, &ofr->ports[port_count]);
                port_count++;
        }
 
@@ -630,78 +669,44 @@ fill_data_hello(struct datapath *dp, struct ofp_data_hello *odh)
 }
 
 int
-dp_send_hello(struct datapath *dp)
+dp_send_features_reply(struct datapath *dp, const struct sender *sender)
 {
        struct sk_buff *skb;
-       struct nlattr *attr;
-       struct ofp_data_hello *odh;
-       size_t odh_max_len, odh_len, port_max_len, len;
-       void *data;
-       int err = -ENOMEM;
+       struct ofp_switch_features *ofr;
+       size_t ofr_len, port_max_len;
        int port_count;
 
-
-       /* Overallocate, since we can't reliably determine the number of
-        * ports a priori. */
+       /* Overallocate. */
        port_max_len = sizeof(struct ofp_phy_port) * OFPP_MAX;
+       ofr = alloc_openflow_skb(dp, sizeof(*ofr) + port_max_len,
+                                OFPT_FEATURES_REPLY, sender, &skb);
+       if (!ofr)
+               return -ENOMEM;
 
-       len = nla_total_size(sizeof(*odh) + port_max_len) 
-                               + nla_total_size(sizeof(uint32_t));
-
-       skb = genlmsg_new(len, GFP_ATOMIC);
-       if (!skb) {
-               if (net_ratelimit())
-                       printk("dp_send_hello: genlmsg_new failed\n");
-               goto error;
-       }
-
-       data = genlmsg_put(skb, 0, 0, &dp_genl_family, 0,
-                          DP_GENL_C_OPENFLOW);
-       if (data == NULL) {
-               if (net_ratelimit())
-                       printk("dp_send_hello: genlmsg_put failed\n");
-               goto error;
-       }
-
-       NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp->dp_idx);
-
-       odh_max_len = sizeof(*odh) + port_max_len;
-       attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, odh_max_len);
-       if (!attr) {
-               if (net_ratelimit())
-                       printk("dp_send_hello: nla_reserve failed\n");
-               goto error;
-       }
-       odh = nla_data(attr);
-       port_count = fill_data_hello(dp, odh);
-
-       /* Only now that we know how many ports we've added can we say
-        * say something about the length. */
-       odh_len = sizeof(*odh) + (sizeof(struct ofp_phy_port) * port_count);
-       odh->header.length = htons(odh_len);
+       /* Fill. */
+       port_count = fill_features_reply(dp, ofr);
 
-       /* Take back the unused part that was reserved */
-       nla_unreserve(skb, attr, (odh_max_len - odh_len));
+       /* Shrink to fit. */
+       ofr_len = sizeof(*ofr) + (sizeof(struct ofp_phy_port) * port_count);
+       resize_openflow_skb(skb, &ofr->header, ofr_len);
+       return send_openflow_skb(skb, sender);
+}
 
-       err = genlmsg_end(skb, data);
-       if (err < 0) {
-               if (net_ratelimit())
-                       printk("dp_send_hello: genlmsg_end failed\n");
-               goto error;
-       }
+int
+dp_send_config_reply(struct datapath *dp, const struct sender *sender)
+{
+       struct sk_buff *skb;
+       struct ofp_switch_config *osc;
 
-       err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
-       if (err && net_ratelimit())
-               printk(KERN_WARNING "dp_send_hello: genlmsg_multicast failed: %d\n", err);
+       osc = alloc_openflow_skb(dp, sizeof *osc, OFPT_GET_CONFIG_REPLY, sender,
+                                &skb);
+       if (!osc)
+               return -ENOMEM;
 
-       return err;
+       osc->flags = htons(dp->flags);
+       osc->miss_send_len = htons(dp->miss_send_len);
 
-nla_put_failure:
-error:
-       kfree_skb(skb);
-       if (net_ratelimit())
-               printk(KERN_ERR "dp_send_hello: failed to send: %d\n", err);
-       return err;
+       return send_openflow_skb(skb, sender);
 }
 
 int
@@ -725,132 +730,181 @@ static int
 send_port_status(struct net_bridge_port *p, uint8_t status)
 {
        struct sk_buff *skb;
-       struct nlattr *attr;
        struct ofp_port_status *ops;
-       void *data;
-       int err = -ENOMEM;
-
-
-       skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
-       if (!skb) {
-               if (net_ratelimit())
-                       printk("send_port_status: genlmsg_new failed\n");
-               goto error;
-       }
-
-       data = genlmsg_put(skb, 0, 0, &dp_genl_family, 0,
-                          DP_GENL_C_OPENFLOW);
-       if (data == NULL) {
-               if (net_ratelimit())
-                       printk("send_port_status: genlmsg_put failed\n");
-               goto error;
-       }
-
-       NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, p->dp->dp_idx);
-
-       attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, sizeof(*ops));
-       if (!attr) {
-               if (net_ratelimit())
-                       printk("send_port_status: nla_reserve failed\n");
-               goto error;
-       }
-
-       ops = nla_data(attr);
-       ops->header.version = OFP_VERSION;
-       ops->header.type    = OFPT_PORT_STATUS;
-       ops->header.length  = htons(sizeof(*ops));
-       ops->header.xid     = htonl(0);
 
-       ops->reason         = status;
+       ops = alloc_openflow_skb(p->dp, sizeof *ops, OFPT_PORT_STATUS, NULL,
+                                &skb);
+       if (!ops)
+               return -ENOMEM;
+       ops->reason = status;
        fill_port_desc(p, &ops->desc);
 
-       err = genlmsg_end(skb, data);
-       if (err < 0) {
-               if (net_ratelimit())
-                       printk("send_port_status: genlmsg_end failed\n");
-               goto error;
-       }
-
-       err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
-       if (err && net_ratelimit())
-               printk(KERN_WARNING "send_port_status: genlmsg_multicast failed: %d\n", err);
-
-       return err;
-
-nla_put_failure:
-error:
-       kfree_skb(skb);
-       if (net_ratelimit())
-               printk(KERN_ERR "send_port_status: failed to send: %d\n", err);
-       return err;
+       return send_openflow_skb(skb, NULL);
 }
 
 int 
 dp_send_flow_expired(struct datapath *dp, struct sw_flow *flow)
 {
        struct sk_buff *skb;
-       struct nlattr *attr;
        struct ofp_flow_expired *ofe;
-       void *data;
        unsigned long duration_j;
-       int err = -ENOMEM;
 
+       ofe = alloc_openflow_skb(dp, sizeof *ofe, OFPT_FLOW_EXPIRED, 0, &skb);
+       if (!ofe)
+               return -ENOMEM;
 
-       skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
-       if (!skb) {
-               if (net_ratelimit())
-                       printk("dp_send_flow_expired: genlmsg_new failed\n");
-               goto error;
-       }
+       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);
+}
 
-       data = genlmsg_put(skb, 0, 0, &dp_genl_family, 0,
-                          DP_GENL_C_OPENFLOW);
-       if (data == NULL) {
-               if (net_ratelimit())
-                       printk("dp_send_flow_expired: genlmsg_put failed\n");
-               goto error;
+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_stats_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_stats_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_STATS_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;
+
+               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);
+}
 
-       NLA_PUT_U32(skb, DP_GENL_A_DP_IDX, dp->dp_idx);
+static int 
+fill_port_stats_reply(struct datapath *dp, struct ofp_port_stats_reply *psr)
+{
+       struct net_bridge_port *p;
+       int port_count = 0;
 
-       attr = nla_reserve(skb, DP_GENL_A_OPENFLOW, sizeof(*ofe));
-       if (!attr) {
-               if (net_ratelimit())
-                       printk("dp_send_flow_expired: nla_reserve failed\n");
-               goto error;
+       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);
        }
 
-       ofe = nla_data(attr);
-       ofe->header.version = OFP_VERSION;
-       ofe->header.type    = OFPT_FLOW_EXPIRED;
-       ofe->header.length  = htons(sizeof(*ofe));
-       ofe->header.xid     = htonl(0);
+       return port_count;
+}
 
-       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);
+int
+dp_send_port_stats(struct datapath *dp, const struct sender *sender)
+{
+       struct sk_buff *skb;
+       struct ofp_port_stats_reply *psr;
+       size_t psr_len, port_max_len;
+       int port_count;
 
-       err = genlmsg_end(skb, data);
-       if (err < 0) {
-               if (net_ratelimit())
-                       printk("dp_send_flow_expired: genlmsg_end failed\n");
-               goto error;
-       }
+       /* 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)
+               return -ENOMEM;
 
-       err = genlmsg_multicast(skb, 0, mc_group.id, GFP_ATOMIC);
-       if (err && net_ratelimit())
-               printk(KERN_WARNING "send_flow_expired: genlmsg_multicast failed: %d\n", err);
+       /* Fill. */
+       port_count = fill_port_stats_reply(dp, psr);
 
-       return err;
+       /* 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);
+}
 
-nla_put_failure:
-error:
-       kfree_skb(skb);
-       if (net_ratelimit())
-               printk(KERN_ERR "send_flow_expired: failed to send: %d\n", err);
-       return err;
+int
+dp_send_table_stats(struct datapath *dp, const struct sender *sender)
+{
+       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)
+               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);
 }
 
 /* Generic Netlink interface.
@@ -980,431 +1034,6 @@ nla_put_failure:
        return err;
 }
 
-/*
- * Fill flow entry for nl flow query.  Called with rcu_lock  
- *
- */
-static
-int
-dp_fill_flow(struct ofp_flow_mod* ofm, struct swt_iterator* iter)
-{
-       ofm->header.version  = OFP_VERSION;
-       ofm->header.type     = OFPT_FLOW_MOD;
-       ofm->header.length   = htons(sizeof(struct ofp_flow_mod) 
-                               + sizeof(ofm->actions[0]));
-       ofm->header.xid      = htonl(0);
-
-       ofm->match.wildcards = htons(iter->flow->key.wildcards);
-       ofm->match.in_port   = iter->flow->key.in_port;
-       ofm->match.dl_vlan   = iter->flow->key.dl_vlan;
-       memcpy(ofm->match.dl_src, iter->flow->key.dl_src, ETH_ALEN);
-       memcpy(ofm->match.dl_dst, iter->flow->key.dl_dst, ETH_ALEN);
-       ofm->match.dl_type   = iter->flow->key.dl_type;
-       ofm->match.nw_src    = iter->flow->key.nw_src;
-       ofm->match.nw_dst    = iter->flow->key.nw_dst;
-       ofm->match.nw_proto  = iter->flow->key.nw_proto;
-       ofm->match.tp_src    = iter->flow->key.tp_src;
-       ofm->match.tp_dst    = iter->flow->key.tp_dst;
-       ofm->group_id        = iter->flow->group_id;
-       ofm->max_idle        = iter->flow->max_idle;
-       /* TODO support multiple actions  */
-       ofm->actions[0]      = iter->flow->actions[0];
-
-       return 0;
-}
-
-static int dp_genl_show(struct sk_buff *skb, struct genl_info *info)
-{
-       struct datapath *dp;
-       int err = -ENOMEM;
-       struct sk_buff *ans_skb = NULL;
-       void *data;
-       struct nlattr *attr;
-       struct ofp_data_hello *odh;
-       size_t odh_max_len, odh_len, port_max_len, len;
-       int port_count;
-
-       if (!info->attrs[DP_GENL_A_DP_IDX])
-               return -EINVAL;
-
-       mutex_lock(&dp_mutex);
-       dp = dp_get(nla_get_u32((info->attrs[DP_GENL_A_DP_IDX])));
-       if (!dp)
-               goto error;
-
-       /* Overallocate, since we can't reliably determine the number of
-        * ports a priori. */
-       port_max_len = sizeof(struct ofp_phy_port) * OFPP_MAX;
-
-       len = nla_total_size(sizeof(*odh) + port_max_len)
-                       + nla_total_size(sizeof(uint32_t));
-
-       ans_skb = nlmsg_new(len, GFP_KERNEL);
-       if (!ans_skb)
-               goto error;
-
-       data = genlmsg_put_reply(ans_skb, info, &dp_genl_family,
-                                0, DP_GENL_C_SHOW_DP);
-       if (data == NULL) 
-               goto error;
-
-       NLA_PUT_U32(ans_skb, DP_GENL_A_DP_IDX, dp->dp_idx);
-
-       odh_max_len = sizeof(*odh) + port_max_len;
-       attr = nla_reserve(ans_skb, DP_GENL_A_DP_INFO, odh_max_len);
-       if (!attr)
-               goto error;
-       odh = nla_data(attr);
-       port_count = fill_data_hello(dp, odh);
-
-       /* Only now that we know how many ports we've added can we say
-        * say something about the length. */
-       odh_len = sizeof(*odh) + (sizeof(struct ofp_phy_port) * port_count);
-       odh->header.length = htons(odh_len);
-
-       /* Take back the unused part that was reserved */
-       nla_unreserve(ans_skb, attr, (odh_max_len - odh_len));
-
-       genlmsg_end(ans_skb, data);
-       err = genlmsg_reply(ans_skb, info);
-       if (!err)
-               ans_skb = NULL;
-
-error:
-nla_put_failure:
-       if (ans_skb)
-               kfree_skb(ans_skb);
-       mutex_unlock(&dp_mutex);
-       return err;
-}
-
-static struct genl_ops dp_genl_ops_show_dp = {
-       .cmd = DP_GENL_C_SHOW_DP,
-       .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
-       .policy = dp_genl_policy,
-       .doit = dp_genl_show,
-       .dumpit = NULL,
-};
-
-/* Convenience function */
-static
-void* 
-dp_init_nl_flow_msg(uint32_t dp_idx, uint16_t table_idx, 
-               struct genl_info *info, struct sk_buff* skb)
-{
-       void* data;
-
-       data = genlmsg_put_reply(skb, info, &dp_genl_family, 0, 
-                               DP_GENL_C_QUERY_FLOW);
-       if (data == NULL)
-               return NULL;
-       NLA_PUT_U32(skb, DP_GENL_A_DP_IDX,   dp_idx);
-       NLA_PUT_U16(skb, DP_GENL_A_TABLEIDX, table_idx);
-
-       return data;
-
-nla_put_failure:
-       return NULL;
-}
-
-/*  Iterate through the specified table and send all flow entries over
- *  netlink to userspace.  Each flow message has the following format:
- *
- *  32bit dpix
- *  16bit tabletype
- *  32bit number of flows
- *  openflow-flow-entries
- *
- *  The full table may require multiple messages.  A message with 0 flows
- *  signifies end-of message.
- */
-
-static 
-int 
-dp_dump_table(struct datapath *dp, uint16_t table_idx, struct genl_info *info, struct ofp_flow_mod* matchme) 
-{ 
-       struct sk_buff  *skb = 0; 
-       struct sw_table *table = 0;
-       struct swt_iterator iter;
-       struct sw_flow_key in_flow; 
-       struct nlattr   *attr;
-       int count = 0, sum_count = 0;
-       void *data; 
-       uint8_t* ofm_ptr = 0;
-       struct nlattr   *num_attr; 
-       int err = -ENOMEM;
-
-       table = dp->chain->tables[table_idx]; 
-       if ( table == NULL ) {
-               dprintk("dp::dp_dump_table error, non-existant table at position %d\n", table_idx);
-               return -EINVAL;
-       }
-
-       if (!table->iterator(table, &iter)) {
-               dprintk("dp::dp_dump_table couldn't initialize empty table iterator\n");
-               return -ENOMEM;
-       }
-
-       while (iter.flow) {
-
-               /* verify that we can fit all NL_FLOWS_PER_MESSAGE in a single
-                * sk_buf */
-               if( (sizeof(dp_genl_family) + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint32_t) + 
-                                       (NL_FLOWS_PER_MESSAGE * sizeof(struct ofp_flow_mod))) > (8192 - 64)){
-                       dprintk("dp::dp_dump_table NL_FLOWS_PER_MESSAGE may cause overrun in skbuf\n");
-                       return -ENOMEM;
-               }
-
-               skb = nlmsg_new(8192 - 64, GFP_ATOMIC);
-               if (skb == NULL) {
-                       return -ENOMEM;
-               }
-
-               data = dp_init_nl_flow_msg(dp->dp_idx, table_idx, info, skb);
-               if (data == NULL){
-                       err= -ENOMEM;   
-                       goto error_free_skb;
-               } 
-
-               /* reserve space to put the number of flows for this message, to
-                * be filled after the loop*/
-               num_attr = nla_reserve(skb, DP_GENL_A_NUMFLOWS, sizeof(uint32_t));
-               if(!num_attr){
-                       err = -ENOMEM;
-                       goto error_free_skb;
-               }
-
-               /* Only load NL_FLOWS_PER_MESSAGE flows at a time */
-               attr = nla_reserve(skb, DP_GENL_A_FLOW, 
-                               (sizeof(struct ofp_flow_mod) + sizeof(struct ofp_action)) * NL_FLOWS_PER_MESSAGE);
-               if (!attr){
-                       err = -ENOMEM;
-                       goto error_free_skb;
-               }
-
-               /* internal loop to fill NL_FLOWS_PER_MESSAGE flows */
-               ofm_ptr = nla_data(attr);
-               flow_extract_match(&in_flow, &matchme->match);
-               while (iter.flow && count < NL_FLOWS_PER_MESSAGE) {
-                       if(flow_matches(&in_flow, &iter.flow->key)){
-                               if((err = dp_fill_flow((struct ofp_flow_mod*)ofm_ptr, &iter))) 
-                                       goto error_free_skb;
-                               count++; 
-                               /* TODO support multiple actions  */
-                               ofm_ptr += sizeof(struct ofp_flow_mod) + sizeof(struct ofp_action);
-                       }
-                       table->iterator_next(&iter);
-               }
-
-               *((uint32_t*)nla_data(num_attr)) = count;
-               genlmsg_end(skb, data); 
-
-               sum_count += count; 
-               count = 0;
-
-               err = genlmsg_unicast(skb, info->snd_pid); 
-               skb = 0;
-       }
-
-       /* send a sentinal message saying we're done */
-       skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
-       if (skb == NULL) {
-               return -ENOMEM;
-       }
-       data = dp_init_nl_flow_msg(dp->dp_idx, table_idx, info, skb);
-       if (data == NULL){
-               err= -ENOMEM;   
-               goto error_free_skb;
-       } 
-
-       NLA_PUT_U32(skb, DP_GENL_A_NUMFLOWS,   0);
-       /* dummy flow so nl doesn't complain */
-       attr = nla_reserve(skb, DP_GENL_A_FLOW, sizeof(struct ofp_flow_mod));
-       if (!attr){
-               err = -ENOMEM;
-               goto error_free_skb;
-       }
-       genlmsg_end(skb, data); 
-       err = genlmsg_reply(skb, info); skb = 0;
-
-nla_put_failure:
-error_free_skb:
-       if(skb)
-               kfree_skb(skb);
-       return err;
-}
-
-/* Helper function to query_table which creates and sends a message packed with
- * table stats.  Message form is:
- *
- * u32 DP_IDX
- * u32 NUM_TABLES
- * OFP_TABLE (list of OFP_TABLES)
- *
- */
-
-static 
-int 
-dp_dump_table_stats(struct datapath *dp, int dp_idx, struct genl_info *info) 
-{ 
-       struct sk_buff   *skb = 0; 
-       struct ofp_table *ot = 0;
-       struct nlattr   *attr;
-       struct sw_table_stats stats; 
-       void *data; 
-       int err = -ENOMEM;
-       int i = 0;
-       int nt = dp->chain->n_tables;
-
-       /* u32 IDX, u32 NUMTABLES, list-of-tables */
-       skb = nlmsg_new(4 + 4 + (sizeof(struct ofp_table) * nt), GFP_ATOMIC);
-       if (skb == NULL) {
-               return -ENOMEM;
-       }
-       
-       data = genlmsg_put_reply(skb, info, &dp_genl_family, 0, 
-                               DP_GENL_C_QUERY_TABLE);
-       if (data == NULL){
-               return -ENOMEM;
-       } 
-
-       NLA_PUT_U32(skb, DP_GENL_A_DP_IDX,      dp_idx);
-       NLA_PUT_U32(skb, DP_GENL_A_NUMTABLES, nt);
-
-       /* ... we assume that all tables can fit in a single message.
-        * Probably a reasonable assumption seeing that we only have
-        * 3 atm */
-       attr = nla_reserve(skb, DP_GENL_A_TABLE, (sizeof(struct ofp_table) * nt));
-       if (!attr){
-               err = -ENOMEM;
-               goto error_free_skb;
-       }
-
-       ot = nla_data(attr);
-
-       for (i = 0; i < nt; ++i) {
-               dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
-               ot->header.version = OFP_VERSION;
-               ot->header.type    = OFPT_TABLE;
-               ot->header.length  = htons(sizeof(struct ofp_table));
-               ot->header.xid     = htonl(0);
-
-               strncpy(ot->name, stats.name, OFP_MAX_TABLE_NAME_LEN); 
-               ot->table_id  = htons(i);
-               ot->n_flows   = htonl(stats.n_flows);
-               ot->max_flows = htonl(stats.max_flows);
-               ot++;
-       }
-
-
-       genlmsg_end(skb, data); 
-       err = genlmsg_reply(skb, info); skb = 0;
-
-nla_put_failure:
-error_free_skb:
-       if(skb)
-               kfree_skb(skb);
-       return err;
-}
-
-/* 
- * Queries a datapath for flow-table statistics 
- */
-
-
-static int dp_genl_table_query(struct sk_buff *skb, struct genl_info *info)
-{
-       struct   datapath* dp;
-       int       err = 0;
-
-       if (!info->attrs[DP_GENL_A_DP_IDX]) {
-               dprintk("dp::dp_genl_table_query received message with missing attributes\n");
-               return -EINVAL;
-       }
-
-       rcu_read_lock();
-       dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
-       if (!dp) {
-               err = -ENOENT;
-               goto err_out;
-       }
-
-       err = dp_dump_table_stats(dp, nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]), info); 
-
-err_out:
-       rcu_read_unlock();
-       return err;
-}
-
-/* 
- * Queries a datapath for flow-table entries.
- */
-
-static int dp_genl_flow_query(struct sk_buff *skb, struct genl_info *info)
-{
-       struct datapath* dp;
-       struct ofp_flow_mod*  ofm;
-       u16     table_idx;
-       int     err = 0;
-
-       if (!info->attrs[DP_GENL_A_DP_IDX]
-                               || !info->attrs[DP_GENL_A_TABLEIDX]
-                               || !info->attrs[DP_GENL_A_FLOW]) {
-               dprintk("dp::dp_genl_flow_query received message with missing attributes\n");
-               return -EINVAL;
-       }
-
-       rcu_read_lock();
-       dp = dp_get(nla_get_u32(info->attrs[DP_GENL_A_DP_IDX]));
-       if (!dp) {
-               err = -ENOENT;
-               goto err_out;
-       }
-
-       table_idx = nla_get_u16(info->attrs[DP_GENL_A_TABLEIDX]);
-
-       if (dp->chain->n_tables <= table_idx){
-               printk("table index %d invalid (dp has %d tables)\n",
-                               table_idx, dp->chain->n_tables);
-       err = -EINVAL;
-               goto err_out;
-       }
-
-       ofm = nla_data(info->attrs[DP_GENL_A_FLOW]);
-       err = dp_dump_table(dp, table_idx, info, ofm); 
-
-err_out:
-       rcu_read_unlock();
-       return err;
-}
-
-static struct nla_policy dp_genl_flow_policy[DP_GENL_A_MAX + 1] = {
-       [DP_GENL_A_DP_IDX]      = { .type = NLA_U32 },
-       [DP_GENL_A_TABLEIDX] = { .type = NLA_U16 },
-       [DP_GENL_A_NUMFLOWS]  = { .type = NLA_U32 },
-};
-
-static struct genl_ops dp_genl_ops_query_flow = {
-       .cmd    = DP_GENL_C_QUERY_FLOW,
-       .flags  = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
-       .policy = dp_genl_flow_policy,
-       .doit   = dp_genl_flow_query,
-       .dumpit = NULL,
-};
-
-static struct nla_policy dp_genl_table_policy[DP_GENL_A_MAX + 1] = {
-       [DP_GENL_A_DP_IDX]      = { .type = NLA_U32 },
-};
-
-static struct genl_ops dp_genl_ops_query_table = {
-       .cmd    = DP_GENL_C_QUERY_TABLE,
-       .flags  = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
-       .policy = dp_genl_table_policy,
-       .doit   = dp_genl_table_query,
-       .dumpit = NULL,
-};
-
-
 static struct genl_ops dp_genl_ops_query_dp = {
        .cmd = DP_GENL_C_QUERY_DP,
        .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
@@ -1476,6 +1105,8 @@ static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
 {
        struct nlattr *va = info->attrs[DP_GENL_A_OPENFLOW];
        struct datapath *dp;
+       struct ofp_header *oh;
+       struct sender sender;
        int err;
 
        if (!info->attrs[DP_GENL_A_DP_IDX] || !va)
@@ -1488,9 +1119,16 @@ static int dp_genl_openflow(struct sk_buff *skb, struct genl_info *info)
                goto out;
        }
 
-       va = info->attrs[DP_GENL_A_OPENFLOW];
+       if (nla_len(va) < sizeof(struct ofp_header)) {
+               err = -EINVAL;
+               goto out;
+       }
+       oh = nla_data(va);
 
-       err = fwd_control_input(dp->chain, nla_data(va), nla_len(va));
+       sender.xid = oh->xid;
+       sender.pid = info->snd_pid;
+       sender.seq = info->snd_seq;
+       err = fwd_control_input(dp->chain, &sender, nla_data(va), nla_len(va));
 
 out:
        rcu_read_unlock();
@@ -1529,9 +1167,6 @@ static struct genl_ops *dp_genl_all_ops[] = {
         * front. */
        &dp_genl_ops_openflow,
 
-       &dp_genl_ops_query_flow,
-       &dp_genl_ops_query_table,
-       &dp_genl_ops_show_dp,
        &dp_genl_ops_add_dp,
        &dp_genl_ops_del_dp,
        &dp_genl_ops_query_dp,
@@ -1575,7 +1210,7 @@ static void dp_uninit_netlink(void)
 #define DRV_NAME               "openflow"
 #define DRV_VERSION     VERSION
 #define DRV_DESCRIPTION "OpenFlow switching datapath implementation"
-#define DRV_COPYRIGHT   "Copyright (c) 2007 The Board of Trustees of The Leland Stanford Junior University"
+#define DRV_COPYRIGHT   "Copyright (c) 2007, 2008 The Board of Trustees of The Leland Stanford Junior University"
 
 
 static int __init dp_init(void)