Add IxNextgen API for settings IP priority
[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                     'priority': outer_l3.get('priority')
115                 })
116
117             outer_l4 = value.get('outer_l4')
118             if outer_l4:
119                 src_port = src_port_mask = dst_port = dst_port_mask = None
120                 if outer_l4.get('srcport'):
121                     src_port, src_port_mask = (
122                         self._get_fixed_and_mask(outer_l4['srcport']))
123
124                 if outer_l4.get('dstport'):
125                     dst_port, dst_port_mask = (
126                         self._get_fixed_and_mask(outer_l4['dstport']))
127
128                 result[traffickey]['outer_l4'].update({
129                     'srcport': src_port,
130                     'dstport': dst_port,
131                     'srcportmask': src_port_mask,
132                     'dstportmask': dst_port_mask,
133                     'count': outer_l4.get('count', 1),
134                     'seed': outer_l4.get('seed', 1),
135                 })
136
137         return result
138
139     def _ixia_traffic_generate(self, traffic, ixia_obj):
140         ixia_obj.update_frame(traffic, self.config.duration)
141         ixia_obj.update_ip_packet(traffic)
142         ixia_obj.update_l4(traffic)
143         ixia_obj.start_traffic()
144
145     def update_traffic_profile(self, traffic_generator):
146         def port_generator():
147             for vld_id, intfs in sorted(traffic_generator.networks.items()):
148                 if not vld_id.startswith((self.UPLINK, self.DOWNLINK)):
149                     continue
150                 profile_data = self.params.get(vld_id)
151                 if not profile_data:
152                     continue
153                 self.profile_data = profile_data
154                 self.full_profile.update({vld_id: self.profile_data})
155                 for intf in intfs:
156                     yield traffic_generator.vnfd_helper.port_num(intf)
157
158         self.ports = [port for port in port_generator()]
159
160     def execute_traffic(self, traffic_generator, ixia_obj=None, mac=None):
161         mac = {} if mac is None else mac
162         first_run = self.first_run
163         if self.first_run:
164             self.first_run = False
165             self.full_profile = {}
166             self.pg_id = 0
167             self.update_traffic_profile(traffic_generator)
168             self.max_rate = self.rate
169             self.min_rate = 0.0
170         else:
171             self.rate = round(float(self.max_rate + self.min_rate) / 2.0,
172                               self.RATE_ROUND)
173
174         traffic = self._get_ixia_traffic_profile(self.full_profile, mac)
175         self._ixia_traffic_generate(traffic, ixia_obj)
176         return first_run
177
178     def get_drop_percentage(self, samples, tol_min, tolerance,
179                             first_run=False):
180         completed = False
181         drop_percent = 100
182         num_ifaces = len(samples)
183         duration = self.config.duration
184         in_packets_sum = sum(
185             [samples[iface]['in_packets'] for iface in samples])
186         out_packets_sum = sum(
187             [samples[iface]['out_packets'] for iface in samples])
188         rx_throughput = round(float(in_packets_sum) / duration, 3)
189         tx_throughput = round(float(out_packets_sum) / duration, 3)
190         packet_drop = abs(out_packets_sum - in_packets_sum)
191
192         try:
193             drop_percent = round(
194                 (packet_drop / float(out_packets_sum)) * 100,
195                 self.DROP_PERCENT_ROUND)
196         except ZeroDivisionError:
197             LOG.info('No traffic is flowing')
198
199         if first_run:
200             completed = True if drop_percent <= tolerance else False
201         if (first_run and
202                 self.rate_unit == tp_base.TrafficProfileConfig.RATE_FPS):
203             self.rate = float(out_packets_sum) / duration / num_ifaces
204
205         if drop_percent > tolerance:
206             self.max_rate = self.rate
207         elif drop_percent < tol_min:
208             self.min_rate = self.rate
209         else:
210             completed = True
211
212         latency_ns_avg = float(
213             sum([samples[iface]['Store-Forward_Avg_latency_ns']
214             for iface in samples])) / num_ifaces
215         latency_ns_min = float(
216             sum([samples[iface]['Store-Forward_Min_latency_ns']
217             for iface in samples])) / num_ifaces
218         latency_ns_max = float(
219             sum([samples[iface]['Store-Forward_Max_latency_ns']
220             for iface in samples])) / num_ifaces
221
222         samples['TxThroughput'] = tx_throughput
223         samples['RxThroughput'] = rx_throughput
224         samples['DropPercentage'] = drop_percent
225         samples['latency_ns_avg'] = latency_ns_avg
226         samples['latency_ns_min'] = latency_ns_min
227         samples['latency_ns_max'] = latency_ns_max
228
229         return completed, samples