Added lookup count to ofp_table_stats.
[openvswitch] / datapath / datapath.c
index d405fc1525370a9a7e1a4ad4dffca34b57283c93..0ec226d9dcf061c73428a4bbe2df51d7e69a4b8d 100644 (file)
@@ -65,30 +65,10 @@ MODULE_PARM(serial_num, "s");
 /* Number of milliseconds between runs of the maintenance thread. */
 #define MAINT_SLEEP_MSECS 1000
 
-enum br_port_flags {
-       BRPF_NO_FLOOD = 1 << 0,
-};
-
-enum br_port_status {
-       BRPS_PORT_DOWN = 1 << 0,
-       BRPS_LINK_DOWN = 1 << 1,
-};
-
 #define UINT32_MAX                       4294967295U
 #define UINT16_MAX                       65535
 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
 
-struct net_bridge_port {
-       u16     port_no;
-       u32 flags;             /* BRPF_* flags */
-       u32 status;            /* BRPS_* flags */
-       spinlock_t lock;
-       struct work_struct port_task;
-       struct datapath *dp;
-       struct net_device *dev;
-       struct list_head node; /* Element in datapath.ports. */
-};
-
 static struct genl_family dp_genl_family;
 static struct genl_multicast_group mc_group;
 
@@ -485,7 +465,7 @@ do_port_input(struct net_bridge_port *p, struct sk_buff *skb)
 {
        /* Push the Ethernet header back on. */
        skb_push(skb, ETH_HLEN);
-       fwd_port_input(p->dp->chain, skb, p->port_no);
+       fwd_port_input(p->dp->chain, skb, p);
 }
 
 /*
@@ -537,7 +517,7 @@ static inline unsigned packet_length(const struct sk_buff *skb)
 static int
 output_all(struct datapath *dp, struct sk_buff *skb, int flood)
 {
-       u32 disable = flood ? BRPF_NO_FLOOD : 0;
+       u32 disable = flood ? OFPPFL_NO_FLOOD : 0;
        struct net_bridge_port *p;
        int prev_port = -1;
 
@@ -550,12 +530,12 @@ output_all(struct datapath *dp, struct sk_buff *skb, int flood)
                                kfree_skb(skb);
                                return -ENOMEM;
                        }
-                       dp_output_port(dp, clone, prev_port); 
+                       dp_output_port(dp, clone, prev_port, 0); 
                }
                prev_port = p->port_no;
        }
        if (prev_port != -1)
-               dp_output_port(dp, skb, prev_port);
+               dp_output_port(dp, skb, prev_port, 0);
        else
                kfree_skb(skb);
 
@@ -594,7 +574,8 @@ static int xmit_skb(struct sk_buff *skb)
 
 /* Takes ownership of 'skb' and transmits it to 'out_port' on 'dp'.
  */
-int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
+int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port,
+                  int ignore_no_fwd)
 {
        BUG_ON(!skb);
        switch (out_port){
@@ -610,10 +591,8 @@ int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
                return xmit_skb(skb);
                
        case OFPP_TABLE: {
-               struct net_bridge_port *p = skb->dev->br_port;
-               int retval;
-               retval = run_flow_through_tables(dp->chain, skb,
-                                                p ? p->port_no : OFPP_LOCAL);
+               int retval = run_flow_through_tables(dp->chain, skb,
+                                                    skb->dev->br_port);
                if (retval)
                        kfree_skb(skb);
                return retval;
@@ -645,6 +624,10 @@ int dp_output_port(struct datapath *dp, struct sk_buff *skb, int out_port)
                                printk("can't directly forward to input port\n");
                        return -EINVAL;
                }
+               if (p->flags & OFPPFL_NO_FWD && !ignore_no_fwd) {
+                       kfree_skb(skb);
+                       return 0;
+               }
                skb->dev = p->dev; 
                return xmit_skb(skb);
        }
@@ -714,12 +697,7 @@ static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
        desc->speed = 0;
 
        spin_lock_irqsave(&p->lock, flags);
-       if (p->flags & BRPF_NO_FLOOD) 
-               desc->flags |= htonl(OFPPFL_NO_FLOOD);
-       else if (p->status & BRPS_PORT_DOWN)
-               desc->flags |= htonl(OFPPFL_PORT_DOWN);
-       else if (p->status & BRPS_LINK_DOWN)
-               desc->flags |= htonl(OFPPFL_LINK_DOWN);
+       desc->flags = htonl(p->flags | p->status);
        spin_unlock_irqrestore(&p->lock, flags);
 
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,24)
@@ -743,11 +721,11 @@ static void fill_port_desc(struct net_bridge_port *p, struct ofp_phy_port *desc)
                        if (ecmd.supported & SUPPORTED_10000baseT_Full)
                                desc->features |= OFPPF_10GB_FD;
 
