Skip to content

Commit

Permalink
[interfaces]: Set hwaddr of VLAN interfaces to system MAC upon creati…
Browse files Browse the repository at this point in the history
…on (#1042)
  • Loading branch information
jleveque authored Oct 17, 2017
1 parent 463c5e4 commit 37dc7bd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
17 changes: 15 additions & 2 deletions files/image_config/interfaces/interfaces-config.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions files/image_config/interfaces/interfaces.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
1 change: 1 addition & 0 deletions src/sonic-config-engine/tests/sample_output/interfaces
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
2 changes: 1 addition & 1 deletion src/sonic-config-engine/tests/test_j2files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 37dc7bd

Please sign in to comment.