NFVBENCH-115 SRIOV multi-chain with non shared networks fails
[nfvbench.git] / nfvbench / chaining.py
index fa9b799..99080ba 100644 (file)
@@ -194,9 +194,17 @@ class ChainNetwork(object):
     """Could be a shared network across all chains or a chain private network."""
 
     def __init__(self, manager, network_config, chain_id=None, lookup_only=False):
-        """Create a network for given chain."""
+        """Create a network for given chain.
+
+        network_config: a dict containing the network properties
+                        (segmentation_id and physical_network)
+        chain_id: to which chain the networks belong.
+                  a None value will mean that these networks are shared by all chains
+        """
         self.manager = manager
         self.name = network_config.name
+        self.segmentation_id = self._get_item(network_config.segmentation_id, chain_id)
+        self.physical_network = self._get_item(network_config.physical_network, chain_id)
         if chain_id is not None:
             self.name += str(chain_id)
         self.reuse = False
@@ -212,6 +220,24 @@ class ChainNetwork(object):
             self.delete()
             raise
 
+    def _get_item(self, item_field, index):
+        """Retrieve an item from a list or a single value.
+
+        item_field: can be None, a tuple of a single value
+        index: if None is same as 0, else is the index in the list
+
+        If the item_field is not a tuple, it is considered same as a tuple with same value at any
+        index.
+        If a list is provided, its length must be > index
+        """
+        if not item_field:
+            return None
+        if index is None:
+            index = 0
+        if isinstance(item_field, tuple):
+            return item_field[index]
+        return item_field
+
     def _setup(self, network_config, lookup_only):
         # Lookup if there is a matching network with same name
         networks = self.manager.neutron_client.list_networks(name=self.name)
@@ -219,23 +245,23 @@ class ChainNetwork(object):
             network = networks['networks'][0]
             # a network of same name already exists, we need to verify it has the same
             # characteristics
-            if network_config.segmentation_id:
-                if network['provider:segmentation_id'] != network_config.segmentation_id:
+            if self.segmentation_id:
+                if network['provider:segmentation_id'] != self.segmentation_id:
                     raise ChainException("Mismatch of 'segmentation_id' for reused "
                                          "network '{net}'. Network has id '{seg_id1}', "
                                          "configuration requires '{seg_id2}'."
                                          .format(net=self.name,
                                                  seg_id1=network['provider:segmentation_id'],
-                                                 seg_id2=network_config.segmentation_id))
+                                                 seg_id2=self.segmentation_id))
 
-            if network_config.physical_network:
-                if network['provider:physical_network'] != network_config.physical_network:
+            if self.physical_network:
+                if network['provider:physical_network'] != self.physical_network:
                     raise ChainException("Mismatch of 'physical_network' for reused "
                                          "network '{net}'. Network has '{phys1}', "
                                          "configuration requires '{phys2}'."
                                          .format(net=self.name,
                                                  phys1=network['provider:physical_network'],
-                                                 phys2=network_config.physical_network))
+                                                 phys2=self.physical_network))
 
             LOG.info('Reusing existing network %s', self.name)
             self.reuse = True
@@ -251,10 +277,10 @@ class ChainNetwork(object):
             }
             if network_config.network_type:
                 body['network']['provider:network_type'] = network_config.network_type
