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

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