datapath: Avoid NULL mask check while building mask
[cascardo/ovs.git] / datapath / flow_netlink.c
index e1eadbb..17aee02 100644 (file)
 
 #include "flow_netlink.h"
 
-static void update_range__(struct sw_flow_match *match,
-                          size_t offset, size_t size, bool is_mask)
+static void update_range(struct sw_flow_match *match,
+                        size_t offset, size_t size, bool is_mask)
 {
-       struct sw_flow_key_range *range = NULL;
+       struct sw_flow_key_range *range;
        size_t start = rounddown(offset, sizeof(long));
        size_t end = roundup(offset + size, sizeof(long));
 
        if (!is_mask)
                range = &match->range;
-       else if (match->mask)
+       else
                range = &match->mask->range;
 
-       if (!range)
-               return;
-
        if (range->start == range->end) {
                range->start = start;
                range->end = end;
@@ -81,43 +78,37 @@ static void update_range__(struct sw_flow_match *match,
 
 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
        do { \
-               update_range__(match, offsetof(struct sw_flow_key, field),  \
-                                    sizeof((match)->key->field), is_mask); \
-               if (is_mask) {                                              \
-                       if ((match)->mask)                                  \
-                               (match)->mask->key.field = value;           \
-               } else {                                                    \
+               update_range(match, offsetof(struct sw_flow_key, field),    \
+                            sizeof((match)->key->field), is_mask);         \
+               if (is_mask)                                                \
+                       (match)->mask->key.field = value;                   \
+               else                                                        \
                        (match)->key->field = value;                        \
-               }                                                           \
        } while (0)
 
-#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
-       do { \
-               update_range__(match, offset, len, is_mask);                \
-               if (is_mask) {                                              \
-                       if ((match)->mask)                                  \
-                               memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
-               } else {                                                    \
-                       memcpy((u8 *)(match)->key + offset, value_p, len);         \
-               }                                                           \
+#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask)            \
+       do {                                                                \
+               update_range(match, offset, len, is_mask);                  \
+               if (is_mask)                                                \
+                       memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
+               else                                                        \
+                       memcpy((u8 *)(match)->key + offset, value_p, len);  \
        } while (0)
 
-#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
+#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask)                      \
        SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
                                  value_p, len, is_mask)
 
-#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
-       do { \
-               update_range__(match, offsetof(struct sw_flow_key, field),  \
-                                    sizeof((match)->key->field), is_mask); \
-               if (is_mask) {                                              \
-                       if ((match)->mask)                                  \
-                               memset((u8 *)&(match)->mask->key.field, value,\
-                                      sizeof((match)->mask->key.field));   \
-               } else {                                                    \
+#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask)             \
+       do {                                                                \
+               update_range(match, offsetof(struct sw_flow_key, field),    \
+                            sizeof((match)->key->field), is_mask);         \
+               if (is_mask)                                                \
+                       memset((u8 *)&(match)->mask->key.field, value,      \
+                              sizeof((match)->mask->key.field));           \
+               else                                                        \
                        memset((u8 *)&(match)->key->field, value,           \
                               sizeof((match)->key->field));                \
-               }                                                           \
        } while (0)
 
 static bool match_validate(const struct sw_flow_match *match,
@@ -421,6 +412,7 @@ static int ipv4_tun_from_nlattr(const struct nlattr *attr,
                        tun_flags |= TUNNEL_OAM;
                        break;
                case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
+                       tun_flags |= TUNNEL_OPTIONS_PRESENT;
                        if (nla_len(a) > sizeof(match->key->tun_opts)) {
                                OVS_NLERR("Geneve option length exceeds "
                                          "maximum size (len %d, max %zu).\n",
@@ -632,8 +624,7 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
 
                SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
                attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
-       } else if (!is_mask)
-               SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
+       }
 
        if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
                __be16 eth_type;
@@ -858,8 +849,8 @@ static void mask_set_nlattr(struct nlattr *attr, u8 val)
  * attribute specifies the mask field of the wildcarded flow.
  */
 int ovs_nla_get_match(struct sw_flow_match *match,
-                     const struct nlattr *key,
-                     const struct nlattr *mask)
+                     const struct nlattr *nla_key,
+                     const struct nlattr *nla_mask)
 {
        const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
        const struct nlattr *encap;
@@ -869,7 +860,7 @@ int ovs_nla_get_match(struct sw_flow_match *match,
        bool encap_valid = false;
        int err;
 
-       err = parse_flow_nlattrs(key, a, &key_attrs);
+       err = parse_flow_nlattrs(nla_key, a, &key_attrs);
        if (err)
                return err;
 
@@ -910,35 +901,44 @@ int ovs_nla_get_match(struct sw_flow_match *match,
        if (err)
                return err;
 
-       if (match->mask && !mask) {
-               /* Create an exact match mask. We need to set to 0xff all the
-                * 'match->mask' fields that have been touched in 'match->key'.
-                * We cannot simply memset 'match->mask', because padding bytes
-                * and fields not specified in 'match->key' should be left to 0.
-                * Instead, we use a stream of netlink attributes, copied from
-                * 'key' and set to 0xff: ovs_key_from_nlattrs() will take care
-                * of filling 'match->mask' appropriately.
-                */
-               newmask = kmemdup(key, nla_total_size(nla_len(key)),
-                                 GFP_KERNEL);
-               if (!newmask)
-                       return -ENOMEM;
+       if (match->mask) {
+
+               if (!nla_mask) {
+                       /* Create an exact match mask. We need to set to 0xff
+                        * all the 'match->mask' fields that have been touched
+                        * in 'match->key'. We cannot simply memset
+                        * 'match->mask', because padding bytes and fields not
+                        * specified in 'match->key' should be left to 0.
+                        * Instead, we use a stream of netlink attributes,
+                        * copied from 'key' and set to 0xff: ovs_key_from_nlattrs()
+                        * will take care of filling 'match->mask'
+                        * appropriately.
+                        */
+                       newmask = kmemdup(nla_key,
+                                         nla_total_size(nla_len(nla_key)),
+                                         GFP_KERNEL);
+                       if (!newmask)
+                               return -ENOMEM;
 
-               mask_set_nlattr(newmask, 0xff);
+                       mask_set_nlattr(newmask, 0xff);
 
-               /* The userspace does not send tunnel attributes that are 0,
-                * but we should not wildcard them nonetheless. */
-               if (match->key->tun_key.ipv4_dst)
-                       SW_FLOW_KEY_MEMSET_FIELD(match, tun_key, 0xff, true);
+                       /* The userspace does not send tunnel attributes that
+                        * are 0, but we should not wildcard them nonetheless.
+                        */
+                       if (match->key->tun_key.ipv4_dst)
+                               SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
+                                                        0xff, true);
 
-               mask = newmask;
-       }
+                       nla_mask = newmask;
+               }
 
-       if (mask) {
-               err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
+               err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs);
                if (err)
                        goto free_newmask;
 
+               /* Always match on tci. */
+               SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
+
                if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
                        __be16 eth_type = 0;
                        __be16 tci = 0;
@@ -991,7 +991,7 @@ free_newmask:
 
 /**
  * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
- * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
+ * @key: Receives extracted in_port, priority, tun_key and skb_mark.
  * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
  * sequence.
  *
@@ -1000,35 +1000,25 @@ free_newmask:
  * get the metadata, that is, the parts of the flow key that cannot be
  * extracted from the packet itself.
  */
-
-int ovs_nla_get_flow_metadata(struct sw_flow *flow,
-                             const struct nlattr *attr)
+int ovs_nla_get_flow_metadata(const struct nlattr *attr,
+                             struct sw_flow_key *key)
 {
-       struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
        const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
+       struct sw_flow_match match;
        u64 attrs = 0;
        int err;
-       struct sw_flow_match match;
-
-       flow->key.phy.in_port = DP_MAX_PORTS;
-       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);
        if (err)
                return -EINVAL;
 
        memset(&match, 0, sizeof(match));
-       match.key = &flow->key;
+       match.key = key;
 
-       err = metadata_from_nlattrs(&match, &attrs, a, false);
-       if (err)
-               return err;
+       memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE);
+       key->phy.in_port = DP_MAX_PORTS;
 
-       return 0;
+       return metadata_from_nlattrs(&match, &attrs, a, false);
 }
 
 int ovs_nla_put_flow(struct datapath *dp, const struct sw_flow_key *swkey,
@@ -1050,16 +1040,8 @@ int ovs_nla_put_flow(struct datapath *dp, const struct sw_flow_key *swkey,
        if ((swkey->tun_key.ipv4_dst || is_mask)) {
                const struct geneve_opt *opts = NULL;
 
-               if (!is_mask) {
-                       struct vport *in_port;
-
-                       in_port = ovs_vport_ovsl_rcu(dp, swkey->phy.in_port);
-                       if (in_port->ops->type == OVS_VPORT_TYPE_GENEVE)
-                               opts = GENEVE_OPTS(output, swkey->tun_opts_len);
-               } else {
-                       if (output->tun_opts_len)
-                               opts = GENEVE_OPTS(output, swkey->tun_opts_len);
-               }
+               if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
+                       opts = GENEVE_OPTS(output, swkey->tun_opts_len);
 
                if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
                                        swkey->tun_opts_len))
@@ -1256,7 +1238,7 @@ nla_put_failure:
 
 #define MAX_ACTIONS_BUFSIZE    (32 * 1024)
 
-struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
+static struct sw_flow_actions *nla_alloc_flow_actions(int size)
 {
        struct sw_flow_actions *sfa;
 
@@ -1310,7 +1292,7 @@ static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
                new_acts_size = MAX_ACTIONS_BUFSIZE;
        }
 
-       acts = ovs_nla_alloc_flow_actions(new_acts_size);
+       acts = nla_alloc_flow_actions(new_acts_size);
        if (IS_ERR(acts))
                return (void *)acts;
 
@@ -1377,7 +1359,7 @@ 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(const struct nlattr *attr,
                                  const struct sw_flow_key *key,
                                  int depth, struct sw_flow_actions **sfa,
                                  __be16 eth_type, __be16 vlan_tci);
@@ -1422,7 +1404,7 @@ static int validate_and_copy_sample(const struct nlattr *attr,
        if (st_acts < 0)
                return st_acts;
 
-       err = ovs_nla_copy_actions__(actions, key, depth + 1, sfa,
+       err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa,
                                     eth_type, vlan_tci);
        if (err)
                return err;
@@ -1662,7 +1644,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(const struct nlattr *attr,
                                  const struct sw_flow_key *key,
                                  int depth, struct sw_flow_actions **sfa,
                                  __be16 eth_type, __be16 vlan_tci)
@@ -1811,8 +1793,18 @@ int ovs_nla_copy_actions(const struct nlattr *attr,
                         const struct sw_flow_key *key,
                         struct sw_flow_actions **sfa)
 {
-       return ovs_nla_copy_actions__(attr, key, 0, sfa, key->eth.type,
-                                     key->eth.tci);
+       int err;
+
+       *sfa = nla_alloc_flow_actions(nla_len(attr));
+       if (IS_ERR(*sfa))
+               return PTR_ERR(*sfa);
+
+       err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type,
+                                    key->eth.tci);
+       if (err)
+               kfree(*sfa);
+
+       return err;
 }
 
 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)