quick pass for small cleanup within bsd nodes
This commit is contained in:
parent
1f9a8879c1
commit
78ff7f2189
4 changed files with 146 additions and 116 deletions
|
@ -16,14 +16,16 @@ from core.bsd.netgraph import createngnode
|
|||
from core.bsd.netgraph import destroyngnode
|
||||
from core.bsd.netgraph import ngmessage
|
||||
from core.coreobj import PyCoreNet
|
||||
from core.misc import log
|
||||
|
||||
logger = log.get_logger(__name__)
|
||||
|
||||
|
||||
class NetgraphNet(PyCoreNet):
|
||||
ngtype = None
|
||||
nghooks = ()
|
||||
|
||||
def __init__(self, session, objid=None, name=None, verbose=False,
|
||||
start=True, policy=None):
|
||||
def __init__(self, session, objid=None, name=None, start=True, policy=None):
|
||||
PyCoreNet.__init__(self, session, objid, name)
|
||||
if name is None:
|
||||
name = str(self.objid)
|
||||
|
@ -32,7 +34,6 @@ class NetgraphNet(PyCoreNet):
|
|||
self.name = name
|
||||
self.ngname = "n_%s_%s" % (str(self.objid), self.session.session_id)
|
||||
self.ngid = None
|
||||
self.verbose = verbose
|
||||
self._netif = {}
|
||||
self._linked = {}
|
||||
self.up = False
|
||||
|
@ -40,7 +41,7 @@ class NetgraphNet(PyCoreNet):
|
|||
self.startup()
|
||||
|
||||
def startup(self):
|
||||
tmp, self.ngid = createngnode(type=self.ngtype, hookstr=self.nghooks, name=self.ngname)
|
||||
tmp, self.ngid = createngnode(node_type=self.ngtype, hookstr=self.nghooks, name=self.ngname)
|
||||
self.up = True
|
||||
|
||||
def shutdown(self):
|
||||
|
@ -61,12 +62,13 @@ class NetgraphNet(PyCoreNet):
|
|||
destroyngnode(self.ngname)
|
||||
|
||||
def attach(self, netif):
|
||||
""" Attach an interface to this netgraph node. Create a pipe between
|
||||
the interface and the hub/switch/wlan node.
|
||||
(Note that the PtpNet subclass overrides this method.)
|
||||
"""
|
||||
Attach an interface to this netgraph node. Create a pipe between
|
||||
the interface and the hub/switch/wlan node.
|
||||
(Note that the PtpNet subclass overrides this method.)
|
||||
"""
|
||||
if self.up:
|
||||
pipe = self.session.addobj(cls=NetgraphPipeNet, verbose=self.verbose, start=True)
|
||||
pipe = self.session.addobj(cls=NetgraphPipeNet, start=True)
|
||||
pipe.attach(netif)
|
||||
hook = "link%d" % len(self._netif)
|
||||
pipe.attachnet(self, hook)
|
||||
|
@ -107,17 +109,19 @@ class NetgraphNet(PyCoreNet):
|
|||
self._linked[netif1][netif2] = True
|
||||
|
||||
def linknet(self, net):
|
||||
""" Link this bridge with another by creating a veth pair and installing
|
||||
each device into each bridge.
|
||||
"""
|
||||
Link this bridge with another by creating a veth pair and installing
|
||||
each device into each bridge.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def linkconfig(self, netif, bw=None, delay=None,
|
||||
loss=None, duplicate=None, jitter=None, netif2=None):
|
||||
""" Set link effects by modifying the pipe connected to an interface.
|
||||
"""
|
||||
Set link effects by modifying the pipe connected to an interface.
|
||||
"""
|
||||
if not netif.pipe:
|
||||
self.warn("linkconfig for %s but interface %s has no pipe" % (self.name, netif.name))
|
||||
logger.warn("linkconfig for %s but interface %s has no pipe", self.name, netif.name)
|
||||
return
|
||||
return netif.pipe.linkconfig(netif, bw, delay, loss, duplicate, jitter, netif2)
|
||||
|
||||
|
@ -126,41 +130,40 @@ class NetgraphPipeNet(NetgraphNet):
|
|||
ngtype = "pipe"
|
||||
nghooks = "upper lower"
|
||||
|
||||
def __init__(self, session, objid=None, name=None, verbose=False,
|
||||
start=True, policy=None):
|
||||
NetgraphNet.__init__(self, session, objid, name, verbose, start, policy)
|
||||
def __init__(self, session, objid=None, name=None, start=True, policy=None):
|
||||
NetgraphNet.__init__(self, session, objid, name, start, policy)
|
||||
if start:
|
||||
# account for Ethernet header
|
||||
ngmessage(self.ngname, ["setcfg", "{", "header_offset=14", "}"])
|
||||
|
||||
def attach(self, netif):
|
||||
""" Attach an interface to this pipe node.
|
||||
The first interface is connected to the "upper" hook, the second
|
||||
connected to the "lower" hook.
|
||||
"""
|
||||
Attach an interface to this pipe node.
|
||||
The first interface is connected to the "upper" hook, the second
|
||||
connected to the "lower" hook.
|
||||
"""
|
||||
if len(self._netif) > 1:
|
||||
raise ValueError, \
|
||||
"Netgraph pipes support at most 2 network interfaces"
|
||||
raise ValueError("Netgraph pipes support at most 2 network interfaces")
|
||||
if self.up:
|
||||
hook = self.gethook()
|
||||
connectngnodes(self.ngname, netif.localname, hook, netif.hook)
|
||||
if netif.pipe:
|
||||
raise ValueError, \
|
||||
"Interface %s already attached to pipe %s" % \
|
||||
(netif.name, netif.pipe.name)
|
||||
raise ValueError("Interface %s already attached to pipe %s" % (netif.name, netif.pipe.name))
|
||||
netif.pipe = self
|
||||
self._netif[netif] = netif
|
||||
self._linked[netif] = {}
|
||||
|
||||
def attachnet(self, net, hook):
|
||||
""" Attach another NetgraphNet to this pipe node.
|
||||
"""
|
||||
Attach another NetgraphNet to this pipe node.
|
||||
"""
|
||||
localhook = self.gethook()
|
||||
connectngnodes(self.ngname, net.ngname, localhook, hook)
|
||||
|
||||
def gethook(self):
|
||||
""" Returns the first hook (e.g. "upper") then the second hook
|
||||
(e.g. "lower") based on the number of connections.
|
||||
"""
|
||||
Returns the first hook (e.g. "upper") then the second hook
|
||||
(e.g. "lower") based on the number of connections.
|
||||
"""
|
||||
hooks = self.nghooks.split()
|
||||
if len(self._netif) == 0:
|
||||
|
@ -170,7 +173,8 @@ class NetgraphPipeNet(NetgraphNet):
|
|||
|
||||
def linkconfig(self, netif, bw=None, delay=None,
|
||||
loss=None, duplicate=None, jitter=None, netif2=None):
|
||||
""" Set link effects by sending a Netgraph setcfg message to the pipe.
|
||||
"""
|
||||
Set link effects by sending a Netgraph setcfg message to the pipe.
|
||||
"""
|
||||
netif.setparam("bw", bw)
|
||||
netif.setparam("delay", delay)
|
||||
|
@ -201,7 +205,7 @@ class NetgraphPipeNet(NetgraphNet):
|
|||
upstream += ["duplicate=%s" % duplicate, ]
|
||||
downstream += ["duplicate=%s" % duplicate, ]
|
||||
if jitter:
|
||||
self.warn("jitter parameter ignored for link %s" % self.name)
|
||||
logger.warn("jitter parameter ignored for link %s", self.name)
|
||||
if len(params) > 0 or len(upstream) > 0 or len(downstream) > 0:
|
||||
setcfg = ["setcfg", "{", ] + params
|
||||
if len(upstream) > 0:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue