naccyde / rpms / iproute

Forked from rpms/iproute 7 months ago
Clone

Blame SOURCES/0035-tc-flower-add-support-for-matching-on-ip-tos-and-ttl.patch

be97f7
From f8e5b20689cdc1f488140d9da4adf6f3ca421d3f Mon Sep 17 00:00:00 2001
be97f7
From: Kamal Heib <kheib@redhat.com>
be97f7
Date: Thu, 9 Nov 2017 04:44:32 -0500
be97f7
Subject: [PATCH] tc: flower: add support for matching on ip tos and ttl
be97f7
be97f7
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1456539
be97f7
be97f7
commit 6ea2c2b1cff676be2d01029a01cbd84d0675213c
be97f7
Author: Or Gerlitz <ogerlitz@mellanox.com>
be97f7
Date:   Wed Jun 7 15:17:54 2017 +0300
be97f7
be97f7
    tc: flower: add support for matching on ip tos and ttl
be97f7
be97f7
    Allow users to set flower classifier filter rules which
be97f7
    include matches for ip tos and ttl.
be97f7
be97f7
    Signed-off-by: Or Gerlitz <ogerlitz@mellanox.com>
be97f7
    Reviewed-by: Jiri Pirko <jiri@mellanox.com>
be97f7
be97f7
Signed-off-by: Kamal Heib <kheib@redhat.com>
be97f7
---
be97f7
 man/man8/tc-flower.8 | 17 +++++++++++-
be97f7
 tc/f_flower.c        | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++
be97f7
 2 files changed, 91 insertions(+), 1 deletion(-)
be97f7
be97f7
diff --git a/man/man8/tc-flower.8 b/man/man8/tc-flower.8
be97f7
index 7648079..be46f02 100644
be97f7
--- a/man/man8/tc-flower.8
be97f7
+++ b/man/man8/tc-flower.8
be97f7
@@ -30,7 +30,11 @@ flower \- flow based traffic control filter
be97f7
 .BR vlan_ethtype " { " ipv4 " | " ipv6 " | "
be97f7
 .IR ETH_TYPE " } | "
be97f7
 .BR ip_proto " { " tcp " | " udp " | " sctp " | " icmp " | " icmpv6 " | "
be97f7
-.IR IP_PROTO " } | { "
be97f7
+.IR IP_PROTO " } | "
be97f7
+.B ip_tos
be97f7
+.IR MASKED_IP_TOS " | "
be97f7
+.B ip_ttl
be97f7
+.IR MASKED_IP_TTL " | { "
be97f7
 .BR dst_ip " | " src_ip " } "
be97f7
 .IR PREFIX " | { "
be97f7
 .BR dst_port " | " src_port " } "
be97f7
@@ -122,6 +126,17 @@ may be
be97f7
 .BR tcp ", " udp ", " sctp ", " icmp ", " icmpv6
be97f7
 or an unsigned 8bit value in hexadecimal format.
be97f7
 .TP
be97f7
+.BI ip_tos " MASKED_IP_TOS"
be97f7
+Match on ipv4 TOS or ipv6 traffic-class - eight bits in hexadecimal format.
be97f7
+A mask may be optionally provided to limit the bits which are matched. A mask
be97f7
+is provided by following the value with a slash and then the mask. If the mask
be97f7
+is missing then a match on all bits is assumed.
be97f7
+.TP
be97f7
+.BI ip_ttl " MASKED_IP_TTL"
be97f7
+Match on ipv4 TTL or ipv6 hop-limit  - eight bits value in decimal or hexadecimal format.
be97f7
+A mask may be optionally provided to limit the bits which are matched. Same
be97f7
+logic is used for the mask as with matching on ip_tos.
be97f7
+.TP
be97f7
 .BI dst_ip " PREFIX"
be97f7
 .TQ
be97f7
 .BI src_ip " PREFIX"
be97f7
diff --git a/tc/f_flower.c b/tc/f_flower.c
be97f7
index 1b6b46e..5be693a 100644
be97f7
--- a/tc/f_flower.c
be97f7
+++ b/tc/f_flower.c
be97f7
@@ -53,6 +53,8 @@ static void explain(void)
be97f7
 		"                       dst_mac MASKED-LLADDR |\n"
be97f7
 		"                       src_mac MASKED-LLADDR |\n"
be97f7
 		"                       ip_proto [tcp | udp | sctp | icmp | icmpv6 | IP-PROTO ] |\n"
be97f7
+		"                       ip_tos MASKED-IP_TOS |\n"
be97f7
+		"                       ip_ttl MASKED-IP_TTL |\n"
be97f7
 		"                       dst_ip PREFIX |\n"
be97f7
 		"                       src_ip PREFIX |\n"
be97f7
 		"                       dst_port PORT-NUMBER |\n"
be97f7
@@ -510,6 +512,41 @@ err:
be97f7
 	return err;
be97f7
 }
be97f7
 
