naccyde / rpms / iproute

Forked from rpms/iproute 5 months ago
Clone

Blame SOURCES/0003-m_vlan-add-pop_eth-and-push_eth-actions.patch

d62593
From cac52dd831b6982f6b27b02c26243edbe0b7d747 Mon Sep 17 00:00:00 2001
d62593
Message-Id: <cac52dd831b6982f6b27b02c26243edbe0b7d747.1611877215.git.aclaudi@redhat.com>
d62593
In-Reply-To: <cb7ce51cc1abd7b98370b903ec96205ebfe48661.1611877215.git.aclaudi@redhat.com>
d62593
References: <cb7ce51cc1abd7b98370b903ec96205ebfe48661.1611877215.git.aclaudi@redhat.com>
d62593
From: Andrea Claudi <aclaudi@redhat.com>
d62593
Date: Fri, 29 Jan 2021 00:35:03 +0100
d62593
Subject: [PATCH] m_vlan: add pop_eth and push_eth actions
d62593
d62593
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1885770
d62593
Upstream Status: unknown commit d61167dd
d62593
d62593
commit d61167dd88b45832843b1458cd156f3b85c8ff16
d62593
Author: Guillaume Nault <gnault@redhat.com>
d62593
Date:   Mon Oct 19 17:23:01 2020 +0200
d62593
d62593
    m_vlan: add pop_eth and push_eth actions
d62593
d62593
    Add support for the new TCA_VLAN_ACT_POP_ETH and TCA_VLAN_ACT_PUSH_ETH
