Blame SOURCES/0004-m_mpls-add-mac_push-action.patch

26f871
From 0afe12a4a9471ed1343693338ec6350dc66ba295 Mon Sep 17 00:00:00 2001
26f871
Message-Id: <0afe12a4a9471ed1343693338ec6350dc66ba295.1611877215.git.aclaudi@redhat.com>
26f871
In-Reply-To: <cb7ce51cc1abd7b98370b903ec96205ebfe48661.1611877215.git.aclaudi@redhat.com>
26f871
References: <cb7ce51cc1abd7b98370b903ec96205ebfe48661.1611877215.git.aclaudi@redhat.com>
26f871
From: Andrea Claudi <aclaudi@redhat.com>
26f871
Date: Fri, 29 Jan 2021 00:35:03 +0100
26f871
Subject: [PATCH] m_mpls: add mac_push action
26f871
26f871
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1885770
26f871
Upstream Status: unknown commit 02a261b5
26f871
26f871
commit 02a261b5ba1c8580ac2a35bc6c87faa2ec9f5c96
26f871
Author: Guillaume Nault <gnault@redhat.com>
26f871
Date:   Mon Oct 19 17:23:08 2020 +0200
26f871
26f871
    m_mpls: add mac_push action
26f871
26f871
    Add support for the new TCA_MPLS_ACT_MAC_PUSH action (kernel commit
26f871
    a45294af9e96 ("net/sched: act_mpls: Add action to push MPLS LSE before
26f871
    Ethernet header")). This action let TC push an MPLS header before the
26f871
    MAC header of a frame.
26f871
26f871
    Example (encapsulate all outgoing frames with label 20, then add an
26f871
    outer Ethernet header):
26f871
     # tc filter add dev ethX matchall \
26f871
           action mpls mac_push label 20 ttl 64 \
26f871
           action vlan push_eth dst_mac 0a:00:00:00:00:02 \
26f871
                                src_mac 0a:00:00:00:00:01
26f871
26f871
    This patch also adds an alias for ETH_P_TEB, since it is useful when
26f871
    decapsulating MPLS packets that contain an Ethernet frame.
26f871
26f871
    With MAC_PUSH, there's no previous Ethertype to modify. However, the
26f871
    "protocol" option is still needed, because the kernel uses it to set
26f871
    skb->protocol. So rename can_modify_ethtype() to can_set_ethtype().
26f871
26f871
    Also add a test suite for m_mpls, which covers the new action and the
26f871
    pre-existing ones.
26f871
26f871
    Signed-off-by: Guillaume Nault <gnault@redhat.com>
26f871
    Signed-off-by: David Ahern <dsahern@gmail.com>
26f871
---
26f871
 lib/ll_proto.c            |  1 +
26f871
 man/man8/tc-mpls.8        | 44 +++++++++++++++++++++++--
26f871
 man/man8/tc-vlan.8        |  5 ++-
26f871
 tc/m_mpls.c               | 43 ++++++++++++++++--------
26f871
 testsuite/tests/tc/mpls.t | 69 +++++++++++++++++++++++++++++++++++++++
26f871
 5 files changed, 145 insertions(+), 17 deletions(-)
26f871
 create mode 100755 testsuite/tests/tc/mpls.t
26f871
26f871
diff --git a/lib/ll_proto.c b/lib/ll_proto.c
26f871
index 2a0c1cb3..78179311 100644
26f871
--- a/lib/ll_proto.c
26f871
+++ b/lib/ll_proto.c
26f871
@@ -80,6 +80,7 @@ __PF(8021Q,802.1Q)
26f871
 __PF(8021AD,802.1ad)
26f871
 __PF(MPLS_UC,mpls_uc)
26f871
 __PF(MPLS_MC,mpls_mc)
26f871
+__PF(TEB,teb)
26f871
 
26f871
 { 0x8100, "802.1Q" },
26f871
 { 0x88cc, "LLDP" },
26f871
diff --git a/man/man8/tc-mpls.8 b/man/man8/tc-mpls.8
26f871
index 84ef2ef1..9e563e98 100644
26f871
--- a/man/man8/tc-mpls.8
26f871
+++ b/man/man8/tc-mpls.8
26f871
@@ -17,7 +17,7 @@ mpls - mpls manipulation module
26f871
 
26f871
 .ti -8
26f871
 .IR PUSH " := "
26f871
-.BR push " [ " protocol
26f871
+.RB "{ " push " | " mac_push " } [ " protocol
26f871
 .IR MPLS_PROTO " ]"
26f871
 .RB " [ " tc
26f871
 .IR MPLS_TC " ] "
26f871
@@ -64,7 +64,14 @@ requires no arguments and simply subtracts 1 from the MPLS header TTL field.
26f871
 Decapsulation mode. Requires the protocol of the next header.
26f871
 .TP
26f871
 .B push
26f871
-Encapsulation mode. Requires at least the
26f871
+Encapsulation mode. Adds the MPLS header between the MAC and the network
26f871
+headers. Requires at least the
26f871
+.B label
26f871
+option.
26f871
+.TP
26f871
+.B mac_push
26f871
+Encapsulation mode. Adds the MPLS header before the MAC header. Requires at
26f871
+least the
26f871
 .B label
26f871
 option.
26f871
 .TP
26f871
@@ -152,5 +159,36 @@ ip packets and output to eth1:
26f871
 .EE
26f871
 .RE
26f871
 
26f871
+Here is another example, where incoming Ethernet frames are encapsulated into
26f871
+MPLS with label 123 and TTL 64. Then, an outer Ethernet header is added and the
26f871
+resulting frame is finally sent on eth1:
26f871
+
26f871
+.RS
26f871
+.EX
26f871
+#tc qdisc add dev eth0 ingress
26f871
+#tc filter add dev eth0 ingress matchall \\
26f871
+	action mpls mac_push label 123 ttl 64 \\
26f871
+	action vlan push_eth \\
26f871
+		dst_mac 02:00:00:00:00:02 \\
26f871
+		src_mac 02:00:00:00:00:01 \\
26f871
+	action mirred egress redirect dev eth1
26f871
+.EE
26f871
+.RE
26f871
+
26f871
+The following example assumes that incoming MPLS packets with label 123
26f871
+transport Ethernet frames. The outer Ethernet and the MPLS headers are
26f871
+stripped, then the inner Ethernet frame is sent on eth1:
26f871
+
26f871
+.RS
26f871
+.EX
26f871
+#tc qdisc add dev eth0 ingress
26f871
+#tc filter add dev eth0 ingress protocol mpls_uc \\
26f871
+	flower mpls_label 123 mpls_bos 1 \\
26f871
+	action vlan pop_eth \\
26f871
+	action mpls pop protocol teb \\
26f871
+	action mirred egress redirect dev eth1
26f871
+.EE
26f871
+.RE
26f871
+
26f871
 .SH SEE ALSO
26f871
-.BR tc (8)
26f871
+.BR tc "(8), " tc-mirred "(8), " tc-vlan (8)
26f871
diff --git a/man/man8/tc-vlan.8 b/man/man8/tc-vlan.8
26f871
index 5c2808b1..264053d3 100644
26f871
--- a/man/man8/tc-vlan.8
26f871
+++ b/man/man8/tc-vlan.8
26f871
@@ -157,5 +157,8 @@ process then restarted for the plain packet:
26f871
 .EE
26f871
 .RE
26f871
 
26f871
+For an example of the
26f871
+.BR pop_eth " and " push_eth " modes, see " tc-mpls (8).
26f871
+
26f871
 .SH SEE ALSO
26f871
-.BR tc (8)
26f871
+.BR tc "(8), " tc-mpls (8)
26f871
diff --git a/tc/m_mpls.c b/tc/m_mpls.c
26f871
index 3d5d9b25..cb8019b1 100644
26f871
--- a/tc/m_mpls.c
26f871
+++ b/tc/m_mpls.c
26f871
@@ -17,6 +17,7 @@ static const char * const action_names[] = {
26f871
 	[TCA_MPLS_ACT_PUSH] = "push",
26f871
 	[TCA_MPLS_ACT_MODIFY] = "modify",
26f871
 	[TCA_MPLS_ACT_DEC_TTL] = "dec_ttl",
26f871
+	[TCA_MPLS_ACT_MAC_PUSH] = "mac_push",
26f871
 };
26f871
 
26f871
 static void explain(void)
26f871
@@ -25,9 +26,11 @@ static void explain(void)
26f871
 		"Usage: mpls pop [ protocol MPLS_PROTO ]\n"
26f871
 		"       mpls push [ protocol MPLS_PROTO ] [ label MPLS_LABEL ] [ tc MPLS_TC ]\n"
26f871
 		"                 [ ttl MPLS_TTL ] [ bos MPLS_BOS ] [CONTROL]\n"
26f871
+		"       mpls mac_push [ protocol MPLS_PROTO ] [ label MPLS_LABEL ] [ tc MPLS_TC ]\n"
26f871
+		"                     [ ttl MPLS_TTL ] [ bos MPLS_BOS ] [CONTROL]\n"
26f871
 		"       mpls modify [ label MPLS_LABEL ] [ tc MPLS_TC ] [ ttl MPLS_TTL ] [CONTROL]\n"
26f871
-		"           for pop MPLS_PROTO is next header of packet - e.g. ip or mpls_uc\n"
26f871
-		"           for push MPLS_PROTO is one of mpls_uc or mpls_mc\n"
26f871
+		"           for pop, MPLS_PROTO is next header of packet - e.g. ip or mpls_uc\n"
26f871
+		"           for push and mac_push, MPLS_PROTO is one of mpls_uc or mpls_mc\n"
26f871
 		"               with default: mpls_uc\n"
26f871
 		"       CONTROL := reclassify | pipe | drop | continue | pass |\n"
26f871
 		"                  goto chain <CHAIN_INDEX>\n");
26f871
@@ -41,12 +44,14 @@ static void usage(void)
26f871
 
26f871
 static bool can_modify_mpls_fields(unsigned int action)
26f871
 {
26f871
-	return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MODIFY;
26f871
+	return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MAC_PUSH ||
26f871
+		action == TCA_MPLS_ACT_MODIFY;
26f871
 }
26f871
 
26f871
-static bool can_modify_ethtype(unsigned int action)
26f871
+static bool can_set_ethtype(unsigned int action)
26f871
 {
26f871
-	return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_POP;
26f871
+	return action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MAC_PUSH ||
26f871
+		action == TCA_MPLS_ACT_POP;
26f871
 }
26f871
 
26f871
 static bool is_valid_label(__u32 label)
26f871
@@ -94,6 +99,10 @@ static int parse_mpls(struct action_util *a, int *argc_p, char ***argv_p,
26f871
 			if (check_double_action(action, *argv))
26f871
 				return -1;
26f871
 			action = TCA_MPLS_ACT_PUSH;
26f871
+		} else if (matches(*argv, "mac_push") == 0) {
26f871
+			if (check_double_action(action, *argv))
26f871
+				return -1;
26f871
+			action = TCA_MPLS_ACT_MAC_PUSH;
26f871
 		} else if (matches(*argv, "modify") == 0) {
26f871
 			if (check_double_action(action, *argv))
26f871
 				return -1;
26f871
@@ -104,31 +113,36 @@ static int parse_mpls(struct action_util *a, int *argc_p, char ***argv_p,
26f871
 			action = TCA_MPLS_ACT_DEC_TTL;
26f871
 		} else if (matches(*argv, "label") == 0) {
26f871
 			if (!can_modify_mpls_fields(action))
26f871
-				invarg("only valid for push/modify", *argv);
26f871
+				invarg("only valid for push, mac_push and modify",
26f871
+				       *argv);
26f871
 			NEXT_ARG();
26f871
 			if (get_u32(&label, *argv, 0) || !is_valid_label(label))
26f871
 				invarg("label must be <=0xFFFFF", *argv);
26f871
 		} else if (matches(*argv, "tc") == 0) {
26f871
 			if (!can_modify_mpls_fields(action))
26f871
-				invarg("only valid for push/modify", *argv);
26f871
+				invarg("only valid for push, mac_push and modify",
26f871
+				       *argv);
26f871
 			NEXT_ARG();
26f871
 			if (get_u8(&tc, *argv, 0) || (tc & ~0x7))
26f871
 				invarg("tc field is 3 bits max", *argv);
26f871
 		} else if (matches(*argv, "ttl") == 0) {
26f871
 			if (!can_modify_mpls_fields(action))
26f871
-				invarg("only valid for push/modify", *argv);
26f871
+				invarg("only valid for push, mac_push and modify",
26f871
+				       *argv);
26f871
 			NEXT_ARG();
26f871
 			if (get_u8(&ttl, *argv, 0) || !ttl)
26f871
 				invarg("ttl must be >0 and <=255", *argv);
26f871
 		} else if (matches(*argv, "bos") == 0) {
26f871
 			if (!can_modify_mpls_fields(action))
26f871
-				invarg("only valid for push/modify", *argv);
26f871
+				invarg("only valid for push, mac_push and modify",
26f871
+				       *argv);
26f871
 			NEXT_ARG();
26f871
 			if (get_u8(&bos, *argv, 0) || (bos & ~0x1))
26f871
 				invarg("bos must be 0 or 1", *argv);
26f871
 		} else if (matches(*argv, "protocol") == 0) {
26f871
-			if (!can_modify_ethtype(action))
26f871
-				invarg("only valid for push/pop", *argv);
26f871
+			if (!can_set_ethtype(action))
26f871
+				invarg("only valid for push, mac_push and pop",
26f871
+				       *argv);
26f871
 			NEXT_ARG();
26f871
 			if (ll_proto_a2n(&proto, *argv))
26f871
 				invarg("protocol is invalid", *argv);
26f871
@@ -159,10 +173,12 @@ static int parse_mpls(struct action_util *a, int *argc_p, char ***argv_p,
26f871
 	if (action == TCA_MPLS_ACT_PUSH && label == 0xffffffff)
26f871
 		missarg("label");
26f871
 
26f871
-	if (action == TCA_MPLS_ACT_PUSH && proto &&
26f871
+	if ((action == TCA_MPLS_ACT_PUSH || action == TCA_MPLS_ACT_MAC_PUSH) &&
26f871
+	    proto &&
26f871
 	    proto != htons(ETH_P_MPLS_UC) && proto != htons(ETH_P_MPLS_MC)) {
26f871
 		fprintf(stderr,
26f871
-			"invalid push protocol \"0x%04x\" - use mpls_(uc|mc)\n",
26f871
+			"invalid %spush protocol \"0x%04x\" - use mpls_(uc|mc)\n",
26f871
+			action == TCA_MPLS_ACT_MAC_PUSH ? "mac_" : "",
26f871
 			ntohs(proto));
26f871
 		return -1;
26f871
 	}
26f871
@@ -223,6 +239,7 @@ static int print_mpls(struct action_util *au, FILE *f, struct rtattr *arg)
26f871
 		}
26f871
 		break;
26f871
 	case TCA_MPLS_ACT_PUSH:
26f871
+	case TCA_MPLS_ACT_MAC_PUSH:
26f871
 		if (tb[TCA_MPLS_PROTO]) {
26f871
 			__u16 proto;
26f871
 
26f871
diff --git a/testsuite/tests/tc/mpls.t b/testsuite/tests/tc/mpls.t
26f871
new file mode 100755
26f871
index 00000000..cb25f361
26f871
--- /dev/null
26f871
+++ b/testsuite/tests/tc/mpls.t
26f871
@@ -0,0 +1,69 @@
26f871
+#!/bin/sh
26f871
+
26f871
+. lib/generic.sh
26f871
+
26f871
+DEV="$(rand_dev)"
26f871
+ts_ip "$0" "Add $DEV dummy interface" link add dev $DEV up type dummy
26f871
+ts_tc "$0" "Add ingress qdisc" qdisc add dev $DEV ingress
26f871
+
26f871
+reset_qdisc()
26f871
+{
26f871
+	ts_tc "$0" "Remove ingress qdisc" qdisc del dev $DEV ingress
26f871
+	ts_tc "$0" "Add ingress qdisc" qdisc add dev $DEV ingress
26f871
+}
26f871
+
26f871
+ts_tc "$0" "Add mpls action pop"                              \
26f871
+	filter add dev $DEV ingress protocol mpls_uc matchall \
26f871
+	action mpls pop protocol ip
26f871
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
26f871
+test_on "mpls"
26f871
+test_on "pop protocol ip pipe"
26f871
+
26f871
+reset_qdisc
26f871
+ts_tc "$0" "Add mpls action push"                        \
26f871
+	filter add dev $DEV ingress protocol ip matchall \
26f871
+	action mpls push protocol mpls_uc label 20 tc 3 bos 1 ttl 64
26f871
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
26f871
+test_on "mpls"
26f871
+test_on "push"
26f871
+test_on "protocol mpls_uc"
26f871
+test_on "label 20"
26f871
+test_on "tc 3"
26f871
+test_on "bos 1"
26f871
+test_on "ttl 64"
26f871
+test_on "pipe"
26f871
+
26f871
+reset_qdisc
26f871
+ts_tc "$0" "Add mpls action mac_push"        \
26f871
+	filter add dev $DEV ingress matchall \
26f871
+	action mpls mac_push protocol mpls_uc label 20 tc 3 bos 1 ttl 64
26f871
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
26f871
+test_on "mpls"
26f871
+test_on "mac_push"
26f871
+test_on "protocol mpls_uc"
26f871
+test_on "label 20"
26f871
+test_on "tc 3"
26f871
+test_on "bos 1"
26f871
+test_on "ttl 64"
26f871
+test_on "pipe"
26f871
+
26f871
+reset_qdisc
26f871
+ts_tc "$0" "Add mpls action modify"                           \
26f871
+	filter add dev $DEV ingress protocol mpls_uc matchall \
26f871
+	action mpls modify label 20 tc 3 ttl 64
26f871
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
26f871
+test_on "mpls"
26f871
+test_on "modify"
26f871
+test_on "label 20"
26f871
+test_on "tc 3"
26f871
+test_on "ttl 64"
26f871
+test_on "pipe"
26f871
+
26f871
+reset_qdisc
26f871
+ts_tc "$0" "Add mpls action dec_ttl"                          \
26f871
+	filter add dev $DEV ingress protocol mpls_uc matchall \
26f871
+	action mpls dec_ttl
26f871
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
26f871
+test_on "mpls"
26f871
+test_on "dec_ttl"
26f871
+test_on "pipe"
26f871
-- 
26f871
2.29.2
26f871