be97f7
+static int flower_parse_ip_tos_ttl(char *str, int key_type, int mask_type,
be97f7
+				   struct nlmsghdr *n)
be97f7
+{
be97f7
+	char *slash;
be97f7
+	int ret, err = -1;
be97f7
+	__u8 tos_ttl;
be97f7
+
be97f7
+	slash = strchr(str, '/');
be97f7
+	if (slash)
be97f7
+		*slash = '\0';
be97f7
+
be97f7
+	ret = get_u8(&tos_ttl, str, 10);
be97f7
+	if (ret < 0)
be97f7
+		ret = get_u8(&tos_ttl, str, 16);
be97f7
+	if (ret < 0)
be97f7
+		goto err;
be97f7
+
be97f7
+	addattr8(n, MAX_MSG, key_type, tos_ttl);
be97f7
+
be97f7
+	if (slash) {
be97f7
+		ret = get_u8(&tos_ttl, slash + 1, 16);
be97f7
+		if (ret < 0)
be97f7
+			goto err;
be97f7
+	} else {
be97f7
+		tos_ttl = 0xff;
be97f7
+	}
be97f7
+	addattr8(n, MAX_MSG, mask_type, tos_ttl);
be97f7
+
be97f7
+	err = 0;
be97f7
+err:
be97f7
+	if (slash)
be97f7
+		*slash = '/';
be97f7
+	return err;
be97f7
+}
be97f7
+
be97f7
 static int flower_parse_key_id(const char *str, int type, struct nlmsghdr *n)
be97f7
 {
be97f7
 	int ret;
be97f7
@@ -665,6 +702,26 @@ static int flower_parse_opt(struct filter_util *qu, char *handle,
be97f7
 				fprintf(stderr, "Illegal \"ip_proto\"\n");
be97f7
 				return -1;
be97f7
 			}
be97f7
+		} else if (matches(*argv, "ip_tos") == 0) {
be97f7
+			NEXT_ARG();
be97f7
+			ret = flower_parse_ip_tos_ttl(*argv,
be97f7
+						      TCA_FLOWER_KEY_IP_TOS,
be97f7
+						      TCA_FLOWER_KEY_IP_TOS_MASK,
be97f7
+						      n);
be97f7
+			if (ret < 0) {
be97f7
+				fprintf(stderr, "Illegal \"ip_tos\"\n");
be97f7
+				return -1;
be97f7
+			}
be97f7
+		} else if (matches(*argv, "ip_ttl") == 0) {
be97f7
+			NEXT_ARG();
be97f7
+			ret = flower_parse_ip_tos_ttl(*argv,
be97f7
+						      TCA_FLOWER_KEY_IP_TTL,
be97f7
+						      TCA_FLOWER_KEY_IP_TTL_MASK,
be97f7
+						      n);
be97f7
+			if (ret < 0) {
be97f7
+				fprintf(stderr, "Illegal \"ip_ttl\"\n");
be97f7
+				return -1;
be97f7
+			}
be97f7
 		} else if (matches(*argv, "dst_ip") == 0) {
be97f7
 			NEXT_ARG();
be97f7
 			ret = flower_parse_ip_addr(*argv, vlan_ethtype ?
be97f7
@@ -963,6 +1020,19 @@ static void flower_print_ip_proto(FILE *f, __u8 *p_ip_proto,
be97f7
 	*p_ip_proto = ip_proto;
be97f7
 }
be97f7
 
be97f7
+static void flower_print_ip_attr(FILE *f, char *name,
be97f7
+				 struct rtattr *key_attr,
be97f7
+				 struct rtattr *mask_attr)
be97f7
+{
be97f7
+	if (!key_attr)
be97f7
+		return;
be97f7
+
be97f7
+	fprintf(f, "\n  %s %x", name, rta_getattr_u8(key_attr));
be97f7
+	if (!mask_attr)
be97f7
+		return;
be97f7
+	fprintf(f, "/%x", rta_getattr_u8(mask_attr));
be97f7
+}
be97f7
+
be97f7
 static void flower_print_matching_flags(FILE *f, char *name,
be97f7
 					enum flower_matching_flags type,
be97f7
 					struct rtattr *attr,
be97f7
@@ -1150,6 +1220,11 @@ static int flower_print_opt(struct filter_util *qu, FILE *f,
be97f7
 	flower_print_eth_type(f, &eth_type, tb[TCA_FLOWER_KEY_ETH_TYPE]);
be97f7
 	flower_print_ip_proto(f, &ip_proto, tb[TCA_FLOWER_KEY_IP_PROTO]);
be97f7
 
be97f7
+	flower_print_ip_attr(f, "ip_tos", tb[TCA_FLOWER_KEY_IP_TOS],
be97f7
+			    tb[TCA_FLOWER_KEY_IP_TOS_MASK]);
be97f7
+	flower_print_ip_attr(f, "ip_ttl", tb[TCA_FLOWER_KEY_IP_TTL],
be97f7
+			    tb[TCA_FLOWER_KEY_IP_TTL_MASK]);
be97f7
+
be97f7
 	flower_print_ip_addr(f, "dst_ip", eth_type,
be97f7
 			     tb[TCA_FLOWER_KEY_IPV4_DST],
be97f7
 			     tb[TCA_FLOWER_KEY_IPV4_DST_MASK],
be97f7
-- 
be97f7
1.8.3.1
be97f7