568fae2feaf44fb0b6e8a561f273d62966cbf8c8
[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     # src_mac (6) + dst_mac (6) + mac_type (2) + frame_check (4) = 18
27     l2_header_size = 18
28
29     imix_l2_sizes = [64, 594, 1518]
30     imix_l3_sizes = [size - l2_header_size for size in imix_l2_sizes]
31     imix_ratios = [7, 4, 1]
32
33     imix_avg_l2_size = sum(
34         [1.0 * imix[0] * imix[1] for imix in zip(imix_l2_sizes, imix_ratios)]) / sum(imix_ratios)
35
36     traffic_utils.imix_avg_l2_sizes = imix_avg_l2_size
37
38     def __init__(self, config):
39         self.config = config
40
41     @abc.abstractmethod
42     def get_version(self):
43         # Must be implemented by sub classes
44         return None
45
46     @abc.abstractmethod
47     def init(self):
48         # Must be implemented by sub classes
49         return None
50
51     @abc.abstractmethod
52     def connect(self):
53         # Must be implemented by sub classes
54         return None
55
56     @abc.abstractmethod
57     def config_interface(self):
58         # Must be implemented by sub classes
59         return None
60
61     @abc.abstractmethod
62     def create_traffic(self):
63         # Must be implemented by sub classes
64         return None
65
66     def modify_rate(self, rate, reverse):
67         port_index = int(reverse)
68         port = self.port_handle[port_index]
69         self.rates[port_index] = traffic_utils.to_rate_str(rate)
70         LOG.info('Modified traffic stream for %s, new rate=%s.', port,
71                  traffic_utils.to_rate_str(rate))
72
73     def modify_traffic(self):
74         # Must be implemented by sub classes
75         return None
76
77     @abc.abstractmethod
78     def get_stats(self):
79         # Must be implemented by sub classes
80         return None
81
82     def clear_traffic(self):
83         # Must be implemented by sub classes
84         return None
85
86     @abc.abstractmethod
87     def start_traffic(self):
88         # Must be implemented by sub classes
89         return None
90
91     @abc.abstractmethod
92     def stop_traffic(self):
93         # Must be implemented by sub classes
94         return None
95
96     @abc.abstractmethod
97     def cleanup(self):
98         # Must be implemented by sub classes
99         return None