Merge "NSB: fix port topology"
[yardstick.git] / yardstick / network_services / traffic_profile / 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 """ RFC2544 Throughput implemenation """
15
16 from __future__ import absolute_import
17 from __future__ import division
18 import logging
19
20 from trex_stl_lib.trex_stl_client import STLStream
21 from trex_stl_lib.trex_stl_streams import STLFlowLatencyStats
22 from trex_stl_lib.trex_stl_streams import STLTXCont
23
24 from yardstick.network_services.traffic_profile.traffic_profile \
25     import TrexProfile
26
27 LOGGING = logging.getLogger(__name__)
28
29
30 class RFC2544Profile(TrexProfile):
31     """ This class handles rfc2544 implemenation. """
32
33     def __init__(self, traffic_generator):
34         super(RFC2544Profile, self).__init__(traffic_generator)
35         self.generator = None
36         self.max_rate = None
37         self.min_rate = None
38         self.ports = None
39         self.rate = 100
40         self.drop_percent_at_max_tx = None
41         self.throughput_max = None
42
43     def register_generator(self, generator):
44         self.generator = generator
45
46     def execute_traffic(self, traffic_generator=None):
47         """ Generate the stream and run traffic on the given ports """
48         if traffic_generator is not None and self.generator is None:
49             self.generator = traffic_generator
50
51         if self.ports is not None:
52             return
53
54         self.ports = []
55         for vld_id, intfs in sorted(self.generator.networks.items()):
56             profile_data = self.params.get(vld_id)
57             # no profile for this port
58             if not profile_data:
59                 continue
60             # correlated traffic doesn't use public traffic?
61             if vld_id.startswith("public") and self.generator.rfc2544_helper.correlated_traffic:
62                 continue
63             for intf in intfs:
64                 port = self.generator.vnfd_helper.port_num(intf)
65                 self.ports.append(port)
66                 self.generator.client.add_streams(self.get_streams(profile_data), ports=port)
67
68         self.max_rate = self.rate
69         self.min_rate = 0
70         self.generator.client.start(ports=self.ports, mult=self.get_multiplier(),
71                                     duration=30, force=True)
72         self.drop_percent_at_max_tx = 0
73         self.throughput_max = 0
74
75     def get_multiplier(self):
76         """ Get the rate at which next iteration to run """
77         self.rate = round((self.max_rate + self.min_rate) / 2.0, 2)
78         multiplier = round(self.rate / self.pps, 2)
79         return str(multiplier)
80
81     def get_drop_percentage(self, generator=None):
82         """ Calculate the drop percentage and run the traffic """
83         if generator is None:
84             generator = self.generator
85         run_duration = self.generator.RUN_DURATION
86         samples = self.generator.generate_samples(self.ports)
87
88         in_packets = sum([value['in_packets'] for value in samples.values()])
89         out_packets = sum([value['out_packets'] for value in samples.values()])
90
91         packet_drop = abs(out_packets - in_packets)
92         drop_percent = 100.0
93         try:
94             drop_percent = round((packet_drop / float(out_packets)) * 100, 5)
95         except ZeroDivisionError:
96             LOGGING.info('No traffic is flowing')
97
98         # TODO(esm): RFC2544 doesn't tolerate packet loss, why do we?
99         tolerance_low = generator.rfc2544_helper.tolerance_low
100         tolerance_high = generator.rfc2544_helper.tolerance_high
101
102         tx_rate = out_packets / run_duration
103         rx_rate = in_packets / run_duration
104
105         throughput_max = self.throughput_max
106         drop_percent_at_max_tx = self.drop_percent_at_max_tx
107
108         if self.drop_percent_at_max_tx is None:
109             self.rate = tx_rate
110             self.first_run = False
111
112         if drop_percent > tolerance_high:
113             # TODO(esm): why don't we discard results that are out of tolerance?
114             self.max_rate = self.rate
115             if throughput_max == 0:
116                 throughput_max = rx_rate
117                 drop_percent_at_max_tx = drop_percent
118
119         elif drop_percent >= tolerance_low:
120             # TODO(esm): why do we update the samples dict in this case
121             #            and not update our tracking values?
122             throughput_max = rx_rate
123             drop_percent_at_max_tx = drop_percent
124
125         elif drop_percent >= self.drop_percent_at_max_tx:
126             # TODO(esm): why don't we discard results that are out of tolerance?
127             self.min_rate = self.rate
128             self.drop_percent_at_max_tx = drop_percent_at_max_tx = drop_percent
129             self.throughput_max = throughput_max = rx_rate
130
131         else:
132             # TODO(esm): why don't we discard results that are out of tolerance?
133             self.min_rate = self.rate
134
135         generator.clear_client_stats(self.ports)
136         generator.start_client(self.ports, mult=self.get_multiplier(),
137                                duration=run_duration, force=True)
138
139         # if correlated traffic update the Throughput
140         if generator.rfc2544_helper.correlated_traffic:
141             throughput_max *= 2
142
143         samples.update({
144             'TxThroughput': tx_rate,
145             'RxThroughput': rx_rate,
146             'CurrentDropPercentage': drop_percent,
147             'Throughput': throughput_max,
148             'DropPercentage': drop_percent_at_max_tx,
149         })
150
151         return samples
152
153     def execute_latency(self, generator=None, samples=None):
154         if generator is None:
155             generator = self.generator
156
157         if samples is None:
158             samples = generator.generate_samples()
159
160         self.pps, multiplier = self.calculate_pps(samples)
161         self.ports = []
162         self.pg_id = self.params['traffic_profile'].get('pg_id', 1)
163         priv_ports = generator.priv_ports
164         pub_ports = generator.pub_ports
165         for index, (priv_port, pub_port) in enumerate(zip(priv_ports, pub_ports), 1):
166             profile_data = self.params.get('private_{}'.format(index), '')
167             self.ports.append(priv_port)
168             generator.client.add_streams(self.get_streams(profile_data),
169                                          ports=priv_port)
170
171             profile_data = self.params.get('public_{}'.format(index), '')
172             if not profile_data or generator.correlated_traffic:
173                 continue
174
175             pub_port = generator.pub_ports[index]
176             self.ports.append(pub_port)
177             generator.client.add_streams(self.get_streams(profile_data),
178                                          ports=pub_port)
179
180         generator.start_client(ports=self.ports, mult=str(multiplier),
181                                duration=120, force=True)
182         self.first_run = False
183
184     def calculate_pps(self, samples):
185         pps = round(samples['Throughput'] / 2, 2)
186         multiplier = round(self.rate / self.pps, 2)
187         return pps, multiplier
188
189     def create_single_stream(self, packet_size, pps, isg=0):
190         packet = self._create_single_packet(packet_size)
191         if pps:
192             stl_mode = STLTXCont(pps=pps)
193         else:
194             stl_mode = STLTXCont(pps=self.pps)
195         if self.pg_id:
196             LOGGING.debug("pg_id: %s", self.pg_id)
197             stl_flow_stats = STLFlowLatencyStats(pg_id=self.pg_id)
198             stream = STLStream(isg=isg, packet=packet, mode=stl_mode,
199                                flow_stats=stl_flow_stats)
200             self.pg_id += 1
201         else:
202             stream = STLStream(isg=isg, packet=packet, mode=stl_mode)
203         return stream