Add IP mask to IxNetwork configuration 47/60147/1
authorRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Mon, 23 Jul 2018 16:00:52 +0000 (17:00 +0100)
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Tue, 24 Jul 2018 07:59:37 +0000 (08:59 +0100)
Assign the IP mask to IxNetwork configuration, depending on the IP range
provided by the user in the traffic profile. In case of single IP, a
default mask will be provided:
  - IP_VERSION_4_MASK = 24
  - IP_VERSION_6_MASK = 64

JIRA: YARDSTICK-1351

Change-Id: I029473ab8b9966a76cf559ffef53b34ca11462ad
Signed-off-by: Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
yardstick/common/utils.py
yardstick/network_services/libs/ixia_libs/ixnet/ixnet_api.py
yardstick/network_services/traffic_profile/ixia_rfc2544.py
yardstick/tests/unit/common/test_utils.py
yardstick/tests/unit/network_services/libs/ixia_libs/test_ixnet_api.py
yardstick/tests/unit/network_services/traffic_profile/test_ixia_rfc2544.py

index 85cecc7..83ddbd4 100644 (file)
@@ -335,6 +335,14 @@ def ip_to_hex(ip_addr, separator=''):
     return separator.join('{:02x}'.format(octet) for octet in address.packed)
 
 
+def get_mask_from_ip_range(ip_low, ip_high):
+    _ip_low = ipaddress.ip_address(ip_low)
+    _ip_high = ipaddress.ip_address(ip_high)
+    _ip_low_int = int(_ip_low)
+    _ip_high_int = int(_ip_high)
+    return _ip_high.max_prefixlen - (_ip_high_int ^ _ip_low_int).bit_length()
+
+
 def try_int(s, *args):
     """Convert to integer if possible."""
     try:
index cee768d..9058fa7 100644 (file)
@@ -33,8 +33,8 @@ PROTO_UDP = 'udp'
 PROTO_TCP = 'tcp'
 PROTO_VLAN = 'vlan'
 
-IP_VERSION_4_MASK = '0.0.0.255'
-IP_VERSION_6_MASK = '0:0:0:0:0:0:0:ff'
+IP_VERSION_4_MASK = 24
+IP_VERSION_6_MASK = 64
 
 TRAFFIC_STATUS_STARTED = 'started'
 TRAFFIC_STATUS_STOPPED = 'stopped'
@@ -424,15 +424,17 @@ class IxNextgen(object):  # pragma: no cover
                 raise exceptions.IxNetworkFlowNotPresent(flow_group=fg_id)
 
             count = traffic_param['outer_l3']['count']
