Merge "Modify handling Ixia traffic profile parameters"
[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_fixed_and_mask(self, port_range):
46         _port_range = str(port_range).split('-')
47         if len(_port_range) == 1:
48             return int(_port_range[0]), 0
49
50         return int(_port_range[0]), int(_port_range[1])
51
52     def _get_ixia_traffic_profile(self, profile_data, mac=None):
53         mac = {} if mac is None else mac
54         result = {}
55         for traffickey, values in profile_data.items():
56             if not traffickey.startswith((self.UPLINK, self.DOWNLINK)):
57                 continue
58
59             # values should be single-item dict, so just grab the first item
60             try:
61                 key, value = next(iter(values.items()))
62             except StopIteration:
63                 result[traffickey] = {}
64                 continue
65
66             port_id = value.get('id', 1)
67             port_index = port_id - 1
68
69             result[traffickey] = {
70                 'bidir': False,
71                 'id': port_id,
72                 'rate': self.rate,
73                 'rate_unit': self.rate_unit,
74                 'outer_l2': {},
75                 'outer_l3': {},
76                 'outer_l4': {},
77             }
78
79             outer_l2 = value.get('outer_l2')
80             if outer_l2:
81                 result[traffickey]['outer_l2'].update({
82                     'framesize': outer_l2.get('framesize'),
83                     'framesPerSecond': True,
84                     'QinQ': outer_l2.get('QinQ'),
85                     'srcmac': mac.get('src_mac_{}'.format(port_index)),
86                     'dstmac': mac.get('dst_mac_{}'.format(port_index)),
87                 })
88
89             if value.get('outer_l3v4'):
90                 outer_l3 = value['outer_l3v4']
91                 src_key, dst_key = 'srcip4', 'dstip4'
92             else:
93                 outer_l3 = value.get('outer_l3v6')
94                 src_key, dst_key = 'srcip6', 'dstip6'
95             if outer_l3:
96                 srcip = srcmask = dstip = dstmask = None
97                 if outer_l3.get(src_key):
98                     srcip, srcmask = self._get_ip_and_mask(outer_l3[src_key])
99                 if outer_l3.get(dst_key):
100                     dstip, dstmask = self._get_ip_and_mask(outer_l3[dst_key])
101
102                 result[traffickey]['outer_l3'].update({
103                     'count': outer_l3.get('count', 1),
104                     'dscp': outer_l3.get('dscp'),
105                     'ttl': outer_l3.get('ttl'),
106                     'srcseed': outer_l3.get('srcseed', 1),
107                     'dstseed': outer_l3.get('dstseed', 1),
108                     'srcip': srcip,
109                     'dstip': dstip,
110                     'srcmask': srcmask,
111                     'dstmask': dstmask,
112                     'type': key,
113                     'proto': outer_l3.get('proto'),
114                 })
115
116             outer_l4 = value.get('outer_l4')
117             if outer_l4:
118                 src_port = src_port_mask = dst_port = dst_port_mask = None
119                 if outer_l4.get('srcport'):
120                     src_port, src_port_mask = (
121                         self._get_fixed_and_mask(outer_l4['srcport']))
122
123                 if outer_l4.get('dstport'):
124                     dst_port, dst_port_mask = (
125                         self._get_fixed_and_mask(outer_l4['dstport']))
126
127                 result[traffickey]['outer_l4'].update({
128                     'srcport': src_port,
129                     'dstport': dst_port,
130                     'srcportmask': src_port_mask,
131                     'dstportmask': dst_port_mask,
132                     'count': outer_l4.get('count', 1),
133                     'seed': outer_l4.get('seed', 1),
134                 })
135
136         return result
137
138     def _ixia_traffic_generate(self, traffic, ixia_obj):
139         ixia_obj.update_frame(traffic, self.config.duration)
140         ixia_obj.update_ip_packet(traffic)
141         ixia_obj.update_l4(traffic)
142         ixia_obj.start_traffic()
143
144     def update_traffic_profile(self, traffic_generator):
145         def port_generator():
146             for vld_id, intfs in sorted(traffic_generator.networks.items()):
147                 if not vld_id.startswith((self.UPLINK, self.DOWNLINK)):
148                     continue
149                 profile_data = self.params.get(vld_id)
150                 if not profile_data:
151                     continue
152                 self.profile_data = profile_data
153                 self.full_profile.update({vld_id: self.profile_data})
154                 for intf in intfs:
155                     yield traffic_generator.vnfd_helper.port_num(intf)
156
157         self.ports = [port for port in port_generator()]
158
159     def execute_traffic(self, traffic_generator, ixia_obj=None, mac=None):
160         mac = {} if mac is None else mac
161         first_run = self.first_run
162         if self.first_run:
163             self.first_run = False
164             self.full_profile = {}
165             self.pg_id = 0
166             self.update_traffic_profile(traffic_generator)
167             self.max_rate = self.rate
168             self.min_rate = 0.0
169         else:
170             self.rate = round(float(self.max_rate + self.min_rate) / 2.0,
171                               self.RATE_ROUND)
172
173         traffic = self._get_ixia_traffic_profile(self.full_profile, mac)
174         self._ixia_traffic_generate(traffic, ixia_obj)
175         return first_run
176
177     def get_drop_percentage(self, samples, tol_min, tolerance,
178                             first_run=False):
179         completed = False
180         drop_percent = 100
181         num_ifaces = len(samples)
182         duration = self.config.duration
183         in_packets_sum = sum(
184             [samples[iface]['in_packets'] for iface in samples])
185         out_packets_sum = sum(
186             [samples[iface]['out_packets'] for iface in samples])
187         rx_throughput = round(float(in_packets_sum) / duration, 3)
188         tx_throughput = round(float(out_packets_sum) / duration, 3)
189         packet_drop = abs(out_packets_sum - in_packets_sum)
190
191         try:
192             drop_percent = round(
193                 (packet_drop / float(out_packets_sum)) * 100,
194                 self.DROP_PERCENT_ROUND)
195         except ZeroDivisionError:
196             LOG.info('No traffic is flowing')
197
198         if first_run:
199             completed = True if drop_percent <= tolerance else False
200         if (first_run and
201                 self.rate_unit == tp_base.TrafficProfileConfig.RATE_FPS):
202             self.rate = float(out_packets_sum) / duration / num_ifaces
203
204         if drop_percent > tolerance:
205             self.max_rate = self.rate
206         elif drop_percent < tol_min:
207             self.min_rate = self.rate
208         else:
209             completed = True
210
211         latency_ns_avg = float(
212             sum([samples[iface]['Store-Forward_Avg_latency_ns']
213             for iface in samples])) / num_ifaces
214         latency_ns_min = float(
215             sum([samples[iface]['Store-Forward_Min_latency_ns']
216             for iface in samples])) / num_ifaces
217         latency_ns_max = float(
218             sum([samples[iface]['Store-Forward_Max_latency_ns']
219             for iface in samples])) / num_ifaces
220
221         samples['TxThroughput'] = tx_throughput
222         samples['RxThroughput'] = rx_throughput
223         samples['DropPercentage'] = drop_percent
224         samples['latency_ns_avg'] = latency_ns_avg
225         samples['latency_ns_min'] = latency_ns_min
226         samples['latency_ns_max'] = latency_ns_max
227
228         return completed, samples