Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions framework/python/src/net_orc/ip_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def add_link(self, interface_name, peer_name):
def add_namespace(self, namespace):
"""Add a network namespace"""
exists = self.namespace_exists(namespace)
LOGGER.info("Namespace exists: " + str(exists))
LOGGER.info('Namespace exists: ' + str(exists))
if exists:
return True
else:
Expand All @@ -58,14 +58,11 @@ def link_exists(self, link_name):
def namespace_exists(self, namespace):
"""Check if a namespace already exists"""
namespaces = self.get_namespaces()
if namespace in namespaces:
return True
else:
return False
return namespace in namespaces

def get_links(self):
stdout, stderr = util.run_command('ip link list')
links = stdout.strip().split('\n')
result = util.run_command('ip link list')
links = result[0].strip().split('\n')
netns_links = []
for link in links:
match = re.search(r'\d+:\s+(\S+)', link)
Expand All @@ -78,9 +75,9 @@ def get_links(self):
return netns_links

def get_namespaces(self):
stdout, stderr = util.run_command('ip netns list')
result = util.run_command('ip netns list')
#Strip ID's from the namespace results
namespaces = re.findall(r'(\S+)(?:\s+\(id: \d+\))?', stdout)
namespaces = re.findall(r'(\S+)(?:\s+\(id: \d+\))?', result[0])
return namespaces

def set_namespace(self, interface_name, namespace):
Expand Down Expand Up @@ -187,9 +184,8 @@ def configure_container_interface(self,

# Rename container interface name
if not self.rename_interface(container_intf, namespace, namespace_intf):
LOGGER.error(
f'Failed to rename container interface {container_intf} to {namespace_intf}'
)
LOGGER.error((f'Failed to rename container interface {container_intf} ' +
'to {namespace_intf}'))
return False

# Set MAC address of container interface
Expand Down
92 changes: 45 additions & 47 deletions framework/python/src/net_orc/network_orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Network orchestrator is responsible for managing
all of the virtual network services"""
import ipaddress
Expand Down Expand Up @@ -44,13 +43,11 @@
PRIVATE_DOCKER_NET = 'tr-private-net'
CONTAINER_NAME = 'network_orchestrator'


class NetworkOrchestrator:
"""Manage and controls a virtual testing network."""

def __init__(self,
session,
validate=True,
single_intf=False):
def __init__(self, session, validate=True, single_intf=False):

self._session = session
self._monitor_in_progress = False
Expand Down Expand Up @@ -103,8 +100,9 @@ def check_config(self):
return False
else:
if not device_interface_ready and not internet_interface_ready:
LOGGER.error('Both device and internet interfaces are not ready for use. ' +
'Ensure both interfaces are connected.')
LOGGER.error(
'Both device and internet interfaces are not ready for use. ' +
'Ensure both interfaces are connected.')
return False
elif not device_interface_ready:
LOGGER.error('Device interface is not ready for use. ' +
Expand Down Expand Up @@ -169,14 +167,13 @@ def _device_discovered(self, mac_addr):
f'Discovered device {mac_addr}. Waiting for device to obtain IP')

if device is None:
LOGGER.debug(f'Device with MAC address {mac_addr} does not exist in device repository')
LOGGER.debug(f'Device with MAC address {mac_addr} does not exist' +
' in device repository')
# Ignore device if not registered
return

device_runtime_dir = os.path.join(RUNTIME_DIR,
TEST_DIR,
mac_addr.replace(':', '')
)
device_runtime_dir = os.path.join(RUNTIME_DIR, TEST_DIR,
mac_addr.replace(':', ''))

# Cleanup any old current test files
shutil.rmtree(device_runtime_dir, ignore_errors=True)
Expand All @@ -187,11 +184,7 @@ def _device_discovered(self, mac_addr):
packet_capture = sniff(iface=self._session.get_device_interface(),
timeout=self._session.get_startup_timeout(),
stop_filter=self._device_has_ip)
wrpcap(
os.path.join(device_runtime_dir,
'startup.pcap'
),
packet_capture)
wrpcap(os.path.join(device_runtime_dir, 'startup.pcap'), packet_capture)

if device.ip_addr is None:
LOGGER.info(
Expand Down Expand Up @@ -228,23 +221,17 @@ def _start_device_monitor(self, device):
callback the steady state method for this device."""
LOGGER.info(f'Monitoring device with mac addr {device.mac_addr} '
f'for {str(self._session.get_monitor_period())} seconds')

device_runtime_dir = os.path.join(RUNTIME_DIR,
TEST_DIR,
device.mac_addr.replace(':', '')
)

packet_capture = sniff(iface=self._session.get_device_interface(), timeout=self._session.get_monitor_period())
wrpcap(
os.path.join(device_runtime_dir,
'monitor.pcap'
),
packet_capture)

device_runtime_dir = os.path.join(RUNTIME_DIR, TEST_DIR,
device.mac_addr.replace(':', ''))

packet_capture = sniff(iface=self._session.get_device_interface(),
timeout=self._session.get_monitor_period())
wrpcap(os.path.join(device_runtime_dir, 'monitor.pcap'), packet_capture)

self._monitor_in_progress = False
self.get_listener().call_callback(
NetworkEvent.DEVICE_STABLE,
device.mac_addr)
self.get_listener().call_callback(NetworkEvent.DEVICE_STABLE,
device.mac_addr)

def _check_network_services(self):
LOGGER.debug('Checking network modules...')
Expand Down Expand Up @@ -297,24 +284,32 @@ def _ci_pre_network_create(self):
'ip route | head -n 1 | awk \'{print $3}\'',
shell=True).decode('utf-8').strip()
self._ipv4 = subprocess.check_output(
f'ip a show {self._session.get_internet_interface()} | grep \"inet \" | awk \'{{print $2}}\'',
(f'ip a show {self._session.get_internet_interface()} | ' +
'grep \"inet \" | awk \'{{print $2}}\''),
shell=True).decode('utf-8').strip()
self._ipv6 = subprocess.check_output(
f'ip a show {self._session.get_internet_interface()} | grep inet6 | awk \'{{print $2}}\'',
(f'ip a show {self._session.get_internet_interface()} | grep inet6 | ' +
'awk \'{{print $2}}\''),
shell=True).decode('utf-8').strip()
self._brd = subprocess.check_output(
f'ip a show {self._session.get_internet_interface()} | grep \"inet \" | awk \'{{print $4}}\'',
(f'ip a show {self._session.get_internet_interface()} | grep \"inet \" '
+ '| awk \'{{print $4}}\''),
shell=True).decode('utf-8').strip()

