Merge "ixia: VLAN support without l3/l4 headers"
[vswitchperf.git] / core / pktfwd_controller.py
1 # Copyright 2016 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain 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,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """Packet forwarder controller for Physical to Physical deployment
16 """
17
18 import logging
19 from conf import settings
20
21 class PktFwdController(object):
22     """Packet forwarder controller for P2P deployment scenario.
23
24     Attributes:
25         _pktfwd_class: The packet forwarder class to be used.
26         _pktfwd: The packet forwarder object controlled by this controller
27     """
28     def __init__(self, deployment, pktfwd_class):
29         """Initializes up the prerequisites for the P2P deployment scenario.
30
31         :vswitch_class: the vSwitch class to be used.
32         """
33         self._deployment = deployment
34         self._logger = logging.getLogger(__name__)
35         self._pktfwd_class = pktfwd_class
36         self._pktfwd = pktfwd_class(guest=True if deployment == "pvp" and
37                                     settings.getValue('VNF') != "QemuPciPassthrough" else False)
38         self._logger.debug('Creation using ' + str(self._pktfwd_class))
39
40     def setup(self):
41         """Sets up the packet forwarder for p2p.
42         """
43         self._logger.debug('Setup using ' + str(self._pktfwd_class))
44
45         try:
46             self._pktfwd.start()
47         except:
48             self._pktfwd.stop()
49             raise
50
51     def get_vswitch(self):
52         """See IVswitchController for description
53         """
54         return self._pktfwd
55
56     def setup_for_guest(self):
57         """Sets up the packet forwarder for pvp.
58         """
59         self._logger.debug('Setup using ' + str(self._pktfwd_class))
60
61         try:
62             self._pktfwd.start_for_guest()
63         except:
64             self._pktfwd.stop()
65             raise
66
67     def stop(self):
68         """Tears down the packet forwarder created in setup().
69         """
70         self._logger.debug('Stop using ' + str(self._pktfwd_class))
71         self._pktfwd.stop()
72
73     def __enter__(self):
74         if self._deployment.find("p2p") == 0:
75             self.setup()
76         elif self._deployment == "pvp" and settings.getValue('VNF') != "QemuPciPassthrough":
77             self.setup_for_guest()
78
79     def __exit__(self, type_, value, traceback):
80         if self._deployment.find("p2p") == 0:
81             self.stop()
82         elif self._deployment == "pvp" and settings.getValue('VNF') != "QemuPciPassthrough":
83             self.stop()
84
85     def get_pktfwd(self):
86         """Get the controlled packet forwarder
87
88         :return: The controlled IPktFwd
89         """
90         return self._pktfwd
91
92     def dump_vswitch_flows(self):
93         """ Dumps flows from vswitch
94         """
95         raise NotImplementedError(
96             "The PktFwdController does not implement the "
97             "\"dump_vswitch_flows\" function.")