81537b32721000a0a43a1a124335286fa227a683
[nfvbench.git] / nfvbench / traffic_gen / traffic_base.py
1 # Copyright 2016 Cisco Systems, Inc.  All rights reserved.
2 #
3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #    not use this file except in compliance with the License. You may obtain
5 #    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, WITHOUT
11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #    License for the specific language governing permissions and limitations
13 #    under the License.
14
15 import abc
16
17 from nfvbench.log import LOG
18 import traffic_utils
19
20
21 class TrafficGeneratorException(Exception):
22     pass
23
24
25 class AbstractTrafficGenerator(object):
26     def __init__(self, config):
27         self.config = config
28         self.imix_l2_sizes = [64, 594, 1518]
29         self.imix_ratios = [7, 4, 1]
30         self.imix_avg_l2_size = 0
31         self.adjust_imix_min_size(64)
32
33     @abc.abstractmethod
34     def get_version(self):
35         # Must be implemented by sub classes
36         return None
37
38     @abc.abstractmethod
39     def init(self):
40         # Must be implemented by sub classes
41         return None
42
43     @abc.abstractmethod
44     def connect(self):
45         # Must be implemented by sub classes
46         return None
47
48     @abc.abstractmethod
49     def config_interface(self):
50         # Must be implemented by sub classes
51         return None
52
53     @abc.abstractmethod
54     def create_traffic(self, l2frame_size, rates, bidirectional, latency=True):
55         # Must be implemented by sub classes
56         return None
57
58     def modify_rate(self, rate, reverse):
59         port_index = int(reverse)
60         port = self.port_handle[port_index]
61         self.rates[port_index] = traffic_utils.to_rate_str(rate)
62         LOG.info('Modified traffic stream for %s, new rate=%s.', port,
63                  traffic_utils.to_rate_str(rate))
64
65     def modify_traffic(self):
66         # Must be implemented by sub classes
67         return None
68
69     @abc.abstractmethod
70     def get_stats(self):
71         # Must be implemented by sub classes
72         return None
73
74     def clear_traffic(self):
75         # Must be implemented by sub classes
76         return None
77
78     @abc.abstractmethod
79     def start_traffic(self):
80         # Must be implemented by sub classes
81         return None
82
83     @abc.abstractmethod
84     def stop_traffic(self):
85         # Must be implemented by sub classes
86         return None
87
88     @abc.abstractmethod
89     def cleanup(self):
90         # Must be implemented by sub classes
91         return None
92
93     def adjust_imix_min_size(self, min_size):
94         # assume the min size is always the first entry
95         self.imix_l2_sizes[0] = min_size
96         self.imix_avg_l2_size = sum(
97             [1.0 * imix[0] * imix[1] for imix in zip(self.imix_l2_sizes, self.imix_ratios)]) / sum(
98                 self.imix_ratios)
99         traffic_utils.imix_avg_l2_size = self.imix_avg_l2_size