Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensuring ipv4 config ahead of ipv6 in /etc/network/interfaces file for mgmt interface #1544

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion files/image_config/interfaces/interfaces.j2
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ iface lo {{ 'inet' if prefix | ipv4 else 'inet6' }} static
# The management network interface
auto eth0
{% if MGMT_INTERFACE %}
{% for (name, prefix) in MGMT_INTERFACE %}
{% for (name, prefix) in MGMT_INTERFACE|sort_by_prefix %}
iface eth0 {{ 'inet' if prefix | ipv4 else 'inet6' }} static
address {{ prefix | ip }}
netmask {{ prefix | netmask if prefix | ipv4 else prefix | prefixlen }}
Expand Down
8 changes: 8 additions & 0 deletions src/sonic-config-engine/sonic-cfggen
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ from swsssdk import ConfigDBConnector
from collections import OrderedDict
from natsort import natsorted

# Sort by interface name. For configs to the same interface (name),
# sort by prefix to put v4 address ahead of v6 address in the list
def sort_by_prefix(value):
if not value:
return None
return sorted(value.keys(), key = lambda k: (k[0], 4) if is_ipv4(k[1]) else (k[0], 6))

def is_ipv4(value):
if not value:
return False
Expand Down Expand Up @@ -210,6 +217,7 @@ def main():
loader = jinja2.FileSystemLoader(paths)

env = jinja2.Environment(loader=loader, trim_blocks=True)
env.filters['sort_by_prefix'] = sort_by_prefix
env.filters['ipv4'] = is_ipv4
env.filters['ipv6'] = is_ipv6
env.filters['unique_name'] = unique_name
Expand Down
14 changes: 7 additions & 7 deletions src/sonic-config-engine/tests/sample_output/interfaces
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ iface eth0 inet static
down ip -4 route delete 10.0.0.0/24 dev eth0 table default
down ip -4 rule delete from 10.0.0.100/32 table default
iface eth0 inet6 static
address 2603:10e2:0:2902::8
address 603:10e2:0:2902::8
netmask 64
########## 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
up ip -6 route add 2603:10e2:0:2902::/64 dev eth0 table default
up ip -6 rule add from 2603:10e2:0:2902::8/128 table default
up ip -6 route add default via 603:10e2:0:2902::1 dev eth0 table default
up ip -6 route add 603:10e2:0:2902::/64 dev eth0 table default
up ip -6 rule add from 603:10e2:0:2902::8/128 table default
# management port down rules
down ip -6 route delete default via 2603:10e2:0:2902::1 dev eth0 table default
down ip -6 route delete 2603:10e2:0:2902::/64 dev eth0 table default
down ip -6 rule delete from 2603:10e2:0:2902::8/128 table default
down ip -6 route delete default via 603:10e2:0:2902::1 dev eth0 table default
down ip -6 route delete 603:10e2:0:2902::/64 dev eth0 table default
down ip -6 rule delete from 603:10e2:0:2902::8/128 table default
#
# The switch front panel interfaces
# "|| true" is added to suppress the error when interface is already a member of LAG
Expand Down
4 changes: 2 additions & 2 deletions src/sonic-config-engine/tests/t0-sample-graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@
<Name>HostIP</Name>
<AttachTo>eth0</AttachTo>
<a:Prefix xmlns:b="Microsoft.Search.Autopilot.Evolution">
<b:IPPrefix>2603:10e2:0:2902::8/64</b:IPPrefix>
<b:IPPrefix>603:10e2:0:2902::8/64</b:IPPrefix>
</a:Prefix>
<a:PrefixStr>2603:10e2:0:2902::8/64</a:PrefixStr>
<a:PrefixStr>603:10e2:0:2902::8/64</a:PrefixStr>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the ipv6 address such that without the sort_by_prefix filter, the interfaces.j2 template will place ipv6 profile ahead of ipv4:

--- sample_output/interfaces 2018-04-02 21:28:10.122688400 +0000
+++ /home/localadmin/interfaces 2018-04-02 23:36:40.086688400 +0000
@@ -17,6 +17,18 @@
#
# The management network interface
auto eth0
+iface eth0 inet6 static
+ address 603:10e2:0:2902::8
+ netmask 64
+ ########## management network policy routing rules
+ # management port up rules
+ up ip -6 route add default via 603:10e2:0:2902::1 dev eth0 table default
+ up ip -6 route add 603:10e2:0:2902::/64 dev eth0 table default
+ up ip -6 rule add from 603:10e2:0:2902::8/128 table default
+ # management port down rules
+ down ip -6 route delete default via 603:10e2:0:2902::1 dev eth0 table default
+ down ip -6 route delete 603:10e2:0:2902::/64 dev eth0 table default
+ down ip -6 rule delete from 603:10e2:0:2902::8/128 table default
iface eth0 inet static
address 10.0.0.100
netmask 255.255.255.0
@@ -31,18 +43,6 @@
down ip -4 route delete 10.0.0.0/24 dev eth0 table default
down ip -4 rule delete from 10.0.0.100/32 table default
down ip rule delete to 10.20.6.16/32 table default
-iface eth0 inet6 static
- address 2603:10e2:0:2902::8
- netmask 64
- ########## 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
- up ip -6 route add 2603:10e2:0:2902::/64 dev eth0 table default
- up ip -6 rule add from 2603:10e2:0:2902::8/128 table default
- # management port down rules
- down ip -6 route delete default via 2603:10e2:0:2902::1 dev eth0 table default
- down ip -6 route delete 2603:10e2:0:2902::/64 dev eth0 table default
- down ip -6 rule delete from 2603:10e2:0:2902::8/128 table default
#
# The switch front panel interfaces
# "|| true" is added to suppress the error when interface is already a member of LAG

</a:ManagementIPInterface>
</ManagementIPInterfaces>
<ManagementVIPInterfaces xmlns:a="http://schemas.datacontract.org/2004/07/Microsoft.Search.Autopilot.Evolution"/>
Expand Down