[NFVBENCH-86] In case of lossy loopback device, packet drops are reported incorrectly
[nfvbench.git] / nfvbench / network.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
16
17 class Interface(object):
18     """A class to hold the RX and TX counters for a virtual or physical interface."""
19
20     def __init__(self, name, device, tx_packets, rx_packets):
21         """Create a new interface instance."""
22         self.name = name
23         self.device = device
24         self.packets = {
25             'tx': tx_packets,
26             'rx': rx_packets
27         }
28
29     def set_packets(self, tx, rx):
30         """Set tx and rx counters for this interface."""
31         self.packets = {
32             'tx': tx,
33             'rx': rx
34         }
35
36     def set_packets_diff(self, tx, rx):
37         """Subtract current counters from new set of counters and update with results."""
38         self.packets = {
39             'tx': tx - self.packets['tx'],
40             'rx': rx - self.packets['rx'],
41         }
42
43     def is_no_op(self):
44         """Check if this interface is a no-opn interface."""
45         return self.name is None
46
47     def get_packet_count(self, traffic_type):
48         """Get packet count for given direction."""
49         return self.packets.get(traffic_type, 0)
50
51     @staticmethod
52     def no_op():
53         """Return an interface that doe snot pass any traffic."""
54         return Interface(None, None, 0, 0)
55
56
57 class Network(object):
58     """This class holds all interfaces that make up a logical neutron network.
59
60     A loopback packet path has exactly 2 networks.
61     The first interface is always one of the 2 traffic gen interface.
62     Subsequent interfaces are sorted along the path from the TG to the loopback point
63     which could be interfaces in a switch, a vswitch or a VM.
64     """
65
66     def __init__(self, interfaces=None, reverse=False):
67         """Create a network with initial interface list and direction.
68
69         :param interfaces: initial interface list
70         :param reverse: specifies the order of interfaces returned by get_interfaces
71         """
72         if interfaces is None:
73             interfaces = []
74         self.interfaces = interfaces
75         self.reverse = reverse
76
77     def add_interface(self, interface):
78         """Add one more interface to this network.
79
80         Order if important as interfaces must be added from traffic generator ports towards then
81         looping back device.
82         """
83         self.interfaces.append(interface)
84
85     def get_interfaces(self):
86         """Get interfaces associated to this network.
87
88         Returned interface list is ordered from traffic generator port towards looping device if
89         reverse is false. Else returms the list in the reverse order.
90         """
91         return self.interfaces[::-1] if self.reverse else self.interfaces