[ovs-dev] [PATCH 2/3] datapath: Make __parse_flow_nlattrs() more generic.

Joe Stringer joestringer at nicira.com
Fri Aug 15 05:03:06 UTC 2014


This allows future patches to reuse the nla error detection code, by
passing their own definitions for expected attribute lengths and types.

Signed-off-by: Joe Stringer <joestringer at nicira.com>
---
 datapath/flow_netlink.c |   34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/datapath/flow_netlink.c b/datapath/flow_netlink.c
index e525c9d..933b2a1 100644
--- a/datapath/flow_netlink.c
+++ b/datapath/flow_netlink.c
@@ -285,9 +285,10 @@ static bool is_all_zero(const u8 *fp, size_t size)
 	return true;
 }
 
-static int __parse_flow_nlattrs(const struct nlattr *attr,
-				const struct nlattr *a[],
-				u64 *attrsp, bool nz)
+static int parse_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
+			 const int expected_lens[], int max_attr,
+			 bool allow_duplicate_attrs, const char *attr_str,
+			 u64 *attrsp, bool nz)
 {
 	const struct nlattr *nla;
 	u64 attrs;
@@ -298,22 +299,23 @@ static int __parse_flow_nlattrs(const struct nlattr *attr,
 		u16 type = nla_type(nla);
 		int expected_len;
 
-		if (type > OVS_KEY_ATTR_MAX) {
-			OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
-				  type, OVS_KEY_ATTR_MAX);
+		if (type > max_attr) {
+			OVS_NLERR("Unknown %s attribute (type=%d, max=%d).\n",
+				  attr_str, type, max_attr);
 			return -EINVAL;
 		}
 
-		if (attrs & (1ULL << type)) {
-			OVS_NLERR("Duplicate key attribute (type %d).\n", type);
+		if (!allow_duplicate_attrs && (attrs & (1ULL << type))) {
+			OVS_NLERR("Duplicate %s attribute (type %d).\n",
+				  attr_str, type);
 			return -EINVAL;
 		}
 
-		expected_len = ovs_key_lens[type];
+		expected_len = expected_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);
+			OVS_NLERR("%s attribute has unexpected length (type=%d"
+				  ", length=%d, expected=%d).\n", attr_str,
+				  type, nla_len(nla), expected_len);
 			return -EINVAL;
 		}
 
@@ -323,7 +325,7 @@ static int __parse_flow_nlattrs(const struct nlattr *attr,
 		}
 	}
 	if (rem) {
-		OVS_NLERR("Message has %d unknown bytes.\n", rem);
+		OVS_NLERR("%s message has %d unknown bytes.\n", attr_str, rem);
 		return -EINVAL;
 	}
 
@@ -334,13 +336,15 @@ static int __parse_flow_nlattrs(const struct nlattr *attr,
 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
 				   const struct nlattr *a[], u64 *attrsp)
 {
-	return __parse_flow_nlattrs(attr, a, attrsp, true);
+	return parse_nlattrs(attr, a, ovs_key_lens, OVS_KEY_ATTR_MAX + 1,
+			     false, "mask", attrsp, true);
 }
 
 static int parse_flow_nlattrs(const struct nlattr *attr,
 			      const struct nlattr *a[], u64 *attrsp)
 {
-	return __parse_flow_nlattrs(attr, a, attrsp, false);
+	return parse_nlattrs(attr, a, ovs_key_lens, OVS_KEY_ATTR_MAX + 1,
+			     false, "key", attrsp, false);
 }
 
 static int ipv4_tun_from_nlattr(const struct nlattr *attr,
-- 
1.7.10.4




More information about the dev mailing list