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