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