-                       desc->features = htonl(desc->features);
                        desc->speed = htonl(ecmd.speed);
                }
        }
 #endif
+       desc->features = htonl(desc->features);
 }
 
 static int 
@@ -756,15 +734,13 @@ fill_features_reply(struct datapath *dp, struct ofp_switch_features *ofr)
        struct net_bridge_port *p;
        int port_count = 0;
 
-       ofr->datapath_id    = cpu_to_be64(dp->id); 
+       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);
+       ofr->n_buffers    = htonl(N_PKT_BUFFERS);
+       ofr->n_tables     = dp->chain->n_tables;
+       ofr->capabilities = htonl(OFP_SUPPORTED_CAPABILITIES);
+       ofr->actions      = htonl(OFP_SUPPORTED_ACTIONS);
+       memset(ofr->pad, 0, sizeof ofr->pad);
 
        list_for_each_entry_rcu (p, &dp->port_list, node) {
                fill_port_desc(p, &ofr->ports[port_count]);
@@ -815,6 +791,29 @@ dp_send_config_reply(struct datapath *dp, const struct sender *sender)
        return send_openflow_skb(skb, sender);
 }
 
+int
+dp_send_hello(struct datapath *dp, const struct sender *sender,
+             const struct ofp_header *request)
+{
+       if (request->version < OFP_VERSION) {
+               char err[64];
+               sprintf(err, "Only version 0x%02x supported", OFP_VERSION);
+               dp_send_error_msg(dp, sender, OFPET_HELLO_FAILED,
+                                 OFPHFC_INCOMPATIBLE, err, strlen(err));
+               return -EINVAL;
+       } else {
+               struct sk_buff *skb;
+               struct ofp_header *reply;
+
+               reply = alloc_openflow_skb(dp, sizeof *reply,
+                                          OFPT_HELLO, sender, &skb);
+               if (!reply)
+                       return -ENOMEM;
+
+               return send_openflow_skb(skb, sender);
+       }
+}
+
 /* Callback function for a workqueue to disable an interface */
 static void
 down_port_cb(struct work_struct *work)
@@ -827,7 +826,7 @@ down_port_cb(struct work_struct *work)
                if (net_ratelimit())
                        printk("problem bringing up port %s\n", p->dev->name);
        rtnl_unlock();
-       p->status |= BRPS_PORT_DOWN;
+       p->status |= OFPPFL_PORT_DOWN;
 }
 
 /* Callback function for a workqueue to enable an interface */
@@ -842,7 +841,7 @@ up_port_cb(struct work_struct *work)
                if (net_ratelimit())
                        printk("problem bringing down port %s\n", p->dev->name);
        rtnl_unlock();
-       p->status &= ~BRPS_PORT_DOWN;
+       p->status &= ~OFPPFL_PORT_DOWN;
 }
 
 int
@@ -854,16 +853,17 @@ dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
        struct net_bridge_port *p = (port_no < OFPP_MAX ? dp->ports[port_no]
                                     : port_no == OFPP_LOCAL ? dp->local_port
                                     : NULL);
+       uint32_t flag_mask;
+
        /* Make sure the port id hasn't changed since this was sent */
        if (!p || memcmp(opp->hw_addr, p->dev->dev_addr, ETH_ALEN))
                return -1;
 
        spin_lock_irqsave(&p->lock, flags);
