How best to set management interface (eth0) MAC address

414 views
Skip to first unread message

Dennis Flynn

unread,
Feb 19, 2020, 10:47:13 AM2/19/20
to sonicproject
Hi,

I am working on changes to add a new Sonic platform.
I am trying to follow the guidelines set forth by the Platform API 2.0 as I understand it
Initially I have code working to provide access to the ONIE syseeprom.

My specific question is about how the ONIE "Base MAC Address" field within the ONIE syseeprom is treated by Sonic.
The HW platform in question was delivered with a base MAC address correctly programmed in the ONIE syseeprom.
Note that in my case the HW platform CPU SOC includes an embedded Ethernet Controller having its own dedicated eeprom.
The MAC address programmed within this internal eeprom has not been programmed - hence all instances have same default MAC address.

My thinking was that somewhere within Sonic init I would find logic that ensures that the eth0 interface is configured with the base MAC address from the ONIE syseeprom. Perhaps by customizing /etc/network/interfaces? I do see logic that reads the ONIE syseeprom base MAC address and stores that away within the Config DB DEVICE_METADATA section. But running 'ifconfig eth0' from the Sonic CLI shows that the MAC address is set to the default address within the Ethernet Controller eeprom.

So I'm wondering if there is an established convention for setting the eth0 MAC address?
I can generate an ONIE image to set the eth0 MAC address in the Ethernet Controller eeprom if preferred.
Alternatively I could use ethtool to set that value during platform initialization.

Suggestions welcome.

Best,
Dennis

P.S. Is there a formal Spec. or user guide for the Platform 2.0 API?
The Porting Guide still talks about the earlier version of the API.


Selva

unread,
Oct 5, 2021, 3:34:51 PM10/5/21
to sonicproject
Hi
May I know if this issue still exist or fixed already in the latest code base? I am having a switch running 201911 SONiC that still showing this issue. I hope somebody should have fixed this by now.  Looking for cherry pick the fix if already available.

@Dennis .. what do you mean by "I can generate an ONIE image to set the eth0 MAC address in the Ethernet Controller eeprom if preferred"
Are you saying you can modify SONiC code to assign eth0's MAC address to  EEPROM ? 

Thank you,
Selva.

Christian Svensson

unread,
Oct 5, 2021, 4:32:41 PM10/5/21
to Selva, sonicproject
For what it is worth, we set eth0 management MAC address by creating a file called /etc/network/interfaces.d/eth0-mac with the following contents:
auto eth0
iface eth0 inet manual
  pre-up /sbin/ip link set dev eth0 addr "{{ mgmt_mac_address }}"

We use Ansible to set up and configure our SONiC switches with the base configuration we use.

--
You received this message because you are subscribed to the Google Groups "sonicproject" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonicproject...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonicproject/791e0f9f-fd32-4c6d-b107-41df01594be0n%40googlegroups.com.

Selva

unread,
Oct 5, 2021, 6:00:29 PM10/5/21
to sonicproject
Do you not see /etc/network/interfaces.d/eth0-mac gets removed after an image upgrade ?

Probably, you should have already noticed the front panel interfaces also show the same problem. They all being assigned with the same random MAC address. Do you run similar fix for the front panel interfaces as well?  

Christian Svensson

unread,
Oct 5, 2021, 6:07:04 PM10/5/21
to Selva, sonicproject
Hi,

Do you not see /etc/network/interfaces.d/eth0-mac gets removed after an image upgrade ?

Yes, but we run the Ansible playbook after each upgrade to combat this.

Probably, you should have already noticed the front panel interfaces also show the same problem. They all being assigned with the same random MAC address. Do you run similar fix for the front panel interfaces as well?  

For front interfaces we use the SONiC JSON configuration to set the MAC addresses where needed;
    "INTERFACE": {
[..]
        "Ethernet124": {
            "mac_addr": "76:c0:f8:4b:c8:5d",
            "vrf_name": "VrfA"
        },
        "Ethernet92": {
            "mac_addr": "76:c0:f8:4b:c8:5c",
            "vrf_name": "VrfB"
        },
[..]

Like so.

Selva

unread,
Oct 5, 2021, 7:01:07 PM10/5/21
to sonicproject
Thanks for sharing those details. They are indeed cool if that helps you going.
Unfortunately, those don't work for my case. Instead, I would indeed want to fix SONiC to behave good.
I don't know if I have proper understanding about SONiC to fix that yet :-) 

thanks,
Selva

Albert Akchurin

unread,
Dec 18, 2022, 12:47:09 PM12/18/22
to sonicproject
I wrote a script that creates systemd unit that adds 'hwaddress ether YOUR_MAC' to /etc/network/interfaces on each boot. So you only need to run the scrip once:

-------------------------------
mgmt_mac=00:11:22:33:44:55

cat > /etc/systemd/system/set-eth0-mac.service <<EOF
[Unit]
Description=Set eth0 mac address to $mgmt_mac
DefaultDependencies=no
Wants=local-fs.target network-pre.target
Before=network-pre.target
After=local-fs.target

[Service]
Type=oneshot
ExecStart=sed -ri '/^iface eth0 inet dhcp/,/^\S/{/^\s+hwaddress/d};/^iface eth0 inet dhcp/a\    hwaddress ether $mgmt_mac' /etc/network/interfaces /usr/share/sonic/templates/interfaces.j2

[Install]
WantedBy=basic.target
EOF
systemctl enable --now set-eth0-mac
-------------------------------

Note, that we also have to patch the template (/usr/share/sonic/templates/interfaces.j2) from which the file is generated, because the generation from the template is also happens each boot.
The provided service executed earlier and has its effect when the network initialises, but later during the boot it is regenerated by SONiC from the template.
Therefore for the consistency reasons I also patch the template.
Reply all
Reply to author
Forward
0 new messages