Skip to content

Commit

Permalink
Add support for generating interface configuration in /etc/network/in…
Browse files Browse the repository at this point in the history
…terfaces for multiple management interfaces (#11204)

* [Interfaces] Modify template to support multiple management interfaces

* Modify minigraph to process interfaces in sorted order

Signed-off-by: Ubuntu <gechen@gechen-sonic-dev.d0r25nej54guppclip4gpy5b5a.jx.internal.cloudapp.net>

* Add UT minigraph

Signed-off-by: Ubuntu <gechen@gechen-sonic-dev.d0r25nej54guppclip4gpy5b5a.jx.internal.cloudapp.net>

* make case insensitve comparison

Signed-off-by: George Chen <gechen@microsoft.com>

* Use natural sort

Signed-off-by: George Chen <gechen@microsoft.com>

Co-authored-by: Ubuntu <gechen@gechen-sonic-dev.d0r25nej54guppclip4gpy5b5a.jx.internal.cloudapp.net>
  • Loading branch information
geogchen and Ubuntu authored Jun 21, 2022
1 parent 0ae4903 commit 90a849e
Show file tree
Hide file tree
Showing 6 changed files with 1,132 additions and 12 deletions.
30 changes: 19 additions & 11 deletions files/image_config/interfaces/interfaces.j2
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,27 @@ iface lo inet loopback
{% block mgmt_interface %}

# The management network interface
auto eth0
{% if MGMT_INTERFACE %}
{% for (name, prefix) in MGMT_INTERFACE|pfx_filter|unique(attribute=0) %}
auto {{ name }}
{% endfor %}
{% endif %}
{% if (ZTP_DHCP_DISABLED is not defined) and (ZTP is defined) and (ZTP['mode'] is defined and ZTP['mode']['profile'] == 'active') %}


# ZTP out-of-band interface
allow-hotplug eth0
{% if MGMT_INTERFACE %}
{% for (name, prefix) in MGMT_INTERFACE|pfx_filter|unique(attribute=0) %}
allow-hotplug {{ name }}
{% if ZTP['mode']['ipv4'] == 'true' %}
iface eth0 inet dhcp
iface {{ name }} inet dhcp
{% endif %}
{% if ZTP['mode']['ipv6'] == 'true' %}
iface eth0 inet6 dhcp
up sysctl net.ipv6.conf.eth0.accept_ra=1
down sysctl net.ipv6.conf.eth0.accept_ra=0
iface {{ name }} inet6 dhcp
up sysctl net.ipv6.conf.{{ name }}.accept_ra=1
down sysctl net.ipv6.conf.{{ name }}.accept_ra=0
{% endif %}
{% endfor %}
{% endif %}

{% if ZTP['mode']['inband'] == 'true' %}
Expand All @@ -65,7 +73,7 @@ iface {{ port }} inet6 dhcp
{% else %}
{% if MGMT_INTERFACE %}
{% for (name, prefix) in MGMT_INTERFACE|pfx_filter %}
iface eth0 {{ 'inet' if prefix | ipv4 else 'inet6' }} static
iface {{ name }} {{ 'inet' if prefix | ipv4 else 'inet6' }} static
address {{ prefix | ip }}
netmask {{ prefix | netmask if prefix | ipv4 else prefix | prefixlen }}
network {{ prefix | network }}
Expand All @@ -77,15 +85,15 @@ iface eth0 {{ 'inet' if prefix | ipv4 else 'inet6' }} static
{% endif %}
########## management network policy routing rules
# management port up rules
up ip {{ '-4' if prefix | ipv4 else '-6' }} route add default via {{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }} dev eth0 table {{ vrf_table }} metric 201
up ip {{ '-4' if prefix | ipv4 else '-6' }} route add {{ prefix | network }}/{{ prefix | prefixlen }} dev eth0 table {{ vrf_table }}
up ip {{ '-4' if prefix | ipv4 else '-6' }} route add default via {{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }} dev {{ name }} table {{ vrf_table }} metric 201
up ip {{ '-4' if prefix | ipv4 else '-6' }} route add {{ prefix | network }}/{{ prefix | prefixlen }} dev {{ name }} table {{ vrf_table }}
up ip {{ '-4' if prefix | ipv4 else '-6' }} rule add pref 32765 from {{ prefix | ip }}/{{ '32' if prefix | ipv4 else '128' }} table {{ vrf_table }}
{% for route in MGMT_INTERFACE[(name, prefix)]['forced_mgmt_routes'] %}
up ip rule add pref 32764 to {{ route }} table {{ vrf_table }}
{% endfor %}
# management port down rules
pre-down ip {{ '-4' if prefix | ipv4 else '-6' }} route delete default via {{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }} dev eth0 table {{ vrf_table }}
pre-down ip {{ '-4' if prefix | ipv4 else '-6' }} route delete {{ prefix | network }}/{{ prefix | prefixlen }} dev eth0 table {{ vrf_table }}
pre-down ip {{ '-4' if prefix | ipv4 else '-6' }} route delete default via {{ MGMT_INTERFACE[(name, prefix)]['gwaddr'] }} dev {{ name }} table {{ vrf_table }}
pre-down ip {{ '-4' if prefix | ipv4 else '-6' }} route delete {{ prefix | network }}/{{ prefix | prefixlen }} dev {{ name }} table {{ vrf_table }}
pre-down ip {{ '-4' if prefix | ipv4 else '-6' }} rule delete pref 32765 from {{ prefix | ip }}/{{ '32' if prefix | ipv4 else '128' }} table {{ vrf_table }}
{% for route in MGMT_INTERFACE[(name, prefix)]['forced_mgmt_routes'] %}
pre-down ip rule delete pref 32764 to {{ route }} table {{ vrf_table }}
Expand Down
4 changes: 3 additions & 1 deletion src/sonic-config-engine/minigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from lxml import etree as ET
from lxml.etree import QName

from natsort import natsorted

from portconfig import get_port_config
from sonic_py_common.interface import backplane_prefix
Expand Down Expand Up @@ -1432,7 +1433,8 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
results['MGMT_INTERFACE'] = {}
mgmt_intf_count = 0
mgmt_alias_reverse_mapping = {}
for key in mgmt_intf:
sorted_keys = natsorted(mgmt_intf.keys(), key=lambda x : x[0].lower())
for key in sorted_keys:
alias = key[0]
if alias in mgmt_alias_reverse_mapping:
name = mgmt_alias_reverse_mapping[alias]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# =============== Managed by SONiC Config Engine DO NOT EDIT! ===============
# generated from /usr/share/sonic/templates/interfaces.j2 using sonic-cfggen
# file: /etc/network/interfaces
#
# The loopback network interface
auto lo
iface lo inet loopback
address 127.0.0.1
netmask 255.255.0.0
scope host
post-up ip addr del 127.0.0.1/8 dev lo

# The management network interface
auto eth1
auto eth0
iface eth1 inet static
address 10.0.10.100
netmask 255.255.255.0
network 10.0.10.0
broadcast 10.0.10.255
########## management network policy routing rules
# management port up rules
up ip -4 route add default via 10.0.10.1 dev eth1 table default metric 201
up ip -4 route add 10.0.10.0/24 dev eth1 table default
up ip -4 rule add pref 32765 from 10.0.10.100/32 table default
# management port down rules
pre-down ip -4 route delete default via 10.0.10.1 dev eth1 table default
pre-down ip -4 route delete 10.0.10.0/24 dev eth1 table default
pre-down ip -4 rule delete pref 32765 from 10.0.10.100/32 table default
iface eth0 inet static
address 10.0.0.100
netmask 255.255.255.0
network 10.0.0.0
broadcast 10.0.0.255
########## management network policy routing rules
# management port up rules
up ip -4 route add default via 10.0.0.1 dev eth0 table default metric 201
up ip -4 route add 10.0.0.0/24 dev eth0 table default
up ip -4 rule add pref 32765 from 10.0.0.100/32 table default
# management port down rules
pre-down ip -4 route delete default via 10.0.0.1 dev eth0 table default
pre-down ip -4 route delete 10.0.0.0/24 dev eth0 table default
pre-down ip -4 rule delete pref 32765 from 10.0.0.100/32 table default
iface eth1 inet6 static
address 2603:10e2:0:abcd::8
netmask 64
network 2603:10e2:0:abcd::
broadcast 2603:10e2:0:abcd:ffff:ffff:ffff:ffff
########## management network policy routing rules
# management port up rules
up ip -6 route add default via 2603:10e2:0:abcd::1 dev eth1 table default metric 201
up ip -6 route add 2603:10e2:0:abcd::/64 dev eth1 table default
up ip -6 rule add pref 32765 from 2603:10e2:0:abcd::8/128 table default
# management port down rules
pre-down ip -6 route delete default via 2603:10e2:0:abcd::1 dev eth1 table default
pre-down ip -6 route delete 2603:10e2:0:abcd::/64 dev eth1 table default
pre-down ip -6 rule delete pref 32765 from 2603:10e2:0:abcd::8/128 table default
iface eth0 inet6 static
address 2603:10e2:0:2902::8
netmask 64
network 2603:10e2:0:2902::
broadcast 2603:10e2:0:2902:ffff:ffff:ffff:ffff
########## management network policy routing rules
# management port up rules
up ip -6 route add default via 2603:10e2:0:2902::1 dev eth0 table default metric 201
up ip -6 route add 2603:10e2:0:2902::/64 dev eth0 table default
up ip -6 rule add pref 32765 from 2603:10e2:0:2902::8/128 table default
# management port down rules
pre-down ip -6 route delete default via 2603:10e2:0:2902::1 dev eth0 table default
pre-down ip -6 route delete 2603:10e2:0:2902::/64 dev eth0 table default
pre-down ip -6 rule delete pref 32765 from 2603:10e2:0:2902::8/128 table default
#
source /etc/network/interfaces.d/*
#

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# =============== Managed by SONiC Config Engine DO NOT EDIT! ===============
# generated from /usr/share/sonic/templates/interfaces.j2 using sonic-cfggen
# file: /etc/network/interfaces
#
# The loopback network interface
auto lo
iface lo inet loopback
address 127.0.0.1
netmask 255.255.0.0
scope host
post-up ip addr del 127.0.0.1/8 dev lo

# The management network interface
auto eth0
auto eth1
iface eth0 inet static
address 10.0.0.100
netmask 255.255.255.0
network 10.0.0.0
broadcast 10.0.0.255
########## management network policy routing rules
# management port up rules
up ip -4 route add default via 10.0.0.1 dev eth0 table default metric 201
up ip -4 route add 10.0.0.0/24 dev eth0 table default
up ip -4 rule add pref 32765 from 10.0.0.100/32 table default
# management port down rules
pre-down ip -4 route delete default via 10.0.0.1 dev eth0 table default
pre-down ip -4 route delete 10.0.0.0/24 dev eth0 table default
pre-down ip -4 rule delete pref 32765 from 10.0.0.100/32 table default
iface eth0 inet6 static
address 2603:10e2:0:2902::8
netmask 64
network 2603:10e2:0:2902::
broadcast 2603:10e2:0:2902:ffff:ffff:ffff:ffff
########## management network policy routing rules
# management port up rules
up ip -6 route add default via 2603:10e2:0:2902::1 dev eth0 table default metric 201
up ip -6 route add 2603:10e2:0:2902::/64 dev eth0 table default
up ip -6 rule add pref 32765 from 2603:10e2:0:2902::8/128 table default
# management port down rules
pre-down ip -6 route delete default via 2603:10e2:0:2902::1 dev eth0 table default
pre-down ip -6 route delete 2603:10e2:0:2902::/64 dev eth0 table default
pre-down ip -6 rule delete pref 32765 from 2603:10e2:0:2902::8/128 table default
iface eth1 inet static
address 10.0.10.100
netmask 255.255.255.0
network 10.0.10.0
broadcast 10.0.10.255
########## management network policy routing rules
# management port up rules
up ip -4 route add default via 10.0.10.1 dev eth1 table default metric 201
up ip -4 route add 10.0.10.0/24 dev eth1 table default
up ip -4 rule add pref 32765 from 10.0.10.100/32 table default
# management port down rules
pre-down ip -4 route delete default via 10.0.10.1 dev eth1 table default
pre-down ip -4 route delete 10.0.10.0/24 dev eth1 table default
pre-down ip -4 rule delete pref 32765 from 10.0.10.100/32 table default
iface eth1 inet6 static
address 2603:10e2:0:abcd::8
netmask 64
network 2603:10e2:0:abcd::
broadcast 2603:10e2:0:abcd:ffff:ffff:ffff:ffff
########## management network policy routing rules
# management port up rules
up ip -6 route add default via 2603:10e2:0:abcd::1 dev eth1 table default metric 201
up ip -6 route add 2603:10e2:0:abcd::/64 dev eth1 table default
up ip -6 rule add pref 32765 from 2603:10e2:0:abcd::8/128 table default
# management port down rules
pre-down ip -6 route delete default via 2603:10e2:0:abcd::1 dev eth1 table default
pre-down ip -6 route delete 2603:10e2:0:abcd::/64 dev eth1 table default
pre-down ip -6 rule delete pref 32765 from 2603:10e2:0:abcd::8/128 table default
#
source /etc/network/interfaces.d/*
#

Loading

0 comments on commit 90a849e

Please sign in to comment.