-       if (opm->mask & htonl(OFPPFL_NO_FLOOD)) {
-               if (opp->flags & htonl(OFPPFL_NO_FLOOD))
-                       p->flags |= BRPF_NO_FLOOD;
-               else 
-                       p->flags &= ~BRPF_NO_FLOOD;
+       flag_mask = ntohl(opm->mask) & PORT_FLAG_BITS;
+       if (flag_mask) {
+               p->flags &= ~flag_mask;
+               p->flags |= ntohl(opp->flags) & flag_mask;
        }
 
        /* Modifying the status of an interface requires taking a lock
@@ -872,11 +872,11 @@ dp_update_port_flags(struct datapath *dp, const struct ofp_port_mod *opm)
         * context. */
        if (opm->mask & htonl(OFPPFL_PORT_DOWN)) {
                if ((opp->flags & htonl(OFPPFL_PORT_DOWN))
-                                       && (p->status & BRPS_PORT_DOWN) == 0) {
+                   && (p->status & OFPPFL_PORT_DOWN) == 0) {
                        PREPARE_WORK(&p->port_task, down_port_cb);
                        schedule_work(&p->port_task);
                } else if ((opp->flags & htonl(OFPPFL_PORT_DOWN)) == 0
-                                       && (p->status & BRPS_PORT_DOWN)) {
+                          && (p->status & OFPPFL_PORT_DOWN)) {
                        PREPARE_WORK(&p->port_task, up_port_cb);
                        schedule_work(&p->port_task);
                }
@@ -902,14 +902,14 @@ update_port_status(struct net_bridge_port *p)
        orig_status = p->status;
 
        if (p->dev->flags & IFF_UP) 
-               p->status &= ~BRPS_PORT_DOWN;
+               p->status &= ~OFPPFL_PORT_DOWN;
        else
-               p->status |= BRPS_PORT_DOWN;
+               p->status |= OFPPFL_PORT_DOWN;
 
        if (netif_carrier_ok(p->dev))
-               p->status &= ~BRPS_LINK_DOWN;
+               p->status &= ~OFPPFL_LINK_DOWN;
        else
-               p->status |= BRPS_LINK_DOWN;
+               p->status |= OFPPFL_LINK_DOWN;
 
        spin_unlock_irqrestore(&p->lock, flags);
        return (orig_status != p->status);
@@ -963,13 +963,13 @@ EXPORT_SYMBOL(dp_send_flow_expired);
 
 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)
+               uint16_t type, uint16_t code, const void *data, size_t len)
 {
        struct sk_buff *skb;
        struct ofp_error_msg *oem;
 
 
-       oem = alloc_openflow_skb(dp, sizeof(*oem)+len, OFPT_ERROR_MSG
+       oem = alloc_openflow_skb(dp, sizeof(*oem)+len, OFPT_ERROR, 
                        sender, &skb);
        if (!oem)
                return -ENOMEM;
@@ -1265,13 +1265,14 @@ static int flow_stats_init(struct datapath *dp, const void *body, int body_len,
 
 static int flow_stats_dump_callback(struct sw_flow *flow, void *private)
 {
+       struct sw_flow_actions *sf_acts = rcu_dereference(flow->sf_acts);
        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;
+       actions_length = sizeof *ofs->actions * sf_acts->n_actions;
+       length = sizeof *ofs + actions_length;
        if (length + s->bytes_used > s->bytes_allocated)
                return 1;
 
@@ -1298,7 +1299,7 @@ static int flow_stats_dump_callback(struct sw_flow *flow, void *private)
        memset(ofs->pad2, 0, sizeof ofs->pad2);
        ofs->packet_count    = cpu_to_be64(flow->packet_count);
        ofs->byte_count      = cpu_to_be64(flow->byte_count);
-       memcpy(ofs->actions, flow->actions, actions_length);
+       memcpy(ofs->actions, sf_acts->actions, actions_length);
 
        s->bytes_used += length;
        return 0;
@@ -1415,9 +1416,11 @@ static int table_stats_dump(struct datapath *dp, void *state,
                dp->chain->tables[i]->stats(dp->chain->tables[i], &stats);
                strncpy(ots->name, stats.name, sizeof ots->name);
                ots->table_id = i;
+               ots->wildcards = htonl(stats.wildcards);
                memset(ots->pad, 0, sizeof ots->pad);
                ots->max_entries = htonl(stats.max_flows);
                ots->active_count = htonl(stats.n_flows);
+               ots->lookup_count = cpu_to_be64(stats.n_lookup);
                ots->matched_count = cpu_to_be64(stats.n_matched);
        }
        return 0;
@@ -1565,6 +1568,8 @@ dp_genl_openflow_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
         * struct genl_ops.  This kluge supports earlier versions also. */
        cb->done = dp_genl_openflow_done;
 
+       sender.pid = NETLINK_CB(cb->skb).pid;
+       sender.seq = cb->nlh->nlmsg_seq;
        if (!cb->args[0]) {
                struct nlattr *attrs[DP_GENL_A_MAX + 1];
                struct ofp_stats_request *rq;
@@ -1590,13 +1595,22 @@ dp_genl_openflow_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
                        return -EINVAL;
 
                rq = nla_data(va);
+               sender.xid = rq->header.xid;
                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)
+               if (rq->header.version != OFP_VERSION) {
+                       dp_send_error_msg(dp, &sender, OFPET_BAD_REQUEST,
+                                         OFPBRC_BAD_VERSION, rq, len);
+                       return -EINVAL;
+               }
+               if (rq->header.type != OFPT_STATS_REQUEST
+                   || ntohs(rq->header.length) != len)
+                       return -EINVAL;
+
+               if (type >= ARRAY_SIZE(stats) || !stats[type].dump) {
+                       dp_send_error_msg(dp, &sender, OFPET_BAD_REQUEST,
+                                         OFPBRC_BAD_STAT, rq, len);
                        return -EINVAL;
+               }
 
                s = &stats[type];
                body_len = len - offsetof(struct ofp_stats_request, body);
@@ -1615,6 +1629,7 @@ dp_genl_openflow_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
                        cb->args[4] = (long) state;
                }
        } else if (cb->args[0] == 1) {
+               sender.xid = cb->args[3];
                dp_idx = cb->args[1];
                s = &stats[cb->args[2]];
 
@@ -1625,10 +1640,6 @@ dp_genl_openflow_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
                return 0;
        }
 
-       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))