43455330fa49152356ec0e0fa1d59200fcec4f5b
[yardstick.git] / yardstick / network_services / traffic_profile / ixia_rfc2544.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import logging
16
17 from yardstick.common import utils
18 from yardstick.network_services.traffic_profile import base as tp_base
19 from yardstick.network_services.traffic_profile import trex_traffic_profile
20
21
22 LOG = logging.getLogger(__name__)
23
24
25 class IXIARFC2544Profile(trex_traffic_profile.TrexProfile):
26
27     UPLINK = 'uplink'
28     DOWNLINK = 'downlink'
29     DROP_PERCENT_ROUND = 6
30     RATE_ROUND = 5
31
32     def __init__(self, yaml_data):
33         super(IXIARFC2544Profile, self).__init__(yaml_data)
34         self.rate = self.config.frame_rate
35         self.rate_unit = self.config.rate_unit
36
37     def _get_ip_and_mask(self, ip_range):
38         _ip_range = ip_range.split('-')
39         if len(_ip_range) == 1:
40             return _ip_range[0], None
41
42         mask = utils.get_mask_from_ip_range(_ip_range[0], _ip_range[1])
43         return _ip_range[0], mask
44
45     def _get_ixia_traffic_profile(self, profile_data, mac=None):
46         mac = {} if mac is None else mac
47         result = {}
48         for traffickey, values in profile_data.items():
49             if not traffickey.startswith((self.UPLINK, self.DOWNLINK)):
50                 continue
51
52             try:
53                 # values should be single-item dict, so just grab the first item
54                 try:
55                     key, value = next(iter(values.items()))
56                 except StopIteration:
57                     result[traffickey] = {}
58                     continue
59
60                 port_id = value.get('id', 1)
61                 port_index = port_id - 1
62
63                 if value.get('outer_l3v4'):
64                     ip = value['outer_l3v4']
65                     src_key, dst_key = 'srcip4', 'dstip4'
66                 else:
67                     ip = value['outer_l3v6']
68                     src_key, dst_key = 'srcip6', 'dstip6'
69
70                 srcip, srcmask = self._get_ip_and_mask(ip[src_key])
71                 dstip, dstmask = self._get_ip_and_mask(ip[dst_key])
72
73                 result[traffickey] = {
74                     'bidir': False,
75                     'id': port_id,
76                     'rate': self.rate,
77                     'rate_unit': self.rate_unit,
78                     'outer_l2': {
79                         'framesize': value['outer_l2']['framesize'],
80                         'framesPerSecond': True,
81                         'srcmac': mac['src_mac_{}'.format(port_index)],
82                         'dstmac': mac['dst_mac_{}'.format(port_index)],
83                     },
84                     'outer_l3': {
85                         'count': ip['count'],
86                         'dscp': ip['dscp'],
87                         'ttl': ip['ttl'],
88                         'srcip': srcip,
89                         'dstip': dstip,
90                         'srcmask': srcmask,
91                         'dstmask': dstmask,
92                         'type': key,
93                         'proto': ip['proto'],
94                     },
95                     'outer_l4': value['outer_l4'],
96                 }
97             except KeyError:
98                 continue
99
100         return result
101
102     def _ixia_traffic_generate(self, traffic, ixia_obj):
103         ixia_obj.update_frame(traffic, self.config.duration)
104         ixia_obj.update_ip_packet(traffic)
105         ixia_obj.start_traffic()
106
107     def update_traffic_profile(self, traffic_generator):
108         def port_generator():
109             for vld_id, intfs in sorted(traffic_generator.networks.items()):
110                 if not vld_id.startswith((self.UPLINK, self.DOWNLINK)):
111                     continue
112                 profile_data = self.params.get(vld_id)
113                 if not profile_data:
114                     continue
115                 self.profile_data = profile_data
116                 self.full_profile.update({vld_id: self.profile_data})
117                 for intf in intfs:
118                     yield traffic_generator.vnfd_helper.port_num(intf)
119
120         self.ports = [port for port in port_generator()]
121
122     def execute_traffic(self, traffic_generator, ixia_obj=None, mac=None):
123         mac = {} if mac is None else mac
124         first_run = self.first_run
125         if self.first_run:
126             self.first_run = False
127             self.full_profile = {}
128             self.pg_id = 0
129             self.update_traffic_profile(traffic_generator)
130             self.max_rate = self.rate
131             self.min_rate = 0.0
132         else:
133             self.rate = round(float(self.max_rate + self.min_rate) / 2.0,
134                               self.RATE_ROUND)
135
136         traffic = self._get_ixia_traffic_profile(self.full_profile, mac)
137         self._ixia_traffic_generate(traffic, ixia_obj)
138         return first_run
139
140     def get_drop_percentage(self, samples, tol_min, tolerance,
141                             first_run=False):
142         completed = False
143         drop_percent = 100
144         num_ifaces = len(samples)
145         duration = self.config.duration
146         in_packets_sum = sum(
147             [samples[iface]['in_packets'] for iface in samples])
148         out_packets_sum = sum(
149             [samples[iface]['out_packets'] for iface in samples])
150         rx_throughput = sum(
151             [samples[iface]['RxThroughput'] for iface in samples])
152         rx_throughput = round(float(rx_throughput), 2)
153         tx_throughput = sum(
154             [samples[iface]['TxThroughput'] for iface in samples])
155         tx_throughput = round(float(tx_throughput), 2)
156         packet_drop = abs(out_packets_sum - in_packets_sum)
157
158         try:
159             drop_percent = round(
160                 (packet_drop / float(out_packets_sum)) * 100,
161                 self.DROP_PERCENT_ROUND)
162         except ZeroDivisionError:
163             LOG.info('No traffic is flowing')
164
165         samples['TxThroughput'] = tx_throughput
166         samples['RxThroughput'] = rx_throughput
167         samples['DropPercentage'] = drop_percent
168
169         if first_run:
170             completed = True if drop_percent <= tolerance else False
171         if (first_run and
172                 self.rate_unit == tp_base.TrafficProfileConfig.RATE_FPS):
173             self.rate = float(out_packets_sum) / duration / num_ifaces
174
175         if drop_percent > tolerance:
176             self.max_rate = self.rate
177         elif drop_percent < tol_min:
178             self.min_rate = self.rate
179         else:
180             completed = True
181
182         return completed, samples