improve default route service to detect connected routers and use the addresses of the first one found
This commit is contained in:
parent
72189a5c28
commit
7d392c43ac
3 changed files with 34 additions and 30 deletions
|
@ -1,10 +1,10 @@
|
||||||
import logging
|
|
||||||
from typing import Any, Dict
|
from typing import Any, Dict
|
||||||
|
|
||||||
import netaddr
|
import netaddr
|
||||||
|
|
||||||
from core import utils
|
from core import utils
|
||||||
from core.configservice.base import ConfigService, ConfigServiceMode
|
from core.configservice.base import ConfigService, ConfigServiceMode
|
||||||
|
from core.nodes.base import CoreNode
|
||||||
|
|
||||||
GROUP_NAME = "Utility"
|
GROUP_NAME = "Utility"
|
||||||
|
|
||||||
|
@ -24,16 +24,21 @@ class DefaultRouteService(ConfigService):
|
||||||
modes = {}
|
modes = {}
|
||||||
|
|
||||||
def data(self) -> Dict[str, Any]:
|
def data(self) -> Dict[str, Any]:
|
||||||
addresses = []
|
# only add default routes for linked routing nodes
|
||||||
for netif in self.node.netifs():
|
routes = []
|
||||||
if getattr(netif, "control", False):
|
for other_node in self.node.session.nodes.values():
|
||||||
|
if not isinstance(other_node, CoreNode):
|
||||||
continue
|
continue
|
||||||
for addr in netif.addrlist:
|
if other_node.type not in ["router", "mdr"]:
|
||||||
logging.info("default route address: %s", addr)
|
continue
|
||||||
net = netaddr.IPNetwork(addr)
|
commonnets = self.node.commonnets(other_node)
|
||||||
if net[1] != net[-2]:
|
if commonnets:
|
||||||
addresses.append(net[1])
|
_, _, router_eth = commonnets[0]
|
||||||
return dict(addresses=addresses)
|
for x in router_eth.addrlist:
|
||||||
|
addr, prefix = x.split("/")
|
||||||
|
routes.append(addr)
|
||||||
|
break
|
||||||
|
return dict(routes=routes)
|
||||||
|
|
||||||
|
|
||||||
class DefaultMulticastRouteService(ConfigService):
|
class DefaultMulticastRouteService(ConfigService):
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
# auto-generated by DefaultRoute service
|
# auto-generated by DefaultRoute service
|
||||||
% for address in addresses:
|
% for route in routes:
|
||||||
ip route add default via ${address}
|
ip route add default via ${route}
|
||||||
% endfor
|
% endfor
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
"""
|
"""
|
||||||
utility.py: defines miscellaneous utility services.
|
utility.py: defines miscellaneous utility services.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import netaddr
|
import netaddr
|
||||||
|
|
||||||
from core import constants, utils
|
from core import constants, utils
|
||||||
from core.errors import CoreCommandError
|
from core.errors import CoreCommandError
|
||||||
|
from core.nodes.base import CoreNode
|
||||||
from core.services.coreservices import CoreService, ServiceMode
|
from core.services.coreservices import CoreService, ServiceMode
|
||||||
|
|
||||||
|
|
||||||
|
@ -77,27 +77,26 @@ class DefaultRouteService(UtilService):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def generate_config(cls, node, filename):
|
def generate_config(cls, node, filename):
|
||||||
|
# only add default routes for linked routing nodes
|
||||||
|
routes = []
|
||||||
|
for other_node in node.session.nodes.values():
|
||||||
|
if not isinstance(other_node, CoreNode):
|
||||||
|
continue
|
||||||
|
if other_node.type not in ["router", "mdr"]:
|
||||||
|
continue
|
||||||
|
commonnets = node.commonnets(other_node)
|
||||||
|
if commonnets:
|
||||||
|
_, _, router_eth = commonnets[0]
|
||||||
|
for x in router_eth.addrlist:
|
||||||
|
addr, prefix = x.split("/")
|
||||||
|
routes.append(addr)
|
||||||
|
break
|
||||||
cfg = "#!/bin/sh\n"
|
cfg = "#!/bin/sh\n"
|
||||||
cfg += "# auto-generated by DefaultRoute service (utility.py)\n"
|
cfg += "# auto-generated by DefaultRoute service (utility.py)\n"
|
||||||
for ifc in node.netifs():
|
for route in routes:
|
||||||
if hasattr(ifc, "control") and ifc.control is True:
|
cfg += f"ip route add default via {route}\n"
|
||||||
continue
|
|
||||||
cfg += "\n".join(map(cls.addrstr, ifc.addrlist))
|
|
||||||
cfg += "\n"
|
|
||||||
return cfg
|
return cfg
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def addrstr(x):
|
|
||||||
net = netaddr.IPNetwork(x)
|
|
||||||
if net[1] == net[-2]:
|
|
||||||
return ""
|
|
||||||
else:
|
|
||||||
if os.uname()[0] == "Linux":
|
|
||||||
rtcmd = "ip route add default via"
|
|
||||||
else:
|
|
||||||
raise Exception("unknown platform")
|
|
||||||
return "%s %s" % (rtcmd, net[1])
|
|
||||||
|
|
||||||
|
|
||||||
class DefaultMulticastRouteService(UtilService):
|
class DefaultMulticastRouteService(UtilService):
|
||||||
name = "DefaultMulticastRoute"
|
name = "DefaultMulticastRoute"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue