9c78d7e0eb167f9808c5f74e74b4dd8e2142b711
[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 import sys
17
18 from nfvbench.log import LOG
19 import traffic_utils
20
21
22 class Latency(object):
23     """A class to hold latency data."""
24
25     def __init__(self, latency_list=None):
26         """Create a latency instance.
27
28         latency_list: aggregate all latency values from list if not None
29         """
30         self.min_usec = sys.maxint
31         self.max_usec = 0
32         self.avg_usec = 0
33         self.hdrh = None
34         if latency_list:
35             for lat in latency_list:
36                 if lat.available():
37                     self.min_usec = min(self.min_usec, lat.min_usec)
38                     self.max_usec = max(self.max_usec, lat.max_usec)
39                     self.avg_usec += lat.avg_usec
40             # round to nearest usec
41             self.avg_usec = int(round(float(self.avg_usec) / len(latency_list)))
42
43     def available(self):
44         """Return True if latency information is available."""
45         return self.min_usec != sys.maxint
46
47
48 class TrafficGeneratorException(Exception):
49     """Exception for traffic generator."""
50
51     pass
52
53
54 class AbstractTrafficGenerator(object):
55
56     def __init__(self, traffic_client):
57         self.traffic_client = traffic_client
58         self.generator_config = traffic_client.generator_config
59         self.config = traffic_client.config
60
61     @abc.abstractmethod
62     def get_version(self):
63         # Must be implemented by sub classes
64         return None
65
66     @abc.abstractmethod
67     def connect(self):
68         # Must be implemented by sub classes
69         return None
70
71     @abc.abstractmethod
72     def create_traffic(self, l2frame_size, rates, bidirectional, latency=True, e2e=False):
73         # Must be implemented by sub classes
74         return None
75
76     def modify_rate(self, rate, reverse):
77         """Change the rate per port.
78
79         rate: new rate in % (0 to 100)
80         reverse: 0 for port 0, 1 for port 1
81         """
82         port_index = int(reverse)
83         port = self.port_handle[port_index]
84         self.rates[port_index] = traffic_utils.to_rate_str(rate)
85         LOG.info('Modified traffic stream for port %s, new rate=%s.', port, self.rates[port_index])
86
87     @abc.abstractmethod
88     def get_stats(self):
89         # Must be implemented by sub classes
90         return None
91
92     @abc.abstractmethod
93     def start_traffic(self):
94         # Must be implemented by sub classes
95         return None
96
97     @abc.abstractmethod
98     def stop_traffic(self):
99         # Must be implemented by sub classes
100         return None
101
102     @abc.abstractmethod
103     def cleanup(self):
104         """Cleanup the traffic generator."""
105         return None
106
107     def clear_streamblock(self):
108         """Clear all streams from the traffic generator."""
109         pass
110
111     @abc.abstractmethod
112     def resolve_arp(self):
113         """Resolve all configured remote IP addresses.
114
115         return: None if ARP failed to resolve for all IP addresses
116                 else a dict of list of dest macs indexed by port#
117                 the dest macs in the list are indexed by the chain id
118         """
119         pass
120
121     @abc.abstractmethod
122     def get_macs(self):
123         """Return the local port MAC addresses.
124
125         return: a list of MAC addresses indexed by the port#
126         """
127         pass
128
129     @abc.abstractmethod
130     def get_port_speed_gbps(self):
131         """Return the local port speeds.
132
133         return: a list of speed in Gbps indexed by the port#
134         """
135         pass