def __init__( self, session: "Session", remoteip: str = None, _id: int = None, name: str = None, policy: str = "ACCEPT", localip: str = None, ttl: int = 255, key: int = None, start: bool = True, server: "DistributedServer" = None, ) -> None: """ Create a GreTapBridge instance. :param session: core session instance :param remoteip: remote address :param _id: object id :param name: object name :param policy: network policy :param localip: local address :param ttl: ttl value :param key: gre tap key :param start: start flag :param server: remote server node will run on, default is None for localhost """ CoreNetwork.__init__(self, session, _id, name, False, server, policy) self.grekey = key if self.grekey is None: self.grekey = self.session.id ^ self.id 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 create_gre_tunnel(self, node: CoreNetwork, server: DistributedServer) -> Tuple[GreTap, GreTap]: """ Create gre tunnel using a pair of gre taps between the local and remote server. :param node: node to create gre tunnel for :param server: server to create tunnel for :return: local and remote gre taps created for tunnel """ host = server.host key = self.tunnel_key(node.id, netaddr.IPAddress(host).value) tunnel = self.tunnels.get(key) if tunnel is not None: return tunnel # local to server logging.info("local tunnel node(%s) to remote(%s) key(%s)", node.name, host, key) local_tap = GreTap(session=self.session, remoteip=host, key=key) local_tap.net_client.create_interface(node.brname, local_tap.localname) # server to local logging.info("remote tunnel node(%s) to local(%s) key(%s)", node.name, self.address, key) remote_tap = GreTap(session=self.session, remoteip=self.address, key=key, server=server) remote_tap.net_client.create_interface(node.brname, remote_tap.localname) # save tunnels for shutdown tunnel = (local_tap, remote_tap) self.tunnels[key] = tunnel return tunnel
def create_gre_tunnel(self, node, server): """ Create gre tunnel using a pair of gre taps between the local and remote server. :param core.nodes.network.CoreNetwork node: node to create gre tunnel for :param core.emulator.distributed.DistributedServer server: server to create tunnel for :return: local and remote gre taps created for tunnel :rtype: tuple """ host = server.host key = self.tunnel_key(node.id, IpAddress.to_int(host)) tunnel = self.tunnels.get(key) if tunnel is not None: return tunnel # local to server logging.info("local tunnel node(%s) to remote(%s) key(%s)", node.name, host, key) local_tap = GreTap(session=self.session, remoteip=host, key=key) local_tap.net_client.create_interface(node.brname, local_tap.localname) # server to local logging.info("remote tunnel node(%s) to local(%s) key(%s)", node.name, self.address, key) remote_tap = GreTap(session=self.session, remoteip=self.address, key=key, server=server) remote_tap.net_client.create_interface(node.brname, remote_tap.localname) # save tunnels for shutdown tunnel = (local_tap, remote_tap) self.tunnels[key] = tunnel return tunnel
def __init__(self, session, remoteip=None, _id=None, name=None, policy="ACCEPT", localip=None, ttl=255, key=None, start=True): """ Create a GreTapBridge instance. :param core.emulator.session.Session session: core session instance :param str remoteip: remote address :param int _id: 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 """ CoreNetwork.__init__(self, session=session, _id=_id, name=name, policy=policy, start=False) self.grekey = key if self.grekey is None: self.grekey = self.session.id ^ self.id 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 __init__( self, session: "Session", remoteip: str = None, _id: int = None, name: str = None, policy: NetworkPolicy = NetworkPolicy.ACCEPT, localip: str = None, ttl: int = 255, key: int = None, server: "DistributedServer" = None, ) -> None: """ Create a GreTapBridge instance. :param session: core session instance :param remoteip: remote address :param _id: object id :param name: object name :param policy: network policy :param localip: local address :param ttl: ttl value :param key: gre tap key :param server: remote server node will run on, default is None for localhost """ CoreNetwork.__init__(self, session, _id, name, server, policy) if key is None: key = self.session.id ^ self.id self.grekey: int = key self.localnum: Optional[int] = None self.remotenum: Optional[int] = None self.remoteip: Optional[str] = remoteip self.localip: Optional[str] = localip self.ttl: int = ttl self.gretap: Optional[GreTap] = None if self.remoteip is not None: self.gretap = GreTap( session, remoteip, key=self.grekey, node=self, localip=localip, ttl=ttl, mtu=self.mtu, )
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(): logging.warning( "tunnel with key %s (%s-%s) already exists!", key, n1num, n2num ) else: _id = key & ((1 << 16) - 1) logging.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.create_node( cls=GreTapBridge, _id=_id, policy="ACCEPT", remoteip=remoteip, key=key, ) gt.localnum = localnum gt.remotenum = remotenum self.tunnels[key] = gt
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 __init__( self, session, remoteip=None, _id=None, name=None, policy="ACCEPT", localip=None, ttl=255, key=None, start=True, ): OvsNet.__init__(self, session=session, _id=_id, name=name, policy=policy, start=False) self.grekey = key if self.grekey is None: self.grekey = self.session.id ^ self.id 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 add_ips(self, ips: List[str]) -> None: """ 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 ips: address list :return: nothing """ if self.gretap: raise ValueError(f"gretap already exists for {self.name}") remoteip = ips[0].split("/")[0] localip = None if len(ips) > 1: localip = ips[1].split("/")[0] self.gretap = GreTap( session=self.session, remoteip=remoteip, localip=localip, ttl=self.ttl, key=self.grekey, ) self.attach(self.gretap)
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_node(node_id) logging.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): logging.warning("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: logging.warning( "control networks with server interfaces do not need a tunnel") return None servers = self.getserversbynode(node_id) if len(servers) < 2: logging.warning("not enough servers to create a tunnel: %s", servers) return None hosts = [] for server in servers: if server.host is None: continue logging.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] logging.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(): logging.info( "tunnel already exists, returning existing tunnel: %s", key) gt = self.tunnels[key] r.append(gt) continue logging.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