-            srcip4 = str(traffic_param['outer_l3']['srcip4'])
-            dstip4 = str(traffic_param['outer_l3']['dstip4'])
+            srcip = str(traffic_param['outer_l3']['srcip'])
+            dstip = str(traffic_param['outer_l3']['dstip'])
+            srcmask = traffic_param['outer_l3']['srcmask'] or IP_VERSION_4_MASK
+            dstmask = traffic_param['outer_l3']['dstmask'] or IP_VERSION_4_MASK
 
             self._update_ipv4_address(
                 self._get_stack_item(fg_id, PROTO_IPV4)[0],
-                'srcIp', srcip4, 1, IP_VERSION_4_MASK, count)
+                'srcIp', srcip, 1, srcmask, count)
             self._update_ipv4_address(
                 self._get_stack_item(fg_id, PROTO_IPV4)[0],
-                'dstIp', dstip4, 1, IP_VERSION_4_MASK, count)
+                'dstIp', dstip, 1, dstmask, count)
 
     def _build_stats_map(self, view_obj, name_map):
         return {data_yardstick: self.ixnet.execute(
index 49bac27..c32e9d8 100644 (file)
@@ -14,6 +14,7 @@
 
 import logging
 
+from yardstick.common import utils
 from yardstick.network_services.traffic_profile import base as tp_base
 from yardstick.network_services.traffic_profile import trex_traffic_profile
 
@@ -33,6 +34,14 @@ class IXIARFC2544Profile(trex_traffic_profile.TrexProfile):
         self.rate = self.config.frame_rate
         self.rate_unit = self.config.rate_unit
 
+    def _get_ip_and_mask(self, ip_range):
+        _ip_range = ip_range.split('-')
+        if len(_ip_range) == 1:
+            return _ip_range[0], None
+
+        mask = utils.get_mask_from_ip_range(_ip_range[0], _ip_range[1])
+        return _ip_range[0], mask
+
     def _get_ixia_traffic_profile(self, profile_data, mac=None):
         mac = {} if mac is None else mac
         result = {}
@@ -50,14 +59,17 @@ class IXIARFC2544Profile(trex_traffic_profile.TrexProfile):
 
                 port_id = value.get('id', 1)
                 port_index = port_id - 1
-                try:
-                    ip = value['outer_l3v6']
-                except KeyError:
+
+                if value.get('outer_l3v4'):
                     ip = value['outer_l3v4']
                     src_key, dst_key = 'srcip4', 'dstip4'
                 else:
+                    ip = value['outer_l3v6']
                     src_key, dst_key = 'srcip6', 'dstip6'
 
+                srcip, srcmask = self._get_ip_and_mask(ip[src_key])
+                dstip, dstmask = self._get_ip_and_mask(ip[dst_key])
+
                 result[traffickey] = {
                     'bidir': False,
                     'id': port_id,
@@ -73,8 +85,10 @@ class IXIARFC2544Profile(trex_traffic_profile.TrexProfile):
                         'count': ip['count'],
                         'dscp': ip['dscp'],
                         'ttl': ip['ttl'],
-                        src_key: ip[src_key].split("-")[0],
-                        dst_key: ip[dst_key].split("-")[0],
+                        'srcip': srcip,
+                        'dstip': dstip,
+                        'srcmask': srcmask,
+                        'dstmask': dstmask,
                         'type': key,
                         'proto': ip['proto'],
                     },
index 446afdd..bf0b518 100644 (file)
@@ -1196,6 +1196,20 @@ class TestUtilsIpAddrMethods(ut_base.BaseUnitTestCase):
         for value in chain(value_iter, self.INVALID_IP_ADDRESS_STR_LIST):
             self.assertEqual(utils.ip_to_hex(value), value)
 
+    def test_get_mask_from_ip_range_ipv4(self):
+        ip_str = '1.1.1.1'
+        for mask in range(8, 30):
+            ip = ipaddress.ip_network(ip_str + '/' + str(mask), strict=False)
+            result = utils.get_mask_from_ip_range(ip[2], ip[-2])
+            self.assertEqual(mask, result)
+
+    def test_get_mask_from_ip_range_ipv6(self):
+        ip_str = '2001::1'
+        for mask in range(8, 120):
+            ip = ipaddress.ip_network(ip_str + '/' + str(mask), strict=False)
+            result = utils.get_mask_from_ip_range(ip[2], ip[-2])
+            self.assertEqual(mask, result)
+
 
 class SafeDecodeUtf8TestCase(ut_base.BaseUnitTestCase):
 
index afa4cc3..458ea0f 100644 (file)
@@ -36,25 +36,12 @@ TRAFFIC_PARAMETERS = {
         'outer_l3': {
             'count': 512,
             'dscp': 0,
-            'dstip4': '152.16.40.20',
             'proto': 'udp',
-            'srcip4': '152.16.100.20',
-            'ttl': 32
-        },
-        'outer_l3v4': {
-            'dscp': 0,
-            'dstip4': '152.16.40.20',
-            'proto': 'udp',
-            'srcip4': '152.16.100.20',
-            'ttl': 32
-        },
-        'outer_l3v6': {
-            'count': 1024,
-            'dscp': 0,
-            'dstip4': '152.16.100.20',
-            'proto': 'udp',
-            'srcip4': '152.16.40.20',
-            'ttl': 32
+            'ttl': 32,
+            'dstip': '152.16.40.20',
+            'srcip': '152.16.100.20',
+            'dstmask': 24,
+            'srcmask': 24
         },
         'outer_l4': {
             'dstport': '2001',
@@ -74,26 +61,12 @@ TRAFFIC_PARAMETERS = {
         'outer_l3': {
             'count': 1024,
             'dscp': 0,
-            'dstip4': '152.16.100.20',
-            'proto': 'udp',
-            'srcip4': '152.16.40.20',
-            'ttl': 32
-        },
-        'outer_l3v4': {
-            'count': 1024,
-            'dscp': 0,
-            'dstip4': '152.16.100.20',
-            'proto': 'udp',
-            'srcip4': '152.16.40.20',
-            'ttl': 32
-        },
-        'outer_l3v6': {
-            'count': 1024,
-            'dscp': 0,
-            'dstip4': '152.16.100.20',
             'proto': 'udp',
-            'srcip4': '152.16.40.20',
-            'ttl': 32
+            'ttl': 32,
+            'dstip': '2001::10',
+            'srcip': '2021::10',
+            'dstmask': 64,
+            'srcmask': 64
         },
         'outer_l4': {
             'dstport': '1234',
index 4ea19a1..1a33304 100644 (file)
@@ -446,6 +446,22 @@ class TestIXIARFC2544Profile(unittest.TestCase):
         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(t_profile_data)
         self.assertEqual(12345678, r_f_c2544_profile.rate)
 
+    def test__get_ip_and_mask_range(self):
+        ip_range = '1.2.0.2-1.2.255.254'
+        r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
+            self.TRAFFIC_PROFILE)
+        ip, mask = r_f_c2544_profile._get_ip_and_mask(ip_range)
+        self.assertEqual('1.2.0.2', ip)
+        self.assertEqual(16, mask)
+
+    def test__get_ip_and_mask_single(self):
+        ip_range = '192.168.1.10'
+        r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
+            self.TRAFFIC_PROFILE)
+        ip, mask = r_f_c2544_profile._get_ip_and_mask(ip_range)
+        self.assertEqual('192.168.1.10', ip)
+        self.assertIsNone(mask)
+
     def test__get_ixia_traffic_profile_default_args(self):
         r_f_c2544_profile = ixia_rfc2544.IXIARFC2544Profile(
             self.TRAFFIC_PROFILE)