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