datapath: Restore OVS_CB after skb_segment.
[cascardo/ovs.git] / datapath / datapath.c
index fae0ac7..9a3c052 100644 (file)
@@ -48,8 +48,6 @@
 #include <linux/openvswitch.h>
 #include <linux/rculist.h>
 #include <linux/dmi.h>
-#include <linux/genetlink.h>
-#include <net/genetlink.h>
 #include <net/genetlink.h>
 #include <net/net_namespace.h>
 #include <net/netns/generic.h>
@@ -82,11 +80,12 @@ struct genl_multicast_group ovs_dp_vport_multicast_group = {
 
 /* Check if need to build a reply message.
  * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
-static bool ovs_must_notify(struct genl_info *info,
-                           const struct genl_multicast_group *grp)
+static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
+                           unsigned int group)
 {
        return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
-               netlink_has_listeners(genl_info_net(info)->genl_sock, GROUP_ID(grp));
+              genl_has_listeners(family, genl_info_net(info)->genl_sock,
+                                 group);
 }
 
 static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
@@ -136,8 +135,9 @@ int lockdep_ovsl_is_held(void)
 #endif
 
 static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
-                            const struct dp_upcall_info *);
+                            struct sw_flow_key *, const struct dp_upcall_info *);
 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
+                                 struct sw_flow_key *key,
                                  const struct dp_upcall_info *);
 
 /* Must be called with rcu_read_lock. */
@@ -251,10 +251,9 @@ void ovs_dp_detach_port(struct vport *p)
 }
 
 /* Must be called with rcu_read_lock. */
-void ovs_dp_process_packet(struct sk_buff *skb)
+void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
 {
        const struct vport *p = OVS_CB(skb)->input_vport;
-       struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
        struct datapath *dp = p->dp;
        struct sw_flow *flow;
        struct sw_flow_actions *sf_acts;
@@ -265,7 +264,7 @@ void ovs_dp_process_packet(struct sk_buff *skb)
        stats = this_cpu_ptr(dp->stats_percpu);
 
        /* Look up flow. */
-       flow = ovs_flow_tbl_lookup_stats(&dp->table, pkt_key, skb_get_hash(skb),
+       flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
                                         &n_mask_hit);
        if (unlikely(!flow)) {
                struct dp_upcall_info upcall;
@@ -276,7 +275,7 @@ void ovs_dp_process_packet(struct sk_buff *skb)
                upcall.portid = ovs_vport_find_upcall_portid(p, skb);
                upcall.egress_tun_info = NULL;
 
-               error = ovs_dp_upcall(dp, skb, &upcall);
+               error = ovs_dp_upcall(dp, skb, key, &upcall);
                if (unlikely(error))
                        kfree_skb(skb);
                else
@@ -286,37 +285,36 @@ void ovs_dp_process_packet(struct sk_buff *skb)
                goto out;
        }
 
-       ovs_flow_stats_update(flow, pkt_key->tp.flags, skb);
+       ovs_flow_stats_update(flow, key->tp.flags, skb);
 
        sf_acts = rcu_dereference(flow->sf_acts);
-       ovs_execute_actions(dp, skb, sf_acts);
+       ovs_execute_actions(dp, skb, key, sf_acts);
        stats_counter = &stats->n_hit;
 
 out:
        /* Update datapath statistics. */
-       u64_stats_update_begin(&stats->sync);
+       u64_stats_update_begin(&stats->syncp);
        (*stats_counter)++;
        stats->n_mask_hit += n_mask_hit;
-       u64_stats_update_end(&stats->sync);
+       u64_stats_update_end(&stats->syncp);
 }
 
 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
+                 struct sw_flow_key *key,
                  const struct dp_upcall_info *upcall_info)
 {
        struct dp_stats_percpu *stats;
        int err;
 
-       BUG_ON(!OVS_CB(skb)->pkt_key);
-
        if (upcall_info->portid == 0) {
                err = -ENOTCONN;
                goto err;
        }
 
        if (!skb_is_gso(skb))
-               err = queue_userspace_packet(dp, skb, upcall_info);
+               err = queue_userspace_packet(dp, skb, key, upcall_info);
        else
-               err = queue_gso_packets(dp, skb, upcall_info);
+               err = queue_gso_packets(dp, skb, key, upcall_info);
        if (err)
                goto err;
 
@@ -325,22 +323,26 @@ int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
 err:
        stats = this_cpu_ptr(dp->stats_percpu);
 
-       u64_stats_update_begin(&stats->sync);
+       u64_stats_update_begin(&stats->syncp);
        stats->n_lost++;
-       u64_stats_update_end(&stats->sync);
+       u64_stats_update_end(&stats->syncp);
 
        return err;
 }
 
 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
