Improve IXIA IxNetwork library and traffic profile (3)
[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 from __future__ import absolute_import
16 import logging
17
18 from yardstick.network_services.traffic_profile.trex_traffic_profile import \
19     TrexProfile
20
21 LOG = logging.getLogger(__name__)
22
23
24 class IXIARFC2544Profile(TrexProfile):
25
26     UPLINK = 'uplink'
27     DOWNLINK = 'downlink'
28
29     def _get_ixia_traffic_profile(self, profile_data, mac=None):
30         if mac is None:
31             mac = {}
32
33         result = {}
34         for traffickey, values in profile_data.items():
35             if not traffickey.startswith((self.UPLINK, self.DOWNLINK)):
36                 continue
37
38             try:
39                 # values should be single-item dict, so just grab the first item
40                 try:
41                     key, value = next(iter(values.items()))
42                 except StopIteration:
43                     result[traffickey] = {}
44                     continue
45
46                 port_id = value.get('id', 1)
47                 port_index = port_id - 1
48                 try:
49                     ip = value['outer_l3v6']
50                 except KeyError:
51                     ip = value['outer_l3v4']
52                     src_key, dst_key = 'srcip4', 'dstip4'
53                 else:
54                     src_key, dst_key = 'srcip6', 'dstip6'
55
56                 result[traffickey] = {
57                     'bidir': False,
58                     'iload': '100',
59                     'id': port_id,
60                     'outer_l2': {
61                         'framesize': value['outer_l2']['framesize'],
62                         'framesPerSecond': True,
63                         'srcmac': mac['src_mac_{}'.format(port_index)],
64                         'dstmac': mac['dst_mac_{}'.format(port_index)],
65                     },
66                     'outer_l3': {
67                         'count': ip['count'],
68                         'dscp': ip['dscp'],
69                         'ttl': ip['ttl'],
70                         src_key: ip[src_key].split("-")[0],
71                         dst_key: ip[dst_key].split("-")[0],
72                         'type': key,
73                         'proto': ip['proto'],
74                     },
75                     'outer_l4': value['outer_l4'],
76                 }
77             except KeyError:
78                 continue
79
80         return result
81
82     def _ixia_traffic_generate(self, traffic, ixia_obj):
83         for key, value in traffic.items():
84             if key.startswith((self.UPLINK, self.DOWNLINK)):
85                 value["iload"] = str(self.rate)
86         ixia_obj.update_frame(traffic)
87         ixia_obj.update_ip_packet(traffic)
88         ixia_obj.start_traffic()
89         self.tmp_drop = 0
90         self.tmp_throughput = 0
91
92     def update_traffic_profile(self, traffic_generator):
93         def port_generator():
94             for vld_id, intfs in sorted(traffic_generator.networks.items()):
95                 if not vld_id.startswith((self.UPLINK, self.DOWNLINK)):
96                     continue
97                 profile_data = self.params.get(vld_id)
98                 if not profile_data:
99                     continue
100                 self.profile_data = profile_data
101                 self.get_streams(self.profile_data)
102                 self.full_profile.update({vld_id: self.profile_data})
103                 for intf in intfs:
104                     yield traffic_generator.vnfd_helper.port_num(intf)
105
106         self.ports = [port for port in port_generator()]
107
108     def execute_traffic(self, traffic_generator, ixia_obj, mac=None):
109         if mac is None:
110             mac = {}
111         if self.first_run:
112             self.full_profile = {}
113             self.pg_id = 0
114             self.update_traffic_profile(traffic_generator)
115             traffic = \
116                 self._get_ixia_traffic_profile(self.full_profile, mac)
117             self.max_rate = self.rate
118             self.min_rate = 0
119             self.get_multiplier()
120             self._ixia_traffic_generate(traffic, ixia_obj)
121
122     def get_multiplier(self):
123         self.rate = round((self.max_rate + self.min_rate) / 2.0, 2)
124         multiplier = round(self.rate / self.pps, 2)
125         return str(multiplier)
126
127     def start_ixia_latency(self, traffic_generator, ixia_obj, mac=None):
128         if mac is None:
129             mac = {}
130         self.update_traffic_profile(traffic_generator)
131         traffic = \
132             self._get_ixia_traffic_profile(self.full_profile, mac)
133         self._ixia_traffic_generate(traffic, ixia_obj)
134
135     def get_drop_percentage(self, samples, tol_min, tolerance, ixia_obj,
136                             mac=None):
137         if mac is None:
138             mac = {}
139         status = 'Running'
140         drop_percent = 100
141         in_packets = sum([samples[iface]['in_packets'] for iface in samples])
142         out_packets = sum([samples[iface]['out_packets'] for iface in samples])
143         rx_throughput = \
144             sum([samples[iface]['RxThroughput'] for iface in samples])
145         tx_throughput = \
146             sum([samples[iface]['TxThroughput'] for iface in samples])
147         packet_drop = abs(out_packets - in_packets)
148         try:
149             drop_percent = round((packet_drop / float(out_packets)) * 100, 2)
150         except ZeroDivisionError:
151             LOG.info('No traffic is flowing')
152         samples['TxThroughput'] = round(tx_throughput / 1.0, 2)
153         samples['RxThroughput'] = round(rx_throughput / 1.0, 2)
154         samples['CurrentDropPercentage'] = drop_percent
155         samples['Throughput'] = self.tmp_throughput
156         samples['DropPercentage'] = self.tmp_drop
157         if drop_percent > tolerance and self.tmp_throughput == 0:
158             samples['Throughput'] = round(rx_throughput / 1.0, 2)
159             samples['DropPercentage'] = drop_percent
160         if self.first_run:
161             max_supported_rate = out_packets / 30.0
162             self.rate = max_supported_rate
163             self.first_run = False
164             if drop_percent <= tolerance:
165                 status = 'Completed'
166         if drop_percent > tolerance:
167             self.max_rate = self.rate
168         elif drop_percent < tol_min:
169             self.min_rate = self.rate
170             if drop_percent >= self.tmp_drop:
171                 self.tmp_drop = drop_percent
172                 self.tmp_throughput = round((rx_throughput / 1.0), 2)
173                 samples['Throughput'] = round(rx_throughput / 1.0, 2)
174                 samples['DropPercentage'] = drop_percent
175         else:
176             samples['Throughput'] = round(rx_throughput / 1.0, 2)
177             samples['DropPercentage'] = drop_percent
178             return status, samples
179         self.get_multiplier()
180         traffic = self._get_ixia_traffic_profile(self.full_profile, mac)
181         self._ixia_traffic_generate(traffic, ixia_obj)
182         return status, samples