-                if network_config.segmentation_id:
-                    body['network']['provider:segmentation_id'] = network_config.segmentation_id
-                if network_config.physical_network:
-                    body['network']['provider:physical_network'] = network_config.physical_network
+                if self.segmentation_id:
+                    body['network']['provider:segmentation_id'] = self.segmentation_id
+                if self.physical_network:
+                    body['network']['provider:physical_network'] = self.physical_network
 
             self.network = self.manager.neutron_client.create_network(body)['network']
             body = {
@@ -288,6 +314,16 @@ class ChainNetwork(object):
             raise ChainException('Trying to retrieve VLAN id for non VLAN network')
         return self.network['provider:segmentation_id']
 
+    def get_vxlan(self):
+        """
+        Extract VNI for this network.
+
+        :return: VNI ID for this network
+        """
+        if self.network['provider:network_type'] != 'vxlan':
+            raise ChainException('Trying to retrieve VNI for non VXLAN network')
+        return self.network['provider:segmentation_id']
+
     def delete(self):
         """Delete this network."""
         if not self.reuse and self.network:
@@ -367,13 +403,13 @@ class ChainVnf(object):
         can use vswitch or SR-IOV based on config.use_sriov_middle_net
         """
         if self.manager.config.sriov:
-            if self.manager.config.use_sriov_middle_net:
+            chain_length = self.chain.get_length()
+            if self.manager.config.use_sriov_middle_net or chain_length == 1:
                 return 'direct'
-            if self.vnf_id == 0:
+            if self.vnf_id == 0 and port_index == 0:
                 # first VNF in chain must use sriov for left port
-                if port_index == 0:
-                    return 'direct'
-            elif (self.vnf_id == self.chain.get_length() - 1) and (port_index == 1):
+                return 'direct'
+            if (self.vnf_id == chain_length - 1) and (port_index == 1):
                 # last VNF in chain must use sriov for right port
                 return 'direct'
         return 'normal'
@@ -631,6 +667,20 @@ class Chain(object):
             port_index = -1
         return self.networks[port_index].get_vlan()
 
+    def get_vxlan(self, port_index):
+        """Get the VXLAN id on a given port.
+
+        port_index: left port is 0, right port is 1
+        return: the vxlan_id or None if there is no vxlan
+        """
+        # for port 1 we need to return the VLAN 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_vxlan()
+
     def get_dest_mac(self, port_index):
         """Get the dest MAC on a given port.
 
@@ -823,16 +873,23 @@ class ChainManager(object):
                 raise
         else:
             # no openstack, no need to create chains
-            # make sure there at least as many entries as chains in each left/right list
-            if len(config.vlans) != 2:
-                raise ChainException('The config vlans property must be a list '
-                                     'with 2 lists of VLAN IDs')
-            if not config.l2_loopback:
-                self._get_dest_macs_from_config()
 
-            re_vlan = "[0-9]*$"
-            self.vlans = [self._check_list('vlans[0]', config.vlans[0], re_vlan),
-                          self._check_list('vlans[1]', config.vlans[1], re_vlan)]
+            if not config.l2_loopback and config.no_arp:
+                self._get_dest_macs_from_config()
+            if config.vlan_tagging:
+                # make sure there at least as many entries as chains in each left/right list
+                if len(config.vlans) != 2:
+                    raise ChainException('The config vlans property must be a list '
+                                         'with 2 lists of VLAN IDs')
+                re_vlan = "[0-9]*$"
+                self.vlans = [self._check_list('vlans[0]', config.vlans[0], re_vlan),
+                              self._check_list('vlans[1]', config.vlans[1], re_vlan)]
+            if config.vxlan:
+                # make sure there are 2 entries
+                if len(config.vnis) != 2:
+                    raise ChainException('The config vnis property must be a list with 2 VNIs')
+                self.vnis = [self._check_list('vnis[0]', config.vnis[0], re_vlan),
+                             self._check_list('vnis[1]', config.vnis[1], re_vlan)]
 
     def _get_dest_macs_from_config(self):
         re_mac = "[0-9a-fA-F]{2}([-:])[0-9a-fA-F]{2}(\\1[0-9a-fA-F]{2}){4}$"
@@ -847,7 +904,8 @@ class ChainManager(object):
         if isinstance(ll, (int, str)):
             ll = [ll]
         if not ll or len(ll) < self.chain_count:
-            raise ChainException('%s=%s must be a list with 1 element per chain' % (list_name, ll))
+            raise ChainException('%s=%s must be a list with %d elements per chain' %
+                                 (list_name, ll, self.chain_count))
         for item in ll:
             if not re.match(pattern, str(item)):
                 raise ChainException("Invalid format '{item}' specified in {fname}"
@@ -928,6 +986,39 @@ class ChainManager(object):
         if initial_instance_count:
             LOG.info('All instances are active')
 
+    def _get_vxlan_net_cfg(self, chain_id):
+        int_nets = self.config.internal_networks
+        net_left = int_nets.left
+        net_right = int_nets.right
+        vnis = self.generator_config.vnis
+        chain_id += 1
+        seg_id_left = vnis[0]
+        if self.config.service_chain == ChainType.PVP:
+            if chain_id > 1:
+                seg_id_left = ((chain_id - 1) * 2) + seg_id_left
+            seg_id_right = seg_id_left + 1
+            if (seg_id_left and seg_id_right) > vnis[1]:
+                raise Exception('Segmentation ID is more than allowed '
+                                'value: {}'.format(vnis[1]))
+            net_left['segmentation_id'] = seg_id_left
+            net_right['segmentation_id'] = seg_id_right
+            net_cfg = [net_left, net_right]
+        else:
+            # PVVP
+            net_middle = int_nets.middle
+            if chain_id > 1:
+                seg_id_left = ((chain_id - 1) * 3) + seg_id_left
+            seg_id_middle = seg_id_left + 1
+            seg_id_right = seg_id_left + 2
+            if (seg_id_left and seg_id_right and seg_id_middle) > vnis[1]:
+                raise Exception('Segmentation ID is more than allowed '
+                                'value: {}'.format(vnis[1]))
+            net_left['segmentation_id'] = seg_id_left
+            net_middle['segmentation_id'] = seg_id_middle
+            net_right['segmentation_id'] = seg_id_right
+            net_cfg = [net_left, net_middle, net_right]
+        return net_cfg
+
     def get_networks(self, chain_id=None):
         """Get the networks for given EXT, PVP or PVVP chain.
 
@@ -950,10 +1041,15 @@ class ChainManager(object):
         else:
             lookup_only = False
             int_nets = self.config.internal_networks
-            if self.config.service_chain == ChainType.PVP:
-                net_cfg = [int_nets.left, int_nets.right]
+            network_type = set([int_nets[net].get('network_type') for net in int_nets])
+            if self.config.vxlan and 'vxlan' in network_type:
+                net_cfg = self._get_vxlan_net_cfg(chain_id)
             else:
-                net_cfg = [int_nets.left, int_nets.middle, int_nets.right]
+                # VLAN
+                if self.config.service_chain == ChainType.PVP:
+                    net_cfg = [int_nets.left, int_nets.right]
+                else:
+                    net_cfg = [int_nets.left, int_nets.middle, int_nets.right]
         networks = []
         try:
             for cfg in net_cfg:
@@ -1046,6 +1142,18 @@ class ChainManager(object):
         # no openstack
         return self.vlans[port_index]
 
+    def get_chain_vxlans(self, port_index):
+        """Get the list of per chain VNIs id on a given port.
+
+        port_index: left port is 0, right port is 1
+        return: a VNIs ID list indexed by the chain index or None if no vlan tagging
+        """
+        if self.chains:
+            return [self.chains[chain_index].get_vxlan(port_index)
+                    for chain_index in range(self.chain_count)]
+        # no openstack
+        return self.vnis[port_index]
+
     def get_dest_macs(self, port_index):
         """Get the list of per chain dest MACs on a given port.
 
@@ -1073,7 +1181,7 @@ class ChainManager(object):
                 return self.chains[0].get_host_ips()
             # in the case of EXT, the compute node must be retrieved from the port
             # associated to any of the dest MACs
-            dst_macs = self.chain_runner.traffic_client.gen.get_dest_macs()
+            dst_macs = self.generator_config.get_dest_macs()
             # dest MAC on port 0, chain 0
             dst_mac = dst_macs[0][0]
             host_ip = self.get_host_ip_from_mac(dst_mac)
@@ -1097,12 +1205,7 @@ class ChainManager(object):
         return []
 
     def delete(self):
-        """Delete resources for all chains.
-
-        Will not delete any resource if no-cleanup has been requested.
-        """
-        if self.config.no_cleanup:
-            return
+        """Delete resources for all chains."""
         for chain in self.chains:
             chain.delete()
         for network in self.networks: