From 2c51bb0705f1190716e1a19f11786cc549fac039 Mon Sep 17 00:00:00 2001 From: "Joshua M. Boniface" Date: Sat, 9 Oct 2021 19:01:45 -0400 Subject: [PATCH] Move MTU validation to function Prevents code duplication and ensures validation runs when an MTU is updated, not just on network creation. --- .../pvcnoded/objects/VXNetworkInstance.py | 104 ++++++++---------- 1 file changed, 44 insertions(+), 60 deletions(-) diff --git a/node-daemon/pvcnoded/objects/VXNetworkInstance.py b/node-daemon/pvcnoded/objects/VXNetworkInstance.py index 0a985c98..1391a85d 100644 --- a/node-daemon/pvcnoded/objects/VXNetworkInstance.py +++ b/node-daemon/pvcnoded/objects/VXNetworkInstance.py @@ -83,36 +83,7 @@ class VXNetworkInstance(object): except Exception: self.vx_mtu = None - update_mtu = False - - # Explicitly set the MTU to max_mtu if unset (in Zookeeper too assuming the key exists) - if self.vx_mtu == '' or self.vx_mtu is None: - self.logger.out( - 'MTU not specified; setting to maximum MTU {} instead'.format(self.max_mtu), - prefix='VNI {}'.format(self.vni), - state='w' - ) - self.vx_mtu = self.max_mtu - update_mtu = True - self.vx_mtu = int(self.vx_mtu) - # Ensure the MTU is valid - if self.vx_mtu > self.max_mtu: - self.logger.out( - 'MTU {} is larger than maximum MTU {}; setting to maximum MTU instead'.format(self.vx_mtu, self.max_mtu), - prefix='VNI {}'.format(self.vni), - state='w' - ) - self.vx_mtu = self.max_mtu - update_mtu = True - - if update_mtu: - # Try block for migration purposes - try: - self.zkhandler.write([ - (('network.mtu', self.vni), self.vx_mtu) - ]) - except Exception: - pass + self.validateNetworkMTU() # Zookeper handlers for changed states @self.zkhandler.zk_conn.DataWatch(self.zkhandler.schema.path('network', self.vni)) @@ -137,6 +108,7 @@ class VXNetworkInstance(object): if data and self.vx_mtu != data.decode('ascii'): self.vx_mtu = data.decode('ascii') + self.validateNetworkMTU() self.updateNetworkMTU() except Exception: pass @@ -165,36 +137,7 @@ class VXNetworkInstance(object): except Exception: self.vx_mtu = None - update_mtu = False - - # Explicitly set the MTU to max_mtu if unset (in Zookeeper too assuming the key exists) - if self.vx_mtu == '' or self.vx_mtu is None: - self.logger.out( - 'MTU not specified; setting to maximum MTU {} instead'.format(self.max_mtu), - prefix='VNI {}'.format(self.vni), - state='w' - ) - self.vx_mtu = self.max_mtu - update_mtu = True - self.vx_mtu = int(self.vx_mtu) - # Ensure the MTU is valid - if self.vx_mtu > self.max_mtu: - self.logger.out( - 'MTU {} is larger than maximum MTU {}; setting to maximum MTU instead'.format(self.vx_mtu, self.max_mtu), - prefix='VNI {}'.format(self.vni), - state='w' - ) - self.vx_mtu = self.max_mtu - update_mtu = True - - if update_mtu: - # Try block for migration purposes - try: - self.zkhandler.write([ - (('network.mtu', self.vni), self.vx_mtu) - ]) - except Exception: - pass + self.validateNetworkMTU() self.nftables_netconf_filename = '{}/networks/{}.nft'.format(self.config['nft_dynamic_directory'], self.vni) self.firewall_rules = [] @@ -311,6 +254,7 @@ add rule inet filter forward ip6 saddr {netaddr6} counter jump {vxlannic}-out if data and self.vx_mtu != data.decode('ascii'): self.vx_mtu = data.decode('ascii') + self.validateNetworkMTU() self.updateNetworkMTU() except Exception: pass @@ -489,6 +433,46 @@ add rule inet filter forward ip6 saddr {netaddr6} counter jump {vxlannic}-out def getvni(self): return self.vni + def validateNetworkMTU(self): + update_mtu = False + + # Explicitly set the MTU to max_mtu if unset (in Zookeeper too assuming the key exists) + if self.vx_mtu == '' or self.vx_mtu is None: + self.logger.out( + 'MTU not specified; setting to maximum MTU {} instead'.format(self.max_mtu), + prefix='VNI {}'.format(self.vni), + state='w' + ) + self.vx_mtu = self.max_mtu + update_mtu = True + + # Set MTU to an integer (if it's not) + if not type(self.vx_mtu, int): + self.vx_mtu = int(self.vx_mtu) + + # Ensure the MTU is valid + if self.vx_mtu > self.max_mtu: + self.logger.out( + 'MTU {} is larger than maximum MTU {}; setting to maximum MTU instead'.format(self.vx_mtu, self.max_mtu), + prefix='VNI {}'.format(self.vni), + state='w' + ) + self.vx_mtu = self.max_mtu + update_mtu = True + + if update_mtu: + # Try block for migration purposes + try: + self.zkhandler.write([ + (('network.mtu', self.vni), self.vx_mtu) + ]) + except Exception as e: + self.logger.out( + 'Could not update MTU in Zookeeper: {}'.format(e), + prefix='VNI {}'.format(self.vni), + state='w' + ) + def updateNetworkMTU(self): self.logger.out( 'Setting network MTU to {}'.format(self.vx_mtu),