+                            struct sw_flow_key *key,
                             const struct dp_upcall_info *upcall_info)
 {
        unsigned short gso_type = skb_shinfo(skb)->gso_type;
        struct sw_flow_key later_key;
        struct sk_buff *segs, *nskb;
+       struct ovs_skb_cb ovs_cb;
        int err;
 
+       ovs_cb = *OVS_CB(skb);
        segs = __skb_gso_segment(skb, NETIF_F_SG, false);
+       *OVS_CB(skb) = ovs_cb;
        if (IS_ERR(segs))
                return PTR_ERR(segs);
 
@@ -349,17 +351,18 @@ static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
                 * in this case is for a first fragment, so we need to
                 * properly mark later fragments.
                 */
-               later_key = *OVS_CB(skb)->pkt_key;
+               later_key = *key;
                later_key.ip.frag = OVS_FRAG_TYPE_LATER;
        }
 
        /* Queue all of the segments. */
        skb = segs;
        do {
+               *OVS_CB(skb) = ovs_cb;
                if (gso_type & SKB_GSO_UDP && skb != segs)
-                       OVS_CB(skb)->pkt_key = &later_key;
+                       key = &later_key;
 
-               err = queue_userspace_packet(dp, skb, upcall_info);
+               err = queue_userspace_packet(dp, skb, key, upcall_info);
                if (err)
                        break;
 
@@ -396,12 +399,12 @@ static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
 }
 
 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
+                                 struct sw_flow_key *key,
                                  const struct dp_upcall_info *upcall_info)
 {
        struct ovs_header *upcall;
        struct sk_buff *nskb = NULL;
        struct sk_buff *user_skb = NULL; /* to be queued to userspace */
-       struct sw_flow_key *pkt_key = OVS_CB(skb)->pkt_key;
        struct nlattr *nla;
        struct genl_info info = {
 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
@@ -462,7 +465,7 @@ static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
        upcall->dp_ifindex = dp_ifindex;
 
        nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
-       err = ovs_nla_put_flow(dp, pkt_key, pkt_key, user_skb);
+       err = ovs_nla_put_flow(key, key, user_skb);
        BUG_ON(err);
        nla_nest_end(user_skb, nla);
 
@@ -568,8 +571,7 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
                goto err_flow_free;
 
        rcu_assign_pointer(flow->sf_acts, acts);
-       OVS_CB(packet)->pkt_key = &flow->key;
-       OVS_CB(skb)->egress_tun_info = NULL;
+       OVS_CB(packet)->egress_tun_info = NULL;
        packet->priority = flow->key.phy.priority;
        packet->mark = flow->key.phy.skb_mark;
 
@@ -590,7 +592,7 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
        sf_acts = rcu_dereference(flow->sf_acts);
 
        local_bh_disable();
-       err = ovs_execute_actions(dp, packet, sf_acts);
+       err = ovs_execute_actions(dp, packet, &flow->key, sf_acts);
        local_bh_enable();
        rcu_read_unlock();
 
@@ -653,9 +655,9 @@ static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
                percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
 
                do {
-                       start = u64_stats_fetch_begin_irq(&percpu_stats->sync);
+                       start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
                        local_stats = *percpu_stats;
-               } while (u64_stats_fetch_retry_irq(&percpu_stats->sync, start));
+               } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
 
                stats->n_hit += local_stats.n_hit;
                stats->n_missed += local_stats.n_missed;
@@ -676,8 +678,7 @@ static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
 }
 
 /* Called with ovs_mutex or RCU read lock. */
