daemon: refactored add_link,update_link,delete_link to have more specific logic, refactored CoreNodeBase to have newnetif and for it to return the interface created

This commit is contained in:
Blake Harnden 2020-06-11 13:59:29 -07:00
parent 9ed42cfba8
commit ccf2646c00
6 changed files with 232 additions and 355 deletions

View file

@ -355,10 +355,11 @@ class CoreNodeBase(NodeBase):
:return: nothing
"""
if ifindex not in self._netif:
raise ValueError(f"ifindex {ifindex} does not exist")
raise CoreError(f"node({self.name}) ifindex({ifindex}) does not exist")
netif = self._netif.pop(ifindex)
logging.info("node(%s) removing interface(%s)", self.name, netif.name)
netif.detachnet()
netif.shutdown()
del netif
def netif(self, ifindex: int) -> Optional[CoreInterface]:
"""
@ -473,6 +474,18 @@ class CoreNodeBase(NodeBase):
"""
raise NotImplementedError
def newnetif(
self, net: "CoreNetworkBase", interface: InterfaceData
) -> CoreInterface:
"""
Create a new network interface.
:param net: network to associate with
:param interface: interface data for new interface
:return: interface index
"""
raise NotImplementedError
class CoreNode(CoreNodeBase):
"""
@ -846,7 +859,9 @@ class CoreNode(CoreNodeBase):
interface_name = self.ifname(ifindex)
self.node_net_client.device_up(interface_name)
def newnetif(self, net: "CoreNetworkBase", interface: InterfaceData) -> int:
def newnetif(
self, net: "CoreNetworkBase", interface: InterfaceData
) -> CoreInterface:
"""
Create a new network interface.
@ -868,16 +883,16 @@ class CoreNode(CoreNodeBase):
netif.sethwaddr(interface.mac)
for address in addresses:
netif.addaddr(address)
return ifindex
else:
ifindex = self.newveth(interface.id, interface.name)
self.attachnet(ifindex, net)
if interface.mac:
self.sethwaddr(ifindex, interface.mac)
for address in addresses:
self.addaddr(ifindex, address)
self.ifup(ifindex)
return ifindex
self.attachnet(ifindex, net)
if interface.mac:
self.sethwaddr(ifindex, interface.mac)
for address in addresses:
self.addaddr(ifindex, address)
self.ifup(ifindex)
netif = self.netif(ifindex)
return netif
def addfile(self, srcname: str, filename: str) -> None:
"""

View file

@ -12,7 +12,7 @@ import netaddr
from core import utils
from core.constants import EBTABLES_BIN, TC_BIN
from core.emulator.data import LinkData, NodeData
from core.emulator.emudata import LinkOptions
from core.emulator.emudata import InterfaceData, LinkOptions
from core.emulator.enumerations import (
LinkTypes,
MessageFlags,
@ -697,15 +697,19 @@ class GreTapBridge(CoreNetwork):
)
self.attach(self.gretap)
def setkey(self, key: int) -> None:
def setkey(self, key: int, interface_data: InterfaceData) -> None:
"""
Set the GRE key used for the GreTap device. This needs to be set
prior to instantiating the GreTap device (before addrconfig).
:param key: gre key
:param interface_data: interface data for setting up tunnel key
:return: nothing
"""
self.grekey = key
addresses = interface_data.get_addresses()
if addresses:
self.addrconfig(addresses)
class CtrlNet(CoreNetwork):

View file

@ -157,7 +157,7 @@ class PhysicalNode(CoreNodeBase):
self.ifindex += 1
return ifindex
def newnetif(self, net: CoreNetworkBase, interface: InterfaceData) -> int:
def newnetif(self, net: CoreNetworkBase, interface: InterfaceData) -> CoreInterface:
logging.info("creating interface")
addresses = interface.get_addresses()
ifindex = interface.id
@ -171,12 +171,12 @@ class PhysicalNode(CoreNodeBase):
# tunnel to net not built yet, so build it now and adopt it
_, remote_tap = self.session.distributed.create_gre_tunnel(net, self.server)
self.adoptnetif(remote_tap, ifindex, interface.mac, addresses)
return ifindex
return remote_tap
else:
# this is reached when configuring services (self.up=False)
netif = GreTap(node=self, name=name, session=self.session, start=False)
self.adoptnetif(netif, ifindex, interface.mac, addresses)
return ifindex
return netif
def privatedir(self, path: str) -> None:
if path[0] != "/":
@ -297,7 +297,7 @@ class Rj45Node(CoreNodeBase):
self.up = False
self.restorestate()
def newnetif(self, net: CoreNetworkBase, interface: InterfaceData) -> int:
def newnetif(self, net: CoreNetworkBase, interface: InterfaceData) -> CoreInterface:
"""
This is called when linking with another node. Since this node
represents an interface, we do not create another object here,
@ -320,7 +320,7 @@ class Rj45Node(CoreNodeBase):
self.interface.attachnet(net)
for addr in interface.get_addresses():
self.addaddr(addr)
return ifindex
return self.interface
def delnetif(self, ifindex: int) -> None:
"""