From 9d9b85fac55a57774f373094e1398d32ae48c684 Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Wed, 19 Apr 2023 11:02:01 -0600 Subject: [PATCH 1/3] net: Use `ip route add` to add static routes Support for rfc3442 routes was added to cloud-init in 07b172. In this commit, `ip route append` was used to add the route. This succeeds at adding static routes, but its documented purpose is related to multipath routing. To avoid confusion around the undocumented behavior being used, switch to `ip route add`, which does the same thing. This will also simplify an abstraction to support distros that do not use iproute2. --- cloudinit/net/ephemeral.py | 2 +- tests/unittests/net/test_init.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cloudinit/net/ephemeral.py b/cloudinit/net/ephemeral.py index 1dfd1c428e1..6cf668764d5 100644 --- a/cloudinit/net/ephemeral.py +++ b/cloudinit/net/ephemeral.py @@ -198,7 +198,7 @@ def _bringup_static_routes(self): if gateway != "0.0.0.0": via_arg = ["via", gateway] subp.subp( - ["ip", "-4", "route", "append", net_address] + ["ip", "-4", "route", "add", net_address] + via_arg + ["dev", self.interface], capture=True, diff --git a/tests/unittests/net/test_init.py b/tests/unittests/net/test_init.py index 7cf5db277d6..d87b59ba138 100644 --- a/tests/unittests/net/test_init.py +++ b/tests/unittests/net/test_init.py @@ -864,7 +864,7 @@ def test_teardown_on_enter_exception(self, m_subp): """ def side_effect(args, **kwargs): - if args[3] == "append" and args[4] == "3.3.3.3/32": + if args[3] == "add" and args[4] == "3.3.3.3/32": raise subp.ProcessExecutionError("oh no!") m_subp.side_effect = side_effect @@ -1167,7 +1167,7 @@ def test_ephemeral_ipv4_network_with_rfc3442_static_routes(self, m_subp): "ip", "-4", "route", - "append", + "add", "192.168.2.1/32", "dev", "eth0", @@ -1179,7 +1179,7 @@ def test_ephemeral_ipv4_network_with_rfc3442_static_routes(self, m_subp): "ip", "-4", "route", - "append", + "add", "169.254.169.254/32", "via", "192.168.2.1", @@ -1193,7 +1193,7 @@ def test_ephemeral_ipv4_network_with_rfc3442_static_routes(self, m_subp): "ip", "-4", "route", - "append", + "add", "0.0.0.0/0", "via", "192.168.2.1", From f71dfc41c4cf19b036e6397f616f266059e271ee Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Thu, 20 Apr 2023 10:17:45 -0600 Subject: [PATCH 2/3] Revert "net: Use `ip route add` to add static routes" This reverts commit 9d9b85fac55a57774f373094e1398d32ae48c684. --- cloudinit/net/ephemeral.py | 2 +- tests/unittests/net/test_init.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cloudinit/net/ephemeral.py b/cloudinit/net/ephemeral.py index 6cf668764d5..1dfd1c428e1 100644 --- a/cloudinit/net/ephemeral.py +++ b/cloudinit/net/ephemeral.py @@ -198,7 +198,7 @@ def _bringup_static_routes(self): if gateway != "0.0.0.0": via_arg = ["via", gateway] subp.subp( - ["ip", "-4", "route", "add", net_address] + ["ip", "-4", "route", "append", net_address] + via_arg + ["dev", self.interface], capture=True, diff --git a/tests/unittests/net/test_init.py b/tests/unittests/net/test_init.py index d87b59ba138..7cf5db277d6 100644 --- a/tests/unittests/net/test_init.py +++ b/tests/unittests/net/test_init.py @@ -864,7 +864,7 @@ def test_teardown_on_enter_exception(self, m_subp): """ def side_effect(args, **kwargs): - if args[3] == "add" and args[4] == "3.3.3.3/32": + if args[3] == "append" and args[4] == "3.3.3.3/32": raise subp.ProcessExecutionError("oh no!") m_subp.side_effect = side_effect @@ -1167,7 +1167,7 @@ def test_ephemeral_ipv4_network_with_rfc3442_static_routes(self, m_subp): "ip", "-4", "route", - "add", + "append", "192.168.2.1/32", "dev", "eth0", @@ -1179,7 +1179,7 @@ def test_ephemeral_ipv4_network_with_rfc3442_static_routes(self, m_subp): "ip", "-4", "route", - "add", + "append", "169.254.169.254/32", "via", "192.168.2.1", @@ -1193,7 +1193,7 @@ def test_ephemeral_ipv4_network_with_rfc3442_static_routes(self, m_subp): "ip", "-4", "route", - "add", + "append", "0.0.0.0/0", "via", "192.168.2.1", From 866853465a5a7bf93bd8d1d43c72378e4af7982b Mon Sep 17 00:00:00 2001 From: Brett Holman Date: Thu, 20 Apr 2023 10:26:19 -0600 Subject: [PATCH 3/3] add comment explaining the requirement for 'ip r add' --- cloudinit/net/ephemeral.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cloudinit/net/ephemeral.py b/cloudinit/net/ephemeral.py index 1dfd1c428e1..651cefa6451 100644 --- a/cloudinit/net/ephemeral.py +++ b/cloudinit/net/ephemeral.py @@ -197,6 +197,14 @@ def _bringup_static_routes(self): via_arg = [] if gateway != "0.0.0.0": via_arg = ["via", gateway] + + # Use "append" rather than "add" since the DHCP server may provide + # rfc3442 classless static routes with multiple routes to the same + # subnet via different routers or local interface addresses. + # + # In this scenario, `ip r add` fails. + # + # RHBZ: #2003231 subp.subp( ["ip", "-4", "route", "append", net_address] + via_arg