def addtunnel(self, remoteip, n1num, n2num, localnum): ''' Add a new GreTapBridge between nodes on two different machines. ''' key = self.tunnelkey(n1num, n2num) if localnum == n2num: remotenum = n1num else: remotenum = n2num if key in list(self.tunnels.keys()): self.session.warn("tunnel with key %s (%s-%s) already exists!" % \ (key, n1num, n2num)) else: objid = key & ((1<<16)-1) self.session.info("Adding tunnel for %s-%s to %s with key %s" % \ (n1num, n2num, remoteip, key)) if localnum in self.phys: # no bridge is needed on physical nodes; use the GreTap directly gt = GreTap(node=None, name=None, session=self.session, remoteip=remoteip, key=key) else: gt = self.session.addobj(cls = GreTapBridge, objid = objid, policy="ACCEPT", remoteip=remoteip, key = key) gt.localnum = localnum gt.remotenum = remotenum self.tunnels[key] = gt
def __init__(self, session, remoteip=None, objid=None, name=None, policy="ACCEPT", localip=None, ttl=255, key=None, start=True): """ Create a GreTapBridge instance. :param core.session.Session session: core session instance :param str remoteip: remote address :param int objid: object id :param str name: object name :param policy: network policy :param str localip: local address :param ttl: ttl value :param key: gre tap key :param bool start: start flag :return: """ LxBrNet.__init__(self, session=session, objid=objid, name=name, policy=policy, start=False) self.grekey = key if self.grekey is None: self.grekey = self.session.session_id ^ self.objid self.localnum = None self.remotenum = None self.remoteip = remoteip self.localip = localip self.ttl = ttl if remoteip is None: self.gretap = None else: self.gretap = GreTap(node=self, session=session, remoteip=remoteip, localip=localip, ttl=ttl, key=self.grekey) if start: self.startup()
def addtunnel(self, remoteip, n1num, n2num, localnum): ''' Add a new GreTapBridge between nodes on two different machines. ''' key = self.tunnelkey(n1num, n2num) if localnum == n2num: remotenum = n1num else: remotenum = n2num if key in self.tunnels.keys(): self.session.warn("tunnel with key %s (%s-%s) already exists!" % \ (key, n1num, n2num)) else: objid = key & ((1<<16)-1) self.session.info("Adding tunnel for %s-%s to %s with key %s" % \ (n1num, n2num, remoteip, key)) if localnum in self.phys: # no bridge is needed on physical nodes; use the GreTap directly gt = GreTap(node=None, name=None, session=self.session, remoteip=remoteip, key=key) else: gt = self.session.addobj(cls = GreTapBridge, objid = objid, policy="ACCEPT", remoteip=remoteip, key = key) gt.localnum = localnum gt.remotenum = remotenum self.tunnels[key] = gt
def addtunnel(self, remoteip, n1num, n2num, localnum): """ Adds a new GreTapBridge between nodes on two different machines. :param str remoteip: remote address for tunnel :param int n1num: node one id :param int n2num: node two id :param int localnum: local id :return: nothing """ key = self.tunnelkey(n1num, n2num) if localnum == n2num: remotenum = n1num else: remotenum = n2num if key in self.tunnels.keys(): logger.warn("tunnel with key %s (%s-%s) already exists!", key, n1num, n2num) else: objid = key & ((1 << 16) - 1) logger.info("adding tunnel for %s-%s to %s with key %s", n1num, n2num, remoteip, key) if localnum in self.physical_nodes: # no bridge is needed on physical nodes; use the GreTap directly gt = GreTap(node=None, name=None, session=self.session, remoteip=remoteip, key=key) else: gt = self.session.add_object(cls=GreTapBridge, objid=objid, policy="ACCEPT", remoteip=remoteip, key=key) gt.localnum = localnum gt.remotenum = remotenum self.tunnels[key] = gt
def newnetif(self, net = None, addrlist = [], hwaddr = None, ifindex = None, ifname = None): if self.up and net is None: raise NotImplementedError if ifindex is None: ifindex = self.newifindex() if self.up: # this is reached when this node is linked to a network node # tunnel to net not built yet, so build it now and adopt it gt = self.session.broker.addnettunnel(net.objid) if gt is None or len(gt) != 1: self.session.warn("Error building tunnel from PhysicalNode." "newnetif()") gt = gt[0] net.detach(gt) self.adoptnetif(gt, ifindex, hwaddr, addrlist) return ifindex # this is reached when configuring services (self.up=False) if ifname is None: ifname = "gt%d" % ifindex netif = GreTap(node = self, name = ifname, session = self.session, start = False) self.adoptnetif(netif, ifindex, hwaddr, addrlist) return ifindex
def addrconfig(self, addrlist): ''' Set the remote tunnel endpoint. This is a one-time method for creating the GreTap device, which requires the remoteip at startup. The 1st address in the provided list is remoteip, 2nd optionally specifies localip. ''' if self.gretap: raise ValueError, "gretap already exists for %s" % self.name remoteip = addrlist[0].split('/')[0] localip = None if len(addrlist) > 1: localip = addrlist[1].split('/')[0] self.gretap = GreTap(session = self.session, remoteip = remoteip, objid = None, name = None, localip = localip, ttl = self.ttl, key = self.grekey) self.attach(self.gretap)
def __init__(self, session, remoteip=None, objid=None, name=None, policy="ACCEPT", localip=None, ttl=255, key=None, start=True): OvsNet.__init__(self, session=session, objid=objid, name=name, policy=policy, start=False) self.grekey = key if self.grekey is None: self.grekey = self.session.session_id ^ self.objid self.localnum = None self.remotenum = None self.remoteip = remoteip self.localip = localip self.ttl = ttl if remoteip is None: self.gretap = None else: self.gretap = GreTap(node=self, session=session, remoteip=remoteip, localip=localip, ttl=ttl, key=self.grekey) if start: self.startup()
def addnettunnel(self, n): try: net = self.session.obj(n) except KeyError: raise KeyError, "network node %s not found" % n # add other nets here that do not require tunnels if isinstance(net, EmaneNet): return None if isinstance(net, CtrlNet): if hasattr(net, 'serverintf'): if net.serverintf is not None: return None servers = self.getserversbynode(n) if len(servers) < 2: return None hosts = [] for server in servers: if server.host is None: continue hosts.append(server.host) if len(hosts) == 0: # get IP address from API message sender (master) self.session._handlerslock.acquire() for h in self.session._handlers: if h.client_address != "": hosts.append(h.client_address[0]) self.session._handlerslock.release() r = [] for host in hosts: if self.myip: # we are the remote emulation server myip = self.myip else: # we are the session master myip = host key = self.tunnelkey(n, IPAddr.toint(myip)) if key in self.tunnels.keys(): continue self.session.info("Adding tunnel for net %s to %s with key %s" % \ (n, host, key)) gt = GreTap(node=None, name=None, session=self.session, remoteip=host, key=key) self.tunnels[key] = gt r.append(gt) # attaching to net will later allow gt to be destroyed # during net.shutdown() net.attach(gt) return r
class GreTapBridge(LxBrNet): """ A network consisting of a bridge with a gretap device for tunneling to another system. """ def __init__(self, session, remoteip=None, objid=None, name=None, policy="ACCEPT", localip=None, ttl=255, key=None, start=True): """ Create a GreTapBridge instance. :param core.session.Session session: core session instance :param str remoteip: remote address :param int objid: object id :param str name: object name :param policy: network policy :param str localip: local address :param ttl: ttl value :param key: gre tap key :param bool start: start flag :return: """ LxBrNet.__init__(self, session=session, objid=objid, name=name, policy=policy, start=False) self.grekey = key if self.grekey is None: self.grekey = self.session.session_id ^ self.objid self.localnum = None self.remotenum = None self.remoteip = remoteip self.localip = localip self.ttl = ttl if remoteip is None: self.gretap = None else: self.gretap = GreTap(node=self, session=session, remoteip=remoteip, localip=localip, ttl=ttl, key=self.grekey) if start: self.startup() def startup(self): """ Creates a bridge and adds the gretap device to it. :return: nothing """ LxBrNet.startup(self) if self.gretap: self.attach(self.gretap) def shutdown(self): """ Detach the gretap device and remove the bridge. :return: nothing """ if self.gretap: self.detach(self.gretap) self.gretap.shutdown() self.gretap = None LxBrNet.shutdown(self) def addrconfig(self, addrlist): """ Set the remote tunnel endpoint. This is a one-time method for creating the GreTap device, which requires the remoteip at startup. The 1st address in the provided list is remoteip, 2nd optionally specifies localip. :param list addrlist: address list :return: nothing """ if self.gretap: raise ValueError("gretap already exists for %s" % self.name) remoteip = addrlist[0].split("/")[0] localip = None if len(addrlist) > 1: localip = addrlist[1].split("/")[0] self.gretap = GreTap(session=self.session, remoteip=remoteip, localip=localip, ttl=self.ttl, key=self.grekey) self.attach(self.gretap) def setkey(self, key): """ 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 :return: nothing """ self.grekey = key
class GreTapBridge(LxBrNet): ''' A network consisting of a bridge with a gretap device for tunneling to another system. ''' def __init__(self, session, remoteip=None, objid=None, name=None, policy="ACCEPT", localip=None, ttl=255, key=None, verbose=False, start=True): LxBrNet.__init__(self, session=session, objid=objid, name=name, verbose=verbose, policy=policy, start=False) self.grekey = key if self.grekey is None: self.grekey = self.session.sessionid ^ self.objid self.localnum = None self.remotenum = None self.remoteip = remoteip self.localip = localip self.ttl = ttl if remoteip is None: self.gretap = None else: self.gretap = GreTap(node=self, name=None, session=session, remoteip=remoteip, objid=None, localip=localip, ttl=ttl, key=self.grekey) if start: self.startup() def startup(self): ''' Creates a bridge and adds the gretap device to it. ''' LxBrNet.startup(self) if self.gretap: self.attach(self.gretap) def shutdown(self): ''' Detach the gretap device and remove the bridge. ''' if self.gretap: self.detach(self.gretap) self.gretap.shutdown() self.gretap = None LxBrNet.shutdown(self) def addrconfig(self, addrlist): ''' Set the remote tunnel endpoint. This is a one-time method for creating the GreTap device, which requires the remoteip at startup. The 1st address in the provided list is remoteip, 2nd optionally specifies localip. ''' if self.gretap: raise ValueError, "gretap already exists for %s" % self.name remoteip = addrlist[0].split('/')[0] localip = None if len(addrlist) > 1: localip = addrlist[1].split('/')[0] self.gretap = GreTap(session=self.session, remoteip=remoteip, objid=None, name=None, localip=localip, ttl=self.ttl, key=self.grekey) self.attach(self.gretap) def setkey(self, key): ''' Set the GRE key used for the GreTap device. This needs to be set prior to instantiating the GreTap device (before addrconfig). ''' self.grekey = key
def addnettunnel(self, node_id): """ Add network tunnel between node and broker. :param int node_id: node id of network to add tunnel to :return: list of gre taps :rtype: list """ try: net = self.session.get_object(node_id) logger.info("adding net tunnel for: id(%s) %s", node_id, net) except KeyError: raise KeyError("network node %s not found" % node_id) # add other nets here that do not require tunnels if nodeutils.is_node(net, NodeTypes.EMANE_NET): logger.warn("emane network does not require a tunnel") return None server_interface = getattr(net, "serverintf", None) if nodeutils.is_node(net, NodeTypes.CONTROL_NET) and server_interface is not None: logger.warn("control networks with server interfaces do not need a tunnel") return None servers = self.getserversbynode(node_id) if len(servers) < 2: logger.warn("not enough servers to create a tunnel: %s", servers) return None hosts = [] for server in servers: if server.host is None: continue logger.info("adding server host for net tunnel: %s", server.host) hosts.append(server.host) if len(hosts) == 0: for session_client in self.session_clients: # get IP address from API message sender (master) if session_client.client_address != "": address = session_client.client_address[0] logger.info("adding session_client host: %s", address) hosts.append(address) r = [] for host in hosts: if self.myip: # we are the remote emulation server myip = self.myip else: # we are the session master myip = host key = self.tunnelkey(node_id, IpAddress.to_int(myip)) if key in self.tunnels.keys(): logger.info("tunnel already exists, returning existing tunnel: %s", key) gt = self.tunnels[key] r.append(gt) continue logger.info("adding tunnel for net %s to %s with key %s", node_id, host, key) gt = GreTap(node=None, name=None, session=self.session, remoteip=host, key=key) self.tunnels[key] = gt r.append(gt) # attaching to net will later allow gt to be destroyed # during net.shutdown() net.attach(gt) return r
class GreTapBridge(LxBrNet): ''' A network consisting of a bridge with a gretap device for tunneling to another system. ''' def __init__(self, session, remoteip = None, objid = None, name = None, policy = "ACCEPT", localip = None, ttl = 255, key = None, verbose = False, start = True): LxBrNet.__init__(self, session = session, objid = objid, name = name, verbose = verbose, policy = policy, start = False) self.grekey = key if self.grekey is None: self.grekey = self.session.sessionid ^ self.objid self.localnum = None self.remotenum = None self.remoteip = remoteip self.localip = localip self.ttl = ttl if remoteip is None: self.gretap = None else: self.gretap = GreTap(node = self, name = None, session = session, remoteip = remoteip, objid = None, localip = localip, ttl = ttl, key = self.grekey) if start: self.startup() def startup(self): ''' Creates a bridge and adds the gretap device to it. ''' LxBrNet.startup(self) if self.gretap: self.attach(self.gretap) def shutdown(self): ''' Detach the gretap device and remove the bridge. ''' if self.gretap: self.detach(self.gretap) self.gretap.shutdown() self.gretap = None LxBrNet.shutdown(self) def addrconfig(self, addrlist): ''' Set the remote tunnel endpoint. This is a one-time method for creating the GreTap device, which requires the remoteip at startup. The 1st address in the provided list is remoteip, 2nd optionally specifies localip. ''' if self.gretap: raise ValueError, "gretap already exists for %s" % self.name remoteip = addrlist[0].split('/')[0] localip = None if len(addrlist) > 1: localip = addrlist[1].split('/')[0] self.gretap = GreTap(session = self.session, remoteip = remoteip, objid = None, name = None, localip = localip, ttl = self.ttl, key = self.grekey) self.attach(self.gretap) def setkey(self, key): ''' Set the GRE key used for the GreTap device. This needs to be set prior to instantiating the GreTap device (before addrconfig). ''' self.grekey = key