Adding ixia latency support for dynamic cgnapt
[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 import json
18
19 from yardstick.network_services.traffic_profile.traffic_profile import \
20     TrexProfile
21
22 LOG = logging.getLogger(__name__)
23
24
25 class IXIARFC2544Profile(TrexProfile):
26     def _get_ixia_traffic_profile(self, profile_data, mac={},
27                                   xfile=None, static_traffic={}):
28         result = {}
29         if xfile:
30             with open(xfile, 'r') as stream:
31                 try:
32                     static_traffic = json.load(stream)
33                 except Exception as exc:
34                     LOG.debug(exc)
35
36         for traffickey, trafficvalue in static_traffic.items():
37             traffic = static_traffic[traffickey]
38             # outer_l2
39             index = 0
40             for key, value in profile_data[traffickey].items():
41                 framesize = value['outer_l2']['framesize']
42                 traffic['outer_l2']['framesize'] = framesize
43                 traffic['framesPerSecond'] = True
44                 traffic['bidir'] = False
45                 traffic['outer_l2']['srcmac'] = \
46                     mac["src_mac_{}".format(traffic['id'])]
47                 traffic['outer_l2']['dstmac'] = \
48                     mac["dst_mac_{}".format(traffic['id'])]
49
50                 # outer_l3
51                 if "outer_l3v6" in list(value.keys()):
52                     traffic['outer_l3'] = value['outer_l3v6']
53                     srcip4 = value['outer_l3v6']['srcip6']
54                     traffic['outer_l3']['srcip4'] = srcip4.split("-")[0]
55                     dstip4 = value['outer_l3v6']['dstip6']
56                     traffic['outer_l3']['dstip4'] = dstip4.split("-")[0]
57                 else:
58                     traffic['outer_l3'] = value['outer_l3v4']
59                     srcip4 = value['outer_l3v4']['srcip4']
60                     traffic['outer_l3']['srcip4'] = srcip4.split("-")[0]
61                     dstip4 = value['outer_l3v4']['dstip4']
62                     traffic['outer_l3']['dstip4'] = dstip4.split("-")[0]
63
64                 traffic['outer_l3']['type'] = key
65                 # outer_l4
66                 traffic['outer_l4'] = value['outer_l4']
67                 index = index + 1
68             result.update({traffickey: traffic})
69
70         return result
71
72     def _ixia_traffic_generate(self, traffic_generator, traffic, ixia_obj):
73         for key, value in traffic.items():
74             if "public" in key or "private" in key:
75                 traffic[key]["iload"] = str(self.rate)
76         ixia_obj.ix_update_frame(traffic)
77         ixia_obj.ix_update_ether(traffic)
78         ixia_obj.add_ip_header(traffic, 4)
79         ixia_obj.ix_start_traffic()
80         self.tmp_drop = 0
81         self.tmp_throughput = 0
82
83     def update_traffic_profile(self):
84         self.profile = 'private_1'
85         for key, value in self.params.items():
86             if "private" in key or "public" in key:
87                 self.profile_data = self.params[key]
88                 self.get_streams(self.profile_data)
89                 self.full_profile.update({key: self.profile_data})
90
91     def execute(self, traffic_generator, ixia_obj, mac={}, xfile=None):
92         if self.first_run:
93             self.full_profile = {}
94             self.pg_id = 0
95             self.update_traffic_profile()
96             traffic = \
97                 self._get_ixia_traffic_profile(self.full_profile, mac, xfile)
98             self.max_rate = self.rate
99             self.min_rate = 0
100             self.get_multiplier()
101             self._ixia_traffic_generate(traffic_generator, traffic, ixia_obj)
102
103     def get_multiplier(self):
104         self.rate = round((self.max_rate + self.min_rate) / 2.0, 2)
105         multiplier = round(self.rate / self.pps, 2)
106         return str(multiplier)
107
108     def start_ixia_latency(self, traffic_generator, ixia_obj,
109                            mac={}, xfile=None):
110         self.update_traffic_profile()
111         traffic = \
112             self._get_ixia_traffic_profile(self.full_profile, mac, xfile)
113         self._ixia_traffic_generate(traffic_generator, traffic,
114                                     ixia_obj, xfile)
115
116     def get_drop_percentage(self, traffic_generator, samples, tol_min,
117                             tolerance, ixia_obj, mac={}, xfile=None):
118         status = 'Running'
119         drop_percent = 100
120         in_packets = sum([samples[iface]['in_packets'] for iface in samples])
121         out_packets = sum([samples[iface]['out_packets'] for iface in samples])
122         rx_throughput = \
123             sum([samples[iface]['RxThroughput'] for iface in samples])
124         tx_throughput = \
125             sum([samples[iface]['TxThroughput'] for iface in samples])
126         packet_drop = abs(out_packets - in_packets)
127         try:
128             drop_percent = round((packet_drop / float(out_packets)) * 100, 2)
129         except ZeroDivisionError:
130             LOG.info('No traffic is flowing')
131         samples['TxThroughput'] = round(tx_throughput / 1.0, 2)
132         samples['RxThroughput'] = round(rx_throughput / 1.0, 2)
133         samples['CurrentDropPercentage'] = drop_percent
134         samples['Throughput'] = self.tmp_throughput
135         samples['DropPercentage'] = self.tmp_drop
136         if drop_percent > tolerance and self.tmp_throughput == 0:
137             samples['Throughput'] = round(rx_throughput / 1.0, 2)
138             samples['DropPercentage'] = drop_percent
139         if self.first_run:
140             max_supported_rate = out_packets / 30.0
141             self.rate = max_supported_rate
142             self.first_run = False
143             if drop_percent <= tolerance:
144                 status = 'Completed'
145         if drop_percent > tolerance:
146             self.max_rate = self.rate
147         elif drop_percent < tol_min:
148             self.min_rate = self.rate
149             if drop_percent >= self.tmp_drop:
150                 self.tmp_drop = drop_percent
151                 self.tmp_throughput = round((rx_throughput / 1.0), 2)
152                 samples['Throughput'] = round(rx_throughput / 1.0, 2)
153                 samples['DropPercentage'] = drop_percent
154         else:
155             samples['Throughput'] = round(rx_throughput / 1.0, 2)
156             samples['DropPercentage'] = drop_percent
157             return status, samples
158         self.get_multiplier()
159         traffic = self._get_ixia_traffic_profile(self.full_profile, mac, xfile)
160         self._ixia_traffic_generate(traffic_generator, traffic, ixia_obj)
161         return status, samples