tests: Add bundle action test with buffer realloc.
[cascardo/ovs.git] / datapath / flow_netlink.c
index f95aa14..6ffcc53 100644 (file)
@@ -48,6 +48,7 @@
 #include <net/vxlan.h>
 
 #include "datapath.h"
+#include "conntrack.h"
 #include "flow.h"
 #include "flow_netlink.h"
 #include "gso.h"
@@ -292,6 +293,10 @@ size_t ovs_key_attr_size(void)
                + nla_total_size(4)   /* OVS_KEY_ATTR_SKB_MARK */
                + nla_total_size(4)   /* OVS_KEY_ATTR_DP_HASH */
                + nla_total_size(4)   /* OVS_KEY_ATTR_RECIRC_ID */
+               + nla_total_size(4)   /* OVS_KEY_ATTR_CT_STATE */
+               + nla_total_size(2)   /* OVS_KEY_ATTR_CT_ZONE */
+               + nla_total_size(4)   /* OVS_KEY_ATTR_CT_MARK */
+               + nla_total_size(16)  /* OVS_KEY_ATTR_CT_LABELS */
                + nla_total_size(12)  /* OVS_KEY_ATTR_ETHERNET */
                + nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
                + nla_total_size(4)   /* OVS_KEY_ATTR_VLAN */
@@ -346,6 +351,10 @@ static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
        [OVS_KEY_ATTR_TUNNEL]    = { .len = OVS_ATTR_NESTED,
                                     .next = ovs_tunnel_key_lens, },
        [OVS_KEY_ATTR_MPLS]      = { .len = sizeof(struct ovs_key_mpls) },
+       [OVS_KEY_ATTR_CT_STATE]  = { .len = sizeof(u32) },
+       [OVS_KEY_ATTR_CT_ZONE]   = { .len = sizeof(u16) },
+       [OVS_KEY_ATTR_CT_MARK]   = { .len = sizeof(u32) },
+       [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
 };
 
 static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
@@ -751,9 +760,9 @@ int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
                                    egress_tun_info->options_len);
 }
 
-static int metadata_from_nlattrs(struct sw_flow_match *match,  u64 *attrs,
-                                const struct nlattr **a, bool is_mask,
-                                bool log)
+static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
+                                 u64 *attrs, const struct nlattr **a,
+                                bool is_mask, bool log)
 {
        if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) {
                u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
@@ -804,16 +813,53 @@ static int metadata_from_nlattrs(struct sw_flow_match *match,  u64 *attrs,
                        return -EINVAL;
                *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
        }
+
+       if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
+           ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
+               u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
+
+               if (ct_state & ~CT_SUPPORTED_MASK) {
+                       OVS_NLERR(log, "ct_state flags %08x unsupported",
+                                 ct_state);
+                       return -EINVAL;
+               }
+
+               SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
+       }
+       if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
+           ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
+               u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
+
+               SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
+       }
+       if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
+           ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
+               u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
+
+               SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
+       }
+       if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
+           ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
+               const struct ovs_key_ct_labels *cl;
+
+               cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
+               SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
+                                  sizeof(*cl), is_mask);
+               *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
+       }
        return 0;
 }
 
-static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
-                               const struct nlattr **a, bool is_mask,
-                               bool log)
+static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
+                               u64 attrs, const struct nlattr **a,
+                               bool is_mask, bool log)
 {
        int err;
 
-       err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
+       err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
        if (err)
                return err;
 
@@ -1056,6 +1102,9 @@ static void nlattr_set(struct nlattr *attr, u8 val,
                } else {
                        memset(nla_data(nla), val, nla_len(nla));
                }
+
+               if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
+                       *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
        }
 }
 
@@ -1069,6 +1118,7 @@ static void mask_set_nlattr(struct nlattr *attr, u8 val)
  * mask. In case the 'mask' is NULL, the flow is treated as exact match
  * flow. Otherwise, it is treated as a wildcarded flow, except the mask
  * does not include any don't care bit.
