From 331866dbe3f4af4716f81adda152b5ed9b1a1dbf Mon Sep 17 00:00:00 2001 From: Kebo Liu Date: Tue, 25 Jun 2019 13:56:35 +0800 Subject: [PATCH] [sonic-cfggen] Add Mellanox platform specific code to read base mac from machine.conf (#2991) * add code to read base mac from machine.conf * rewording the comments * add mac validation with re * fix review comments * remove empty line --- src/sonic-config-engine/sonic_device_util.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/sonic-config-engine/sonic_device_util.py b/src/sonic-config-engine/sonic_device_util.py index ac03ca818d..f9c200eebd 100644 --- a/src/sonic-config-engine/sonic_device_util.py +++ b/src/sonic-config-engine/sonic_device_util.py @@ -2,6 +2,7 @@ import os import yaml import subprocess +import re DOCUMENTATION = ''' --- @@ -44,10 +45,26 @@ def get_sonic_version_info(): data = yaml.load(stream) return data +def valid_mac_address(mac): + return bool(re.match("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", mac)) + def get_system_mac(): version_info = get_sonic_version_info() if (version_info['asic_type'] == 'mellanox'): + # With Mellanox ONIE release(2019.05-5.2.0012) and above + # "onie_base_mac" was added to /host/machine.conf: + # onie_base_mac=e4:1d:2d:44:5e:80 + # So we have another way to get the mac address besides decode syseeprom + # By this can mitigate the dependency on the hw-management service + base_mac_key = "onie_base_mac" + machine_vars = get_machine_info() + if machine_vars is not None and base_mac_key in machine_vars: + mac = machine_vars[base_mac_key] + mac = mac.strip() + if valid_mac_address(mac): + return mac + get_mac_cmd = "sudo decode-syseeprom -m" else: get_mac_cmd = "ip link show eth0 | grep ether | awk '{print $2}'" @@ -59,6 +76,9 @@ def get_system_mac(): mac = mac.strip() + if not valid_mac_address(mac): + return None + # Align last byte of MAC if necessary if version_info and version_info['asic_type'] == 'centec': last_byte = mac[-2:]