From 144baef71a64d21ac32ebbcf523ba10bf020a332 Mon Sep 17 00:00:00 2001
From: Cheeze_It <none@none.com>
Date: Sun, 29 Nov 2020 14:02:08 -0700
Subject: mpls-conf: T915: Add ethernet vif MPLS enable

In this commit we added ethernet sub interface MPLS enablement. Per request by
@bbs2web, this functionality is now possible. This should now allow ethernet
switched networks with VLAN tags to also allow for MPLS packet flow.
---
 src/conf_mode/protocols_mpls.py | 36 ++++++++++++++++++++++--------------
 1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/src/conf_mode/protocols_mpls.py b/src/conf_mode/protocols_mpls.py
index 791b18110..3b27608da 100755
--- a/src/conf_mode/protocols_mpls.py
+++ b/src/conf_mode/protocols_mpls.py
@@ -18,11 +18,13 @@ import os
 
 from sys import exit
 
+from glob import glob
 from vyos.config import Config
 from vyos.configdict import node_changed
 from vyos.template import render_to_string
 from vyos.util import call
 from vyos.util import dict_search
+from vyos.util import read_file
 from vyos import ConfigError
 from vyos import frr
 from vyos import airbag
@@ -118,22 +120,28 @@ def apply(mpls):
     # Enable and disable MPLS processing on interfaces per configuration
     if 'interface' in mpls:
         system_interfaces = []
-        system_interfaces.append(((os.popen('sysctl net.mpls.conf').read()).split('\n')))
-        del system_interfaces[0][-1]
-        for configured_interface in mpls['interface']:
-            for system_interface in system_interfaces[0]:
-                if configured_interface in system_interface:
-                    call(f'sysctl -wq net.mpls.conf.{configured_interface}.input=1')
-                elif system_interface.endswith(' = 1'):
-                    system_interface = system_interface.replace(' = 1', '=0')
-                    call(f'sysctl -wq {system_interface}')
+        # Populate system interfaces list with local MPLS capable interfaces
+        for interface in glob('/proc/sys/net/mpls/conf/*'):
+            system_interfaces.append(os.path.basename(interface))   
+        # This is where the comparison is done on if an interface needs to be enabled/disabled.
+        for system_interface in system_interfaces:
+            interface_state = read_file(f'/proc/sys/net/mpls/conf/{system_interface}/input')
+            if '1' in interface_state:
+                if system_interface not in mpls['interface']:
+                    system_interface = system_interface.replace('.', '/')
+                    call(f'sysctl -wq net.mpls.conf.{system_interface}.input=0')
+            elif '0' in interface_state:
+                if system_interface in mpls['interface']:
+                    system_interface = system_interface.replace('.', '/')
+                    call(f'sysctl -wq net.mpls.conf.{system_interface}.input=1')
     else:
-        # If MPLS interfaces are not configured, set MPLS processing disabled
         system_interfaces = []
-        system_interfaces.append(((os.popen('sysctl net.mpls.conf').read()).replace(" = 1", "=0")).split('\n'))
-        del system_interfaces[0][-1]
-        for interface in (system_interfaces[0]):
-            call(f'sysctl -wq {interface}')
+        # If MPLS interfaces are not configured, set MPLS processing disabled
+        for interface in glob('/proc/sys/net/mpls/conf/*'):
+            system_interfaces.append(os.path.basename(interface)) 
+        for system_interface in system_interfaces:
+            system_interface = system_interface.replace('.', '/')
+            call(f'sysctl -wq net.mpls.conf.{system_interface}.input=0')
 
     return None
 
-- 
cgit v1.2.3