d62593
    actions (kernel commit 19fbcb36a39e ("net/sched: act_vlan:
d62593
    Add {POP,PUSH}_ETH actions"). These action let TC remove or add the
d62593
    Ethernet at the head of a frame.
d62593
d62593
    Drop an Ethernet header:
d62593
     # tc filter add dev ethX matchall action vlan pop_eth
d62593
d62593
    Push an Ethernet header (the original frame must have no MAC header):
d62593
     # tc filter add dev ethX matchall action vlan \
d62593
           push_eth dst_mac 0a:00:00:00:00:02 src_mac 0a:00:00:00:00:01
d62593
d62593
    Also add a test suite for m_vlan, which covers these new actions and
d62593
    the pre-existing ones.
d62593
d62593
    Signed-off-by: Guillaume Nault <gnault@redhat.com>
d62593
    Signed-off-by: David Ahern <dsahern@gmail.com>
d62593
---
d62593
 man/man8/tc-vlan.8        | 39 +++++++++++++++++-
d62593
 tc/m_vlan.c               | 69 +++++++++++++++++++++++++++++++
d62593
 testsuite/tests/tc/vlan.t | 86 +++++++++++++++++++++++++++++++++++++++
d62593
 3 files changed, 192 insertions(+), 2 deletions(-)
d62593
 create mode 100755 testsuite/tests/tc/vlan.t
d62593
d62593
diff --git a/man/man8/tc-vlan.8 b/man/man8/tc-vlan.8
d62593
index f5ffc25f..5c2808b1 100644
d62593
--- a/man/man8/tc-vlan.8
d62593
+++ b/man/man8/tc-vlan.8
d62593
@@ -5,8 +5,8 @@ vlan - vlan manipulation module
d62593
 .SH SYNOPSIS
d62593
 .in +8
d62593
 .ti -8
d62593
-.BR tc " ... " "action vlan" " { " pop " |"
d62593
-.IR PUSH " | " MODIFY " } [ " CONTROL " ]"
d62593
+.BR tc " ... " "action vlan" " { " pop " | " pop_eth " |"
d62593
+.IR PUSH " | " MODIFY " | " PUSH_ETH " } [ " CONTROL " ]"
d62593
 
d62593
 .ti -8
d62593
 .IR PUSH " := "
d62593
@@ -24,6 +24,11 @@ vlan - vlan manipulation module
d62593
 .IR VLANPRIO " ] "
d62593
 .BI id " VLANID"
d62593
 
d62593
+.ti -8
d62593
+.IR PUSH_ETH " := "
d62593
+.B push_eth
d62593
+.BI dst_mac " LLADDR " src_mac " LLADDR "
d62593
+
d62593
 .ti -8
d62593
 .IR CONTROL " := { "
d62593
 .BR reclassify " | " pipe " | " drop " | " continue " | " pass " | " goto " " chain " " CHAIN_INDEX " }"
d62593
@@ -43,6 +48,20 @@ modes require at least a
d62593
 and allow to optionally choose the
d62593
 .I VLANPROTO
d62593
 to use.
d62593
+
d62593
+The
d62593
+.B vlan
d62593
+action can also be used to add or remove the base Ethernet header. The
d62593
+.B pop_eth
d62593
+mode, which takes no argument, is used to remove the base Ethernet header. All
d62593
+existing VLANs must have been previously dropped. The opposite operation,
d62593
+adding a base Ethernet header, is done with the
d62593
+.B push_eth
d62593
+mode. In that case, the packet must have no MAC header (stacking MAC headers is
d62593
+not permitted). This mode is mostly useful when a previous action has
d62593
+encapsulated the whole original frame behind a network header and one needs
d62593
+to prepend an Ethernet header before forwarding the resulting packet.
d62593
+
d62593
 .SH OPTIONS
d62593
 .TP
d62593
 .B pop
d62593
@@ -58,6 +77,16 @@ Replace mode. Existing 802.1Q tag is replaced. Requires at least
d62593
 .B id
d62593
 option.
d62593
 .TP
d62593
+.B pop_eth
d62593
+Ethernet header decapsulation mode. Only works on a plain Ethernet header:
d62593
+VLANs, if any, must be removed first.
d62593
+.TP
d62593
+.B push_eth
d62593
+Ethernet header encapsulation mode. The Ethertype is automatically set
d62593
+using the network header type. Chaining Ethernet headers is not allowed: the
d62593
+packet must have no MAC header when using this mode. Requires the
d62593
+.BR "dst_mac " and " src_mac " options.
d62593
+.TP
d62593
 .BI id " VLANID"
d62593
 Specify the VLAN ID to encapsulate into.
d62593
 .I VLANID
d62593
@@ -73,6 +102,12 @@ Choose the VLAN protocol to use. At the time of writing, the kernel accepts only
d62593
 .BI priority " VLANPRIO"
d62593
 Choose the VLAN priority to use. Decimal number in range of 0-7.
d62593
 .TP
d62593
+.BI dst_mac " LLADDR"
d62593
+Choose the destination MAC address to use.
d62593
+.TP
d62593
+.BI src_mac " LLADDR"
d62593
+Choose the source MAC address to use.
d62593
+.TP
d62593
 .I CONTROL
d62593
 How to continue after executing this action.
d62593
 .RS
d62593
diff --git a/tc/m_vlan.c b/tc/m_vlan.c
d62593
index 1096ba0f..e6b21330 100644
d62593
--- a/tc/m_vlan.c
d62593
+++ b/tc/m_vlan.c
d62593
@@ -23,6 +23,8 @@ static const char * const action_names[] = {
d62593
 	[TCA_VLAN_ACT_POP] = "pop",
d62593
 	[TCA_VLAN_ACT_PUSH] = "push",
d62593
 	[TCA_VLAN_ACT_MODIFY] = "modify",
d62593
+	[TCA_VLAN_ACT_POP_ETH] = "pop_eth",
d62593
+	[TCA_VLAN_ACT_PUSH_ETH] = "push_eth",
d62593
 };
d62593
 
d62593
 static void explain(void)
d62593
@@ -31,6 +33,8 @@ static void explain(void)
d62593
 		"Usage: vlan pop\n"
d62593
 		"       vlan push [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"
d62593
 		"       vlan modify [ protocol VLANPROTO ] id VLANID [ priority VLANPRIO ] [CONTROL]\n"
d62593
+		"       vlan pop_eth [CONTROL]\n"
d62593
+		"       vlan push_eth dst_mac LLADDR src_mac LLADDR [CONTROL]\n"
d62593
 		"       VLANPROTO is one of 802.1Q or 802.1AD\n"
d62593
 		"            with default: 802.1Q\n"
d62593
 		"       CONTROL := reclassify | pipe | drop | continue | pass |\n"
d62593
@@ -63,6 +67,10 @@ static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
d62593
 	char **argv = *argv_p;
d62593
 	struct rtattr *tail;
d62593
 	int action = 0;
d62593
+	char dst_mac[ETH_ALEN] = {};
d62593
+	int dst_mac_set = 0;
d62593
+	char src_mac[ETH_ALEN] = {};
d62593
+	int src_mac_set = 0;
d62593
 	__u16 id;
d62593
 	int id_set = 0;
d62593
 	__u16 proto;
d62593
@@ -95,6 +103,18 @@ static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
d62593
 				return -1;
d62593
 			}
d62593
 			action = TCA_VLAN_ACT_MODIFY;
d62593
+		} else if (matches(*argv, "pop_eth") == 0) {
d62593
+			if (action) {
d62593
+				unexpected(*argv);
d62593
+				return -1;
d62593
+			}
d62593
+			action = TCA_VLAN_ACT_POP_ETH;
d62593
+		} else if (matches(*argv, "push_eth") == 0) {
d62593
+			if (action) {
d62593
+				unexpected(*argv);
d62593
+				return -1;
d62593
+			}
d62593
+			action = TCA_VLAN_ACT_PUSH_ETH;
d62593
 		} else if (matches(*argv, "id") == 0) {
d62593
 			if (!has_push_attribs(action))
d62593
 				invarg("only valid for push/modify", *argv);
d62593
@@ -119,6 +139,22 @@ static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
d62593
 			if (get_u8(&prio, *argv, 0) || (prio & ~0x7))
d62593
 				invarg("prio is invalid", *argv);
d62593
 			prio_set = 1;
d62593
+		} else if (matches(*argv, "dst_mac") == 0) {
d62593
+			if (action != TCA_VLAN_ACT_PUSH_ETH)
d62593
+				invarg("only valid for push_eth", *argv);
d62593
+
d62593
+			NEXT_ARG();
d62593
+			if (ll_addr_a2n(dst_mac, sizeof(dst_mac), *argv) < 0)
d62593
+				invarg("dst_mac is invalid", *argv);
d62593
+			dst_mac_set = 1;
d62593
+		} else if (matches(*argv, "src_mac") == 0) {
d62593
+			if (action != TCA_VLAN_ACT_PUSH_ETH)
d62593
+				invarg("only valid for push_eth", *argv);
d62593
+
d62593
+			NEXT_ARG();
d62593
+			if (ll_addr_a2n(src_mac, sizeof(src_mac), *argv) < 0)
d62593
+				invarg("src_mac is invalid", *argv);
d62593
+			src_mac_set = 1;
d62593
 		} else if (matches(*argv, "help") == 0) {
d62593
 			usage();
d62593
 		} else {
d62593
@@ -150,6 +186,20 @@ static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
d62593
 		return -1;
d62593
 	}
