updates for basic working distrbuted network using fabric

This commit is contained in:
Blake Harnden 2019-10-08 15:09:26 -07:00
parent 212fec916b
commit b7b0e4222c
8 changed files with 261 additions and 96 deletions

View file

@ -14,6 +14,7 @@ from socket import AF_INET, AF_INET6
from tempfile import NamedTemporaryFile
from core import constants, utils
from core.emulator import distributed
from core.emulator.data import LinkData, NodeData
from core.emulator.enumerations import LinkTypes, NodeTypes
from core.errors import CoreCommandError
@ -95,39 +96,7 @@ class NodeBase(object):
:rtype: str
:raises CoreCommandError: when a non-zero exit status occurs
"""
logging.info("net cmd server(%s): %s", self.server, args)
if self.server is None:
return utils.check_cmd(args, env=env)
else:
args = " ".join(args)
return self.remote_cmd(args, env=env)
def remote_cmd(self, cmd, env=None):
"""
Run command remotely using server connection.
:param str cmd: command to run
:param dict env: environment for remote command, default is None
:return: stdout when success
:rtype: str
:raises CoreCommandError: when a non-zero exit status occurs
"""
if env is None:
result = self.server.run(cmd, hide=False)
else:
logging.info("command env: %s", env)
result = self.server.run(cmd, hide=False, env=env, replace_env=True)
if result.exited:
raise CoreCommandError(
result.exited, result.command, result.stdout, result.stderr
)
logging.info(
"fabric result:\n\tstdout: %s\n\tstderr: %s",
result.stdout.strip(),
result.stderr.strip(),
)
return result.stdout.strip()
raise NotImplementedError
def setposition(self, x=None, y=None, z=None):
"""
@ -279,7 +248,8 @@ class CoreNodeBase(NodeBase):
:param int _id: object id
:param str name: object name
:param bool start: boolean for starting
:param str server: remote server node will run on, default is None for localhost
:param fabric.connection.Connection server: remote server node will run on,
default is None for localhost
"""
super(CoreNodeBase, self).__init__(session, _id, name, start, server)
self.services = []
@ -412,6 +382,23 @@ class CoreNodeBase(NodeBase):
return common
def net_cmd(self, args, env=None):
"""
Runs a command that is used to configure and setup the network on the host
system.
:param list[str]|str args: command to run
:param dict env: environment to run command with
:return: combined stdout and stderr
:rtype: str
:raises CoreCommandError: when a non-zero exit status occurs
"""
if self.server is None:
return utils.check_cmd(args, env=env)
else:
args = " ".join(args)
return distributed.remote_cmd(self.server, args, env=env)
def node_net_cmd(self, args):
"""
Runs a command that is used to configure and setup the network within a
@ -493,7 +480,8 @@ class CoreNode(CoreNodeBase):
:param str nodedir: node directory
:param str bootsh: boot shell to use
:param bool start: start flag
:param str server: remote server node will run on, default is None for localhost
:param fabric.connection.Connection server: remote server node will run on,
default is None for localhost
"""
super(CoreNode, self).__init__(session, _id, name, start, server)
self.nodedir = nodedir
@ -653,13 +641,13 @@ class CoreNode(CoreNodeBase):
:rtype: str
:raises CoreCommandError: when a non-zero exit status occurs
"""
logging.info("net cmd server(%s): %s", self.server, args)
if self.server is None:
logging.info("node(%s) cmd: %s", self.name, args)
return self.check_cmd(args)
else:
args = self.client._cmd_args() + args
args = " ".join(args)
return self.remote_cmd(args)
return distributed.remote_cmd(self.server, args)
def check_cmd(self, args):
"""
@ -753,7 +741,11 @@ class CoreNode(CoreNodeBase):
raise ValueError("interface name (%s) too long" % name)
veth = Veth(
node=self, name=name, localname=localname, net=net, start=self.up
node=self,
name=name,
localname=localname,
start=self.up,
server=self.server,
)
if self.up:
@ -806,9 +798,7 @@ class CoreNode(CoreNodeBase):
sessionid = self.session.short_session_id()
localname = "tap%s.%s.%s" % (self.id, ifindex, sessionid)
name = ifname
tuntap = TunTap(
node=self, name=name, localname=localname, net=net, start=self.up
)
tuntap = TunTap(node=self, name=name, localname=localname, start=self.up)
try:
self.addnetif(tuntap, ifindex)
@ -1057,7 +1047,8 @@ class CoreNetworkBase(NodeBase):
:param int _id: object id
:param str name: object name
:param bool start: should object start
:param str server: remote server node will run on, default is None for localhost
:param fabric.connection.Connection server: remote server node will run on,
default is None for localhost
"""
super(CoreNetworkBase, self).__init__(session, _id, name, start, server)
self._linked = {}