+ * @net: Used to determine per-namespace field support.
  * @match: receives the extracted flow match information.
  * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  * sequence. The fields should of the packet that triggered the creation
@@ -1079,7 +1129,7 @@ static void mask_set_nlattr(struct nlattr *attr, u8 val)
  * probing for feature compatibility this should be passed in as false to
  * suppress unnecessary error logging.
  */
-int ovs_nla_get_match(struct sw_flow_match *match,
+int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
                      const struct nlattr *nla_key,
                      const struct nlattr *nla_mask,
                      bool log)
@@ -1129,7 +1179,7 @@ int ovs_nla_get_match(struct sw_flow_match *match,
                }
        }
 
-       err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
+       err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
        if (err)
                return err;
 
@@ -1209,7 +1259,8 @@ int ovs_nla_get_match(struct sw_flow_match *match,
                        }
                }
 
-               err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
+               err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
+                                          log);
                if (err)
                        goto free_newmask;
        }
@@ -1290,7 +1341,7 @@ u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
  * extracted from the packet itself.
  */
 
-int ovs_nla_get_flow_metadata(const struct nlattr *attr,
+int ovs_nla_get_flow_metadata(struct net *net, const struct nlattr *attr,
                              struct sw_flow_key *key,
                              bool log)
 {
@@ -1307,9 +1358,10 @@ int ovs_nla_get_flow_metadata(const struct nlattr *attr,
        match.key = key;
 
        memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
+       memset(&key->ct, 0, sizeof(key->ct));
        key->phy.in_port = DP_MAX_PORTS;
 
-       return metadata_from_nlattrs(&match, &attrs, a, false, log);
+       return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
 }
 
 static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
@@ -1355,6 +1407,9 @@ static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
        if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
                goto nla_put_failure;
 
+       if (ovs_ct_put_key(output, skb))
+               goto nla_put_failure;
+
        nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
        if (!nla)
                goto nla_put_failure;
@@ -1615,6 +1670,9 @@ void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
                case OVS_ACTION_ATTR_SET:
                        ovs_nla_free_set_action(a);
                        break;
+               case OVS_ACTION_ATTR_CT:
+                       ovs_ct_free_action(a);
+                       break;
                }
        }
 
@@ -1661,6 +1719,7 @@ static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
 
        memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
        acts->actions_len = (*sfa)->actions_len;
+       acts->orig_len = (*sfa)->orig_len;
        kfree(*sfa);
        *sfa = acts;
 
@@ -1688,8 +1747,8 @@ static struct nlattr *__add_action(struct sw_flow_actions **sfa,
        return a;
 }
 
-static int add_action(struct sw_flow_actions **sfa, int attrtype,
-                     void *data, int len, bool log)
+int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
+                      int len, bool log)
 {
        struct nlattr *a;
 
@@ -1706,7 +1765,7 @@ static inline int add_nested_action_start(struct sw_flow_actions **sfa,
        int used = (*sfa)->actions_len;
        int err;
 
-       err = add_action(sfa, attrtype, NULL, 0, log);
+       err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
        if (err)
                return err;
 
@@ -1722,12 +1781,12 @@ static inline void add_nested_action_end(struct sw_flow_actions *sfa,
        a->nla_len = sfa->actions_len - st_offset;
 }
 
-static int __ovs_nla_copy_actions(const struct nlattr *attr,
+static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                                  const struct sw_flow_key *key,
                                  int depth, struct sw_flow_actions **sfa,
                                  __be16 eth_type, __be16 vlan_tci, bool log);
 
-static int validate_and_copy_sample(const struct nlattr *attr,
+static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
                                    const struct sw_flow_key *key, int depth,
                                    struct sw_flow_actions **sfa,
                                    __be16 eth_type, __be16 vlan_tci, bool log)
@@ -1759,15 +1818,15 @@ static int validate_and_copy_sample(const struct nlattr *attr,
        start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
        if (start < 0)
                return start;
-       err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
-                        nla_data(probability), sizeof(u32), log);
+       err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
+                                nla_data(probability), sizeof(u32), log);
        if (err)
                return err;
        st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
        if (st_acts < 0)
                return st_acts;
 
-       err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
+       err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
                                     eth_type, vlan_tci, log);
        if (err)
                return err;