def _ci_post_network_create(self):
""" Restore network connection in CI environment """
LOGGER.info('post cr')
util.run_command(f'ip address del {self._ipv4} dev {self._session.get_internet_interface()}')
util.run_command(f'ip -6 address del {self._ipv6} dev {self._session.get_internet_interface()}')
util.run_command(((f'ip address del {self._ipv4} ' +
'dev {self._session.get_internet_interface()}')))
util.run_command((f'ip -6 address del {self._ipv6} ' +
'dev {self._session.get_internet_interface()}'))
util.run_command(
f'ip link set dev {self._session.get_internet_interface()} address 00:B0:D0:63:C2:26')
util.run_command(f'ip addr flush dev {self._session.get_internet_interface()}')
util.run_command(f'ip addr add dev {self._session.get_internet_interface()} 0.0.0.0')
(f'ip link set dev {self._session.get_internet_interface()} ' +
'address 00:B0:D0:63:C2:26'))
util.run_command(
f'ip addr flush dev {self._session.get_internet_interface()}')
util.run_command(
f'ip addr add dev {self._session.get_internet_interface()} 0.0.0.0')
util.run_command(
f'ip addr add dev {INTERNET_BRIDGE} {self._ipv4} broadcast {self._brd}')
util.run_command(f'ip -6 addr add {self._ipv6} dev {INTERNET_BRIDGE} ')
Expand All @@ -331,7 +326,7 @@ def create_net(self):

# TODO: This is not just for CI
#if self._single_intf:
#self._ci_pre_network_create()
#self._ci_pre_network_create()

# Setup the virtual network
if not self._ovs.create_baseline_net(verify=True):
Expand All @@ -341,15 +336,15 @@ def create_net(self):

