core-extra/daemon/core/services/sdn.py

132 lines
5.3 KiB
Python
Raw Normal View History

"""
2017-06-08 21:19:06 +01:00
sdn.py defines services to start Open vSwitch and the Ryu SDN Controller.
"""
2017-06-07 20:41:52 +01:00
import re
from core.nodes.base import CoreNode
from core.services.coreservices import CoreService
2017-06-07 20:41:52 +01:00
2017-06-08 21:19:06 +01:00
class SdnService(CoreService):
"""
Parent class for SDN services.
"""
group: str = "SDN"
2017-06-08 21:19:06 +01:00
@classmethod
def generate_config(cls, node: CoreNode, filename: str) -> str:
2017-06-08 21:19:06 +01:00
return ""
2017-06-08 21:19:06 +01:00
class OvsService(SdnService):
name: str = "OvsService"
group: str = "SDN"
executables: tuple[str, ...] = ("ovs-ofctl", "ovs-vsctl")
dirs: tuple[str, ...] = (
"/etc/openvswitch",
"/var/run/openvswitch",
"/var/log/openvswitch",
)
configs: tuple[str, ...] = ("OvsService.sh",)
startup: tuple[str, ...] = ("bash OvsService.sh",)
shutdown: tuple[str, ...] = ("killall ovs-vswitchd", "killall ovsdb-server")
2017-06-07 20:41:52 +01:00
@classmethod
def generate_config(cls, node: CoreNode, filename: str) -> str:
2017-06-07 20:41:52 +01:00
# Check whether the node is running zebra
has_zebra = 0
for s in node.services:
if s.name == "zebra":
2017-06-07 20:41:52 +01:00
has_zebra = 1
2017-06-07 20:41:52 +01:00
cfg = "#!/bin/sh\n"
cfg += "# auto-generated by OvsService (OvsService.py)\n"
2018-07-30 14:42:02 +01:00
cfg += "## First make sure that the ovs services are up and running\n"
cfg += "/etc/init.d/openvswitch-switch start < /dev/null\n\n"
cfg += "## create the switch itself, set the fail mode to secure, \n"
cfg += "## this stops it from routing traffic without defined flows.\n"
cfg += "## remove the -- and everything after if you want it to act as a regular switch\n"
cfg += "ovs-vsctl add-br ovsbr0 -- set Bridge ovsbr0 fail-mode=secure\n"
cfg += "\n## Now add all our interfaces as ports to the switch\n"
2018-07-30 14:42:02 +01:00
portnum = 1
for iface in node.get_ifaces(control=False):
ifnumstr = re.findall(r"\d+", iface.name)
2017-06-07 20:41:52 +01:00
ifnum = ifnumstr[0]
2017-06-07 20:41:52 +01:00
# create virtual interfaces
2018-07-30 14:42:02 +01:00
cfg += "## Create a veth pair to send the data to\n"
cfg += f"ip link add rtr{ifnum} type veth peer name sw{ifnum}\n"
2017-06-07 20:41:52 +01:00
# remove ip address of eths because quagga/zebra will assign same IPs to rtr interfaces
# or assign them manually to rtr interfaces if zebra is not running
for ip4 in iface.ip4s:
cfg += f"ip addr del {ip4.ip} dev {iface.name}\n"
if has_zebra == 0:
cfg += f"ip addr add {ip4.ip} dev rtr{ifnum}\n"
for ip6 in iface.ip6s:
cfg += f"ip -6 addr del {ip6.ip} dev {iface.name}\n"
if has_zebra == 0:
cfg += f"ip -6 addr add {ip6.ip} dev rtr{ifnum}\n"
2017-06-07 20:41:52 +01:00
# add interfaces to bridge
# Make port numbers explicit so they're easier to follow in
# reading the script
2018-07-30 14:42:02 +01:00
cfg += "## Add the CORE interface to the switch\n"
2020-04-30 20:57:05 +01:00
cfg += (
f"ovs-vsctl add-port ovsbr0 eth{ifnum} -- "
f"set Interface eth{ifnum} ofport_request={portnum:d}\n"
2020-04-30 20:57:05 +01:00
)
2018-07-30 14:42:02 +01:00
cfg += "## And then add its sibling veth interface\n"
2020-04-30 20:57:05 +01:00
cfg += (
f"ovs-vsctl add-port ovsbr0 sw{ifnum} -- "
f"set Interface sw{ifnum} ofport_request={portnum + 1:d}\n"
2020-04-30 20:57:05 +01:00
)
2018-07-30 14:42:02 +01:00
cfg += "## start them up so we can send/receive data\n"
cfg += f"ovs-ofctl mod-port ovsbr0 eth{ifnum} up\n"
cfg += f"ovs-ofctl mod-port ovsbr0 sw{ifnum} up\n"
2018-07-30 14:42:02 +01:00
cfg += "## Bring up the lower part of the veth pair\n"
cfg += f"ip link set dev rtr{ifnum} up\n"
2018-07-30 14:42:02 +01:00
portnum += 2
2017-06-07 20:41:52 +01:00
# Add rule for default controller if there is one local
# (even if the controller is not local, it finds it)
2018-07-30 14:42:02 +01:00
cfg += "\n## We assume there will be an SDN controller on the other end of this, \n"
cfg += "## but it will still function if there's not\n"
2017-06-08 21:19:06 +01:00
cfg += "ovs-vsctl set-controller ovsbr0 tcp:127.0.0.1:6633\n"
2017-06-07 20:41:52 +01:00
2018-07-30 14:42:02 +01:00
cfg += "\n## Now to create some default flows, \n"
cfg += "## if the above controller will be present then you probably want to delete them\n"
2017-06-07 20:41:52 +01:00
# Setup default flows
portnum = 1
for iface in node.get_ifaces(control=False):
2018-07-30 14:42:02 +01:00
cfg += "## Take the data from the CORE interface and put it on the veth and vice versa\n"
cfg += f"ovs-ofctl add-flow ovsbr0 priority=1000,in_port={portnum:d},action=output:{portnum + 1:d}\n"
cfg += f"ovs-ofctl add-flow ovsbr0 priority=1000,in_port={portnum + 1:d},action=output:{portnum:d}\n"
2017-06-07 20:41:52 +01:00
portnum += 2
return cfg
class RyuService(SdnService):
name: str = "ryuService"
group: str = "SDN"
executables: tuple[str, ...] = ("ryu-manager",)
configs: tuple[str, ...] = ("ryuService.sh",)
startup: tuple[str, ...] = ("bash ryuService.sh",)
shutdown: tuple[str, ...] = ("killall ryu-manager",)
2017-06-08 21:19:06 +01:00
@classmethod
def generate_config(cls, node: CoreNode, filename: str) -> str:
"""
Return a string that will be written to filename, or sent to the
GUI for user customization.
"""
2017-06-08 21:19:06 +01:00
cfg = "#!/bin/sh\n"
cfg += "# auto-generated by ryuService (ryuService.py)\n"
cfg += (
"ryu-manager --observe-links ryu.app.ofctl_rest ryu.app.rest_topology &\n"
)
2017-06-08 21:19:06 +01:00
return cfg