Merge "IXIA traffic generator"
[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                 # outer_l3
50                 if "outer_l3v6" in list(value.keys()):
51                     traffic['outer_l3'] = value['outer_l3v6']
52                     srcip4 = value['outer_l3v6']['srcip6']
53                     traffic['outer_l3']['srcip4'] = srcip4.split("-")[0]
54                     dstip4 = value['outer_l3v6']['dstip6']
55                     traffic['outer_l3']['dstip4'] = dstip4.split("-")[0]
56                 else:
57                     traffic['outer_l3'] = value['outer_l3v4']
58                     srcip4 = value['outer_l3v4']['srcip4']
59                     traffic['outer_l3']['srcip4'] = srcip4.split("-")[0]
60                     dstip4 = value['outer_l3v4']['dstip4']
61                     traffic['outer_l3']['dstip4'] = dstip4.split("-")[0]
62
63                 traffic['outer_l3']['type'] = key
64                 # outer_l4
65                 traffic['outer_l4'] = value['outer_l4']
66                 index = index + 1
67             result.update({traffickey: traffic})
68
69         return result
70
71     def _ixia_traffic_generate(self, traffic_generator, traffic, ixia_obj):
72         for key, value in traffic.items():
73             if "public" in key or "private" in key:
74                 traffic[key]["iload"] = str(self.rate)
75         ixia_obj.ix_update_frame(traffic)
76         ixia_obj.ix_update_ether(traffic)
77         # ixia_obj.ix_update_ipv4(traffic)
78         ixia_obj.ix_start_traffic()
79         self.tmp_drop = 0
80         self.tmp_throughput = 0
81
82     def execute(self, traffic_generator, ixia_obj, mac={}, xfile=None):
83         if self.first_run:
84             self.full_profile = {}
85             self.pg_id = 0
86             self.profile = 'private_1'
87             for key, value in self.params.items():
88                 if "private" in key or "public" in key:
89                     self.profile_data = self.params[key]
90                     self.get_streams(self.profile_data)
91                     self.full_profile.update({key: self.profile_data})
92             traffic = \
93                 self._get_ixia_traffic_profile(self.full_profile, mac, xfile)
94             self.max_rate = self.rate
95             self.min_rate = 0
96             self.get_multiplier()
97             self._ixia_traffic_generate(traffic_generator, traffic, ixia_obj)
98
99     def get_multiplier(self):
100         self.rate = round((self.max_rate + self.min_rate) / 2.0, 2)
101         multiplier = round(self.rate / self.pps, 2)
102         return str(multiplier)
103
104     def start_ixia_latency(self, traffic_generator, ixia_obj,
105                            mac={}, xfile=None):
106         traffic = self._get_ixia_traffic_profile(self.full_profile, mac)
107         self._ixia_traffic_generate(traffic_generator, traffic,
108                                     ixia_obj, xfile)
109
110     def get_drop_percentage(self, traffic_generator, samples, tol_min,
111                             tolerance, ixia_obj, mac={}, xfile=None):
112         status = 'Running'
113         drop_percent = 100
114         in_packets = sum([samples[iface]['in_packets'] for iface in samples])
115         out_packets = sum([samples[iface]['out_packets'] for iface in samples])
116         rx_throughput = \
117             sum([samples[iface]['RxThroughput'] for iface in samples])
118         tx_throughput = \
119             sum([samples[iface]['TxThroughput'] for iface in samples])
120         packet_drop = abs(out_packets - in_packets)
121         try:
122             drop_percent = round((packet_drop / float(out_packets)) * 100, 2)
123         except ZeroDivisionError:
124             LOG.info('No traffic is flowing')
125         samples['TxThroughput'] = round(tx_throughput / 1.0, 2)
126         samples['RxThroughput'] = round(rx_throughput / 1.0, 2)
127         samples['CurrentDropPercentage'] = drop_percent
128         samples['Throughput'] = self.tmp_throughput
129         samples['DropPercentage'] = self.tmp_drop
130         if drop_percent > tolerance and self.tmp_throughput == 0:
131             samples['Throughput'] = round(rx_throughput / 1.0, 2)
132             samples['DropPercentage'] = drop_percent
133         if self.first_run:
134             max_supported_rate = out_packets / 30.0
135             self.rate = max_supported_rate
136             self.first_run = False
137             if drop_percent <= tolerance:
138                 status = 'Completed'
139         if drop_percent > tolerance:
140             self.max_rate = self.rate
141         elif drop_percent < tol_min:
142             self.min_rate = self.rate
143             if drop_percent >= self.tmp_drop:
144                 self.tmp_drop = drop_percent
145                 self.tmp_throughput = round((rx_throughput / 1.0), 2)
146                 samples['Throughput'] = round(rx_throughput / 1.0, 2)
147                 samples['DropPercentage'] = drop_percent
148         else:
149             samples['Throughput'] = round(rx_throughput / 1.0, 2)
150             samples['DropPercentage'] = drop_percent
151             return status, samples
152         self.get_multiplier()
153         traffic = self._get_ixia_traffic_profile(self.full_profile, mac, xfile)
154         self._ixia_traffic_generate(traffic_generator, traffic, ixia_obj)
155         return status, samples