vswitches: Affinitize vswitch threads for OVS-DPDK
[vswitchperf.git] / vswitches / ovs_dpdk_vhost.py
1 # Copyright 2015 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 """VSPERF VSwitch implementation using DPDK and vhost ports
16 """
17
18 from conf import settings
19 from vswitches.vswitch import IVSwitch
20 from src.ovs import VSwitchd, OFBridge
21 from src.dpdk import dpdk
22
23 _VSWITCHD_CONST_ARGS = ['--', '--log-file']
24
25 class OvsDpdkVhost(IVSwitch):
26     """VSwitch implementation using DPDK and vhost ports
27
28     Generic OVS wrapper functionality in src.ovs is maximally used. This
29     class wraps DPDK system configuration along with DPDK specific OVS
30     parameters
31
32     The method docstrings document only considerations specific to this
33     implementation. For generic information of the nature of the methods,
34     see the interface.
35     """
36     def __init__(self):
37         vswitchd_args = ['--dpdk']
38         vswitchd_args += settings.getValue('VSWITCHD_DPDK_ARGS')
39         vswitchd_args += _VSWITCHD_CONST_ARGS
40
41         self._vswitchd = VSwitchd(vswitchd_args=vswitchd_args,
42                                   expected_cmd=
43                                   r'EAL: Master l*core \d+ is ready')
44         self._bridges = {}
45
46     def start(self):
47         """See IVswitch for general description
48
49         Activates DPDK kernel modules, ovsdb and vswitchd.
50         """
51         dpdk.init()
52         self._vswitchd.start()
53
54     def stop(self):
55         """See IVswitch for general description
56
57         Kills ovsdb and vswitchd and removes DPDK kernel modules.
58         """
59         self._vswitchd.kill()
60         dpdk.cleanup()
61
62     def add_switch(self, switch_name):
63         """See IVswitch for general description
64         """
65         bridge = OFBridge(switch_name)
66         bridge.create()
67         bridge.set_db_attribute('Open_vSwitch', '.',
68                                 'other_config:max-idle',
69                                 settings.getValue('VSWITCH_FLOW_TIMEOUT'))
70
71         if settings.getValue('VSWITCH_AFFINITIZATION_ON') == 1:
72             # Sets the PMD core mask to VSWITCH_PMD_CPU_MASK
73             # for CPU core affinitization
74             bridge.set_db_attribute('Open_vSwitch', '.',
75                                     'other_config:pmd-cpu-mask',
76                                     settings.getValue('VSWITCH_PMD_CPU_MASK'))
77
78         bridge.set_db_attribute('Bridge', bridge.br_name,
79                                 'datapath_type', 'netdev')
80         self._bridges[switch_name] = bridge
81
82     def del_switch(self, switch_name):
83         """See IVswitch for general description
84         """
85         bridge = self._bridges[switch_name]
86         self._bridges.pop(switch_name)
87         bridge.destroy()
88
89     def add_phy_port(self, switch_name):
90         """See IVswitch for general description
91
92         Creates a port of type dpdk.
93         The new port is named dpdk<n> where n is an integer starting from 0.
94         """
95         bridge = self._bridges[switch_name]
96         dpdk_count = self._get_port_count(bridge, 'type=dpdk')
97         port_name = 'dpdk' + str(dpdk_count)
98         params = ['--', 'set', 'Interface', port_name, 'type=dpdk']
99         of_port = bridge.add_port(port_name, params)
100
101         return (port_name, of_port)
102
103     def add_vport(self, switch_name):
104         """See IVswitch for general description
105
106         Creates a port of type dpdkvhost
107         The new port is named dpdkvhost<n> where n is an integer starting
108         from 0
109         """
110         bridge = self._bridges[switch_name]
111         # Changed dpdkvhost to dpdkvhostuser to be able to run in Qemu 2.2
112         vhost_count = self._get_port_count(bridge, 'type=dpdkvhostuser')
113         port_name = 'dpdkvhostuser' + str(vhost_count)
114         params = ['--', 'set', 'Interface', port_name, 'type=dpdkvhostuser']
115         of_port = bridge.add_port(port_name, params)
116
117         return (port_name, of_port)
118
119     def get_ports(self, switch_name):
120         """See IVswitch for general description
121         """
122         bridge = self._bridges[switch_name]
123         ports = list(bridge.get_ports().items())
124         return [(name, of_port) for (name, (of_port, _)) in ports]
125
126     def del_port(self, switch_name, port_name):
127         """See IVswitch for general description
128         """
129         bridge = self._bridges[switch_name]
130         bridge.del_port(port_name)
131
132     def add_flow(self, switch_name, flow):
133         """See IVswitch for general description
134         """
135         bridge = self._bridges[switch_name]
136         bridge.add_flow(flow)
137
138     def del_flow(self, switch_name, flow=None):
139         """See IVswitch for general description
140         """
141         flow = flow or {}
142         bridge = self._bridges[switch_name]
143         bridge.del_flow(flow)
144
145     @staticmethod
146     def _get_port_count(bridge, param):
147         """Returns the number of ports having a certain parameter
148
149         :param bridge: The src.ovs.ofctl.OFBridge on which to operate
150         :param param: The parameter to search for
151         :returns: Count of matches
152         """
153         port_params = [c for (_, (_, c)) in list(bridge.get_ports().items())]
154         param_hits = [i for i in port_params if param in i]
155         return len(param_hits)