# TODO: This is not just for CI
#if self._single_intf:
#self._ci_post_network_create()
#self._ci_post_network_create()

self._create_private_net()

self._listener = Listener(self._session)
self.get_listener().register_callback(self._device_discovered,
[NetworkEvent.DEVICE_DISCOVERED])
[NetworkEvent.DEVICE_DISCOVERED])
self.get_listener().register_callback(self._dhcp_lease_ack,
[NetworkEvent.DHCP_LEASE_ACK])
[NetworkEvent.DHCP_LEASE_ACK])

def load_network_modules(self):
"""Load network modules from module_config.json."""
Expand Down Expand Up @@ -624,7 +619,7 @@ def _attach_service_to_network(self, net_module):

# Add and configure the interface container
if not self._ip_ctrl.configure_container_interface(
bridge_intf, container_intf, "veth0", container_net_ns, mac_addr,
bridge_intf, container_intf, 'veth0', container_net_ns, mac_addr,
net_module.container_name, ipv4_addr, ipv6_addr):
LOGGER.error('Failed to configure local networking for ' +
net_module.name + '. Exiting.')
Expand All @@ -650,7 +645,7 @@ def _attach_service_to_network(self, net_module):
container_intf = 'tr-cti-' + net_module.dir_name

if not self._ip_ctrl.configure_container_interface(
bridge_intf, container_intf, "eth1", container_net_ns, mac_addr):
bridge_intf, container_intf, 'eth1', container_net_ns, mac_addr):
LOGGER.error('Failed to configure internet networking for ' +
net_module.name + '. Exiting.')
sys.exit(1)
Expand All @@ -667,7 +662,8 @@ def restore_net(self):

LOGGER.info('Clearing baseline network')

if hasattr(self, 'listener') and self.get_listener() is not None and self.get_listener().is_running():
if hasattr(self, 'listener') and self.get_listener(
) is not None and self.get_listener().is_running():
self.get_listener().stop_listener()

client = docker.from_env()
Expand Down Expand Up @@ -719,6 +715,7 @@ def __init__(self):

self.net_config = NetworkModuleNetConfig()


class NetworkModuleNetConfig:
"""Define all the properties of the network config
for a network module"""
Expand All @@ -741,6 +738,7 @@ def get_ipv4_addr_with_prefix(self):
def get_ipv6_addr_with_prefix(self):
return format(self.ipv6_address) + '/' + str(self.ipv6_network.prefixlen)


class NetworkConfig:
"""Define all the properties of the network configuration"""

Expand Down
12 changes: 6 additions & 6 deletions framework/python/src/net_orc/ovs_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""OVS Control Module"""
import json
import os
from common import logger
from common import util

Expand Down Expand Up @@ -80,12 +77,14 @@ def validate_baseline_network(self):
int_bridge = True

# Verify the device bridge
dev_bridge = self.verify_bridge(DEVICE_BRIDGE, [self._session.get_device_interface()])
dev_bridge = self.verify_bridge(DEVICE_BRIDGE,
[self._session.get_device_interface()])
LOGGER.debug('Device bridge verified: ' + str(dev_bridge))

# Verify the internet bridge
if 'single_intf' not in self._session.get_runtime_params():
int_bridge = self.verify_bridge(INTERNET_BRIDGE, [self._session.get_internet_interface()])
int_bridge = self.verify_bridge(INTERNET_BRIDGE,
[self._session.get_internet_interface()])
LOGGER.debug('Internet bridge verified: ' + str(int_bridge))

return dev_bridge and int_bridge
Expand Down Expand Up @@ -118,7 +117,8 @@ def create_baseline_net(self, verify=True):

# Remove IP from internet adapter
if not 'single_intf' in self._session.get_runtime_params():
self.set_interface_ip(interface=self._session.get_internet_interface(), ip_addr='0.0.0.0')
self.set_interface_ip(interface=self._session.get_internet_interface(),
ip_addr='0.0.0.0')
self.add_port(self._session.get_internet_interface(), INTERNET_BRIDGE)

# Enable forwarding of eapol packets
Expand Down
Loading