datapath: refactor ovs flow extract API.
[cascardo/ovs.git] / datapath / datapath.c
index 6f4236b..91c65a0 100644 (file)
@@ -140,19 +140,29 @@ static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
                                  const struct dp_upcall_info *);
 
-/* Must be called with rcu_read_lock or ovs_mutex. */
-static struct datapath *get_dp(struct net *net, int dp_ifindex)
+/* Must be called with rcu_read_lock. */
+static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
 {
-       struct datapath *dp = NULL;
-       struct net_device *dev;
+       struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
 
-       rcu_read_lock();
-       dev = dev_get_by_index_rcu(net, dp_ifindex);
        if (dev) {
                struct vport *vport = ovs_internal_dev_get_vport(dev);
                if (vport)
-                       dp = vport->dp;
+                       return vport->dp;
        }
+
+       return NULL;
+}
+
+/* The caller must hold either ovs_mutex or rcu_read_lock to keep the
+ * returned dp pointer valid. */
+static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
+{
+       struct datapath *dp;
+
+       WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
+       rcu_read_lock();
+       dp = get_dp_rcu(net, dp_ifindex);
        rcu_read_unlock();
 
        return dp;
@@ -252,6 +262,7 @@ void ovs_dp_process_packet_with_key(struct sk_buff *skb,
        u32 n_mask_hit;
 
        stats = this_cpu_ptr(dp->stats_percpu);
+       OVS_CB(skb)->pkt_key = pkt_key;
 
        /* Look up flow. */
        flow = ovs_flow_tbl_lookup_stats(&dp->table, pkt_key, skb_get_hash(skb),
@@ -260,7 +271,6 @@ void ovs_dp_process_packet_with_key(struct sk_buff *skb,
                struct dp_upcall_info upcall;
 
                upcall.cmd = OVS_PACKET_CMD_MISS;
-               upcall.key = pkt_key;
                upcall.userdata = NULL;
                upcall.portid = ovs_vport_find_upcall_portid(p, skb);
                ovs_dp_upcall(dp, skb, &upcall);
@@ -269,7 +279,6 @@ void ovs_dp_process_packet_with_key(struct sk_buff *skb,
                goto out;
        }
 
-       OVS_CB(skb)->pkt_key = pkt_key;
        OVS_CB(skb)->flow = flow;
 
        ovs_flow_stats_update(OVS_CB(skb)->flow, pkt_key->tp.flags, skb);
@@ -285,15 +294,13 @@ out:
 }
 
 /* Must be called with rcu_read_lock. */
-void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
+void ovs_dp_process_received_packet(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);
+       error = ovs_flow_key_extract(skb, &key);
        if (unlikely(error)) {
                kfree_skb(skb);
                return;
@@ -308,6 +315,8 @@ int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
        struct dp_stats_percpu *stats;
        int err;
 
+       BUG_ON(!OVS_CB(skb)->pkt_key);
+
        if (upcall_info->portid == 0) {
                err = -ENOTCONN;
                goto err;
@@ -336,7 +345,6 @@ static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
                             const struct dp_upcall_info *upcall_info)
 {
        unsigned short gso_type = skb_shinfo(skb)->gso_type;
-       struct dp_upcall_info later_info;
        struct sw_flow_key later_key;
        struct sk_buff *segs, *nskb;
        int err;
@@ -345,25 +353,25 @@ static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
        if (IS_ERR(segs))
                return PTR_ERR(segs);
 
+       if (gso_type & SKB_GSO_UDP) {
+               /* The initial flow key extracted by ovs_flow_key_extract()
+                * 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.ip.frag = OVS_FRAG_TYPE_LATER;
+       }
+
        /* Queue all of the segments. */
        skb = segs;
        do {
+               if (gso_type & SKB_GSO_UDP && skb != segs)
+                       OVS_CB(skb)->pkt_key = &later_key;
+
                err = queue_userspace_packet(dp, skb, upcall_info);
                if (err)
                        break;
 
-               if (skb == segs && gso_type & SKB_GSO_UDP) {
-                       /* The initial flow key extracted by ovs_flow_extract()
-                        * in this case is for a first fragment, so we need to
-                        * properly mark later fragments.
-                        */
-                       later_key = *upcall_info->key;
-                       later_key.ip.frag = OVS_FRAG_TYPE_LATER;
-
-                       later_info = *upcall_info;
-                       later_info.key = &later_key;
-                       upcall_info = &later_info;
-               }
        } while ((skb = skb->next));
 
        /* Free all of the segments. */
@@ -382,7 +390,7 @@ static size_t key_attr_size(void)
 {
        /* Whenever adding new OVS_KEY_ FIELDS, we should consider
         * updating this function.  */
-       BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 21);
+       BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
 
        return    nla_total_size(4)   /* OVS_KEY_ATTR_PRIORITY */
                + nla_total_size(0)   /* OVS_KEY_ATTR_TUNNEL */
@@ -429,6 +437,7 @@ static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
        struct ovs_header *upcall;
        struct sk_buff *nskb = NULL;
        struct sk_buff *user_skb; /* 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)
@@ -489,8 +498,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, upcall_info->key,
-                              upcall_info->key, user_skb);
+       err = ovs_nla_put_flow(dp, pkt_key, pkt_key, user_skb);
        BUG_ON(err);
        nla_nest_end(user_skb, nla);
 
@@ -573,20 +581,18 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
        if (IS_ERR(flow))
                goto err_kfree_skb;
 
-       err = ovs_flow_extract(packet, -1, &flow->key);
+       err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
+                                            &flow->key);
        if (err)
                goto err_flow_free;
 
-       err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
-       if (err)
-               goto err_flow_free;
        acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
        err = PTR_ERR(acts);
        if (IS_ERR(acts))
                goto err_flow_free;
 
        err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
-                                  &flow->key, 0, &acts);
+                                  &flow->key, &acts);
        rcu_assign_pointer(flow->sf_acts, acts);
        if (err)
                goto err_flow_free;
@@ -597,7 +603,7 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
        packet->mark = flow->key.phy.skb_mark;
 
        rcu_read_lock();
-       dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
+       dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
        err = -ENODEV;
        if (!dp)
                goto err_unlock;
@@ -675,9 +681,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_bh(&percpu_stats->sync);
+                       start = u64_stats_fetch_begin_irq(&percpu_stats->sync);
                        local_stats = *percpu_stats;
-               } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
+               } while (u64_stats_fetch_retry_irq(&percpu_stats->sync, start));
 
                stats->n_hit += local_stats.n_hit;
                stats->n_missed += local_stats.n_missed;
@@ -698,60 +704,68 @@ 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_info(struct datapath *dp,
-                                 const struct sw_flow *flow, int dp_ifindex,
-                                 struct sk_buff *skb, u32 portid,
-                                 u32 seq, u32 flags, u8 cmd)
+static int ovs_flow_cmd_fill_match(struct datapath *dp,
+                                  const struct sw_flow *flow,
+                                  struct sk_buff *skb)
 {
-       const int skb_orig_len = skb->len;
-       struct nlattr *start;
-       struct ovs_flow_stats stats;
-       __be16 tcp_flags;
-       unsigned long used;
-       struct ovs_header *ovs_header;
        struct nlattr *nla;
        int err;
 
-       ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
-       if (!ovs_header)
-               return -EMSGSIZE;
-
-       ovs_header->dp_ifindex = dp_ifindex;
-
        /* Fill flow key. */
        nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
        if (!nla)
-               goto nla_put_failure;
+               return -EMSGSIZE;
 
        err = ovs_nla_put_flow(dp, &flow->unmasked_key,
                               &flow->unmasked_key, skb);
        if (err)
-               goto error;
+               return err;
        nla_nest_end(skb, nla);
 
+       /* Fill flow mask. */
        nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
        if (!nla)
-               goto nla_put_failure;
+               return -EMSGSIZE;
 
        err = ovs_nla_put_flow(dp, &flow->key, &flow->mask->key, skb);
        if (err)
-               goto error;
-
+               return err;
        nla_nest_end(skb, nla);
 
+       return 0;
+}
+
+/* Called with ovs_mutex or RCU read lock. */
+static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
+                                  struct sk_buff *skb)
+{
+       struct ovs_flow_stats stats;
+       __be16 tcp_flags;
+       unsigned long used;
+
        ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
 
        if (used &&
            nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
-               goto nla_put_failure;
+               return -EMSGSIZE;
 
        if (stats.n_packets &&
            nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
-               goto nla_put_failure;
+               return -EMSGSIZE;
 
        if ((u8)ntohs(tcp_flags) &&
             nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
-               goto nla_put_failure;
+               return -EMSGSIZE;
+
+       return 0;
+}
+
+/* Called with ovs_mutex or RCU read lock. */
+static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
+                                    struct sk_buff *skb, int skb_orig_len)
+{
+       struct nlattr *start;
+       int err;
 
        /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
         * this is the first flow to be dumped into 'skb'.  This is unusual for
@@ -775,17 +789,46 @@ static int ovs_flow_cmd_fill_info(struct datapath *dp,
                        nla_nest_end(skb, start);
                else {
                        if (skb_orig_len)
-                               goto error;
+                               return err;
 
                        nla_nest_cancel(skb, start);
                }
-       } else if (skb_orig_len)
-               goto nla_put_failure;
+       } else if (skb_orig_len) {
+               return -EMSGSIZE;
+       }
+
+       return 0;
+}
+
+/* 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,
+                                 struct sk_buff *skb, u32 portid,
+                                 u32 seq, u32 flags, u8 cmd)
+{
+       const int skb_orig_len = skb->len;
+       struct ovs_header *ovs_header;
+       int err;
+
+       ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
+       if (!ovs_header)
+               return -EMSGSIZE;
+       ovs_header->dp_ifindex = dp_ifindex;
+
+       err = ovs_flow_cmd_fill_match(dp, flow, skb);
+       if (err)
+               goto error;
+
+       err = ovs_flow_cmd_fill_stats(flow, skb);
+       if (err)
+               goto error;
+
+       err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
+       if (err)
+               goto error;
 
        return genlmsg_end(skb, ovs_header);
 
-nla_put_failure:
-       err = -EMSGSIZE;
 error:
        genlmsg_cancel(skb, ovs_header);
        return err;
@@ -845,10 +888,14 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
 
        /* Must have key and actions. */
        error = -EINVAL;
-       if (!a[OVS_FLOW_ATTR_KEY])
+       if (!a[OVS_FLOW_ATTR_KEY]) {
+               OVS_NLERR("Flow key attribute not present in new flow.\n");
                goto error;
-       if (!a[OVS_FLOW_ATTR_ACTIONS])
+       }
+       if (!a[OVS_FLOW_ATTR_ACTIONS]) {
+               OVS_NLERR("Flow actions attribute not present in new flow.\n");
                goto error;
+       }
 
        /* Most of the time we need to allocate a new flow, do it before
         * locking. */
@@ -874,7 +921,7 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
                goto err_kfree_flow;
 
        error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
-                                    0, &acts);
+                                    &acts);
        if (error) {
                OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
                goto err_kfree_acts;
@@ -929,8 +976,12 @@ static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
                }
                /* The unmasked key has to be the same for flow updates. */
                if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
-                       error = -EEXIST;
-                       goto err_unlock_ovs;
+                       /* Look for any overlapping flow. */
+                       flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
+                       if (!flow) {
+                               error = -ENOENT;
+                               goto err_unlock_ovs;
+                       }
                }
                /* Update actions. */
                old_acts = ovsl_dereference(flow->sf_acts);
@@ -978,7 +1029,7 @@ static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
                return acts;
 
        ovs_flow_mask_key(&masked_key, key, mask);
-       error = ovs_nla_copy_actions(a, &masked_key, 0, &acts);
+       error = ovs_nla_copy_actions(a, &masked_key, &acts);
        if (error) {
                OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
                kfree(acts);
@@ -1003,8 +1054,10 @@ static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
 
        /* Extract key. */
        error = -EINVAL;
-       if (!a[OVS_FLOW_ATTR_KEY])
+       if (!a[OVS_FLOW_ATTR_KEY]) {
+               OVS_NLERR("Flow key attribute not present in set flow.\n");
                goto error;
+       }
 
        ovs_match_init(&match, &key, &mask);
        error = ovs_nla_get_match(&match,
@@ -1037,16 +1090,12 @@ static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
                goto err_unlock_ovs;
        }
        /* Check that the flow exists. */
-       flow = ovs_flow_tbl_lookup(&dp->table, &key);
+       flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
        if (unlikely(!flow)) {
                error = -ENOENT;
                goto err_unlock_ovs;
        }
-       /* The unmasked key has to be the same for flow updates. */
-       if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
-               error = -EEXIST;
-               goto err_unlock_ovs;
-       }
+
        /* Update actions, if present. */
        if (likely(acts)) {
                old_acts = ovsl_dereference(flow->sf_acts);
@@ -1119,8 +1168,8 @@ static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
                goto unlock;
        }
 
-       flow = ovs_flow_tbl_lookup(&dp->table, &key);
-       if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
+       flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
+       if (!flow) {
                err = -ENOENT;
                goto unlock;
        }
@@ -1167,8 +1216,8 @@ static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
                err = ovs_flow_tbl_flush(&dp->table);
                goto unlock;
        }
-       flow = ovs_flow_tbl_lookup(&dp->table, &key);
-       if (unlikely(!flow || !ovs_flow_cmp_unmasked_key(flow, &match))) {
+       flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
+       if (unlikely(!flow)) {
                err = -ENOENT;
                goto unlock;
        }
@@ -1211,7 +1260,7 @@ static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
        struct datapath *dp;
 
        rcu_read_lock();
-       dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
+       dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
        if (!dp) {
                rcu_read_unlock();
                return -ENODEV;
@@ -1978,7 +2027,7 @@ static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
        int i, j = 0;
 
        rcu_read_lock();
-       dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
+       dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
        if (!dp) {
                rcu_read_unlock();
                return -ENODEV;