-static int ovs_flow_cmd_fill_match(struct datapath *dp,
-                                  const struct sw_flow *flow,
+static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
                                   struct sk_buff *skb)
 {
        struct nlattr *nla;
@@ -688,7 +689,7 @@ static int ovs_flow_cmd_fill_match(struct datapath *dp,
        if (!nla)
                return -EMSGSIZE;
 
-       err = ovs_nla_put_flow(dp, &flow->unmasked_key,
+       err = ovs_nla_put_flow(&flow->unmasked_key,
                               &flow->unmasked_key, skb);
        if (err)
                return err;
@@ -699,7 +700,7 @@ static int ovs_flow_cmd_fill_match(struct datapath *dp,
        if (!nla)
                return -EMSGSIZE;
 
-       err = ovs_nla_put_flow(dp, &flow->key, &flow->mask->key, skb);
+       err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
        if (err)
                return err;
        nla_nest_end(skb, nla);
@@ -773,8 +774,7 @@ static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
 }
 
 /* Called with ovs_mutex or RCU read lock. */
-static int ovs_flow_cmd_fill_info(struct datapath *dp,
-                                 const struct sw_flow *flow, int dp_ifindex,
+static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
                                  struct sk_buff *skb, u32 portid,
                                  u32 seq, u32 flags, u8 cmd)
 {
@@ -787,7 +787,7 @@ static int ovs_flow_cmd_fill_info(struct datapath *dp,
                return -EMSGSIZE;
        ovs_header->dp_ifindex = dp_ifindex;
 
-       err = ovs_flow_cmd_fill_match(dp, flow, skb);
+       err = ovs_flow_cmd_fill_match(flow, skb);
        if (err)
                goto error;
 
@@ -813,7 +813,8 @@ static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *act
 {
        struct sk_buff *skb;
 
-       if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
+       if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
+                                       GROUP_ID(&ovs_dp_flow_multicast_group)))
                return NULL;
 
        skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
@@ -836,10 +837,10 @@ static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
 
        skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
                                      always);
-       if (!skb || IS_ERR(skb))
+       if (IS_ERR_OR_NULL(skb))
                return skb;
 
-       retval = ovs_flow_cmd_fill_info(dp, flow, dp_ifindex, skb,
+       retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
                                        info->snd_portid, info->snd_seq, 0,
                                        cmd);
        BUG_ON(retval < 0);
@@ -891,7 +892,7 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
                                     &acts);
        if (error) {
                OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
-               goto err_kfree_acts;
+               goto err_kfree_flow;
        }
 
        reply = ovs_flow_cmd_alloc_info(acts, info, false);
@@ -919,7 +920,7 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
                }
 
                if (unlikely(reply)) {
-                       error = ovs_flow_cmd_fill_info(dp, new_flow,
+                       error = ovs_flow_cmd_fill_info(new_flow,
                                                       ovs_header->dp_ifindex,
                                                       reply, info->snd_portid,
                                                       info->snd_seq, 0,
@@ -955,7 +956,7 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
                rcu_assign_pointer(flow->sf_acts, acts);
 
                if (unlikely(reply)) {
-                       error = ovs_flow_cmd_fill_info(dp, flow,
+                       error = ovs_flow_cmd_fill_info(flow,
                                                       ovs_header->dp_ifindex,
                                                       reply, info->snd_portid,
                                                       info->snd_seq, 0,
@@ -1063,7 +1064,7 @@ static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
                rcu_assign_pointer(flow->sf_acts, acts);
 
                if (unlikely(reply)) {
-                       error = ovs_flow_cmd_fill_info(dp, flow,
+                       error = ovs_flow_cmd_fill_info(flow,
                                                       ovs_header->dp_ifindex,
                                                       reply, info->snd_portid,
                                                       info->snd_seq, 0,
@@ -1192,7 +1193,7 @@ static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
        if (likely(reply)) {
                if (likely(!IS_ERR(reply))) {
                        rcu_read_lock(); /* Keep RCU checker happy. */
-                       err = ovs_flow_cmd_fill_info(dp, flow,
+                       err = ovs_flow_cmd_fill_info(flow,
                                                     ovs_header->dp_ifindex,
                                                     reply, info->snd_portid,
                                                     info->snd_seq, 0,
@@ -1238,7 +1239,7 @@ static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
                if (!flow)
                        break;
 
-               if (ovs_flow_cmd_fill_info(dp, flow, ovs_header->dp_ifindex, skb,
+               if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
                                           NETLINK_CB(cb->skb).portid,
                                           cb->nlh->nlmsg_seq, NLM_F_MULTI,
                                           OVS_FLOW_CMD_NEW) < 0)
@@ -1419,18 +1420,12 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
        if (err)
                goto err_free_dp;
 
-       dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
+       dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
        if (!dp->stats_percpu) {
                err = -ENOMEM;
                goto err_destroy_table;
        }
 
-       for_each_possible_cpu(i) {
-               struct dp_stats_percpu *dpath_stats;
-               dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
-               u64_stats_init(&dpath_stats->sync);
-       }
-
        dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
                            GFP_KERNEL);
        if (!dp->ports) {
@@ -1841,10 +1836,6 @@ static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
        if (IS_ERR(vport))
                goto exit_unlock_free;
 
-       err = 0;
-       if (a[OVS_VPORT_ATTR_STATS])
-               ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
-
        err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
                                      info->snd_seq, 0, OVS_VPORT_CMD_NEW);
        BUG_ON(err < 0);
@@ -1888,10 +1879,6 @@ static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
                        goto exit_unlock_free;
        }
 
-       if (a[OVS_VPORT_ATTR_STATS])
-               ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
-
-
        if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
                err = ovs_vport_set_upcall_portids(vport,
                                                   a[OVS_VPORT_ATTR_UPCALL_PID]);