1 # Copyright 2015 Intel Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 """VSPERF VSwitch implementation using DPDK and vhost ports
18 from conf import settings
19 from vswitches.vswitch import IVSwitch
20 from src.ovs import VSwitchd, OFBridge
21 from src.dpdk import dpdk
23 _VSWITCHD_CONST_ARGS = ['--', '--log-file']
25 class OvsDpdkVhost(IVSwitch):
26 """VSwitch implementation using DPDK and vhost ports
28 Generic OVS wrapper functionality in src.ovs is maximally used. This
29 class wraps DPDK system configuration along with DPDK specific OVS
32 The method docstrings document only considerations specific to this
33 implementation. For generic information of the nature of the methods,
37 vswitchd_args = ['--dpdk']
38 vswitchd_args += settings.getValue('VSWITCHD_DPDK_ARGS')
39 vswitchd_args += _VSWITCHD_CONST_ARGS
41 self._vswitchd = VSwitchd(vswitchd_args=vswitchd_args,
43 r'EAL: Master l*core \d+ is ready')
47 """See IVswitch for general description
49 Activates DPDK kernel modules, ovsdb and vswitchd.
52 self._vswitchd.start()
55 """See IVswitch for general description
57 Kills ovsdb and vswitchd and removes DPDK kernel modules.
62 def add_switch(self, switch_name):
63 """See IVswitch for general description
65 bridge = OFBridge(switch_name)
67 bridge.set_db_attribute('Open_vSwitch', '.',
68 'other_config:max-idle', '60000')
69 bridge.set_db_attribute('Bridge', bridge.br_name,
70 'datapath_type', 'netdev')
71 self._bridges[switch_name] = bridge
73 def del_switch(self, switch_name):
74 """See IVswitch for general description
76 bridge = self._bridges[switch_name]
77 self._bridges.pop(switch_name)
80 def add_phy_port(self, switch_name):
81 """See IVswitch for general description
83 Creates a port of type dpdk.
84 The new port is named dpdk<n> where n is an integer starting from 0.
86 bridge = self._bridges[switch_name]
87 dpdk_count = self._get_port_count(bridge, 'type=dpdk')
88 port_name = 'dpdk' + str(dpdk_count)
89 params = ['--', 'set', 'Interface', port_name, 'type=dpdk']
90 of_port = bridge.add_port(port_name, params)
92 return (port_name, of_port)
94 def add_vport(self, switch_name):
95 """See IVswitch for general description
97 Creates a port of type dpdkvhost
98 The new port is named dpdkvhost<n> where n is an integer starting
101 bridge = self._bridges[switch_name]
102 # Changed dpdkvhost to dpdkvhostuser to be able to run in Qemu 2.2
103 vhost_count = self._get_port_count(bridge, 'type=dpdkvhostuser')
104 port_name = 'dpdkvhostuser' + str(vhost_count)
105 params = ['--', 'set', 'Interface', port_name, 'type=dpdkvhostuser']
106 of_port = bridge.add_port(port_name, params)
108 return (port_name, of_port)
110 def get_ports(self, switch_name):
111 """See IVswitch for general description
113 bridge = self._bridges[switch_name]
114 ports = list(bridge.get_ports().items())
115 return [(name, of_port) for (name, (of_port, _)) in ports]
117 def del_port(self, switch_name, port_name):
118 """See IVswitch for general description
120 bridge = self._bridges[switch_name]
121 bridge.del_port(port_name)
123 def add_flow(self, switch_name, flow):
124 """See IVswitch for general description
126 bridge = self._bridges[switch_name]
127 bridge.add_flow(flow)
129 def del_flow(self, switch_name, flow=None):
130 """See IVswitch for general description
133 bridge = self._bridges[switch_name]
134 bridge.del_flow(flow)
137 def _get_port_count(bridge, param):
138 """Returns the number of ports having a certain parameter
140 :param bridge: The src.ovs.ofctl.OFBridge on which to operate
141 :param param: The parameter to search for
142 :returns: Count of matches
144 port_params = [c for (_, (_, c)) in list(bridge.get_ports().items())]
145 param_hits = [i for i in port_params if param in i]
146 return len(param_hits)