View file

@ -7,6 +7,7 @@ import time
from builtins import int, range
from core import utils
from core.emulator import distributed
from core.errors import CoreCommandError
from core.nodes.netclient import LinuxNetClient
@ -16,13 +17,15 @@ class CoreInterface(object):
Base class for network interfaces.
"""
def __init__(self, node, name, mtu):
def __init__(self, node, name, mtu, server=None):
"""
Creates a PyCoreNetIf instance.
:param core.nodes.base.CoreNode node: node for interface
:param str name: interface name
:param mtu: mtu value
:param fabric.connection.Connection server: remote server node will run on,
default is None for localhost
"""
self.node = node
@ -42,7 +45,15 @@ class CoreInterface(object):
self.netindex = None
# index used to find flow data
self.flow_id = None
self.net_client = LinuxNetClient(utils.check_cmd)
self.server = server
self.net_client = LinuxNetClient(self.net_cmd)
def net_cmd(self, args):
if self.server is None:
return utils.check_cmd(args)
else:
args = " ".join(args)
return distributed.remote_cmd(self.server, args)
def startup(self):
"""
@ -191,8 +202,7 @@ class Veth(CoreInterface):
Provides virtual ethernet functionality for core nodes.
"""
# TODO: network is not used, why was it needed?
def __init__(self, node, name, localname, mtu=1500, net=None, start=True):
def __init__(self, node, name, localname, mtu=1500, server=None, start=True):
"""
Creates a VEth instance.
@ -200,12 +210,13 @@ class Veth(CoreInterface):
:param str name: interface name
:param str localname: interface local name
:param mtu: interface mtu
:param net: network
:param fabric.connection.Connection server: remote server node will run on,
default is None for localhost
:param bool start: start flag
:raises CoreCommandError: when there is a command exception
"""
# note that net arg is ignored
CoreInterface.__init__(self, node=node, name=name, mtu=mtu)
CoreInterface.__init__(self, node, name, mtu, server)
self.localname = localname
self.up = False
if start:
@ -251,8 +262,7 @@ class TunTap(CoreInterface):
TUN/TAP virtual device in TAP mode
"""
# TODO: network is not used, why was it needed?
def __init__(self, node, name, localname, mtu=1500, net=None, start=True):
def __init__(self, node, name, localname, mtu=1500, server=None, start=True):
"""
Create a TunTap instance.
@ -260,10 +270,11 @@ class TunTap(CoreInterface):
:param str name: interface name
:param str localname: local interface name
:param mtu: interface mtu
:param core.nodes.base.CoreNetworkBase net: related network
:param fabric.connection.Connection server: remote server node will run on,
default is None for localhost
:param bool start: start flag
"""
CoreInterface.__init__(self, node=node, name=name, mtu=mtu)
CoreInterface.__init__(self, node, name, mtu, server)
self.localname = localname
self.up = False
self.transport_type = "virtual"
@ -427,6 +438,7 @@ class GreTap(CoreInterface):
ttl=255,
key=None,
start=True,
server=None,
):
"""
Creates a GreTap instance.
@ -441,9 +453,11 @@ class GreTap(CoreInterface):
:param ttl: ttl value
:param key: gre tap key
:param bool start: start flag
:param fabric.connection.Connection server: remote server node will run on,
default is None for localhost
:raises CoreCommandError: when there is a command exception
"""
CoreInterface.__init__(self, node=node, name=name, mtu=mtu)
CoreInterface.__init__(self, node, name, mtu, server)
self.session = session
if _id is None:
# from PyCoreObj
@ -460,9 +474,13 @@ class GreTap(CoreInterface):
if remoteip is None:
raise ValueError("missing remote IP required for GRE TAP device")
self.net_client.create_gretap(
self.localname, str(remoteip), str(localip), str(ttl), str(key)
)
if localip is not None:
localip = str(localip)
if ttl is not None:
ttl = str(ttl)
if key is not None:
key = str(key)
self.net_client.create_gretap(self.localname, str(remoteip), localip, ttl, key)
self.net_client.device_up(self.localname)
self.up = True

