Skip to content

Commit 331866d

Browse files
keboliuliat-grozovik
authored andcommitted
[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
1 parent 9a1bebe commit 331866d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/sonic-config-engine/sonic_device_util.py

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import yaml
44
import subprocess
5+
import re
56

67
DOCUMENTATION = '''
78
---
@@ -44,10 +45,26 @@ def get_sonic_version_info():
4445
data = yaml.load(stream)
4546
return data
4647

48+
def valid_mac_address(mac):
49+
return bool(re.match("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", mac))
50+
4751
def get_system_mac():
4852
version_info = get_sonic_version_info()
4953

5054
if (version_info['asic_type'] == 'mellanox'):
55+
# With Mellanox ONIE release(2019.05-5.2.0012) and above
56+
# "onie_base_mac" was added to /host/machine.conf:
57+
# onie_base_mac=e4:1d:2d:44:5e:80
58+
# So we have another way to get the mac address besides decode syseeprom
59+
# By this can mitigate the dependency on the hw-management service
60+
base_mac_key = "onie_base_mac"
61+
machine_vars = get_machine_info()
62+
if machine_vars is not None and base_mac_key in machine_vars:
63+
mac = machine_vars[base_mac_key]
64+
mac = mac.strip()
65+
if valid_mac_address(mac):
66+
return mac
67+
5168
get_mac_cmd = "sudo decode-syseeprom -m"
5269
else:
5370
get_mac_cmd = "ip link show eth0 | grep ether | awk '{print $2}'"
@@ -59,6 +76,9 @@ def get_system_mac():
5976

6077
mac = mac.strip()
6178

79+
if not valid_mac_address(mac):
80+
return None
81+
6282
# Align last byte of MAC if necessary
6383
if version_info and version_info['asic_type'] == 'centec':
6484
last_byte = mac[-2:]

0 commit comments

Comments
 (0)