From c7c60e4ced5e611631eaa311f4df8b58a2208301 Mon Sep 17 00:00:00 2001 From: Adrien Banlin Date: Fri, 8 Apr 2022 14:28:02 +0200 Subject: [PATCH 1/3] policymanager: add a get_driver_name from ifname --- ifupdown2/ifupdown/policymanager.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ifupdown2/ifupdown/policymanager.py b/ifupdown2/ifupdown/policymanager.py index 9825ff1c..51ee9985 100644 --- a/ifupdown2/ifupdown/policymanager.py +++ b/ifupdown2/ifupdown/policymanager.py @@ -28,9 +28,11 @@ ) ''' +import os import json import glob import logging +from contextlib import suppress class policymanager(): @@ -148,6 +150,13 @@ def get_iface_default(self,module_name=None,ifname=None,attr=None): # could not find any system or user default so return Non return val + def _get_driver_name(self, ifname): + ''' get_driver_name: get the driver name from an interface name ''' + with suppress(FileNotFoundError): + symlink = os.readlink(f'/sys/class/net/{ifname}/device/driver/module') + return os.path.basename(symlink) + return None + def get_attr_default(self,module_name=None,attr=None): ''' get_attr_default: Addon modules must use one of two types of access methods to From e8410476d0e418e197e415bf36c0f6cc1c7ef5c2 Mon Sep 17 00:00:00 2001 From: Adrien Banlin Date: Fri, 8 Apr 2022 14:28:36 +0200 Subject: [PATCH 2/3] policymanager: add get_driver_default method This new methods works like the get_attr_default to takes defaults values from the driver name only. --- ifupdown2/ifupdown/policymanager.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/ifupdown2/ifupdown/policymanager.py b/ifupdown2/ifupdown/policymanager.py index 51ee9985..0568bf2b 100644 --- a/ifupdown2/ifupdown/policymanager.py +++ b/ifupdown2/ifupdown/policymanager.py @@ -157,6 +157,31 @@ def _get_driver_name(self, ifname): return os.path.basename(symlink) return None + def get_driver_default(self, module_name=None, ifname=None, attr=None): + ''' + get_driver_default: Addon modules must use one types of access methods to + the default configs. In this method, we expect the default to be in + + [module]['driver_defaults'][driver_name][attr] + + We first check the user_policy_array and return that value. But if + the user did not specify an override, we use the system_policy_array. + ''' + driname = self._get_driver_name(ifname) + # make sure we have an index + if None in (attr, module_name, driname): + return None + + # users can specify defaults to override the systemwide settings + # look for user specific driver attribute driver_defaults first + with suppress(TypeError, KeyError, IndexError): + # looks for user specified value + return self.user_policy_array[module_name]['driver_defaults'][driname][attr] + with suppress(TypeError, KeyError, IndexError): + # failing that, look for system setting + return self.system_policy_array[module_name]['driver_defaults'][driname][attr] + return None + def get_attr_default(self,module_name=None,attr=None): ''' get_attr_default: Addon modules must use one of two types of access methods to From 64aeef5b9129cde8c68136c74646991ea811e197 Mon Sep 17 00:00:00 2001 From: Adrien Banlin Date: Fri, 8 Apr 2022 14:30:43 +0200 Subject: [PATCH 3/3] policymanager: update get_iface_default This update is meant to check if there is a driver default policy in between ifname and default policies. --- ifupdown2/ifupdown/policymanager.py | 45 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/ifupdown2/ifupdown/policymanager.py b/ifupdown2/ifupdown/policymanager.py index 0568bf2b..bf664a14 100644 --- a/ifupdown2/ifupdown/policymanager.py +++ b/ifupdown2/ifupdown/policymanager.py @@ -118,37 +118,36 @@ def get_iface_default(self,module_name=None,ifname=None,attr=None): # make sure we have an index if (not ifname or not attr or not module_name): return None + driname = self._get_driver_name(ifname) - val = None # users can specify defaults to override the systemwide settings # look for user specific interface attribute iface_defaults first - try: - # looks for user specified value - val = self.user_policy_array[module_name]['iface_defaults'][ifname][attr] - return val - except (TypeError, KeyError, IndexError): - pass - try: + with suppress(TypeError, KeyError, IndexError): + # looks for user interface name specified value + return self.user_policy_array[module_name]['iface_defaults'][ifname][attr] + + with suppress(TypeError, KeyError, IndexError): + # failing that, there may be a user driver default specified value + return self.user_policy_array[module_name]['driver_defaults'][driname][attr] + + with suppress(TypeError, KeyError, IndexError): # failing that, there may be a user default for all intefaces - val = self.user_policy_array[module_name]['defaults'][attr] - return val - except (TypeError, KeyError, IndexError): - pass - try: + return self.user_policy_array[module_name]['defaults'][attr] + + with suppress(TypeError, KeyError, IndexError): # failing that, look for system setting for the interface - val = self.system_policy_array[module_name]['iface_defaults'][ifname][attr] - return val - except (TypeError, KeyError, IndexError): - pass - try: + return self.system_policy_array[module_name]['iface_defaults'][ifname][attr] + + with suppress(TypeError, KeyError, IndexError): + # failing that, look for system setting for the driver + return self.system_policy_array[module_name]['driver_defaults'][driname][attr] + + with suppress(TypeError, KeyError, IndexError): # failing that, look for system setting for all interfaces - val = self.system_policy_array[module_name]['defaults'][attr] - return val - except (TypeError, KeyError, IndexError): - pass + return self.system_policy_array[module_name]['defaults'][attr] # could not find any system or user default so return Non - return val + return None def _get_driver_name(self, ifname): ''' get_driver_name: get the driver name from an interface name '''