Skip to content

Commit 8a60b80

Browse files
bhouse-nexthopbradh352
authored andcommitted
Fix static routes to be added to PBR tables in VPC routers
Static routes were only being added to the main routing table, but policy-based routing (PBR) is active on VPC routers. This caused traffic coming in from specific interfaces to not find the static routes, as they use interface-specific routing tables (Table_ethX). This fix: - Adds a helper method to find which interface a gateway belongs to by matching the gateway IP against configured interface subnets - Modifies route add/delete operations to update both the main table and the appropriate interface-specific PBR table - Uses existing CsAddress databag metadata to avoid OS queries - Handles both add and revoke operations for proper cleanup - Adds comprehensive logging for troubleshooting Fixes #12857
1 parent e93ae1a commit 8a60b80

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

systemvm/debian/opt/cloud/bin/cs/CsStaticRoutes.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import logging
2121
from . import CsHelper
2222
from .CsDatabag import CsDataBag
23+
from .CsRoute import CsRoute
2324

2425

2526
class CsStaticRoutes(CsDataBag):
@@ -31,13 +32,68 @@ def process(self):
3132
continue
3233
self.__update(self.dbag[item])
3334

35+
def __find_device_for_gateway(self, gateway_ip):
36+
"""
37+
Find which ethernet device the gateway IP belongs to by checking
38+
if the gateway is in any of the configured interface subnets.
39+
Returns device name (e.g., 'eth2') or None if not found.
40+
"""
41+
try:
42+
# Get all configured interfaces from the address databag
43+
interfaces = self.config.address().get_interfaces()
44+
45+
for interface in interfaces:
46+
if not interface.is_added():
47+
continue
48+
49+
# Check if gateway IP is in this interface's subnet
50+
if interface.ip_in_subnet(gateway_ip):
51+
return interface.get_device()
52+
53+
logging.debug("No matching device found for gateway %s" % gateway_ip)
54+
return None
55+
except Exception as e:
56+
logging.error("Error finding device for gateway %s: %s" % (gateway_ip, e))
57+
return None
58+
3459
def __update(self, route):
60+
network = route['network']
61+
gateway = route['gateway']
62+
3563
if route['revoke']:
36-
command = "ip route del %s via %s" % (route['network'], route['gateway'])
64+
# Delete from main table
65+
command = "ip route del %s via %s" % (network, gateway)
3766
CsHelper.execute(command)
67+
68+
# Delete from PBR table if applicable
69+
device = self.__find_device_for_gateway(gateway)
70+
if device:
71+
cs_route = CsRoute()
72+
table_name = cs_route.get_tablename(device)
73+
command = "ip route del %s via %s table %s" % (network, gateway, table_name)
74+
CsHelper.execute(command)
75+
logging.info("Deleted static route %s via %s from PBR table %s" % (network, gateway, table_name))
3876
else:
39-
command = "ip route show | grep %s | awk '{print $1, $3}'" % route['network']
77+
# Add to main table (existing logic)
78+
command = "ip route show | grep %s | awk '{print $1, $3}'" % network
4079
result = CsHelper.execute(command)
4180
if not result:
42-
route_command = "ip route add %s via %s" % (route['network'], route['gateway'])
81+
route_command = "ip route add %s via %s" % (network, gateway)
4382
CsHelper.execute(route_command)
83+
logging.info("Added static route %s via %s to main table" % (network, gateway))
84+
85+
# Add to PBR table if applicable
86+
device = self.__find_device_for_gateway(gateway)
87+
if device:
88+
cs_route = CsRoute()
89+
table_name = cs_route.get_tablename(device)
90+
# Check if route already exists in the PBR table
91+
check_command = "ip route show table %s | grep %s | awk '{print $1, $3}'" % (table_name, network)
92+
result = CsHelper.execute(check_command)
93+
if not result:
94+
# Add route to the interface-specific table
95+
route_command = "ip route add %s via %s dev %s table %s" % (network, gateway, device, table_name)
96+
CsHelper.execute(route_command)
97+
logging.info("Added static route %s via %s to PBR table %s" % (network, gateway, table_name))
98+
else:
99+
logging.info("Static route %s via %s added to main table only (no matching interface found for PBR table)" % (network, gateway))

0 commit comments

Comments
 (0)