Merge "Resolve NameError in test_utils.py"
[yardstick.git] / yardstick / benchmark / contexts / standalone / sriov.py
1 # Copyright (c) 2016-2017 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 from __future__ import absolute_import
16 import os
17 import logging
18 import collections
19
20 from yardstick import ssh
21 from yardstick.network_services.utils import get_nsb_option
22 from yardstick.benchmark.contexts.base import Context
23 from yardstick.benchmark.contexts.standalone import model
24 from yardstick.network_services.utils import PciAddress
25
26 LOG = logging.getLogger(__name__)
27
28
29 class SriovContext(Context):
30     """ This class handles SRIOV standalone nodes - VM running on Non-Managed NFVi
31     Configuration: sr-iov
32     """
33
34     __context_type__ = "StandaloneSriov"
35
36     def __init__(self):
37         self.file_path = None
38         self.sriov = []
39         self.first_run = True
40         self.dpdk_devbind = os.path.join(get_nsb_option('bin_path'),
41                                          'dpdk-devbind.py')
42         self.vm_names = []
43         self.nfvi_host = []
44         self.nodes = []
45         self.networks = {}
46         self.attrs = {}
47         self.vm_flavor = None
48         self.servers = None
49         self.helper = model.StandaloneContextHelper()
50         self.vnf_node = model.Server()
51         self.drivers = []
52         super(SriovContext, self).__init__()
53
54     def init(self, attrs):
55         """initializes itself from the supplied arguments"""
56         super(SriovContext, self).init(attrs)
57
58         self.file_path = attrs.get("file", "pod.yaml")
59
60         self.nodes, self.nfvi_host, self.host_mgmt = \
61             self.helper.parse_pod_file(self.file_path, 'Sriov')
62
63         self.attrs = attrs
64         self.vm_flavor = attrs.get('flavor', {})
65         self.servers = attrs.get('servers', {})
66         self.vm_deploy = attrs.get("vm_deploy", True)
67         # add optional static network definition
68         self.networks = attrs.get("networks", {})
69
70         LOG.debug("Nodes: %r", self.nodes)
71         LOG.debug("NFVi Node: %r", self.nfvi_host)
72         LOG.debug("Networks: %r", self.networks)
73
74     def deploy(self):
75         """don't need to deploy"""
76
77         # Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
78         if not self.vm_deploy:
79             return
80
81         self.connection = ssh.SSH.from_node(self.host_mgmt)
82
83         #    Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
84         model.StandaloneContextHelper.install_req_libs(self.connection)
85         self.networks = model.StandaloneContextHelper.get_nic_details(
86             self.connection, self.networks, self.dpdk_devbind)
87         self.nodes = self.setup_sriov_context()
88
89         LOG.debug("Waiting for VM to come up...")
90         self.nodes = model.StandaloneContextHelper.wait_for_vnfs_to_start(
91             self.connection, self.servers, self.nodes)
92
93     def undeploy(self):
94         """don't need to undeploy"""
95
96         if not self.vm_deploy:
97             return
98
99         # Todo: NFVi undeploy (sriov, vswitch, ovs etc) based on the config.
100         for vm in self.vm_names:
101             model.Libvirt.check_if_vm_exists_and_delete(vm, self.connection)
102
103         # Bind nics back to kernel
104         for ports in self.networks.values():
105             # enable VFs for given...
106             build_vfs = "echo 0 > /sys/bus/pci/devices/{0}/sriov_numvfs"
107             self.connection.execute(build_vfs.format(ports.get('phy_port')))
108
109     def _get_physical_nodes(self):
110         return self.nfvi_host
111
112     def _get_physical_node_for_server(self, server_name):
113
114         # self.nfvi_host always contain only one host.
115         node_name, ctx_name = self.split_host_name(server_name)
116         if ctx_name is None or self.name != ctx_name:
117             return None
118
119         matching_nodes = [s for s in self.servers if s == node_name]
120         if len(matching_nodes) == 0:
121             return None
122
123         return "{}.{}".format(self.nfvi_host[0]["name"], self._name)
124
125     def _get_server(self, attr_name):
126         """lookup server info by name from context
127
128         Keyword arguments:
129         attr_name -- A name for a server listed in nodes config file
130         """
131         node_name, name = self.split_host_name(attr_name)
132         if name is None or self.name != name:
133             return None
134
135         matching_nodes = (n for n in self.nodes if n["name"] == node_name)
136         try:
137             # A clone is created in order to avoid affecting the
138             # original one.
139             node = dict(next(matching_nodes))
140         except StopIteration:
141             return None
142
143         try:
144             duplicate = next(matching_nodes)
145         except StopIteration:
146             pass
147         else:
148             raise ValueError("Duplicate nodes!!! Nodes: %s %s"
149                              % (node, duplicate))
150
151         node["name"] = attr_name
152         return node
153
154     def _get_network(self, attr_name):
155         if not isinstance(attr_name, collections.Mapping):
156             network = self.networks.get(attr_name)
157
158         else:
159             # Don't generalize too much  Just support vld_id
160             vld_id = attr_name.get('vld_id', {})
161             # for standalone context networks are dicts
162             iter1 = (n for n in self.networks.values() if n.get('vld_id') == vld_id)
163             network = next(iter1, None)
164
165         if network is None:
166             return None
167
168         result = {
169             # name is required
170             "name": network["name"],
171             "vld_id": network.get("vld_id"),
172             "segmentation_id": network.get("segmentation_id"),
173             "network_type": network.get("network_type"),
174             "physical_network": network.get("physical_network"),
175         }
176         return result
177
178     def configure_nics_for_sriov(self):
179         vf_cmd = "ip link set {0} vf 0 mac {1}"
180         for ports in self.networks.values():
181             host_driver = ports.get('driver')
182             if host_driver not in self.drivers:
183                 self.connection.execute("rmmod %svf" % host_driver)
184                 self.drivers.append(host_driver)
185
186             # enable VFs for given...
187             build_vfs = "echo 1 > /sys/bus/pci/devices/{0}/sriov_numvfs"
188             self.connection.execute(build_vfs.format(ports.get('phy_port')))
189
190             # configure VFs...
191             mac = model.StandaloneContextHelper.get_mac_address()
192             interface = ports.get('interface')
193             if interface is not None:
194                 self.connection.execute(vf_cmd.format(interface, mac))
195
196             vf_pci = self._get_vf_data(ports.get('phy_port'), mac, interface)
197             ports.update({
198                 'vf_pci': vf_pci,
199                 'mac': mac
200             })
201
202         LOG.info('Ports %s', self.networks)
203
204     def _enable_interfaces(self, index, idx, vfs, cfg):
205         vf_spoofchk = "ip link set {0} vf 0 spoofchk off"
206
207         vf = self.networks[vfs[0]]
208         vpci = PciAddress(vf['vpci'].strip())
209         # Generate the vpci for the interfaces
210         slot = index + idx + 10
211         vf['vpci'] = \
212             "{}:{}:{:02x}.{}".format(vpci.domain, vpci.bus, slot, vpci.function)
213         self.connection.execute("ifconfig %s up" % vf['interface'])
214         self.connection.execute(vf_spoofchk.format(vf['interface']))
215         return model.Libvirt.add_sriov_interfaces(
216             vf['vpci'], vf['vf_pci']['vf_pci'], vf['mac'], str(cfg))
217
218     def setup_sriov_context(self):
219         nodes = []
220
221         #   1 : modprobe host_driver with num_vfs
222         self.configure_nics_for_sriov()
223
224         for index, (key, vnf) in enumerate(collections.OrderedDict(
225                 self.servers).items()):
226             cfg = '/tmp/vm_sriov_%s.xml' % str(index)
227             vm_name = "vm_%s" % str(index)
228
229             # 1. Check and delete VM if already exists
230             model.Libvirt.check_if_vm_exists_and_delete(vm_name,
231                                                         self.connection)
232             xml_str, mac = model.Libvirt.build_vm_xml(
233                 self.connection, self.vm_flavor, vm_name, index)
234
235             # 2: Cleanup already available VMs
236             network_ports = collections.OrderedDict(
237                 {k: v for k, v in vnf["network_ports"].items() if k != 'mgmt'})
238             for idx, vfs in enumerate(network_ports.values()):
239                 xml_str = self._enable_interfaces(index, idx, vfs, xml_str)
240
241             # copy xml to target...
242             model.Libvirt.write_file(cfg, xml_str)
243             self.connection.put(cfg, cfg)
244
245             # NOTE: launch through libvirt
246             LOG.info("virsh create ...")
247             model.Libvirt.virsh_create_vm(self.connection, cfg)
248
249             self.vm_names.append(vm_name)
250
251             # build vnf node details
252             nodes.append(self.vnf_node.generate_vnf_instance(
253                 self.vm_flavor, self.networks, self.host_mgmt.get('ip'),
254                 key, vnf, mac))
255
256         return nodes
257
258     def _get_vf_data(self, value, vfmac, pfif):
259         vf_data = {
260             "mac": vfmac,
261             "pf_if": pfif
262         }
263         vfs = model.StandaloneContextHelper.get_virtual_devices(
264             self.connection, value)
265         for k, v in vfs.items():
266             m = PciAddress(k.strip())
267             m1 = PciAddress(value.strip())
268             if m.bus == m1.bus:
269                 vf_data.update({"vf_pci": str(v)})
270                 break
271
272         return vf_data