@@ -1929,6 +1988,8 @@ static int validate_set(const struct nlattr *a,
 
        case OVS_KEY_ATTR_PRIORITY:
        case OVS_KEY_ATTR_SKB_MARK:
+       case OVS_KEY_ATTR_CT_MARK:
+       case OVS_KEY_ATTR_CT_LABELS:
        case OVS_KEY_ATTR_ETHERNET:
                break;
 
@@ -2094,7 +2155,7 @@ static int copy_action(const struct nlattr *from,
        return 0;
 }
 
-static int __ovs_nla_copy_actions(const struct nlattr *attr,
+static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                                  const struct sw_flow_key *key,
                                  int depth, struct sw_flow_actions **sfa,
                                  __be16 eth_type, __be16 vlan_tci, bool log)
@@ -2118,7 +2179,8 @@ static int __ovs_nla_copy_actions(const struct nlattr *attr,
                        [OVS_ACTION_ATTR_SET] = (u32)-1,
                        [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
                        [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
-                       [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash)
+                       [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
+                       [OVS_ACTION_ATTR_CT] = (u32)-1,
                };
                const struct ovs_action_push_vlan *vlan;
                int type = nla_type(a);
@@ -2225,13 +2287,20 @@ static int __ovs_nla_copy_actions(const struct nlattr *attr,
                        break;
 
                case OVS_ACTION_ATTR_SAMPLE:
-                       err = validate_and_copy_sample(a, key, depth, sfa,
+                       err = validate_and_copy_sample(net, a, key, depth, sfa,
                                                       eth_type, vlan_tci, log);
                        if (err)
                                return err;
                        skip_copy = true;
                        break;
 
+               case OVS_ACTION_ATTR_CT:
+                       err = ovs_ct_copy_action(net, a, key, sfa, log);
+                       if (err)
+                               return err;
+                       skip_copy = true;
+                       break;
+
                default:
                        OVS_NLERR(log, "Unknown Action type %d", type);
                        return -EINVAL;
@@ -2250,7 +2319,7 @@ static int __ovs_nla_copy_actions(const struct nlattr *attr,
 }
 
 /* 'key' must be the masked key. */
-int ovs_nla_copy_actions(const struct nlattr *attr,
+int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
                         const struct sw_flow_key *key,
                         struct sw_flow_actions **sfa, bool log)
 {
@@ -2260,7 +2329,8 @@ int ovs_nla_copy_actions(const struct nlattr *attr,
        if (IS_ERR(*sfa))
                return PTR_ERR(*sfa);
 
-       err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
+       (*sfa)->orig_len = nla_len(attr);
+       err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
                                     key->eth.tci, log);
        if (err)
                ovs_nla_free_flow_actions(*sfa);
@@ -2342,14 +2412,20 @@ static int masked_set_action_to_set_action_attr(const struct nlattr *a,
                                                struct sk_buff *skb)
 {
        const struct nlattr *ovs_key = nla_data(a);
+       struct nlattr *nla;
        size_t key_len = nla_len(ovs_key) / 2;
 
        /* Revert the conversion we did from a non-masked set action to
         * masked set action.
         */
-       if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a) - key_len, ovs_key))
+       nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
+       if (!nla)
                return -EMSGSIZE;
 
+       if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
+               return -EMSGSIZE;
+
+       nla_nest_end(skb, nla);
        return 0;
 }
 
@@ -2379,6 +2455,13 @@ int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
                        if (err)
                                return err;
                        break;
+
+               case OVS_ACTION_ATTR_CT:
+                       err = ovs_ct_action_to_attr(nla_data(a), skb);
+                       if (err)
+                               return err;
+                       break;
+
                default:
                        if (nla_put(skb, type, nla_len(a), nla_data(a)))
                                return -EMSGSIZE;