naccyde / rpms / iproute

Forked from rpms/iproute 7 months ago
Clone

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

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