datapath: add recirc action
authorAndy Zhou <azhou@nicira.com>
Tue, 8 Apr 2014 11:13:42 +0000 (11:13 +0000)
committerAndy Zhou <azhou@nicira.com>
Mon, 21 Apr 2014 18:06:55 +0000 (11:06 -0700)
Recirculation implementation for Linux kernel data path.

Signed-off-by: Andy Zhou <azhou@nicira.com>
Acked-by: Jesse Gross <jesse@nicira.com>
datapath/actions.c
datapath/datapath.c
datapath/datapath.h
datapath/flow.h
datapath/flow_netlink.c

index c4b4605..5871d82 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007-2013 Nicira, Inc.
+ * Copyright (c) 2007-2014 Nicira, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public
@@ -521,6 +521,26 @@ static int execute_set_action(struct sk_buff *skb,
        return err;
 }
 
+static int execute_recirc(struct datapath *dp, struct sk_buff *skb,
+                                const struct nlattr *a)
+{
+       struct sw_flow_key recirc_key;
+       const struct vport *p = OVS_CB(skb)->input_vport;
+       uint32_t hash = OVS_CB(skb)->pkt_key->ovs_flow_hash;
+       int err;
+
+       err = ovs_flow_extract(skb, p->port_no, &recirc_key);
+       if (err)
+               return err;
+
+       recirc_key.ovs_flow_hash = hash;
+       recirc_key.recirc_id = nla_get_u32(a);
+
+       ovs_dp_process_packet_with_key(skb, &recirc_key);
+
+       return 0;
+}
+
 /* Execute a list of actions against 'skb'. */
 static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
                        const struct nlattr *attr, int len, bool keep_skb)
@@ -565,6 +585,23 @@ static int do_execute_actions(struct datapath *dp, struct sk_buff *skb,
                        err = pop_vlan(skb);
                        break;
 
+               case OVS_ACTION_ATTR_RECIRC: {
+                       struct sk_buff *recirc_skb;
+                       const bool last_action = (a->nla_len == rem);
+
+                       if (!last_action || keep_skb)
+                               recirc_skb = skb_clone(skb, GFP_ATOMIC);
+                       else
+                               recirc_skb = skb;
+
+                       err = execute_recirc(dp, recirc_skb, a);
+
+                       if (last_action || err)
+                               return err;
+
+                       break;
+               }
+
                case OVS_ACTION_ATTR_SET:
                        err = execute_set_action(skb, nla_data(a));
                        break;
index c20d6af..aa4c109 100644 (file)
@@ -239,33 +239,25 @@ void ovs_dp_detach_port(struct vport *p)
        ovs_vport_del(p);
 }
 
-/* Must be called with rcu_read_lock. */
-void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
+void ovs_dp_process_packet_with_key(struct sk_buff *skb,
+               struct sw_flow_key *pkt_key)
 {
+       const struct vport *p = OVS_CB(skb)->input_vport;
        struct datapath *dp = p->dp;
        struct sw_flow *flow;
        struct dp_stats_percpu *stats;
-       struct sw_flow_key key;
        u64 *stats_counter;
        u32 n_mask_hit;
-       int error;
 
        stats = this_cpu_ptr(dp->stats_percpu);
 
-       /* Extract flow from 'skb' into 'key'. */
-       error = ovs_flow_extract(skb, p->port_no, &key);
-       if (unlikely(error)) {
-               kfree_skb(skb);
-               return;
-       }
-
        /* Look up flow. */
-       flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
+       flow = ovs_flow_tbl_lookup_stats(&dp->table, pkt_key, &n_mask_hit);
        if (unlikely(!flow)) {
                struct dp_upcall_info upcall;
 
                upcall.cmd = OVS_PACKET_CMD_MISS;
-               upcall.key = &key;
+               upcall.key = pkt_key;
                upcall.userdata = NULL;
                upcall.portid = ovs_vport_find_upcall_portid(p, skb);
                ovs_dp_upcall(dp, skb, &upcall);
@@ -274,10 +266,10 @@ void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
                goto out;
        }
 
+       OVS_CB(skb)->pkt_key = pkt_key;
        OVS_CB(skb)->flow = flow;
-       OVS_CB(skb)->pkt_key = &key;
 
-       ovs_flow_stats_update(OVS_CB(skb)->flow, key.tp.flags, skb);
+       ovs_flow_stats_update(OVS_CB(skb)->flow, pkt_key->tp.flags, skb);
        ovs_execute_actions(dp, skb);
        stats_counter = &stats->n_hit;
 
@@ -289,6 +281,24 @@ out:
        u64_stats_update_end(&stats->sync);
 }
 
