Adding cpu set to enable affinity for given vcpu
[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_nic_bind = ""
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_nic_bind = provision_tool(
87             self.connection,
88             os.path.join(get_nsb_option("bin_path"), "dpdk_nic_bind.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(self.connection,
93                                                                 self.networks,
94                                                                 self.dpdk_nic_bind)
95         self.nodes = self.setup_sriov_context()
96
97         LOG.debug("Waiting for VM to come up...")
98         self.nodes = StandaloneContextHelper.wait_for_vnfs_to_start(self.connection,
99                                                                     self.servers,
100                                                                     self.nodes)
101
102     def undeploy(self):
103         """don't need to undeploy"""
104
105         if not self.vm_deploy:
106             return
107
108         # Todo: NFVi undeploy (sriov, vswitch, ovs etc) based on the config.
109         for vm in self.vm_names:
110             Libvirt.check_if_vm_exists_and_delete(vm, self.connection)
111
112         # Bind nics back to kernel
113         for key, ports in self.networks.items():
114             # enable VFs for given...
115             build_vfs = "echo 0 > /sys/bus/pci/devices/{0}/sriov_numvfs"
116             self.connection.execute(build_vfs.format(ports.get('phy_port')))
117
118     def _get_server(self, attr_name):
119         """lookup server info by name from context
120
121         Keyword arguments:
122         attr_name -- A name for a server listed in nodes config file
123         """
124         node_name, name = self.split_name(attr_name)
125         if name is None or self.name != name:
126             return None
127
128         matching_nodes = (n for n in self.nodes if n["name"] == node_name)
129         try:
130             # A clone is created in order to avoid affecting the
131             # original one.
132             node = dict(next(matching_nodes))
133         except StopIteration:
134             return None
135
136         try:
137             duplicate = next(matching_nodes)
138         except StopIteration:
139             pass
140         else:
141             raise ValueError("Duplicate nodes!!! Nodes: %s %s",
142                              (node, duplicate))
143
144         node["name"] = attr_name
145         return node
146
147     def _get_network(self, attr_name):
148         if not isinstance(attr_name, collections.Mapping):
149             network = self.networks.get(attr_name)
150
151         else:
152             # Don't generalize too much  Just support vld_id
153             vld_id = attr_name.get('vld_id', {})
154             # for standalone context networks are dicts
155             iter1 = (n for n in self.networks.values() if n.get('vld_id') == vld_id)
156             network = next(iter1, None)
157
158         if network is None:
159             return None
160
161         result = {
162             # name is required
163             "name": network["name"],
164             "vld_id": network.get("vld_id"),
165             "segmentation_id": network.get("segmentation_id"),
166             "network_type": network.get("network_type"),
167             "physical_network": network.get("physical_network"),
168         }
169         return result
170
171     def configure_nics_for_sriov(self):
172         vf_cmd = "ip link set {0} vf 0 mac {1}"
173         for key, ports in self.networks.items():
174             vf_pci = []
175             host_driver = ports.get('driver')
176             if host_driver not in self.drivers:
177                 self.connection.execute("rmmod %svf" % host_driver)
178                 self.drivers.append(host_driver)
179
180             # enable VFs for given...
181             build_vfs = "echo 1 > /sys/bus/pci/devices/{0}/sriov_numvfs"
182             self.connection.execute(build_vfs.format(ports.get('phy_port')))
183
184             # configure VFs...
185             mac = StandaloneContextHelper.get_mac_address()
186             interface = ports.get('interface')
187             if interface is not None:
188                 self.connection.execute(vf_cmd.format(interface, mac))
189
190             vf_pci = self.get_vf_data('vf_pci', ports.get('phy_port'), mac, interface)
191             ports.update({
192                 'vf_pci': vf_pci,
193                 'mac': mac
194             })
195
196         LOG.info("Ports %s" % self.networks)
197
198     def _enable_interfaces(self, index, idx, vfs, cfg):
199         vf = self.networks[vfs[0]]
200         vpci = PciAddress.parse_address(vf['vpci'].strip(), multi_line=True)
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
209     def setup_sriov_context(self):
210         nodes = []
211
212         #   1 : modprobe host_driver with num_vfs
213         self.configure_nics_for_sriov()
214
215         for index, (key, vnf) in enumerate(OrderedDict(self.servers).items()):
216             cfg = '/tmp/vm_sriov_%s.xml' % str(index)
217             vm_name = "vm_%s" % str(index)
218
219             # 1. Check and delete VM if already exists
220             Libvirt.check_if_vm_exists_and_delete(vm_name, self.connection)
221
222             vcpu, mac = Libvirt.build_vm_xml(self.connection, self.vm_flavor, cfg, vm_name, index)
223             # 2: Cleanup already available VMs
224             for idx, (vkey, vfs) in enumerate(OrderedDict(vnf["network_ports"]).items()):
225                 if vkey == "mgmt":
226                     continue
227                 self._enable_interfaces(index, idx, vfs, cfg)
228
229             # copy xml to target...
230             self.connection.put(cfg, cfg)
231
232             #    FIXME: launch through libvirt
233             LOG.info("virsh create ...")
234             Libvirt.virsh_create_vm(self.connection, cfg)
235
236             self.vm_names.append(vm_name)
237
238             # build vnf node details
239             nodes.append(self.vnf_node.generate_vnf_instance(self.vm_flavor,
240                                                              self.networks,
241                                                              self.host_mgmt.get('ip'),
242                                                              key, vnf, mac))
243
244         return nodes
245
246     def get_vf_data(self, key, value, vfmac, pfif):
247         vf_data = {
248             "mac": vfmac,
249             "pf_if": pfif
250         }
251         vfs = StandaloneContextHelper.get_virtual_devices(self.connection, value)
252         for k, v in vfs.items():
253             m = PciAddress.parse_address(k.strip(), multi_line=True)
254             m1 = PciAddress.parse_address(value.strip(), multi_line=True)
255             if m.bus == m1.bus:
256                 vf_data.update({"vf_pci": str(v)})
257                 break
258
259         return vf_data