View file

@ -10,6 +10,7 @@ import time
from socket import AF_INET, AF_INET6
from core import constants, utils
from core.emulator import distributed
from core.emulator.data import LinkData
from core.emulator.enumerations import LinkTypes, NodeTypes, RegisterTlvs
from core.errors import CoreCommandError, CoreError
@ -291,7 +292,8 @@ class CoreNetwork(CoreNetworkBase):
:param int _id: object id
:param str name: object name
:param bool start: start flag
:param str server: remote server node will run on, default is None for localhost
:param fabric.connection.Connection server: remote server node will run on,
default is None for localhost
:param policy: network policy
"""
CoreNetworkBase.__init__(self, session, _id, name, start, server)
@ -307,6 +309,27 @@ class CoreNetwork(CoreNetworkBase):
self.startup()
ebq.startupdateloop(self)
def net_cmd(self, args, env=None):
"""
Runs a command that is used to configure and setup the network on the host
system.
:param list[str]|str args: command to run
:param dict env: environment to run command with
:return: combined stdout and stderr
:rtype: str
:raises CoreCommandError: when a non-zero exit status occurs
"""
logging.info("network node(%s) cmd", self.name)
output = utils.check_cmd(args, env=env)
args = " ".join(args)
for server in self.session.servers:
conn = self.session.servers[server]
distributed.remote_cmd(conn, args, env=env)
return output
def startup(self):
"""
Linux bridge starup logic.
@ -381,11 +404,11 @@ class CoreNetwork(CoreNetworkBase):
"""
Attach a network interface.
:param core.netns.vnode.VEth netif: network interface to attach
:param core.nodes.interface.Veth netif: network interface to attach
:return: nothing
"""
if self.up:
self.net_client.create_interface(self.brname, netif.localname)
netif.net_client.create_interface(self.brname, netif.localname)
CoreNetworkBase.attach(self, netif)
@ -397,7 +420,7 @@ class CoreNetwork(CoreNetworkBase):
:return: nothing
"""
if self.up:
self.net_client.delete_interface(self.brname, netif.localname)
netif.net_client.delete_interface(self.brname, netif.localname)
CoreNetworkBase.detach(self, netif)
@ -591,13 +614,11 @@ class CoreNetwork(CoreNetworkBase):
if len(name) >= 16:
raise ValueError("interface name %s too long" % name)
netif = Veth(
node=None, name=name, localname=localname, mtu=1500, net=self, start=self.up
)
netif = Veth(node=None, name=name, localname=localname, mtu=1500, start=self.up)
self.attach(netif)
if net.up:
# this is similar to net.attach() but uses netif.name instead of localname
self.net_client.create_interface(net.brname, netif.name)
netif.net_client.create_interface(net.brname, netif.name)
i = net.newifindex()
net._netif[i] = netif
with net._linked_lock:
@ -666,6 +687,8 @@ class GreTapBridge(CoreNetwork):
:param ttl: ttl value
:param key: gre tap key
:param bool start: start flag
:param fabric.connection.Connection server: remote server node will run on,
default is None for localhost
"""
CoreNetwork.__init__(self, session, _id, name, False, server, policy)
self.grekey = key