+/* Must be called with rcu_read_lock. */
+void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
+{
+       int error;
+       struct sw_flow_key key;
+
+       OVS_CB(skb)->input_vport = p;
+
+       /* Extract flow from 'skb' into 'key'. */
+       error = ovs_flow_extract(skb, p->port_no, &key);
+       if (unlikely(error)) {
+               kfree_skb(skb);
+               return;
+       }
+
+       ovs_dp_process_packet_with_key(skb, &key);
+}
+
 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
                  const struct dp_upcall_info *upcall_info)
 {
@@ -515,6 +525,7 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
        struct sw_flow *flow;
        struct datapath *dp;
        struct ethhdr *eth;
+       struct vport *input_vport;
        int len;
        int err;
 
@@ -578,6 +589,15 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
        if (!dp)
                goto err_unlock;
 
+       input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
+       if (!input_vport)
+               input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
+
+       if (!input_vport)
+               goto err_unlock;
+
+       OVS_CB(packet)->input_vport = input_vport;
+
        local_bh_disable();
        err = ovs_execute_actions(dp, packet);
        local_bh_enable();
index 40e0f90..6ddf72e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007-2012 Nicira, Inc.
+ * Copyright (c) 2007-2014 Nicira, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public
@@ -99,12 +99,14 @@ struct datapath {
  * @flow: The flow associated with this packet.  May be %NULL if no flow.
  * @pkt_key: The flow information extracted from the packet.  Must be nonnull.
  * @tun_key: Key for the tunnel that encapsulated this packet. NULL if the
- * packet is not being tunneled.
+ * @input_vport: The original vport packet came in on. This value is cached
+ * when a packet is received by OVS.
  */
 struct ovs_skb_cb {
        struct sw_flow          *flow;
        struct sw_flow_key      *pkt_key;
        struct ovs_key_ipv4_tunnel  *tun_key;
+       struct vport    *input_vport;
 };
 #define OVS_CB(skb) ((struct ovs_skb_cb *)(skb)->cb)
 
@@ -188,6 +190,8 @@ extern struct genl_family dp_vport_genl_family;
 extern struct genl_multicast_group ovs_dp_vport_multicast_group;
 
 void ovs_dp_process_received_packet(struct vport *, struct sk_buff *);
+void ovs_dp_process_packet_with_key(struct sk_buff *,
+                                   struct sw_flow_key *pkt_key);
 void ovs_dp_detach_port(struct vport *);
 int ovs_dp_upcall(struct datapath *, struct sk_buff *,
                  const struct dp_upcall_info *);
index a4cb57e..d05a9f4 100644 (file)
@@ -75,6 +75,7 @@ struct sw_flow_key {
                u16     in_port;        /* Input switch port (or DP_MAX_PORTS). */
        } __packed phy; /* Safe when right after 'tun_key'. */
        u32 ovs_flow_hash;              /* Datapath computed hash value.  */
+       u32 recirc_id;                  /* Recirculation ID.  */
        struct {
                u8     src[ETH_ALEN];   /* Ethernet source address. */
                u8     dst[ETH_ALEN];   /* Ethernet destination address. */
index 7db14de..803a94c 100644 (file)
@@ -253,6 +253,7 @@ static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
        [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
        [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
        [OVS_KEY_ATTR_DP_HASH] = sizeof(u32),
+       [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32),
        [OVS_KEY_ATTR_TUNNEL] = -1,
 };
 
@@ -463,6 +464,13 @@ static int metadata_from_nlattrs(struct sw_flow_match *match,  u64 *attrs,
                *attrs &= ~(1ULL << OVS_KEY_ATTR_DP_HASH);
        }
 
+       if (*attrs & (1ULL << OVS_KEY_ATTR_RECIRC_ID)) {
+               u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
+
+               SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID);
+       }
+
        if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
                SW_FLOW_KEY_PUT(match, phy.priority,
                          nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
@@ -868,6 +876,7 @@ int ovs_nla_get_flow_metadata(struct sw_flow *flow,
        flow->key.phy.priority = 0;
        flow->key.phy.skb_mark = 0;
        flow->key.ovs_flow_hash = 0;
+       flow->key.recirc_id = 0;
        memset(tun_key, 0, sizeof(flow->key.tun_key));
 
        err = parse_flow_nlattrs(attr, a, &attrs);
@@ -894,6 +903,9 @@ int ovs_nla_put_flow(const struct sw_flow_key *swkey,
        if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
                goto nla_put_failure;
 
+       if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
+               goto nla_put_failure;
+
        if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
                goto nla_put_failure;
 
@@ -1430,6 +1442,7 @@ int ovs_nla_copy_actions(const struct nlattr *attr,
                /* Expected argument lengths, (u32)-1 for variable length. */
                static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
                        [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
+                       [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
                        [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
                        [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
                        [OVS_ACTION_ATTR_POP_VLAN] = 0,
@@ -1486,6 +1499,9 @@ int ovs_nla_copy_actions(const struct nlattr *attr,
                                return -EINVAL;
                        break;
 
+               case OVS_ACTION_ATTR_RECIRC:
+                       break;
+
                case OVS_ACTION_ATTR_SET:
                        err = validate_set(a, key, sfa, &skip_copy);
                        if (err)