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

114 lines
4 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.service 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 = "SDN"
2017-06-08 21:19:06 +01:00
@classmethod
def generate_config(cls, node, filename):
2017-06-08 21:19:06 +01:00
return ""
2017-06-08 21:19:06 +01:00
class OvsService(SdnService):
name = "OvsService"
executables = ("ovs-ofctl", "ovs-vsctl")
group = "SDN"
dirs = ("/etc/openvswitch", "/var/run/openvswitch", "/var/log/openvswitch")
configs = ('OvsService.sh',)
startup = ('sh OvsService.sh',)
shutdown = ('killall ovs-vswitchd', 'killall ovsdb-server')
2017-06-07 20:41:52 +01:00
@classmethod
def generate_config(cls, node, filename):
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
# Check whether the node is running an SDN controller
has_sdn_ctrlr = 0
for s in node.services:
if s.name == "ryuService":
2017-06-07 20:41:52 +01:00
has_sdn_ctrlr = 1
cfg = "#!/bin/sh\n"
cfg += "# auto-generated by OvsService (OvsService.py)\n"
cfg += "/etc/init.d/openvswitch-switch start < /dev/null\n"
cfg += "ovs-vsctl add-br ovsbr0\n"
cfg += "ifconfig ovsbr0 up\n"
2017-06-07 20:41:52 +01:00
for ifc in node.netifs():
if hasattr(ifc, 'control') and ifc.control is True:
2017-06-07 20:41:52 +01:00
continue
ifnumstr = re.findall(r"\d+", ifc.name)
ifnum = ifnumstr[0]
2017-06-07 20:41:52 +01:00
# create virtual interfaces
cfg += "ip link add rtr%s type veth peer name sw%s\n" % (ifnum, ifnum)
cfg += "ifconfig rtr%s up\n" % ifnum
cfg += "ifconfig sw%s up\n" % ifnum
# 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 ifcaddr in ifc.addrlist:
if ifcaddr.find(".") >= 0:
cfg += "ip addr del %s dev %s\n" % (ifcaddr, ifc.name)
if has_zebra == 0:
cfg += "ip addr add %s dev rtr%s\n" % (ifcaddr, ifnum)
elif ifcaddr.find(":") >= 0:
cfg += "ip -6 addr del %s dev %s\n" % (ifcaddr, ifc.name)
if has_zebra == 0:
cfg += "ip -6 addr add %s dev rtr%s\n" % (ifcaddr, ifnum)
else:
raise ValueError("invalid address: %s" % ifcaddr)
2017-06-07 20:41:52 +01:00
# add interfaces to bridge
cfg += "ovs-vsctl add-port ovsbr0 eth%s\n" % ifnum
cfg += "ovs-vsctl add-port ovsbr0 sw%s\n" % ifnum
2017-06-08 21:19:06 +01:00
# Add rule for default controller if there is one local (even if the controller is not local, it finds it)
cfg += "ovs-vsctl set-controller ovsbr0 tcp:127.0.0.1:6633\n"
2017-06-07 20:41:52 +01:00
# Setup default flows
portnum = 1
for ifc in node.netifs():
if hasattr(ifc, 'control') and ifc.control is True:
2017-06-07 20:41:52 +01:00
continue
cfg += "ovs-ofctl add-flow ovsbr0 priority=1000,in_port=%d,action=output:%d\n" % (portnum, portnum + 1)
cfg += "ovs-ofctl add-flow ovsbr0 priority=1000,in_port=%d,action=output:%d\n" % (portnum + 1, portnum)
2017-06-07 20:41:52 +01:00
portnum += 2
2017-06-07 20:41:52 +01:00
return cfg
class RyuService(SdnService):
name = "ryuService"
executables = ("ryu-manager",)
group = "SDN"
dirs = ()
configs = ('ryuService.sh',)
startup = ('sh ryuService.sh',)
shutdown = ('killall ryu-manager',)
2017-06-08 21:19:06 +01:00
@classmethod
def generate_config(cls, node, filename):
"""
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
app_path = "/usr/local/lib/python2.7/dist-packages/ryu/app"
cfg = "#!/bin/sh\n"
cfg += "# auto-generated by ryuService (ryuService.py)\n"
cfg += '/usr/local/bin/ryu-manager --observe-links %s/ofctl_rest.py %s/rest_topology.py' % (app_path, app_path)
return cfg