d62593
 
d62593
+	if (action == TCA_VLAN_ACT_PUSH_ETH) {
d62593
+		if (!dst_mac_set) {
d62593
+			fprintf(stderr, "dst_mac needs to be set for %s\n",
d62593
+				action_names[action]);
d62593
+			explain();
d62593
+			return -1;
d62593
+		} else if (!src_mac_set) {
d62593
+			fprintf(stderr, "src_mac needs to be set for %s\n",
d62593
+				action_names[action]);
d62593
+			explain();
d62593
+			return -1;
d62593
+		}
d62593
+	}
d62593
+
d62593
 	parm.v_action = action;
d62593
 	tail = addattr_nest(n, MAX_MSG, tca_id);
d62593
 	addattr_l(n, MAX_MSG, TCA_VLAN_PARMS, &parm, sizeof(parm));
d62593
@@ -167,6 +217,12 @@ static int parse_vlan(struct action_util *a, int *argc_p, char ***argv_p,
d62593
 	}
d62593
 	if (prio_set)
d62593
 		addattr8(n, MAX_MSG, TCA_VLAN_PUSH_VLAN_PRIORITY, prio);
d62593
+	if (dst_mac_set)
d62593
+		addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_ETH_DST, dst_mac,
d62593
+			  sizeof(dst_mac));
d62593
+	if (src_mac_set)
d62593
+		addattr_l(n, MAX_MSG, TCA_VLAN_PUSH_ETH_SRC, src_mac,
d62593
+			  sizeof(src_mac));
d62593
 
d62593
 	addattr_nest_end(n, tail);
d62593
 
d62593
@@ -216,6 +272,19 @@ static int print_vlan(struct action_util *au, FILE *f, struct rtattr *arg)
d62593
 			print_uint(PRINT_ANY, "priority", " priority %u", val);
d62593
 		}
d62593
 		break;
d62593
+	case TCA_VLAN_ACT_PUSH_ETH:
d62593
+		if (tb[TCA_VLAN_PUSH_ETH_DST] &&
d62593
+		    RTA_PAYLOAD(tb[TCA_VLAN_PUSH_ETH_DST]) == ETH_ALEN) {
d62593
+			ll_addr_n2a(RTA_DATA(tb[TCA_VLAN_PUSH_ETH_DST]),
d62593
+				    ETH_ALEN, 0, b1, sizeof(b1));
d62593
+			print_string(PRINT_ANY, "dst_mac", " dst_mac %s", b1);
d62593
+		}
d62593
+		if (tb[TCA_VLAN_PUSH_ETH_SRC &&
d62593
+		       RTA_PAYLOAD(tb[TCA_VLAN_PUSH_ETH_SRC]) == ETH_ALEN]) {
d62593
+			ll_addr_n2a(RTA_DATA(tb[TCA_VLAN_PUSH_ETH_SRC]),
d62593
+				    ETH_ALEN, 0, b1, sizeof(b1));
d62593
+			print_string(PRINT_ANY, "src_mac", " src_mac %s", b1);
d62593
+		}
d62593
 	}
