datapath: add netlink error message to help kernel userspace integration.
[sliver-openvswitch.git] / datapath / flow.c
index 79d5d49..c76c18d 100644 (file)
@@ -205,13 +205,19 @@ static bool ovs_match_validate(const struct sw_flow_match *match,
                }
        }
 
-       if ((key_attrs & key_expected) != key_expected)
+       if ((key_attrs & key_expected) != key_expected) {
                /* Key attributes check failed. */
+               OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
+                               key_attrs, key_expected);
                return false;
+       }
 
-       if ((mask_attrs & mask_allowed) != mask_attrs)
+       if ((mask_attrs & mask_allowed) != mask_attrs) {
                /* Mask attributes check failed. */
+               OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
+                               mask_attrs, mask_allowed);
                return false;
+       }
 
        return true;
 }
@@ -443,7 +449,7 @@ static void free_buckets(struct flex_array *buckets)
        flex_array_free(buckets);
 }
 
-struct flow_table *ovs_flow_tbl_alloc(int new_size)
+static struct flow_table *__flow_tbl_alloc(int new_size)
 {
        struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
 
@@ -461,7 +467,7 @@ struct flow_table *ovs_flow_tbl_alloc(int new_size)
        table->node_ver = 0;
        table->keep_flows = false;
        get_random_bytes(&table->hash_seed, sizeof(u32));
-       INIT_LIST_HEAD(&table->mask_list);
+       table->mask_list = NULL;
 
        return table;
 }
@@ -485,11 +491,32 @@ static void __flow_tbl_destroy(struct flow_table *table)
                }
        }
 
+       BUG_ON(!list_empty(table->mask_list));
+       kfree(table->mask_list);
+
 skip_flows:
        free_buckets(table->buckets);
        kfree(table);
 }
 
