[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
This commit is contained in:
Kebo Liu 2019-06-25 13:56:35 +08:00 committed by liat-grozovik
parent 9a1bebe496
commit 331866dbe3

View File

@ -2,6 +2,7 @@
import os import os
import yaml import yaml
import subprocess import subprocess
import re
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
@ -44,10 +45,26 @@ def get_sonic_version_info():
data = yaml.load(stream) data = yaml.load(stream)
return data 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(): def get_system_mac():
version_info = get_sonic_version_info() version_info = get_sonic_version_info()
if (version_info['asic_type'] == 'mellanox'): 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" get_mac_cmd = "sudo decode-syseeprom -m"
else: else:
get_mac_cmd = "ip link show eth0 | grep ether | awk '{print $2}'" get_mac_cmd = "ip link show eth0 | grep ether | awk '{print $2}'"
@ -59,6 +76,9 @@ def get_system_mac():
mac = mac.strip() mac = mac.strip()
if not valid_mac_address(mac):
return None
# Align last byte of MAC if necessary # Align last byte of MAC if necessary
if version_info and version_info['asic_type'] == 'centec': if version_info and version_info['asic_type'] == 'centec':
last_byte = mac[-2:] last_byte = mac[-2:]