d62593
 	print_action_control(f, " ", parm->action, "");
d62593
 
d62593
diff --git a/testsuite/tests/tc/vlan.t b/testsuite/tests/tc/vlan.t
d62593
new file mode 100755
d62593
index 00000000..b86dc364
d62593
--- /dev/null
d62593
+++ b/testsuite/tests/tc/vlan.t
d62593
@@ -0,0 +1,86 @@
d62593
+#!/bin/sh
d62593
+
d62593
+. lib/generic.sh
d62593
+
d62593
+DEV="$(rand_dev)"
d62593
+ts_ip "$0" "Add $DEV dummy interface" link add dev $DEV up type dummy
d62593
+ts_tc "$0" "Add ingress qdisc" qdisc add dev $DEV ingress
d62593
+
d62593
+reset_qdisc()
d62593
+{
d62593
+	ts_tc "$0" "Remove ingress qdisc" qdisc del dev $DEV ingress
d62593
+	ts_tc "$0" "Add ingress qdisc" qdisc add dev $DEV ingress
d62593
+}
d62593
+
d62593
+ts_tc "$0" "Add vlan action pop" \
d62593
+	filter add dev $DEV ingress matchall action vlan pop
d62593
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
d62593
+test_on "vlan"
d62593
+test_on "pop"
d62593
+test_on "pipe"
d62593
+
d62593
+reset_qdisc
d62593
+ts_tc "$0" "Add vlan action push (default parameters)" \
d62593
+	filter add dev $DEV ingress matchall action vlan push id 5
d62593
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
d62593
+test_on "vlan"
d62593
+test_on "push"
d62593
+test_on "id 5"
d62593
+test_on "protocol 802.1Q"
d62593
+test_on "priority 0"
d62593
+test_on "pipe"
d62593
+
d62593
+reset_qdisc
d62593
+ts_tc "$0" "Add vlan action push (explicit parameters)" \
d62593
+	filter add dev $DEV ingress matchall            \
d62593
+	action vlan push id 5 protocol 802.1ad priority 2
d62593
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
d62593
+test_on "vlan"
d62593
+test_on "push"
d62593
+test_on "id 5"
d62593
+test_on "protocol 802.1ad"
d62593
+test_on "priority 2"
d62593
+test_on "pipe"
d62593
+
d62593
+reset_qdisc
d62593
+ts_tc "$0" "Add vlan action modify (default parameters)" \
d62593
+	filter add dev $DEV ingress matchall action vlan modify id 5
d62593
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
d62593
+test_on "vlan"
d62593
+test_on "modify"
d62593
+test_on "id 5"
d62593
+test_on "protocol 802.1Q"
d62593
+test_on "priority 0"
d62593
+test_on "pipe"
d62593
+
d62593
+reset_qdisc
d62593
+ts_tc "$0" "Add vlan action modify (explicit parameters)" \
d62593
+	filter add dev $DEV ingress matchall              \
d62593
+	action vlan modify id 5 protocol 802.1ad priority 2
d62593
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
d62593
+test_on "vlan"
d62593
+test_on "modify"
d62593
+test_on "id 5"
d62593
+test_on "protocol 802.1ad"
d62593
+test_on "priority 2"
d62593
+test_on "pipe"
d62593
+
d62593
+reset_qdisc
d62593
+ts_tc "$0" "Add vlan action pop_eth" \
d62593
+	filter add dev $DEV ingress matchall action vlan pop_eth
d62593
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
d62593
+test_on "vlan"
d62593
+test_on "pop_eth"
d62593
+test_on "pipe"
d62593
+
d62593
+reset_qdisc
d62593
+ts_tc "$0" "Add vlan action push_eth"                  \
d62593
+	filter add dev $DEV ingress matchall           \
d62593
+	action vlan push_eth dst_mac 02:00:00:00:00:02 \
d62593
+	src_mac 02:00:00:00:00:01
d62593
+ts_tc "$0" "Show ingress filters" filter show dev $DEV ingress
d62593
+test_on "vlan"
d62593
+test_on "push_eth"
d62593
+test_on "dst_mac 02:00:00:00:00:02"
d62593
+test_on "src_mac 02:00:00:00:00:01"
d62593
+test_on "pipe"
d62593
-- 
d62593
2.29.2
d62593