+struct flow_table *ovs_flow_tbl_alloc(int new_size)
+{
+       struct flow_table *table = __flow_tbl_alloc(new_size);
+
+       if (!table)
+               return NULL;
+
+       table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
+       if (!table->mask_list) {
+               table->keep_flows = true;
+               __flow_tbl_destroy(table);
+               return NULL;
+       }
+       INIT_LIST_HEAD(table->mask_list);
+
+       return table;
+}
+
 static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
 {
        struct flow_table *table = container_of(rcu, struct flow_table, rcu);
@@ -571,7 +598,7 @@ static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buck
 {
        struct flow_table *new_table;
 
-       new_table = ovs_flow_tbl_alloc(n_buckets);
+       new_table = __flow_tbl_alloc(n_buckets);
        if (!new_table)
                return ERR_PTR(-ENOMEM);
 
@@ -1030,7 +1057,7 @@ struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
        struct sw_flow *flow = NULL;
        struct sw_flow_mask *mask;
 
-       list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
+       list_for_each_entry_rcu(mask, tbl->mask_list, list) {
                flow = ovs_masked_flow_lookup(tbl, key, mask);
                if (flow)  /* Found */
                        break;
@@ -1105,24 +1132,33 @@ static int __parse_flow_nlattrs(const struct nlattr *attr,
                u16 type = nla_type(nla);
                int expected_len;
 
-               if (type > OVS_KEY_ATTR_MAX || attrs & (1ULL << type))
-                       return -EINVAL;
+               if (type > OVS_KEY_ATTR_MAX) {
+                       OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
+                                 type, OVS_KEY_ATTR_MAX);
+               }
 
-               expected_len = ovs_key_lens[type];
-               if (nla_len(nla) != expected_len && expected_len != -1)
+               if (attrs & (1ULL << type)) {
+                       OVS_NLERR("Duplicate key attribute (type %d).\n", type);
                        return -EINVAL;
+               }
 
-               if (attrs & (1ULL << type))
-                       /* Duplicated field. */
+               expected_len = ovs_key_lens[type];
+               if (nla_len(nla) != expected_len && expected_len != -1) {
+                       OVS_NLERR("Key attribute has unexpected length (type=%d"
+                                 ", length=%d, expected=%d).\n", type,
+                                 nla_len(nla), expected_len);
                        return -EINVAL;
+               }
 
                if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
                        attrs |= 1ULL << type;
                        a[type] = nla;
                }
        }
-       if (rem)
+       if (rem) {
+               OVS_NLERR("Message has %d unknown bytes.\n", rem);
                return -EINVAL;
+       }
 
        *attrsp = attrs;
        return 0;
@@ -1146,7 +1182,7 @@ int ipv4_tun_from_nlattr(const struct nlattr *attr,
        struct nlattr *a;
        int rem;
        bool ttl = false;
-       u16 tun_flags = 0;
+       __be16 tun_flags = 0;
 
        nla_for_each_nested(a, attr, rem) {
                int type = nla_type(a);
@@ -1160,15 +1196,24 @@ int ipv4_tun_from_nlattr(const struct nlattr *attr,
                        [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
                };
 
-               if (type > OVS_TUNNEL_KEY_ATTR_MAX ||
-                       ovs_tunnel_key_lens[type] != nla_len(a))
+               if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
+                       OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d)\n",
+                       type, OVS_TUNNEL_KEY_ATTR_MAX);
+                       return -EINVAL;
+               }
+
+               if (ovs_tunnel_key_lens[type] != nla_len(a)) {
+                       OVS_NLERR("IPv4 tunnel attribute type has unexpected "
+                                 " legnth (type=%d, length=%d, expected=%d.)\n",
+                                 type, nla_len(a), ovs_tunnel_key_lens[type]);
                        return -EINVAL;
+               }
 
                switch (type) {
                case OVS_TUNNEL_KEY_ATTR_ID:
                        SW_FLOW_KEY_PUT(match, tun_key.tun_id,
                                        nla_get_be64(a), is_mask);
-                       tun_flags |= OVS_TNL_F_KEY;
+                       tun_flags |= TUNNEL_KEY;
                        break;
                case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
                        SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
@@ -1188,10 +1233,10 @@ int ipv4_tun_from_nlattr(const struct nlattr *attr,
                        ttl = true;
                        break;
                case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
-                       tun_flags |= OVS_TNL_F_DONT_FRAGMENT;
+                       tun_flags |= TUNNEL_DONT_FRAGMENT;
                        break;
                case OVS_TUNNEL_KEY_ATTR_CSUM:
-                       tun_flags |= OVS_TNL_F_CSUM;
+                       tun_flags |= TUNNEL_CSUM;
                        break;
                default:
                        return -EINVAL;
@@ -1200,14 +1245,20 @@ int ipv4_tun_from_nlattr(const struct nlattr *attr,
 
        SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
 
-       if (rem > 0)
+       if (rem > 0) {
+               OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
                return -EINVAL;
+       }
 
-       if (!match->key->tun_key.ipv4_dst)
+       if (!match->key->tun_key.ipv4_dst) {
+               OVS_NLERR("IPv4 tunnel destination address is zero.\n");
                return -EINVAL;
+       }
 
-       if (!ttl)
+       if (!ttl) {
+               OVS_NLERR("IPv4 tunnel TTL is zero.\n");
                return -EINVAL;
+       }
 
        return 0;
 }
@@ -1222,7 +1273,7 @@ int ipv4_tun_to_nlattr(struct sk_buff *skb,
        if (!nla)
                return -EMSGSIZE;
 
-       if (tun_key->tun_flags & OVS_TNL_F_KEY &&
+       if (tun_key->tun_flags & TUNNEL_KEY &&
            nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
                return -EMSGSIZE;
        if (tun_key->ipv4_src &&
@@ -1235,10 +1286,10 @@ int ipv4_tun_to_nlattr(struct sk_buff *skb,
                return -EMSGSIZE;
        if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
                return -EMSGSIZE;
-       if ((tun_key->tun_flags & OVS_TNL_F_DONT_FRAGMENT) &&
+       if ((tun_key->tun_flags & TUNNEL_DONT_FRAGMENT) &&
                nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
                return -EMSGSIZE;
-       if ((tun_key->tun_flags & OVS_TNL_F_CSUM) &&
+       if ((tun_key->tun_flags & TUNNEL_CSUM) &&
                nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
                return -EMSGSIZE;
 
@@ -1268,8 +1319,10 @@ static int metadata_from_nlattrs(struct sw_flow_match *match,  u64 *attrs,
        if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
                uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
-               if (!is_mask && mark != 0)
+               if (!is_mask && mark != 0) {
+                       OVS_NLERR("skb->mark must be zero on this kernel (mark=%d).\n", mark);
                        return -EINVAL;
+               }
 #endif
                SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
                *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
@@ -1287,6 +1340,7 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match,  u64 attrs,
                const struct nlattr **a, bool is_mask)
 {
        int err;
+       u64 orig_attrs = attrs;
 
        err = metadata_from_nlattrs(match, &attrs, a, is_mask);
        if (err)
@@ -1307,8 +1361,11 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match,  u64 attrs,
                __be16 tci;
 
                tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
-               if (!is_mask && (tci & htons(VLAN_TAG_PRESENT)))
-                       return -EINVAL;
+               if (!is_mask)
+                       if (!(tci & htons(VLAN_TAG_PRESENT))) {
+                               OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
+                               return -EINVAL;
+                       }
 
                SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
                attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN);
@@ -1318,8 +1375,11 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match,  u64 attrs,
                __be16 eth_type;
 
                eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
-               if (!is_mask && ntohs(eth_type) < ETH_P_802_3_MIN)
+               if (!is_mask && ntohs(eth_type) < ETH_P_802_3_MIN) {
+                       OVS_NLERR("EtherType is less than mimimum (type=%x, min=%x).\n",
+                                       ntohs(eth_type), ETH_P_802_3_MIN);
                        return -EINVAL;
+               }
 
                SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
                attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
@@ -1331,8 +1391,11 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match,  u64 attrs,
                const struct ovs_key_ipv4 *ipv4_key;
 
                ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
-               if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX)
+               if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
+                       OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
+                               ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
                        return -EINVAL;
+               }
                SW_FLOW_KEY_PUT(match, ip.proto,
                                ipv4_key->ipv4_proto, is_mask);
                SW_FLOW_KEY_PUT(match, ip.tos,
@@ -1352,8 +1415,11 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match,  u64 attrs,
                const struct ovs_key_ipv6 *ipv6_key;
 
                ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
-               if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX)
+               if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
+                       OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
+                               ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
                        return -EINVAL;
+               }
                SW_FLOW_KEY_PUT(match, ipv6.label,
                                ipv6_key->ipv6_label, is_mask);
                SW_FLOW_KEY_PUT(match, ip.proto,
@@ -1380,8 +1446,11 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match,  u64 attrs,
                const struct ovs_key_arp *arp_key;
 
                arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
-               if (!is_mask && (arp_key->arp_op & htons(0xff00)))
+               if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
+                       OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
+                                 arp_key->arp_op);
                        return -EINVAL;
+               }
 
                SW_FLOW_KEY_PUT(match, ipv4.addr.src,
                                arp_key->arp_sip, is_mask);
@@ -1401,10 +1470,17 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match,  u64 attrs,
                const struct ovs_key_tcp *tcp_key;
 
                tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
-               SW_FLOW_KEY_PUT(match, ipv4.tp.src,
-                               tcp_key->tcp_src, is_mask);
-               SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
-                               tcp_key->tcp_dst, is_mask);
+               if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
+                       SW_FLOW_KEY_PUT(match, ipv4.tp.src,
+                                       tcp_key->tcp_src, is_mask);
+                       SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
+                                       tcp_key->tcp_dst, is_mask);
+               } else {
+                       SW_FLOW_KEY_PUT(match, ipv6.tp.src,
+                                       tcp_key->tcp_src, is_mask);
+                       SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
+                                       tcp_key->tcp_dst, is_mask);
+               }
                attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
        }
 
@@ -1412,10 +1488,17 @@ static int ovs_key_from_nlattrs(struct sw_flow_match *match,  u64 attrs,
                const struct ovs_key_udp *udp_key;
 
                udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
-               SW_FLOW_KEY_PUT(match, ipv4.tp.src,
-                               udp_key->udp_src, is_mask);
-               SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
-                               udp_key->udp_dst, is_mask);
+               if (orig_attrs & (1ULL << OVS_KEY_ATTR_IPV4)) {
+                       SW_FLOW_KEY_PUT(match, ipv4.tp.src,
+                                       udp_key->udp_src, is_mask);
+                       SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
+                                       udp_key->udp_dst, is_mask);
+               } else {
+                       SW_FLOW_KEY_PUT(match, ipv6.tp.src,
+                                       udp_key->udp_src, is_mask);
+                       SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
+                                       udp_key->udp_dst, is_mask);
+               }
                attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
        }
 
