From a4af4141f265c4fae5159e3a4c1698dd6055f868 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Mon, 16 Oct 2017 18:08:37 +0000 Subject: [PATCH 1/2] [interfaces]: Set hwaddr of VLAN interfaces to system MAC upon creation --- .../interfaces/interfaces-config.sh | 17 +++++++++++++++-- files/image_config/interfaces/interfaces.j2 | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/files/image_config/interfaces/interfaces-config.sh b/files/image_config/interfaces/interfaces-config.sh index 400b89a594c7..bd5111a01f73 100755 --- a/files/image_config/interfaces/interfaces-config.sh +++ b/files/image_config/interfaces/interfaces-config.sh @@ -1,6 +1,19 @@ #!/bin/bash -sonic-cfggen -d -t /usr/share/sonic/templates/interfaces.j2 >/etc/network/interfaces +SONIC_ASIC_TYPE=$(sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type) +SYSTEM_MAC_ADDRESS=$(ip link show eth0 | grep ether | awk '{print $2}') + +# Align last byte of MAC if necessary +if [ "$SONIC_ASIC_TYPE" == "mellanox" -o "$SONIC_ASIC_TYPE" == "centec" ]; then + last_byte=$(python -c "print '$SYSTEM_MAC_ADDRESS'[-2:]") + aligned_last_byte=$(python -c "print format(int(int('$last_byte', 16) & 0b11000000), '02x')") # put mask and take away the 0x prefix + SYSTEM_MAC_ADDRESS=$(python -c "print '$SYSTEM_MAC_ADDRESS'[:-2] + '$aligned_last_byte'") # put aligned byte into the end of MAC +fi + +sonic-cfggen -d -a '{"hwaddr":"'$SYSTEM_MAC_ADDRESS'"}' -t /usr/share/sonic/templates/interfaces.j2 > /etc/network/interfaces + [ -f /var/run/dhclient.eth0.pid ] && kill `cat /var/run/dhclient.eth0.pid` && rm -f /var/run/dhclient.eth0.pid -service networking restart + +systemctl restart networking + ifdown lo && ifup lo diff --git a/files/image_config/interfaces/interfaces.j2 b/files/image_config/interfaces/interfaces.j2 index 6521eb7a67e1..2fdd04804ca4 100644 --- a/files/image_config/interfaces/interfaces.j2 +++ b/files/image_config/interfaces/interfaces.j2 @@ -90,6 +90,7 @@ iface {{ member }} inet manual auto {{ name }} iface {{ name }} {{ 'inet' if prefix | ipv4 else 'inet6' }} static bridge_ports none + hwaddress ether {{ hwaddr }} address {{ prefix | ip }} netmask {{ prefix | netmask if prefix | ipv4 else prefix | prefixlen }} {% endfor %} From c3ff003e8fb88e417a79f9f70c70321a6b0cd289 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Mon, 16 Oct 2017 21:53:27 +0000 Subject: [PATCH 2/2] Incorporate change into sonic-config-engine interfaces.j2 test --- src/sonic-config-engine/tests/sample_output/interfaces | 1 + src/sonic-config-engine/tests/test_j2files.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sonic-config-engine/tests/sample_output/interfaces b/src/sonic-config-engine/tests/sample_output/interfaces index ec3b6f740bc4..89a23471a7ee 100644 --- a/src/sonic-config-engine/tests/sample_output/interfaces +++ b/src/sonic-config-engine/tests/sample_output/interfaces @@ -214,6 +214,7 @@ iface fortyGigE0/124 inet manual auto Vlan1000 iface Vlan1000 inet static bridge_ports none + hwaddress ether e4:1d:2d:a5:f3:ad address 192.168.0.1 netmask 255.255.255.224 # diff --git a/src/sonic-config-engine/tests/test_j2files.py b/src/sonic-config-engine/tests/test_j2files.py index 4bdc1b41f5b4..7e948f8b6a2a 100644 --- a/src/sonic-config-engine/tests/test_j2files.py +++ b/src/sonic-config-engine/tests/test_j2files.py @@ -20,7 +20,7 @@ def run_script(self, argument): def test_interfaces(self): interfaces_template = os.path.join(self.test_dir, '..', '..', '..', 'files', 'image_config', 'interfaces', 'interfaces.j2') - argument = '-m ' + self.t0_minigraph + ' -t ' + interfaces_template + ' > ' + self.output_file + argument = '-m ' + self.t0_minigraph + ' -a \'{\"hwaddr\":\"e4:1d:2d:a5:f3:ad\"}\' -t ' + interfaces_template + ' > ' + self.output_file self.run_script(argument) self.assertTrue(filecmp.cmp(os.path.join(self.test_dir, 'sample_output', 'interfaces'), self.output_file))