test_spec: Add LTD.MemoryBandwidth.RFC2544.0PacketLoss.Scalability
[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         self._bridges = {}
43
44     def start(self):
45         """See IVswitch for general description
46
47         Activates DPDK kernel modules, ovsdb and vswitchd.
48         """
49         dpdk.init()
50         self._vswitchd.start()
51
52     def stop(self):
53         """See IVswitch for general description
54
55         Kills ovsdb and vswitchd and removes DPDK kernel modules.
56         """
57         self._vswitchd.kill()
58         dpdk.cleanup()
59
60     def add_switch(self, switch_name):
61         """See IVswitch for general description
62         """
63         bridge = OFBridge(switch_name)
64         bridge.create()
65         bridge.set_db_attribute('Open_vSwitch', '.',
66                                 'other_config:max-idle', '60000')
67         bridge.set_db_attribute('Bridge', bridge.br_name,
68                                 'datapath_type', 'netdev')
69         self._bridges[switch_name] = bridge
70
71     def del_switch(self, switch_name):
72         """See IVswitch for general description
73         """
74         bridge = self._bridges[switch_name]
75         self._bridges.pop(switch_name)
76         bridge.destroy()
77
78     def add_phy_port(self, switch_name):
79         """See IVswitch for general description
80
81         Creates a port of type dpdk.
82         The new port is named dpdk<n> where n is an integer starting from 0.
83         """
84         bridge = self._bridges[switch_name]
85         dpdk_count = self._get_port_count(bridge, 'type=dpdk')
86         port_name = 'dpdk' + str(dpdk_count)
87         params = ['--', 'set', 'Interface', port_name, 'type=dpdk']
88         of_port = bridge.add_port(port_name, params)
89
90         return (port_name, of_port)
91
92     def add_vport(self, switch_name):
93         """See IVswitch for general description
94
95         Creates a port of type dpdkvhost
96         The new port is named dpdkvhost<n> where n is an integer starting
97         from 0
98         """
99         bridge = self._bridges[switch_name]
100         vhost_count = self._get_port_count(bridge, 'type=dpdkvhost')
101         port_name = 'dpdkvhost' + str(vhost_count)
102         params = ['--', 'set', 'Interface', port_name, 'type=dpdkvhost']
103         of_port = bridge.add_port(port_name, params)
104
105         return (port_name, of_port)
106
107     def get_ports(self, switch_name):
108         """See IVswitch for general description
109         """
110         bridge = self._bridges[switch_name]
111         ports = list(bridge.get_ports().items())
112         return [(name, of_port) for (name, (of_port, _)) in ports]
113
114     def del_port(self, switch_name, port_name):
115         """See IVswitch for general description
116         """
117         bridge = self._bridges[switch_name]
118         bridge.del_port(port_name)
119
120     def add_flow(self, switch_name, flow):
121         """See IVswitch for general description
122         """
123         bridge = self._bridges[switch_name]
124         bridge.add_flow(flow)
125
126     def del_flow(self, switch_name, flow=None):
127         """See IVswitch for general description
128         """
129         flow = flow or {}
130         bridge = self._bridges[switch_name]
131         bridge.del_flow(flow)
132
133     @staticmethod
134     def _get_port_count(bridge, param):
135         """Returns the number of ports having a certain parameter
136
137         :param bridge: The src.ovs.ofctl.OFBridge on which to operate
138         :param param: The parameter to search for
139         :returns: Count of matches
140         """
141         port_params = [c for (_, (_, c)) in list(bridge.get_ports().items())]
142         param_hits = [i for i in port_params if param in i]
143         return len(param_hits)