From 857ee5b0aa64c8550400dac8636de445814d3414 Mon Sep 17 00:00:00 2001
From: Rahul <rahul.thakur@iopsys.eu>
Date: Tue, 5 May 2020 13:46:10 +0530
Subject: [PATCH] vlanconf: support vlan translation

Update to support vlan translation, all deployments pass.
Also, manual verification that p bit is not overwritten in case
LAN traffic is bridged to WAN, infact even in case of vlan
manipulation, only vlan id is overwritten.

This is good to be merged now and it will work for all VLAN
QoS scenarios as well!
---
 iopsys-brcm63xx-arm/base-files/sbin/vlanconf | 52 ++++++++++++++------
 1 file changed, 37 insertions(+), 15 deletions(-)

diff --git a/iopsys-brcm63xx-arm/base-files/sbin/vlanconf b/iopsys-brcm63xx-arm/base-files/sbin/vlanconf
index 66b1a42db..90f7e64df 100755
--- a/iopsys-brcm63xx-arm/base-files/sbin/vlanconf
+++ b/iopsys-brcm63xx-arm/base-files/sbin/vlanconf
@@ -11,6 +11,7 @@ MACADDR=""
 TYPE="8021q"
 PRIORITY=0
 VLANID=1
+TVLANID=0
 ROUTED=1
 BRIDGED=0
 UNMANAGED=0
@@ -242,13 +243,12 @@ create_vlan() {
 			vlanCtl_cmdPushVlanTag
 			# If type is 8021ad, push inner tag.
 			[ $TYPE == "8021ad" ] && vlanCtl_cmdPushVlanTag
+			# Set pbits and vid in tag number 0, which is always the outer tag of the frame. #
+			TAGINDEX=0
+			vlanCtl_cmdSetTagVid $VLANID $TAGINDEX
+			vlanCtl_cmdSetTagPbits $PRIORITY $TAGINDEX
 		fi
 
-		# Set pbits and vid in tag number 0, which is always the outer tag of the frame. #
-		TAGINDEX=0
-		vlanCtl_cmdSetTagVid $VLANID $TAGINDEX
-		vlanCtl_cmdSetTagPbits $PRIORITY $TAGINDEX
-
 		if [ $TYPE == "8021ad" ]; then
 			# If type is 8021ad, push inner tag.
 			TAGINDEX=1
@@ -267,6 +267,15 @@ create_vlan() {
 		TAGRULEID="-1"
 		vlanCtl_insertTagRule $IFNAME $DIRECTION $TAGS $POSITION $TAGRULEID
 
+		# If translation is enabled on this interface, modify outgoing VLAN ID
+		if [ $TVLANID -ne 0 ]; then
+			TAGINDEX=0
+			vlanCtl_cmdSetTagVid $VLANID $TAGINDEX
+			vlanCtl_cmdSetTagPbits $PRIORITY $TAGINDEX
+			TAGS=1
+			vlanCtl_insertTagRule $IFNAME $DIRECTION $TAGS $POSITION $TAGRULEID
+		fi
+
 		# ======== Set rx rules ======== #
 
 		# Note: Always set bridge interface rx rules at the bottom of the tables
@@ -287,7 +296,13 @@ create_vlan() {
 			TAGINDEX=1
 			[ $TYPE == "8021ad" ] && vlanCtl_filterOnTagVid $INNER_VLANID $TAGINDEX
 
-			# Do not pop the vlan tag in case of bridged interface
+			# If VLAN translated vlan id is configured, then on ingress,
+		        # write the translated vlan id into the header	
+			if [ $TVLANID -ne 0 ]; then
+				TAGINDEX=0
+				vlanCtl_cmdSetTagVid $TVLANID $TAGINDEX
+
+			fi
 
 			# Append this rule to the bottom of rx tag rule table-1 and table-2
 			# using VLANCTL_POSITION_APPEND.
@@ -361,14 +376,12 @@ create_vlan() {
 
 		# Set rule to the top of tx tag rule table-0, table-1 and table-2.
 		DIRECTION="tx"
-		TAGS=0
+		TAGS=1
 		POSITION="rule-insert-before"
 		TAGRULEID="-1"
 		vlanCtl_insertTagRule $IFNAME $DIRECTION $TAGS $POSITION $TAGRULEID
 
 		if [ $BRIDGED -eq 1 ]; then
-			TAGS=1
-			vlanCtl_insertTagRule $IFNAME $DIRECTION $TAGS $POSITION $TAGRULEID
 			TAGS=2
 			vlanCtl_insertTagRule $IFNAME $DIRECTION $TAGS $POSITION $TAGRULEID
 		fi
@@ -381,17 +394,25 @@ create_vlan() {
 		vlanCtl_setReceiveVlanDevice $NAME
 
 		if [ $BRIDGED -eq 1 ]; then
-			# Insert the vlan header into the frame and forward
-			vlanCtl_cmdPushVlanTag
-			TAGINDEX=0
-			vlanCtl_cmdSetTagVid $VLANID $TAGINDEX
-			vlanCtl_cmdSetTagPbits $PRIORITY $TAGINDEX
-			# Set rule to the top of tx tag rule table-0, table-1 and table-2. #
+			# Insert the vlan header into the frame and forward if its
+			# a L2 bridge else, unconditionally forward to the rx vlan
+			# interface
+			if [ $UNMANAGED -eq 1 ]; then
+				vlanCtl_cmdPushVlanTag
+				TAGINDEX=0
+				vlanCtl_cmdSetTagVid $VLANID $TAGINDEX
+				vlanCtl_cmdSetTagPbits $PRIORITY $TAGINDEX
+			fi
+
 			DIRECTION="rx"
 			TAGS=0
 			POSITION="rule-insert-last"
 			TAGRULEID=""
 			vlanCtl_insertTagRule $IFNAME $DIRECTION $TAGS $POSITION $TAGRULEID
+			TAGS=1
+			vlanCtl_insertTagRule $IFNAME $DIRECTION $TAGS $POSITION $TAGRULEID
+			TAGS=2
+			vlanCtl_insertTagRule $IFNAME $DIRECTION $TAGS $POSITION $TAGRULEID
 		else
 			# Filter on receive interface and not allow for multicast.
 			# If hit, drop the frame.
@@ -517,6 +538,7 @@ while [ -n "$2" ]; do
 		-t|--type) TYPE="$3"; shift;;
 		-p|--priority) PRIORITY="$3"; shift;;
 		-v|--vlan-id) VLANID="$3"; shift;;
+		--translated-vlan-id) TVLANID="$3"; shift;;
 		--inner-vlan-id) INNER_VLANID="$3"; shift;;
 		--inner-priority) INNER_PRIORITY="$3"; shift;;
 		--dscp) DSCP="$3"; shift;;
-- 
GitLab