/* 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>
#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 {
static int send_port_status(struct net_bridge_port *p, uint8_t status);
-/* 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: %d\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)
{
printk("datapath: problem setting up 'of' device\n");
#endif
- dp->config.flags = 0;
- dp->config.miss_send_len = 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))
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];
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);
memcpy(opi->data, skb_mac_header(skb), fwd_len);
err = send_openflow_skb(f_skb, NULL);
+out:
kfree_skb(skb);
-
return err;
}
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);
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);
}
return send_openflow_skb(skb, NULL);
}
+int
+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_error_msg *oem;
+
+
+ oem = alloc_openflow_skb(dp, sizeof(*oem)+len, OFPT_ERROR_MSG,
+ sender, &skb);
+ if (!oem)
+ return -ENOMEM;
+
+ oem->type = htons(type);
+ oem->code = htons(code);
+ memcpy(oem->data, data, len);
+
+ return send_openflow_skb(skb, sender);
+}
+
+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);
+}
+
+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);
+ }
+
+ return port_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;
+
+ /* 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;
+
+ /* Fill. */
+ port_count = fill_port_stats_reply(dp, psr);
+
+ /* 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_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.
*
* See netlink(7) for an introduction to netlink. See
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;
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;
-}
-
-/* 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(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;
- }
-
- /* 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;
- size_t len;
- void *data;
- int err = -ENOMEM;
- int i = 0;
- int nt = dp->chain->n_tables;
-
- len = 4 + 4 + (sizeof(struct ofp_table) * nt);
-
- /* u32 IDX, u32 NUMTABLES, list-of-tables */
- skb = nlmsg_new(MAX(len, NLMSG_GOODSIZE), 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. */
goto out;
}
- va = info->attrs[DP_GENL_A_OPENFLOW];
if (nla_len(va) < sizeof(struct ofp_header)) {
err = -EINVAL;
goto out;
[DP_GENL_A_DP_IDX] = { .type = NLA_U32 },
};
+struct flow_stats_cb_state {
+ int dp_idx;
+ int table_idx;
+ struct sw_table_position position;
+ struct ofp_flow_stats_request *rq;
+ int sent_terminator;
+
+ struct ofp_flow_stats *flows;
+ int n_flows, max_flows;
+};
+
+static int muster_callback(struct sw_flow *flow, void *private)
+{
+ struct flow_stats_cb_state *s = private;
+
+ fill_flow_stats(&s->flows[s->n_flows], flow, s->table_idx);
+ return ++s->n_flows >= s->max_flows;
+}
+
+int
+muster_flow_stats(struct datapath *dp, struct flow_stats_cb_state *s,
+ const struct sender *sender, struct sk_buff *skb)
+{
+ struct ofp_flow_stats_reply *fsr;
+ size_t header_size, flow_size;
+ struct sw_flow_key match_key;
+ int max_openflow_len;
+ size_t size;
+
+ 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;
+
+ 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))
+ 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;
+ }
+ size = header_size + flow_size * s->n_flows;
+ resize_openflow_skb(skb, &fsr->header, size);
+ return skb->len;
+}
+
+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;
+ int err;
+
+ if (!cb->args[0]) {
+ struct nlattr *attrs[DP_GENL_A_MAX + 1];
+ struct ofp_flow_stats_request *rq;
+ struct nlattr *va;
+
+ err = nlmsg_parse(cb->nlh, GENL_HDRLEN, attrs, DP_GENL_A_MAX,
+ dp_genl_openflow_policy);
+ if (err < 0)
+ return err;
+
+ if (!attrs[DP_GENL_A_DP_IDX])
+ return -EINVAL;
+
+ va = attrs[DP_GENL_A_OPENFLOW];
+ if (!va || nla_len(va) != sizeof *state->rq)
+ return -EINVAL;
+
+ rq = nla_data(va);
+ 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;
+ }
+
+ rcu_read_lock();
+ dp = dp_get(state->dp_idx);
+ if (!dp) {
+ err = -ENOENT;
+ goto out;
+ }
+
+ sender.xid = state->rq->header.xid;
+ sender.pid = NETLINK_CB(cb->skb).pid;
+ sender.seq = cb->nlh->nlmsg_seq;
+ err = muster_flow_stats(dp, state, &sender, skb);
+
+out:
+ rcu_read_unlock();
+ return err;
+}
+
+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);
+ 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,
+ .done = dp_genl_openflow_done,
};
static struct nla_policy dp_genl_benchmark_policy[DP_GENL_A_MAX + 1] = {
* front. */
&dp_genl_ops_openflow,
- &dp_genl_ops_query_flow,
- &dp_genl_ops_query_table,
&dp_genl_ops_add_dp,
&dp_genl_ops_del_dp,
&dp_genl_ops_query_dp,