MPLS support + loop_vm_arp test fix
[nfvbench.git] / nfvbench / chaining.py
index d035a40..b9ed48b 100644 (file)
@@ -54,10 +54,10 @@ from neutronclient.neutron import client as neutronclient
 from novaclient.client import Client
 
 from attrdict import AttrDict
-from chain_router import ChainRouter
-import compute
-from log import LOG
-from specs import ChainType
+from .chain_router import ChainRouter
+from . import compute
+from .log import LOG
+from .specs import ChainType
 # Left and right index for network and port lists
 LEFT = 0
 RIGHT = 1
@@ -77,9 +77,6 @@ BOOT_SCRIPT_PATHNAME = os.path.join(os.path.dirname(os.path.abspath(__file__)),
 class ChainException(Exception):
     """Exception while operating the chains."""
 
-    pass
-
-
 class NetworkEncaps(object):
     """Network encapsulation."""
 
@@ -373,6 +370,15 @@ class ChainNetwork(object):
 
         return self.network['provider:segmentation_id']
 
+    def get_mpls_inner_label(self):
+        """
+        Extract MPLS VPN Label for this network.
+
+        :return: MPLS VPN Label for this network
+        """
+
+        return self.network['provider:segmentation_id']
+
     def delete(self):
         """Delete this network."""
         if not self.reuse and self.network:
@@ -459,8 +465,12 @@ class ChainVnf(object):
         else:
             tg_gateway1_ip = devices[LEFT].tg_gateway_ip_addrs
             tg_gateway2_ip = devices[RIGHT].tg_gateway_ip_addrs
-            tg_mac1 = remote_mac_pair[0]
-            tg_mac2 = remote_mac_pair[1]
+            if not config.loop_vm_arp:
+                tg_mac1 = remote_mac_pair[0]
+                tg_mac2 = remote_mac_pair[1]
+            else:
+                tg_mac1 = ""
+                tg_mac2 = ""
 
             g1cidr = devices[LEFT].get_gw_ip(
                 self.chain.chain_id) + self.__get_network_mask(
@@ -706,8 +716,8 @@ class ChainVnf(object):
                     # here we MUST wait until this instance is resolved otherwise subsequent
                     # VNF creation can be placed in other hypervisors!
                     config = self.manager.config
-                    max_retries = (config.check_traffic_time_sec +
-                                   config.generic_poll_sec - 1) / config.generic_poll_sec
+                    max_retries = int((config.check_traffic_time_sec +
+                                       config.generic_poll_sec - 1) / config.generic_poll_sec)
                     retry = 0
                     for retry in range(max_retries):
                         status = self.get_status()
@@ -930,6 +940,20 @@ class Chain(object):
             port_index = -1
         return self.networks[port_index].get_vxlan()
 
+    def get_mpls_inner_label(self, port_index):
+        """Get the MPLS VPN Label on a given port.
+
+        port_index: left port is 0, right port is 1
+        return: the mpls_label_id or None if there is no mpls
+        """
+        # for port 1 we need to return the MPLS Label of the last network in the chain
+        # The networks array contains 2 networks for PVP [left, right]
+        # and 3 networks in the case of PVVP [left.middle,right]
+        if port_index:
+            # this will pick the last item in array
+            port_index = -1
+        return self.networks[port_index].get_mpls_inner_label()
+
     def get_dest_mac(self, port_index):
         """Get the dest MAC on a given port.
 
@@ -1247,7 +1271,6 @@ class ChainManager(object):
         for chain in self.chains:
             instances.extend(chain.get_instances())
         initial_instance_count = len(instances)
-        # Give additional 10 seconds per VM
         max_retries = (self.config.check_traffic_time_sec + (initial_instance_count - 1) * 10 +
                        self.config.generic_poll_sec - 1) / self.config.generic_poll_sec
         retry = 0
@@ -1380,7 +1403,7 @@ class ChainManager(object):
         return: the hypervisor where the matching port runs or None if not found
         """
         # _existing_ports is a dict of list of ports indexed by network id
-        for port_list in self.get_existing_ports().values():
+        for port_list in list(self.get_existing_ports().values()):
             for port in port_list:
                 try:
                     if port['mac_address'] == mac:
@@ -1425,6 +1448,18 @@ class ChainManager(object):
         # no openstack
         raise ChainException('VxLAN is only supported with OpenStack and with admin user')
 
+    def get_chain_mpls_inner_labels(self, port_index):
+        """Get the list of per chain MPLS VPN Labels on a given port.
+
+        port_index: left port is 0, right port is 1
+        return: a MPLSs ID list indexed by the chain index or None if no mpls
+        """
+        if self.chains and self.is_admin:
+            return [self.chains[chain_index].get_mpls_inner_label(port_index)
+                    for chain_index in range(self.chain_count)]
+        # no openstack
+        raise ChainException('MPLS is only supported with OpenStack and with admin user')
+
     def get_dest_macs(self, port_index):
         """Get the list of per chain dest MACs on a given port.