@@ -1479,7 +1562,6 @@ int ovs_match_from_nlattrs(struct sw_flow_match *match,
                           const struct nlattr *mask)
 {
        const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
-       const struct nlattr *m[OVS_KEY_ATTR_MAX + 1];
        const struct nlattr *encap;
        u64 key_attrs = 0;
        u64 mask_attrs = 0;
@@ -1503,8 +1585,10 @@ int ovs_match_from_nlattrs(struct sw_flow_match *match,
                                encap_valid = true;
                                key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
                                err = parse_flow_nlattrs(encap, a, &key_attrs);
-                       } else
+                       } else {
+                               OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
                                err = -EINVAL;
+                       }
 
                        if (err)
                                return err;
@@ -1516,27 +1600,32 @@ int ovs_match_from_nlattrs(struct sw_flow_match *match,
                return err;
 
        if (mask) {
-               err = parse_flow_mask_nlattrs(mask, m, &mask_attrs);
+               err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
                if (err)
                        return err;
 
                if ((mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) && encap_valid) {
                        __be16 eth_type = 0;
 
-                       if (m[OVS_KEY_ATTR_ETHERTYPE])
-                               eth_type = nla_get_be16(m[OVS_KEY_ATTR_ETHERTYPE]);
+                       mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP);
+                       if (a[OVS_KEY_ATTR_ETHERTYPE])
+                               eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
                        if (eth_type == htons(0xffff)) {
                                mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE);
-                               encap = m[OVS_KEY_ATTR_ENCAP];
-                               err = parse_flow_mask_nlattrs(encap, m, &mask_attrs);
-                       } else
+                               encap = a[OVS_KEY_ATTR_ENCAP];
+                               err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
+                       } else {
+                               OVS_NLERR("VLAN frames must have an exact match"
+                                        " on the TPID (mask=%x).\n",
+                                        ntohs(eth_type));
                                err = -EINVAL;
+                       }
 
                        if (err)
                                return err;
                }
 
-               err = ovs_key_from_nlattrs(match,  mask_attrs, m, true);
+               err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
                if (err)
                        return err;
        } else {
@@ -1581,7 +1670,8 @@ int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
        if (err)
                return -EINVAL;
 
-       ovs_match_init(&match, &flow->key, NULL);
+       memset(&match, 0, sizeof(match));
+       match.key = &flow->key;
 
        err = metadata_from_nlattrs(&match, &attrs, a, false);
        if (err)
@@ -1844,7 +1934,7 @@ struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
 {
        struct list_head *ml;
 
-       list_for_each(ml, &tbl->mask_list) {
+       list_for_each(ml, tbl->mask_list) {
                struct sw_flow_mask *m;
                m = container_of(ml, struct sw_flow_mask, list);
                if (ovs_sw_flow_mask_equal(mask, m))
@@ -1861,7 +1951,7 @@ struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
  */
 void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
 {
-       list_add_rcu(&mask->list, &tbl->mask_list);
+       list_add_rcu(&mask->list, tbl->mask_list);
 }
 
 /**