changes to replaces known usages of ipaddress, leaving only ipaddress.MacAddress
This commit is contained in:
parent
f431254e15
commit
fd10663323
15 changed files with 354 additions and 380 deletions
|
@ -7,9 +7,10 @@ import threading
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
|
||||||
import grpc
|
import grpc
|
||||||
|
import netaddr
|
||||||
|
|
||||||
from core.api.grpc import core_pb2, core_pb2_grpc
|
from core.api.grpc import core_pb2, core_pb2_grpc
|
||||||
from core.nodes.ipaddress import Ipv4Prefix, Ipv6Prefix, MacAddress
|
from core.nodes.ipaddress import MacAddress
|
||||||
|
|
||||||
|
|
||||||
class InterfaceHelper:
|
class InterfaceHelper:
|
||||||
|
@ -30,10 +31,10 @@ class InterfaceHelper:
|
||||||
|
|
||||||
self.ip4 = None
|
self.ip4 = None
|
||||||
if ip4_prefix:
|
if ip4_prefix:
|
||||||
self.ip4 = Ipv4Prefix(ip4_prefix)
|
self.ip4 = netaddr.IPNetwork(ip4_prefix)
|
||||||
self.ip6 = None
|
self.ip6 = None
|
||||||
if ip6_prefix:
|
if ip6_prefix:
|
||||||
self.ip6 = Ipv6Prefix(ip6_prefix)
|
self.ip6 = netaddr.IPNetwork(ip6_prefix)
|
||||||
|
|
||||||
def ip4_address(self, node_id):
|
def ip4_address(self, node_id):
|
||||||
"""
|
"""
|
||||||
|
@ -45,7 +46,7 @@ class InterfaceHelper:
|
||||||
"""
|
"""
|
||||||
if not self.ip4:
|
if not self.ip4:
|
||||||
raise ValueError("ip4 prefixes have not been set")
|
raise ValueError("ip4 prefixes have not been set")
|
||||||
return str(self.ip4.addr(node_id))
|
return str(self.ip4[node_id])
|
||||||
|
|
||||||
def ip6_address(self, node_id):
|
def ip6_address(self, node_id):
|
||||||
"""
|
"""
|
||||||
|
@ -57,7 +58,7 @@ class InterfaceHelper:
|
||||||
"""
|
"""
|
||||||
if not self.ip6:
|
if not self.ip6:
|
||||||
raise ValueError("ip6 prefixes have not been set")
|
raise ValueError("ip6 prefixes have not been set")
|
||||||
return str(self.ip6.addr(node_id))
|
return str(self.ip6[node_id])
|
||||||
|
|
||||||
def create_interface(self, node_id, interface_id, name=None, mac=None):
|
def create_interface(self, node_id, interface_id, name=None, mac=None):
|
||||||
"""
|
"""
|
||||||
|
@ -75,14 +76,14 @@ class InterfaceHelper:
|
||||||
ip4 = None
|
ip4 = None
|
||||||
ip4_mask = None
|
ip4_mask = None
|
||||||
if self.ip4:
|
if self.ip4:
|
||||||
ip4 = str(self.ip4.addr(node_id))
|
ip4 = self.ip4_address(node_id)
|
||||||
ip4_mask = self.ip4.prefixlen
|
ip4_mask = self.ip4.prefixlen
|
||||||
|
|
||||||
# generate ip6 data
|
# generate ip6 data
|
||||||
ip6 = None
|
ip6 = None
|
||||||
ip6_mask = None
|
ip6_mask = None
|
||||||
if self.ip6:
|
if self.ip6:
|
||||||
ip6 = str(self.ip6.addr(node_id))
|
ip6 = self.ip6_address(node_id)
|
||||||
ip6_mask = self.ip6.prefixlen
|
ip6_mask = self.ip6.prefixlen
|
||||||
|
|
||||||
# random mac
|
# random mac
|
||||||
|
|
|
@ -24,7 +24,7 @@ from core.emulator.enumerations import (
|
||||||
RegisterTlvs,
|
RegisterTlvs,
|
||||||
SessionTlvs,
|
SessionTlvs,
|
||||||
)
|
)
|
||||||
from core.nodes.ipaddress import IpAddress, MacAddress
|
from core.nodes.ipaddress import MacAddress
|
||||||
|
|
||||||
|
|
||||||
class CoreTlvData:
|
class CoreTlvData:
|
||||||
|
@ -258,7 +258,7 @@ class CoreTlvDataIpv4Addr(CoreTlvDataObj):
|
||||||
Utility class for packing/unpacking Ipv4 addresses.
|
Utility class for packing/unpacking Ipv4 addresses.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
data_type = IpAddress.from_string
|
data_type = str
|
||||||
data_format = "!2x4s"
|
data_format = "!2x4s"
|
||||||
pad_len = 2
|
pad_len = 2
|
||||||
|
|
||||||
|
@ -267,21 +267,22 @@ class CoreTlvDataIpv4Addr(CoreTlvDataObj):
|
||||||
"""
|
"""
|
||||||
Retrieve Ipv4 address value from object.
|
Retrieve Ipv4 address value from object.
|
||||||
|
|
||||||
:param core.misc.ipaddress.IpAddress obj: ip address to get value from
|
:param str obj: ip address to get value from
|
||||||
:return:
|
:return: packed address
|
||||||
|
:rtype: bytes
|
||||||
"""
|
"""
|
||||||
return obj.addr
|
return socket.inet_pton(socket.AF_INET, obj)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def new_obj(value):
|
def new_obj(value):
|
||||||
"""
|
"""
|
||||||
Retrieve Ipv4 address from a string representation.
|
Retrieve Ipv4 address from a string representation.
|
||||||
|
|
||||||
:param str value: value to get Ipv4 address from
|
:param bytes value: value to get Ipv4 address from
|
||||||
:return: Ipv4 address
|
:return: Ipv4 address
|
||||||
:rtype: core.nodes.ipaddress.IpAddress
|
:rtype: str
|
||||||
"""
|
"""
|
||||||
return IpAddress(af=socket.AF_INET, address=value)
|
return socket.inet_ntop(socket.AF_INET, value)
|
||||||
|
|
||||||
|
|
||||||
class CoreTlvDataIPv6Addr(CoreTlvDataObj):
|
class CoreTlvDataIPv6Addr(CoreTlvDataObj):
|
||||||
|
@ -290,7 +291,7 @@ class CoreTlvDataIPv6Addr(CoreTlvDataObj):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
data_format = "!16s2x"
|
data_format = "!16s2x"
|
||||||
data_type = IpAddress.from_string
|
data_type = str
|
||||||
pad_len = 2
|
pad_len = 2
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -298,21 +299,22 @@ class CoreTlvDataIPv6Addr(CoreTlvDataObj):
|
||||||
"""
|
"""
|
||||||
Retrieve Ipv6 address value from object.
|
Retrieve Ipv6 address value from object.
|
||||||
|
|
||||||
:param core.nodes.ipaddress.IpAddress obj: ip address to get value from
|
:param str obj: ip address to get value from
|
||||||
:return:
|
:return: packed address
|
||||||
|
:rtype: bytes
|
||||||
"""
|
"""
|
||||||
return obj.addr
|
return socket.inet_pton(socket.AF_INET6, obj)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def new_obj(value):
|
def new_obj(value):
|
||||||
"""
|
"""
|
||||||
Retrieve Ipv6 address from a string representation.
|
Retrieve Ipv6 address from a string representation.
|
||||||
|
|
||||||
:param str value: value to get Ipv4 address from
|
:param bytes value: value to get Ipv4 address from
|
||||||
:return: Ipv4 address
|
:return: Ipv4 address
|
||||||
:rtype: core.nodes.ipaddress.IpAddress
|
:rtype: str
|
||||||
"""
|
"""
|
||||||
return IpAddress(af=socket.AF_INET6, address=value)
|
return socket.inet_ntop(socket.AF_INET6, value)
|
||||||
|
|
||||||
|
|
||||||
class CoreTlvDataMacAddr(CoreTlvDataObj):
|
class CoreTlvDataMacAddr(CoreTlvDataObj):
|
||||||
|
|
|
@ -8,13 +8,13 @@ import threading
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
|
|
||||||
|
import netaddr
|
||||||
from fabric import Connection
|
from fabric import Connection
|
||||||
from invoke import UnexpectedExit
|
from invoke import UnexpectedExit
|
||||||
|
|
||||||
from core import utils
|
from core import utils
|
||||||
from core.errors import CoreCommandError
|
from core.errors import CoreCommandError
|
||||||
from core.nodes.interface import GreTap
|
from core.nodes.interface import GreTap
|
||||||
from core.nodes.ipaddress import IpAddress
|
|
||||||
from core.nodes.network import CoreNetwork, CtrlNet
|
from core.nodes.network import CoreNetwork, CtrlNet
|
||||||
|
|
||||||
LOCK = threading.Lock()
|
LOCK = threading.Lock()
|
||||||
|
@ -196,7 +196,7 @@ class DistributedController:
|
||||||
:rtype: tuple
|
:rtype: tuple
|
||||||
"""
|
"""
|
||||||
host = server.host
|
host = server.host
|
||||||
key = self.tunnel_key(node.id, IpAddress.to_int(host))
|
key = self.tunnel_key(node.id, netaddr.IPAddress(host).value)
|
||||||
tunnel = self.tunnels.get(key)
|
tunnel = self.tunnels.get(key)
|
||||||
if tunnel is not None:
|
if tunnel is not None:
|
||||||
return tunnel
|
return tunnel
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
import netaddr
|
||||||
|
|
||||||
from core.emane.nodes import EmaneNet
|
from core.emane.nodes import EmaneNet
|
||||||
from core.emulator.enumerations import LinkTypes
|
from core.emulator.enumerations import LinkTypes
|
||||||
from core.nodes.ipaddress import Ipv4Prefix, Ipv6Prefix, MacAddress
|
from core.nodes.ipaddress import MacAddress
|
||||||
from core.nodes.physical import PhysicalNode
|
from core.nodes.physical import PhysicalNode
|
||||||
|
|
||||||
|
|
||||||
|
@ -164,10 +166,10 @@ class IpPrefixes:
|
||||||
|
|
||||||
self.ip4 = None
|
self.ip4 = None
|
||||||
if ip4_prefix:
|
if ip4_prefix:
|
||||||
self.ip4 = Ipv4Prefix(ip4_prefix)
|
self.ip4 = netaddr.IPNetwork(ip4_prefix)
|
||||||
self.ip6 = None
|
self.ip6 = None
|
||||||
if ip6_prefix:
|
if ip6_prefix:
|
||||||
self.ip6 = Ipv6Prefix(ip6_prefix)
|
self.ip6 = netaddr.IPNetwork(ip6_prefix)
|
||||||
|
|
||||||
def ip4_address(self, node):
|
def ip4_address(self, node):
|
||||||
"""
|
"""
|
||||||
|
@ -179,7 +181,7 @@ class IpPrefixes:
|
||||||
"""
|
"""
|
||||||
if not self.ip4:
|
if not self.ip4:
|
||||||
raise ValueError("ip4 prefixes have not been set")
|
raise ValueError("ip4 prefixes have not been set")
|
||||||
return str(self.ip4.addr(node.id))
|
return str(self.ip4[node.id])
|
||||||
|
|
||||||
def ip6_address(self, node):
|
def ip6_address(self, node):
|
||||||
"""
|
"""
|
||||||
|
@ -191,7 +193,7 @@ class IpPrefixes:
|
||||||
"""
|
"""
|
||||||
if not self.ip6:
|
if not self.ip6:
|
||||||
raise ValueError("ip6 prefixes have not been set")
|
raise ValueError("ip6 prefixes have not been set")
|
||||||
return str(self.ip6.addr(node.id))
|
return str(self.ip6[node.id])
|
||||||
|
|
||||||
def create_interface(self, node, name=None, mac=None):
|
def create_interface(self, node, name=None, mac=None):
|
||||||
"""
|
"""
|
||||||
|
@ -212,14 +214,14 @@ class IpPrefixes:
|
||||||
ip4 = None
|
ip4 = None
|
||||||
ip4_mask = None
|
ip4_mask = None
|
||||||
if self.ip4:
|
if self.ip4:
|
||||||
ip4 = str(self.ip4.addr(node.id))
|
ip4 = self.ip4_address(node)
|
||||||
ip4_mask = self.ip4.prefixlen
|
ip4_mask = self.ip4.prefixlen
|
||||||
|
|
||||||
# generate ip6 data
|
# generate ip6 data
|
||||||
ip6 = None
|
ip6 = None
|
||||||
ip6_mask = None
|
ip6_mask = None
|
||||||
if self.ip6:
|
if self.ip6:
|
||||||
ip6 = str(self.ip6.addr(node.id))
|
ip6 = self.ip6_address(node)
|
||||||
ip6_mask = self.ip6.prefixlen
|
ip6_mask = self.ip6.prefixlen
|
||||||
|
|
||||||
# random mac
|
# random mac
|
||||||
|
|
|
@ -1764,7 +1764,7 @@ class Session:
|
||||||
control_ip = node.id
|
control_ip = node.id
|
||||||
|
|
||||||
try:
|
try:
|
||||||
address = control_net.prefix.addr(control_ip)
|
address = control_net.prefix[control_ip]
|
||||||
prefix = control_net.prefix.prefixlen
|
prefix = control_net.prefix.prefixlen
|
||||||
addrlist = [f"{address}/{prefix}"]
|
addrlist = [f"{address}/{prefix}"]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
|
|
@ -5,9 +5,7 @@ Defines the base logic for nodes used within core.
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import socket
|
|
||||||
import threading
|
import threading
|
||||||
from socket import AF_INET, AF_INET6
|
|
||||||
|
|
||||||
import netaddr
|
import netaddr
|
||||||
|
|
||||||
|
@ -16,7 +14,7 @@ from core.constants import MOUNT_BIN, VNODED_BIN
|
||||||
from core.emulator.data import LinkData, NodeData
|
from core.emulator.data import LinkData, NodeData
|
||||||
from core.emulator.enumerations import LinkTypes, NodeTypes
|
from core.emulator.enumerations import LinkTypes, NodeTypes
|
||||||
from core.errors import CoreCommandError
|
from core.errors import CoreCommandError
|
||||||
from core.nodes import client, ipaddress
|
from core.nodes import client
|
||||||
from core.nodes.interface import TunTap, Veth
|
from core.nodes.interface import TunTap, Veth
|
||||||
from core.nodes.netclient import get_net_client
|
from core.nodes.netclient import get_net_client
|
||||||
|
|
||||||
|
@ -741,25 +739,24 @@ class CoreNode(CoreNodeBase):
|
||||||
Add interface address.
|
Add interface address.
|
||||||
|
|
||||||
:param int ifindex: index of interface to add address to
|
:param int ifindex: index of interface to add address to
|
||||||
:param core.nodes.ipaddress.IpAddress addr: address to add to interface
|
:param str addr: address to add to interface
|
||||||
:return: nothing
|
:return: nothing
|
||||||
"""
|
"""
|
||||||
interface = self._netif[ifindex]
|
interface = self._netif[ifindex]
|
||||||
interface.addaddr(addr)
|
interface.addaddr(addr)
|
||||||
if self.up:
|
if self.up:
|
||||||
address = str(addr)
|
# ipv4 check
|
||||||
# ipv6 check
|
|
||||||
broadcast = None
|
broadcast = None
|
||||||
if ":" not in address:
|
if netaddr.valid_ipv4(addr):
|
||||||
broadcast = "+"
|
broadcast = "+"
|
||||||
self.node_net_client.create_address(interface.name, address, broadcast)
|
self.node_net_client.create_address(interface.name, addr, broadcast)
|
||||||
|
|
||||||
def deladdr(self, ifindex, addr):
|
def deladdr(self, ifindex, addr):
|
||||||
"""
|
"""
|
||||||
Delete address from an interface.
|
Delete address from an interface.
|
||||||
|
|
||||||
:param int ifindex: index of interface to delete address from
|
:param int ifindex: index of interface to delete address from
|
||||||
:param core.nodes.ipaddress.IpAddress addr: address to delete from interface
|
:param str addr: address to delete from interface
|
||||||
:return: nothing
|
:return: nothing
|
||||||
:raises CoreCommandError: when a non-zero exit status occurs
|
:raises CoreCommandError: when a non-zero exit status occurs
|
||||||
"""
|
"""
|
||||||
|
@ -771,7 +768,7 @@ class CoreNode(CoreNodeBase):
|
||||||
logging.exception("trying to delete unknown address: %s", addr)
|
logging.exception("trying to delete unknown address: %s", addr)
|
||||||
|
|
||||||
if self.up:
|
if self.up:
|
||||||
self.node_net_client.delete_address(interface.name, str(addr))
|
self.node_net_client.delete_address(interface.name, addr)
|
||||||
|
|
||||||
def ifup(self, ifindex):
|
def ifup(self, ifindex):
|
||||||
"""
|
"""
|
||||||
|
@ -1018,14 +1015,10 @@ class CoreNetworkBase(NodeBase):
|
||||||
ip, _sep, mask = address.partition("/")
|
ip, _sep, mask = address.partition("/")
|
||||||
mask = int(mask)
|
mask = int(mask)
|
||||||
if netaddr.valid_ipv4(ip):
|
if netaddr.valid_ipv4(ip):
|
||||||
family = AF_INET
|
interface2_ip4 = ip
|
||||||
ipl = socket.inet_pton(family, ip)
|
|
||||||
interface2_ip4 = ipaddress.IpAddress(af=family, address=ipl)
|
|
||||||
interface2_ip4_mask = mask
|
interface2_ip4_mask = mask
|
||||||
else:
|
else:
|
||||||
family = AF_INET6
|
interface2_ip6 = ip
|
||||||
ipl = socket.inet_pton(family, ip)
|
|
||||||
interface2_ip6 = ipaddress.IpAddress(af=family, address=ipl)
|
|
||||||
interface2_ip6_mask = mask
|
interface2_ip6_mask = mask
|
||||||
|
|
||||||
link_data = LinkData(
|
link_data = LinkData(
|
||||||
|
|
|
@ -202,255 +202,255 @@ class IpAddress:
|
||||||
return struct.unpack("!I", value)[0]
|
return struct.unpack("!I", value)[0]
|
||||||
|
|
||||||
|
|
||||||
class IpPrefix:
|
# class IpPrefix:
|
||||||
"""
|
# """
|
||||||
Provides ip address generation and prefix utilities.
|
# Provides ip address generation and prefix utilities.
|
||||||
"""
|
# """
|
||||||
|
#
|
||||||
def __init__(self, af, prefixstr):
|
# def __init__(self, af, prefixstr):
|
||||||
"""
|
# """
|
||||||
Create a IpPrefix instance.
|
# Create a IpPrefix instance.
|
||||||
|
#
|
||||||
:param int af: address family for ip prefix
|
# :param int af: address family for ip prefix
|
||||||
:param str prefixstr: ip prefix string
|
# :param str prefixstr: ip prefix string
|
||||||
"""
|
# """
|
||||||
# prefixstr format: address/prefixlen
|
# # prefixstr format: address/prefixlen
|
||||||
tmp = prefixstr.split("/")
|
# tmp = prefixstr.split("/")
|
||||||
if len(tmp) > 2:
|
# if len(tmp) > 2:
|
||||||
raise ValueError(f"invalid prefix: {prefixstr}")
|
# raise ValueError(f"invalid prefix: {prefixstr}")
|
||||||
self.af = af
|
# self.af = af
|
||||||
if self.af == AF_INET:
|
# if self.af == AF_INET:
|
||||||
self.addrlen = 32
|
# self.addrlen = 32
|
||||||
elif self.af == AF_INET6:
|
# elif self.af == AF_INET6:
|
||||||
self.addrlen = 128
|
# self.addrlen = 128
|
||||||
else:
|
# else:
|
||||||
raise ValueError(f"invalid address family: {self.af}")
|
# raise ValueError(f"invalid address family: {self.af}")
|
||||||
if len(tmp) == 2:
|
# if len(tmp) == 2:
|
||||||
self.prefixlen = int(tmp[1])
|
# self.prefixlen = int(tmp[1])
|
||||||
else:
|
# else:
|
||||||
self.prefixlen = self.addrlen
|
# self.prefixlen = self.addrlen
|
||||||
self.prefix = socket.inet_pton(self.af, tmp[0])
|
# self.prefix = socket.inet_pton(self.af, tmp[0])
|
||||||
self.prefix = bytes(self.prefix)
|
# self.prefix = bytes(self.prefix)
|
||||||
if self.addrlen > self.prefixlen:
|
# if self.addrlen > self.prefixlen:
|
||||||
addrbits = self.addrlen - self.prefixlen
|
# addrbits = self.addrlen - self.prefixlen
|
||||||
netmask = ((1 << self.prefixlen) - 1) << addrbits
|
# netmask = ((1 << self.prefixlen) - 1) << addrbits
|
||||||
prefix = bytes(b"")
|
# prefix = bytes(b"")
|
||||||
for i in range(-1, -(addrbits >> 3) - 2, -1):
|
# for i in range(-1, -(addrbits >> 3) - 2, -1):
|
||||||
prefix = bytes([self.prefix[i] & (netmask & 0xFF)]) + prefix
|
# prefix = bytes([self.prefix[i] & (netmask & 0xFF)]) + prefix
|
||||||
netmask >>= 8
|
# netmask >>= 8
|
||||||
self.prefix = self.prefix[:i] + prefix
|
# self.prefix = self.prefix[:i] + prefix
|
||||||
|
#
|
||||||
def __str__(self):
|
# def __str__(self):
|
||||||
"""
|
# """
|
||||||
String representation of an ip prefix.
|
# String representation of an ip prefix.
|
||||||
|
#
|
||||||
:return: string representation
|
# :return: string representation
|
||||||
:rtype: str
|
# :rtype: str
|
||||||
"""
|
# """
|
||||||
address = socket.inet_ntop(self.af, self.prefix)
|
# address = socket.inet_ntop(self.af, self.prefix)
|
||||||
return f"{address}/{self.prefixlen}"
|
# return f"{address}/{self.prefixlen}"
|
||||||
|
#
|
||||||
def __eq__(self, other):
|
# def __eq__(self, other):
|
||||||
"""
|
# """
|
||||||
Compare equality with another ip prefix.
|
# Compare equality with another ip prefix.
|
||||||
|
#
|
||||||
:param IpPrefix other: other ip prefix to compare with
|
# :param IpPrefix other: other ip prefix to compare with
|
||||||
:return: True is equal, False otherwise
|
# :return: True is equal, False otherwise
|
||||||
:rtype: bool
|
# :rtype: bool
|
||||||
"""
|
# """
|
||||||
if not isinstance(other, IpPrefix):
|
# if not isinstance(other, IpPrefix):
|
||||||
return False
|
# return False
|
||||||
elif self is other:
|
# elif self is other:
|
||||||
return True
|
# return True
|
||||||
else:
|
# else:
|
||||||
return (
|
# return (
|
||||||
other.af == self.af
|
# other.af == self.af
|
||||||
and other.prefixlen == self.prefixlen
|
# and other.prefixlen == self.prefixlen
|
||||||
and other.prefix == self.prefix
|
# and other.prefix == self.prefix
|
||||||
)
|
# )
|
||||||
|
#
|
||||||
def __add__(self, other):
|
# def __add__(self, other):
|
||||||
"""
|
# """
|
||||||
Add a value to this ip prefix.
|
# Add a value to this ip prefix.
|
||||||
|
#
|
||||||
:param int other: value to add
|
# :param int other: value to add
|
||||||
:return: added ip prefix instance
|
# :return: added ip prefix instance
|
||||||
:rtype: IpPrefix
|
# :rtype: IpPrefix
|
||||||
"""
|
# """
|
||||||
try:
|
# try:
|
||||||
tmp = int(other)
|
# tmp = int(other)
|
||||||
except ValueError:
|
# except ValueError:
|
||||||
logging.exception("error during addition")
|
# logging.exception("error during addition")
|
||||||
return NotImplemented
|
# return NotImplemented
|
||||||
|
#
|
||||||
a = IpAddress(self.af, self.prefix) + (tmp << (self.addrlen - self.prefixlen))
|
# a = IpAddress(self.af, self.prefix) + (tmp << (self.addrlen - self.prefixlen))
|
||||||
prefixstr = f"{a}/{self.prefixlen}"
|
# prefixstr = f"{a}/{self.prefixlen}"
|
||||||
if self.__class__ == IpPrefix:
|
# if self.__class__ == IpPrefix:
|
||||||
return self.__class__(self.af, prefixstr)
|
# return self.__class__(self.af, prefixstr)
|
||||||
else:
|
# else:
|
||||||
return self.__class__(prefixstr)
|
# return self.__class__(prefixstr)
|
||||||
|
#
|
||||||
def __sub__(self, other):
|
# def __sub__(self, other):
|
||||||
"""
|
# """
|
||||||
Subtract value from this ip prefix.
|
# Subtract value from this ip prefix.
|
||||||
|
#
|
||||||
:param int other: value to subtract
|
# :param int other: value to subtract
|
||||||
:return: subtracted ip prefix instance
|
# :return: subtracted ip prefix instance
|
||||||
:rtype: IpPrefix
|
# :rtype: IpPrefix
|
||||||
"""
|
# """
|
||||||
try:
|
# try:
|
||||||
tmp = -int(other)
|
# tmp = -int(other)
|
||||||
except ValueError:
|
# except ValueError:
|
||||||
logging.exception("error during subtraction")
|
# logging.exception("error during subtraction")
|
||||||
return NotImplemented
|
# return NotImplemented
|
||||||
|
#
|
||||||
return self.__add__(tmp)
|
# return self.__add__(tmp)
|
||||||
|
#
|
||||||
def addr(self, hostid):
|
# def addr(self, hostid):
|
||||||
"""
|
# """
|
||||||
Create an ip address for a given host id.
|
# Create an ip address for a given host id.
|
||||||
|
#
|
||||||
:param hostid: host id for an ip address
|
# :param hostid: host id for an ip address
|
||||||
:return: ip address
|
# :return: ip address
|
||||||
:rtype: IpAddress
|
# :rtype: IpAddress
|
||||||
"""
|
# """
|
||||||
tmp = int(hostid)
|
# tmp = int(hostid)
|
||||||
if tmp in [-1, 0, 1] and self.addrlen == self.prefixlen:
|
# if tmp in [-1, 0, 1] and self.addrlen == self.prefixlen:
|
||||||
return IpAddress(self.af, self.prefix)
|
# return IpAddress(self.af, self.prefix)
|
||||||
|
#
|
||||||
if (
|
# if (
|
||||||
tmp == 0
|
# tmp == 0
|
||||||
or tmp > (1 << (self.addrlen - self.prefixlen)) - 1
|
# or tmp > (1 << (self.addrlen - self.prefixlen)) - 1
|
||||||
or (
|
# or (
|
||||||
self.af == AF_INET and tmp == (1 << (self.addrlen - self.prefixlen)) - 1
|
# self.af == AF_INET and tmp == (1 << (self.addrlen - self.prefixlen)) - 1
|
||||||
)
|
# )
|
||||||
):
|
# ):
|
||||||
raise ValueError(f"invalid hostid for prefix {self}: {hostid}")
|
# raise ValueError(f"invalid hostid for prefix {self}: {hostid}")
|
||||||
|
#
|
||||||
addr = bytes(b"")
|
# addr = bytes(b"")
|
||||||
prefix_endpoint = -1
|
# prefix_endpoint = -1
|
||||||
for i in range(-1, -(self.addrlen >> 3) - 1, -1):
|
# for i in range(-1, -(self.addrlen >> 3) - 1, -1):
|
||||||
prefix_endpoint = i
|
# prefix_endpoint = i
|
||||||
addr = bytes([self.prefix[i] | (tmp & 0xFF)]) + addr
|
# addr = bytes([self.prefix[i] | (tmp & 0xFF)]) + addr
|
||||||
tmp >>= 8
|
# tmp >>= 8
|
||||||
if not tmp:
|
# if not tmp:
|
||||||
break
|
# break
|
||||||
addr = self.prefix[:prefix_endpoint] + addr
|
# addr = self.prefix[:prefix_endpoint] + addr
|
||||||
return IpAddress(self.af, addr)
|
# return IpAddress(self.af, addr)
|
||||||
|
#
|
||||||
def min_addr(self):
|
# def min_addr(self):
|
||||||
"""
|
# """
|
||||||
Return the minimum ip address for this prefix.
|
# Return the minimum ip address for this prefix.
|
||||||
|
#
|
||||||
:return: minimum ip address
|
# :return: minimum ip address
|
||||||
:rtype: IpAddress
|
# :rtype: IpAddress
|
||||||
"""
|
# """
|
||||||
return self.addr(1)
|
# return self.addr(1)
|
||||||
|
#
|
||||||
def max_addr(self):
|
# def max_addr(self):
|
||||||
"""
|
# """
|
||||||
Return the maximum ip address for this prefix.
|
# Return the maximum ip address for this prefix.
|
||||||
|
#
|
||||||
:return: maximum ip address
|
# :return: maximum ip address
|
||||||
:rtype: IpAddress
|
# :rtype: IpAddress
|
||||||
"""
|
# """
|
||||||
if self.af == AF_INET:
|
# if self.af == AF_INET:
|
||||||
return self.addr((1 << (self.addrlen - self.prefixlen)) - 2)
|
# return self.addr((1 << (self.addrlen - self.prefixlen)) - 2)
|
||||||
else:
|
# else:
|
||||||
return self.addr((1 << (self.addrlen - self.prefixlen)) - 1)
|
# return self.addr((1 << (self.addrlen - self.prefixlen)) - 1)
|
||||||
|
#
|
||||||
def num_addr(self):
|
# def num_addr(self):
|
||||||
"""
|
# """
|
||||||
Retrieve the number of ip addresses for this prefix.
|
# Retrieve the number of ip addresses for this prefix.
|
||||||
|
#
|
||||||
:return: maximum number of ip addresses
|
# :return: maximum number of ip addresses
|
||||||
:rtype: int
|
# :rtype: int
|
||||||
"""
|
# """
|
||||||
return max(0, (1 << (self.addrlen - self.prefixlen)) - 2)
|
# return max(0, (1 << (self.addrlen - self.prefixlen)) - 2)
|
||||||
|
#
|
||||||
def prefix_str(self):
|
# def prefix_str(self):
|
||||||
"""
|
# """
|
||||||
Retrieve the prefix string for this ip address.
|
# Retrieve the prefix string for this ip address.
|
||||||
|
#
|
||||||
:return: prefix string
|
# :return: prefix string
|
||||||
:rtype: str
|
# :rtype: str
|
||||||
"""
|
# """
|
||||||
return socket.inet_ntop(self.af, self.prefix)
|
# return socket.inet_ntop(self.af, self.prefix)
|
||||||
|
#
|
||||||
def netmask_str(self):
|
# def netmask_str(self):
|
||||||
"""
|
# """
|
||||||
Retrieve the netmask string for this ip address.
|
# Retrieve the netmask string for this ip address.
|
||||||
|
#
|
||||||
:return: netmask string
|
# :return: netmask string
|
||||||
:rtype: str
|
# :rtype: str
|
||||||
"""
|
# """
|
||||||
addrbits = self.addrlen - self.prefixlen
|
# addrbits = self.addrlen - self.prefixlen
|
||||||
netmask = ((1 << self.prefixlen) - 1) << addrbits
|
# netmask = ((1 << self.prefixlen) - 1) << addrbits
|
||||||
netmaskbytes = struct.pack("!L", netmask)
|
# netmaskbytes = struct.pack("!L", netmask)
|
||||||
return IpAddress(af=AF_INET, address=netmaskbytes).__str__()
|
# return IpAddress(af=AF_INET, address=netmaskbytes).__str__()
|
||||||
|
|
||||||
|
|
||||||
class Ipv4Prefix(IpPrefix):
|
# class Ipv4Prefix(IpPrefix):
|
||||||
"""
|
# """
|
||||||
Provides an ipv4 specific class for ip prefixes.
|
# Provides an ipv4 specific class for ip prefixes.
|
||||||
"""
|
# """
|
||||||
|
#
|
||||||
def __init__(self, prefixstr):
|
# def __init__(self, prefixstr):
|
||||||
"""
|
# """
|
||||||
Create a Ipv4Prefix instance.
|
# Create a Ipv4Prefix instance.
|
||||||
|
#
|
||||||
:param str prefixstr: ip prefix
|
# :param str prefixstr: ip prefix
|
||||||
"""
|
# """
|
||||||
super().__init__(AF_INET, prefixstr)
|
# super().__init__(AF_INET, prefixstr)
|
||||||
|
#
|
||||||
|
#
|
||||||
class Ipv6Prefix(IpPrefix):
|
# class Ipv6Prefix(IpPrefix):
|
||||||
"""
|
# """
|
||||||
Provides an ipv6 specific class for ip prefixes.
|
# Provides an ipv6 specific class for ip prefixes.
|
||||||
"""
|
# """
|
||||||
|
#
|
||||||
def __init__(self, prefixstr):
|
# def __init__(self, prefixstr):
|
||||||
"""
|
# """
|
||||||
Create a Ipv6Prefix instance.
|
# Create a Ipv6Prefix instance.
|
||||||
|
#
|
||||||
:param str prefixstr: ip prefix
|
# :param str prefixstr: ip prefix
|
||||||
"""
|
# """
|
||||||
super().__init__(AF_INET6, prefixstr)
|
# super().__init__(AF_INET6, prefixstr)
|
||||||
|
#
|
||||||
|
#
|
||||||
def is_ip_address(af, addrstr):
|
# def is_ip_address(af, addrstr):
|
||||||
"""
|
# """
|
||||||
Check if ip address string is a valid ip address.
|
# Check if ip address string is a valid ip address.
|
||||||
|
#
|
||||||
:param int af: address family
|
# :param int af: address family
|
||||||
:param str addrstr: ip address string
|
# :param str addrstr: ip address string
|
||||||
:return: True if a valid ip address, False otherwise
|
# :return: True if a valid ip address, False otherwise
|
||||||
:rtype: bool
|
# :rtype: bool
|
||||||
"""
|
# """
|
||||||
try:
|
# try:
|
||||||
socket.inet_pton(af, addrstr)
|
# socket.inet_pton(af, addrstr)
|
||||||
return True
|
# return True
|
||||||
except IOError:
|
# except IOError:
|
||||||
return False
|
# return False
|
||||||
|
#
|
||||||
|
#
|
||||||
def is_ipv4_address(addrstr):
|
# def is_ipv4_address(addrstr):
|
||||||
"""
|
# """
|
||||||
Check if ipv4 address string is a valid ipv4 address.
|
# Check if ipv4 address string is a valid ipv4 address.
|
||||||
|
#
|
||||||
:param str addrstr: ipv4 address string
|
# :param str addrstr: ipv4 address string
|
||||||
:return: True if a valid ipv4 address, False otherwise
|
# :return: True if a valid ipv4 address, False otherwise
|
||||||
:rtype: bool
|
# :rtype: bool
|
||||||
"""
|
# """
|
||||||
return is_ip_address(AF_INET, addrstr)
|
# return is_ip_address(AF_INET, addrstr)
|
||||||
|
#
|
||||||
|
#
|
||||||
def is_ipv6_address(addrstr):
|
# def is_ipv6_address(addrstr):
|
||||||
"""
|
# """
|
||||||
Check if ipv6 address string is a valid ipv6 address.
|
# Check if ipv6 address string is a valid ipv6 address.
|
||||||
|
#
|
||||||
:param str addrstr: ipv6 address string
|
# :param str addrstr: ipv6 address string
|
||||||
:return: True if a valid ipv6 address, False otherwise
|
# :return: True if a valid ipv6 address, False otherwise
|
||||||
:rtype: bool
|
# :rtype: bool
|
||||||
"""
|
# """
|
||||||
return is_ip_address(AF_INET6, addrstr)
|
# return is_ip_address(AF_INET6, addrstr)
|
||||||
|
|
|
@ -3,10 +3,8 @@ Defines network nodes used within core.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import socket
|
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from socket import AF_INET, AF_INET6
|
|
||||||
|
|
||||||
import netaddr
|
import netaddr
|
||||||
|
|
||||||
|
@ -15,7 +13,6 @@ from core.constants import EBTABLES_BIN, TC_BIN
|
||||||
from core.emulator.data import LinkData
|
from core.emulator.data import LinkData
|
||||||
from core.emulator.enumerations import LinkTypes, NodeTypes, RegisterTlvs
|
from core.emulator.enumerations import LinkTypes, NodeTypes, RegisterTlvs
|
||||||
from core.errors import CoreCommandError, CoreError
|
from core.errors import CoreCommandError, CoreError
|
||||||
from core.nodes import ipaddress
|
|
||||||
from core.nodes.base import CoreNetworkBase
|
from core.nodes.base import CoreNetworkBase
|
||||||
from core.nodes.interface import GreTap, Veth
|
from core.nodes.interface import GreTap, Veth
|
||||||
from core.nodes.netclient import get_net_client
|
from core.nodes.netclient import get_net_client
|
||||||
|
@ -752,28 +749,30 @@ class CtrlNet(CoreNetwork):
|
||||||
:param serverintf: server interface
|
:param serverintf: server interface
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self.prefix = ipaddress.Ipv4Prefix(prefix)
|
self.prefix = netaddr.IPNetwork(prefix).cidr
|
||||||
self.hostid = hostid
|
self.hostid = hostid
|
||||||
self.assign_address = assign_address
|
self.assign_address = assign_address
|
||||||
self.updown_script = updown_script
|
self.updown_script = updown_script
|
||||||
self.serverintf = serverintf
|
self.serverintf = serverintf
|
||||||
super().__init__(session, _id, name, start, server)
|
super().__init__(session, _id, name, start, server)
|
||||||
|
|
||||||
def add_addresses(self, address):
|
def add_addresses(self, index):
|
||||||
"""
|
"""
|
||||||
Add addresses used for created control networks,
|
Add addresses used for created control networks,
|
||||||
|
|
||||||
:param core.nodes.interfaces.IpAddress address: starting address to use
|
:param int index: starting address index
|
||||||
:return:
|
:return: nothing
|
||||||
"""
|
"""
|
||||||
use_ovs = self.session.options.get_config("ovs") == "True"
|
use_ovs = self.session.options.get_config("ovs") == "True"
|
||||||
|
address = self.prefix[index]
|
||||||
current = f"{address}/{self.prefix.prefixlen}"
|
current = f"{address}/{self.prefix.prefixlen}"
|
||||||
net_client = get_net_client(use_ovs, utils.cmd)
|
net_client = get_net_client(use_ovs, utils.cmd)
|
||||||
net_client.create_address(self.brname, current)
|
net_client.create_address(self.brname, current)
|
||||||
servers = self.session.distributed.servers
|
servers = self.session.distributed.servers
|
||||||
for name in servers:
|
for name in servers:
|
||||||
server = servers[name]
|
server = servers[name]
|
||||||
address -= 1
|
index -= 1
|
||||||
|
address = self.prefix[index]
|
||||||
current = f"{address}/{self.prefix.prefixlen}"
|
current = f"{address}/{self.prefix.prefixlen}"
|
||||||
net_client = get_net_client(use_ovs, server.remote_cmd)
|
net_client = get_net_client(use_ovs, server.remote_cmd)
|
||||||
net_client.create_address(self.brname, current)
|
net_client.create_address(self.brname, current)
|
||||||
|
@ -792,11 +791,9 @@ class CtrlNet(CoreNetwork):
|
||||||
logging.info("added control network bridge: %s %s", self.brname, self.prefix)
|
logging.info("added control network bridge: %s %s", self.brname, self.prefix)
|
||||||
|
|
||||||
if self.hostid and self.assign_address:
|
if self.hostid and self.assign_address:
|
||||||
address = self.prefix.addr(self.hostid)
|
self.add_addresses(self.hostid)
|
||||||
self.add_addresses(address)
|
|
||||||
elif self.assign_address:
|
elif self.assign_address:
|
||||||
address = self.prefix.max_addr()
|
self.add_addresses(-2)
|
||||||
self.add_addresses(address)
|
|
||||||
|
|
||||||
if self.updown_script:
|
if self.updown_script:
|
||||||
logging.info(
|
logging.info(
|
||||||
|
@ -911,14 +908,10 @@ class PtpNet(CoreNetwork):
|
||||||
ip, _sep, mask = address.partition("/")
|
ip, _sep, mask = address.partition("/")
|
||||||
mask = int(mask)
|
mask = int(mask)
|
||||||
if netaddr.valid_ipv4(ip):
|
if netaddr.valid_ipv4(ip):
|
||||||
family = AF_INET
|
interface1_ip4 = ip
|
||||||
ipl = socket.inet_pton(family, ip)
|
|
||||||
interface1_ip4 = ipaddress.IpAddress(af=family, address=ipl)
|
|
||||||
interface1_ip4_mask = mask
|
interface1_ip4_mask = mask
|
||||||
else:
|
else:
|
||||||
family = AF_INET6
|
interface1_ip6 = ip
|
||||||
ipl = socket.inet_pton(family, ip)
|
|
||||||
interface1_ip6 = ipaddress.IpAddress(af=family, address=ipl)
|
|
||||||
interface1_ip6_mask = mask
|
interface1_ip6_mask = mask
|
||||||
|
|
||||||
interface2_ip4 = None
|
interface2_ip4 = None
|
||||||
|
@ -929,14 +922,10 @@ class PtpNet(CoreNetwork):
|
||||||
ip, _sep, mask = address.partition("/")
|
ip, _sep, mask = address.partition("/")
|
||||||
mask = int(mask)
|
mask = int(mask)
|
||||||
if netaddr.valid_ipv4(ip):
|
if netaddr.valid_ipv4(ip):
|
||||||
family = AF_INET
|
interface2_ip4 = ip
|
||||||
ipl = socket.inet_pton(family, ip)
|
|
||||||
interface2_ip4 = ipaddress.IpAddress(af=family, address=ipl)
|
|
||||||
interface2_ip4_mask = mask
|
interface2_ip4_mask = mask
|
||||||
else:
|
else:
|
||||||
family = AF_INET6
|
interface2_ip6 = ip
|
||||||
ipl = socket.inet_pton(family, ip)
|
|
||||||
interface2_ip6 = ipaddress.IpAddress(af=family, address=ipl)
|
|
||||||
interface2_ip6_mask = mask
|
interface2_ip6_mask = mask
|
||||||
|
|
||||||
link_data = LinkData(
|
link_data = LinkData(
|
||||||
|
|
|
@ -5,7 +5,6 @@ nrl.py: defines services provided by NRL protolib tools hosted here:
|
||||||
import netaddr
|
import netaddr
|
||||||
|
|
||||||
from core import utils
|
from core import utils
|
||||||
from core.nodes.ipaddress import Ipv4Prefix
|
|
||||||
from core.services.coreservices import CoreService
|
from core.services.coreservices import CoreService
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,8 +38,7 @@ class NrlService(CoreService):
|
||||||
for a in ifc.addrlist:
|
for a in ifc.addrlist:
|
||||||
a = a.split("/")[0]
|
a = a.split("/")[0]
|
||||||
if netaddr.valid_ipv4(a):
|
if netaddr.valid_ipv4(a):
|
||||||
pre = Ipv4Prefix("%s/%s" % (a, prefixlen))
|
return f"{a}/{prefixlen}"
|
||||||
return str(pre)
|
|
||||||
# raise ValueError, "no IPv4 address found"
|
# raise ValueError, "no IPv4 address found"
|
||||||
return "0.0.0.0/%s" % prefixlen
|
return "0.0.0.0/%s" % prefixlen
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ import netaddr
|
||||||
|
|
||||||
from core import constants, utils
|
from core import constants, utils
|
||||||
from core.errors import CoreCommandError
|
from core.errors import CoreCommandError
|
||||||
from core.nodes.ipaddress import Ipv4Prefix, Ipv6Prefix
|
|
||||||
from core.services.coreservices import CoreService, ServiceMode
|
from core.services.coreservices import CoreService, ServiceMode
|
||||||
|
|
||||||
|
|
||||||
|
@ -89,19 +88,15 @@ class DefaultRouteService(UtilService):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def addrstr(x):
|
def addrstr(x):
|
||||||
addr = x.split("/")[0]
|
net = netaddr.IPNetwork(x)
|
||||||
if netaddr.valid_ipv6(addr):
|
if net[1] == net[-2]:
|
||||||
net = Ipv6Prefix(x)
|
|
||||||
else:
|
|
||||||
net = Ipv4Prefix(x)
|
|
||||||
if net.max_addr() == net.min_addr():
|
|
||||||
return ""
|
return ""
|
||||||
else:
|
else:
|
||||||
if os.uname()[0] == "Linux":
|
if os.uname()[0] == "Linux":
|
||||||
rtcmd = "ip route add default via"
|
rtcmd = "ip route add default via"
|
||||||
else:
|
else:
|
||||||
raise Exception("unknown platform")
|
raise Exception("unknown platform")
|
||||||
return "%s %s" % (rtcmd, net.min_addr())
|
return "%s %s" % (rtcmd, net[1])
|
||||||
|
|
||||||
|
|
||||||
class DefaultMulticastRouteService(UtilService):
|
class DefaultMulticastRouteService(UtilService):
|
||||||
|
@ -152,19 +147,18 @@ class StaticRouteService(UtilService):
|
||||||
def routestr(x):
|
def routestr(x):
|
||||||
addr = x.split("/")[0]
|
addr = x.split("/")[0]
|
||||||
if netaddr.valid_ipv6(addr):
|
if netaddr.valid_ipv6(addr):
|
||||||
net = Ipv6Prefix(x)
|
|
||||||
dst = "3ffe:4::/64"
|
dst = "3ffe:4::/64"
|
||||||
else:
|
else:
|
||||||
net = Ipv4Prefix(x)
|
|
||||||
dst = "10.9.8.0/24"
|
dst = "10.9.8.0/24"
|
||||||
if net.max_addr() == net.min_addr():
|
net = netaddr.IPNetwork(x)
|
||||||
|
if net[-2] == net[1]:
|
||||||
return ""
|
return ""
|
||||||
else:
|
else:
|
||||||
if os.uname()[0] == "Linux":
|
if os.uname()[0] == "Linux":
|
||||||
rtcmd = "#/sbin/ip route add %s via" % dst
|
rtcmd = "#/sbin/ip route add %s via" % dst
|
||||||
else:
|
else:
|
||||||
raise Exception("unknown platform")
|
raise Exception("unknown platform")
|
||||||
return "%s %s" % (rtcmd, net.min_addr())
|
return "%s %s" % (rtcmd, net[1])
|
||||||
|
|
||||||
|
|
||||||
class SshService(UtilService):
|
class SshService(UtilService):
|
||||||
|
@ -289,11 +283,11 @@ ddns-update-style none;
|
||||||
if netaddr.valid_ipv6(addr):
|
if netaddr.valid_ipv6(addr):
|
||||||
return ""
|
return ""
|
||||||
else:
|
else:
|
||||||
addr = x.split("/")[0]
|
net = netaddr.IPNetwork(x)
|
||||||
net = Ipv4Prefix(x)
|
|
||||||
# divide the address space in half
|
# divide the address space in half
|
||||||
rangelow = net.addr(net.num_addr() / 2)
|
index = (net.size - 2) / 2
|
||||||
rangehigh = net.max_addr()
|
rangelow = net[index]
|
||||||
|
rangehigh = net[-2]
|
||||||
return """
|
return """
|
||||||
subnet %s netmask %s {
|
subnet %s netmask %s {
|
||||||
pool {
|
pool {
|
||||||
|
@ -303,8 +297,8 @@ subnet %s netmask %s {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
""" % (
|
""" % (
|
||||||
net.prefix_str(),
|
net.ip,
|
||||||
net.netmask_str(),
|
net.netmask,
|
||||||
rangelow,
|
rangelow,
|
||||||
rangehigh,
|
rangehigh,
|
||||||
addr,
|
addr,
|
||||||
|
@ -710,8 +704,7 @@ interface %s
|
||||||
"""
|
"""
|
||||||
addr = x.split("/")[0]
|
addr = x.split("/")[0]
|
||||||
if netaddr.valid_ipv6(addr):
|
if netaddr.valid_ipv6(addr):
|
||||||
net = Ipv6Prefix(x)
|
return x
|
||||||
return str(net)
|
|
||||||
else:
|
else:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
|
import netaddr
|
||||||
import pytest
|
import pytest
|
||||||
from mock import MagicMock
|
from mock import MagicMock
|
||||||
|
|
||||||
|
@ -26,7 +27,6 @@ from core.emulator.enumerations import (
|
||||||
)
|
)
|
||||||
from core.errors import CoreError
|
from core.errors import CoreError
|
||||||
from core.location.mobility import BasicRangeModel
|
from core.location.mobility import BasicRangeModel
|
||||||
from core.nodes.ipaddress import Ipv4Prefix
|
|
||||||
|
|
||||||
|
|
||||||
def dict_to_str(values):
|
def dict_to_str(values):
|
||||||
|
@ -101,8 +101,8 @@ class TestGui:
|
||||||
coretlv.session.add_node(_id=node_one)
|
coretlv.session.add_node(_id=node_one)
|
||||||
switch = 2
|
switch = 2
|
||||||
coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
|
coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
|
||||||
ip_prefix = Ipv4Prefix("10.0.0.0/24")
|
ip_prefix = netaddr.IPNetwork("10.0.0.0/24")
|
||||||
interface_one = ip_prefix.addr(node_one)
|
interface_one = str(ip_prefix[node_one])
|
||||||
message = coreapi.CoreLinkMessage.create(
|
message = coreapi.CoreLinkMessage.create(
|
||||||
MessageFlags.ADD.value,
|
MessageFlags.ADD.value,
|
||||||
[
|
[
|
||||||
|
@ -125,8 +125,8 @@ class TestGui:
|
||||||
coretlv.session.add_node(_id=node_one)
|
coretlv.session.add_node(_id=node_one)
|
||||||
switch = 2
|
switch = 2
|
||||||
coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
|
coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
|
||||||
ip_prefix = Ipv4Prefix("10.0.0.0/24")
|
ip_prefix = netaddr.IPNetwork("10.0.0.0/24")
|
||||||
interface_one = ip_prefix.addr(node_one)
|
interface_one = str(ip_prefix[node_one])
|
||||||
message = coreapi.CoreLinkMessage.create(
|
message = coreapi.CoreLinkMessage.create(
|
||||||
MessageFlags.ADD.value,
|
MessageFlags.ADD.value,
|
||||||
[
|
[
|
||||||
|
@ -149,9 +149,9 @@ class TestGui:
|
||||||
coretlv.session.add_node(_id=node_one)
|
coretlv.session.add_node(_id=node_one)
|
||||||
node_two = 2
|
node_two = 2
|
||||||
coretlv.session.add_node(_id=node_two)
|
coretlv.session.add_node(_id=node_two)
|
||||||
ip_prefix = Ipv4Prefix("10.0.0.0/24")
|
ip_prefix = netaddr.IPNetwork("10.0.0.0/24")
|
||||||
interface_one = ip_prefix.addr(node_one)
|
interface_one = str(ip_prefix[node_one])
|
||||||
interface_two = ip_prefix.addr(node_two)
|
interface_two = str(ip_prefix[node_two])
|
||||||
message = coreapi.CoreLinkMessage.create(
|
message = coreapi.CoreLinkMessage.create(
|
||||||
MessageFlags.ADD.value,
|
MessageFlags.ADD.value,
|
||||||
[
|
[
|
||||||
|
@ -179,8 +179,8 @@ class TestGui:
|
||||||
coretlv.session.add_node(_id=node_one)
|
coretlv.session.add_node(_id=node_one)
|
||||||
switch = 2
|
switch = 2
|
||||||
coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
|
coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
|
||||||
ip_prefix = Ipv4Prefix("10.0.0.0/24")
|
ip_prefix = netaddr.IPNetwork("10.0.0.0/24")
|
||||||
interface_one = ip_prefix.addr(node_one)
|
interface_one = str(ip_prefix[node_one])
|
||||||
message = coreapi.CoreLinkMessage.create(
|
message = coreapi.CoreLinkMessage.create(
|
||||||
MessageFlags.ADD.value,
|
MessageFlags.ADD.value,
|
||||||
[
|
[
|
||||||
|
@ -221,9 +221,9 @@ class TestGui:
|
||||||
coretlv.session.add_node(_id=node_one)
|
coretlv.session.add_node(_id=node_one)
|
||||||
node_two = 2
|
node_two = 2
|
||||||
coretlv.session.add_node(_id=node_two)
|
coretlv.session.add_node(_id=node_two)
|
||||||
ip_prefix = Ipv4Prefix("10.0.0.0/24")
|
ip_prefix = netaddr.IPNetwork("10.0.0.0/24")
|
||||||
interface_one = ip_prefix.addr(node_one)
|
interface_one = str(ip_prefix[node_one])
|
||||||
interface_two = ip_prefix.addr(node_two)
|
interface_two = str(ip_prefix[node_two])
|
||||||
message = coreapi.CoreLinkMessage.create(
|
message = coreapi.CoreLinkMessage.create(
|
||||||
MessageFlags.ADD.value,
|
MessageFlags.ADD.value,
|
||||||
[
|
[
|
||||||
|
@ -265,8 +265,8 @@ class TestGui:
|
||||||
coretlv.session.add_node(_id=node_one)
|
coretlv.session.add_node(_id=node_one)
|
||||||
switch = 2
|
switch = 2
|
||||||
coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
|
coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
|
||||||
ip_prefix = Ipv4Prefix("10.0.0.0/24")
|
ip_prefix = netaddr.IPNetwork("10.0.0.0/24")
|
||||||
interface_one = ip_prefix.addr(node_one)
|
interface_one = str(ip_prefix[node_one])
|
||||||
message = coreapi.CoreLinkMessage.create(
|
message = coreapi.CoreLinkMessage.create(
|
||||||
MessageFlags.ADD.value,
|
MessageFlags.ADD.value,
|
||||||
[
|
[
|
||||||
|
@ -301,8 +301,8 @@ class TestGui:
|
||||||
coretlv.session.add_node(_id=node_one)
|
coretlv.session.add_node(_id=node_one)
|
||||||
switch = 2
|
switch = 2
|
||||||
coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
|
coretlv.session.add_node(_id=switch, _type=NodeTypes.SWITCH)
|
||||||
ip_prefix = Ipv4Prefix("10.0.0.0/24")
|
ip_prefix = netaddr.IPNetwork("10.0.0.0/24")
|
||||||
interface_one = ip_prefix.addr(node_one)
|
interface_one = str(ip_prefix[node_one])
|
||||||
message = coreapi.CoreLinkMessage.create(
|
message = coreapi.CoreLinkMessage.create(
|
||||||
MessageFlags.ADD.value,
|
MessageFlags.ADD.value,
|
||||||
[
|
[
|
||||||
|
|
|
@ -8,13 +8,12 @@ import logging
|
||||||
import optparse
|
import optparse
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import netaddr
|
||||||
import ns.core
|
import ns.core
|
||||||
import ns.mobility
|
import ns.mobility
|
||||||
from corens3.obj import Ns3LteNet
|
from corens3.obj import Ns3LteNet
|
||||||
from corens3.obj import Ns3Session
|
from corens3.obj import Ns3Session
|
||||||
|
|
||||||
from core.nodes import ipaddress
|
|
||||||
|
|
||||||
|
|
||||||
def ltesession(opt):
|
def ltesession(opt):
|
||||||
"""
|
"""
|
||||||
|
@ -28,10 +27,10 @@ def ltesession(opt):
|
||||||
stream = ascii_helper.CreateFileStream('/tmp/ns3lte.tr')
|
stream = ascii_helper.CreateFileStream('/tmp/ns3lte.tr')
|
||||||
lte.lte.EnableAsciiAll(stream)
|
lte.lte.EnableAsciiAll(stream)
|
||||||
|
|
||||||
prefix = ipaddress.Ipv4Prefix("10.0.0.0/16")
|
prefix = netaddr.IPNetwork("10.0.0.0/16")
|
||||||
mobb = None
|
mobb = None
|
||||||
nodes = []
|
nodes = []
|
||||||
for i in xrange(1, opt.numnodes + 1):
|
for i in range(1, opt.numnodes + 1):
|
||||||
node = session.addnode(name="n%d" % i)
|
node = session.addnode(name="n%d" % i)
|
||||||
mob = ns.mobility.ConstantPositionMobilityModel()
|
mob = ns.mobility.ConstantPositionMobilityModel()
|
||||||
mob.SetPosition(ns.core.Vector3D(10.0 * i, 0.0, 0.0))
|
mob.SetPosition(ns.core.Vector3D(10.0 * i, 0.0, 0.0))
|
||||||
|
@ -39,7 +38,7 @@ def ltesession(opt):
|
||||||
# first node is nodeb
|
# first node is nodeb
|
||||||
lte.setnodeb(node)
|
lte.setnodeb(node)
|
||||||
mobb = mob
|
mobb = mob
|
||||||
node.newnetif(lte, ["%s/%s" % (prefix.addr(i), prefix.prefixlen)])
|
node.newnetif(lte, ["%s/%s" % (prefix[i], prefix.prefixlen)])
|
||||||
nodes.append(node)
|
nodes.append(node)
|
||||||
if i == 1:
|
if i == 1:
|
||||||
_tmp, ns3dev = lte.findns3dev(node)
|
_tmp, ns3dev = lte.findns3dev(node)
|
||||||
|
|
|
@ -26,12 +26,11 @@ import logging
|
||||||
import optparse
|
import optparse
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import netaddr
|
||||||
import ns.core
|
import ns.core
|
||||||
from corens3.obj import Ns3Session
|
from corens3.obj import Ns3Session
|
||||||
from corens3.obj import Ns3WifiNet
|
from corens3.obj import Ns3WifiNet
|
||||||
|
|
||||||
from core.nodes import ipaddress
|
|
||||||
|
|
||||||
|
|
||||||
def add_to_server(session):
|
def add_to_server(session):
|
||||||
"""
|
"""
|
||||||
|
@ -60,11 +59,11 @@ def wifisession(opt):
|
||||||
wifi.setposition(30, 30, 0)
|
wifi.setposition(30, 30, 0)
|
||||||
wifi.phy.Set("RxGain", ns.core.DoubleValue(18.0))
|
wifi.phy.Set("RxGain", ns.core.DoubleValue(18.0))
|
||||||
|
|
||||||
prefix = ipaddress.Ipv4Prefix("10.0.0.0/16")
|
prefix = netaddr.IPNetwork("10.0.0.0/16")
|
||||||
nodes = []
|
nodes = []
|
||||||
for i in xrange(1, opt.numnodes + 1):
|
for i in range(1, opt.numnodes + 1):
|
||||||
node = session.addnode(name="n%d" % i)
|
node = session.addnode(name="n%d" % i)
|
||||||
node.newnetif(wifi, ["%s/%s" % (prefix.addr(i), prefix.prefixlen)])
|
node.newnetif(wifi, ["%s/%s" % (prefix[i], prefix.prefixlen)])
|
||||||
nodes.append(node)
|
nodes.append(node)
|
||||||
session.setupconstantmobility()
|
session.setupconstantmobility()
|
||||||
wifi.usecorepositions()
|
wifi.usecorepositions()
|
||||||
|
|
|
@ -16,13 +16,12 @@ import optparse
|
||||||
import sys
|
import sys
|
||||||
from builtins import range
|
from builtins import range
|
||||||
|
|
||||||
|
import netaddr
|
||||||
import ns.core
|
import ns.core
|
||||||
import ns.network
|
import ns.network
|
||||||
from corens3.obj import Ns3Session
|
from corens3.obj import Ns3Session
|
||||||
from corens3.obj import Ns3WifiNet
|
from corens3.obj import Ns3WifiNet
|
||||||
|
|
||||||
from core.nodes import ipaddress
|
|
||||||
|
|
||||||
|
|
||||||
def add_to_server(session):
|
def add_to_server(session):
|
||||||
"""
|
"""
|
||||||
|
@ -51,12 +50,12 @@ def wifisession(opt):
|
||||||
# for improved connectivity
|
# for improved connectivity
|
||||||
wifi.phy.Set("RxGain", ns.core.DoubleValue(18.0))
|
wifi.phy.Set("RxGain", ns.core.DoubleValue(18.0))
|
||||||
|
|
||||||
prefix = ipaddress.Ipv4Prefix("10.0.0.0/16")
|
prefix = netaddr.IPNetwork("10.0.0.0/16")
|
||||||
services_str = "zebra|OSPFv3MDR|IPForward"
|
services_str = "zebra|OSPFv3MDR|IPForward"
|
||||||
nodes = []
|
nodes = []
|
||||||
for i in range(1, opt.numnodes + 1):
|
for i in range(1, opt.numnodes + 1):
|
||||||
node = session.addnode(name="n%d" % i)
|
node = session.addnode(name="n%d" % i)
|
||||||
node.newnetif(wifi, ["%s/%s" % (prefix.addr(i), prefix.prefixlen)])
|
node.newnetif(wifi, ["%s/%s" % (prefix[i], prefix.prefixlen)])
|
||||||
nodes.append(node)
|
nodes.append(node)
|
||||||
session.services.add_services(node, "router", services_str.split("|"))
|
session.services.add_services(node, "router", services_str.split("|"))
|
||||||
session.services.boot_services(node)
|
session.services.boot_services(node)
|
||||||
|
|
|
@ -14,11 +14,10 @@ import optparse
|
||||||
import sys
|
import sys
|
||||||
from builtins import range
|
from builtins import range
|
||||||
|
|
||||||
|
import netaddr
|
||||||
from corens3.obj import Ns3Session
|
from corens3.obj import Ns3Session
|
||||||
from corens3.obj import Ns3WimaxNet
|
from corens3.obj import Ns3WimaxNet
|
||||||
|
|
||||||
from core.nodes import ipaddress
|
|
||||||
|
|
||||||
|
|
||||||
def wimaxsession(opt):
|
def wimaxsession(opt):
|
||||||
"""
|
"""
|
||||||
|
@ -28,7 +27,7 @@ def wimaxsession(opt):
|
||||||
wimax = session.create_node(cls=Ns3WimaxNet, name="wlan1")
|
wimax = session.create_node(cls=Ns3WimaxNet, name="wlan1")
|
||||||
# wimax.wimax.EnableLogComponents()
|
# wimax.wimax.EnableLogComponents()
|
||||||
|
|
||||||
prefix = ipaddress.Ipv4Prefix("10.0.0.0/16")
|
prefix = netaddr.IPNetwork("10.0.0.0/16")
|
||||||
# create one classifier for ICMP (protocol 1) traffic
|
# create one classifier for ICMP (protocol 1) traffic
|
||||||
# src port low/high, dst port low/high, protocol, priority
|
# src port low/high, dst port low/high, protocol, priority
|
||||||
# classifier = (0, 65000, 0, 65000, 1, 1)
|
# classifier = (0, 65000, 0, 65000, 1, 1)
|
||||||
|
@ -38,7 +37,7 @@ def wimaxsession(opt):
|
||||||
node = session.addnode(name="n%d" % i)
|
node = session.addnode(name="n%d" % i)
|
||||||
if i == 1:
|
if i == 1:
|
||||||
wimax.setbasestation(node)
|
wimax.setbasestation(node)
|
||||||
node.newnetif(wimax, ["%s/%s" % (prefix.addr(i), prefix.prefixlen)])
|
node.newnetif(wimax, ["%s/%s" % (prefix[i], prefix.prefixlen)])
|
||||||
if i > 2:
|
if i > 2:
|
||||||
wimax.addflow(nodes[-1], node, classifier, classifier)
|
wimax.addflow(nodes[-1], node, classifier, classifier)
|
||||||
nodes.append(node)
|
nodes.append(node)
|
||||||
|
|
Loading…
Add table
Reference in a new issue