1 # Copyright (c) 2016-2017 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 from __future__ import absolute_import
20 from yardstick import ssh
21 from yardstick.network_services.utils import get_nsb_option
22 from yardstick.network_services.utils import provision_tool
23 from yardstick.benchmark.contexts.base import Context
24 from yardstick.benchmark.contexts.standalone import model
25 from yardstick.network_services.utils import PciAddress
27 LOG = logging.getLogger(__name__)
30 class SriovContext(Context):
31 """ This class handles SRIOV standalone nodes - VM running on Non-Managed NFVi
35 __context_type__ = "StandaloneSriov"
41 self.dpdk_devbind = ''
49 self.helper = model.StandaloneContextHelper()
50 self.vnf_node = model.Server()
52 super(SriovContext, self).__init__()
54 def init(self, attrs):
55 """initializes itself from the supplied arguments"""
56 super(SriovContext, self).init(attrs)
58 self.file_path = attrs.get("file", "pod.yaml")
60 self.nodes, self.nfvi_host, self.host_mgmt = \
61 self.helper.parse_pod_file(self.file_path, 'Sriov')
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", {})
70 LOG.debug("Nodes: %r", self.nodes)
71 LOG.debug("NFVi Node: %r", self.nfvi_host)
72 LOG.debug("Networks: %r", self.networks)
75 """don't need to deploy"""
77 # Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
78 if not self.vm_deploy:
81 self.connection = ssh.SSH.from_node(self.host_mgmt)
82 self.dpdk_devbind = provision_tool(
84 os.path.join(get_nsb_option("bin_path"), "dpdk-devbind.py"))
86 # Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
87 model.StandaloneContextHelper.install_req_libs(self.connection)
88 self.networks = model.StandaloneContextHelper.get_nic_details(
89 self.connection, self.networks, self.dpdk_devbind)
90 self.nodes = self.setup_sriov_context()
92 LOG.debug("Waiting for VM to come up...")
93 self.nodes = model.StandaloneContextHelper.wait_for_vnfs_to_start(
94 self.connection, self.servers, self.nodes)
97 """don't need to undeploy"""
99 if not self.vm_deploy:
102 # Todo: NFVi undeploy (sriov, vswitch, ovs etc) based on the config.
103 for vm in self.vm_names:
104 model.Libvirt.check_if_vm_exists_and_delete(vm, self.connection)
106 # Bind nics back to kernel
107 for ports in self.networks.values():
108 # enable VFs for given...
109 build_vfs = "echo 0 > /sys/bus/pci/devices/{0}/sriov_numvfs"
110 self.connection.execute(build_vfs.format(ports.get('phy_port')))
112 def _get_server(self, attr_name):
113 """lookup server info by name from context
116 attr_name -- A name for a server listed in nodes config file
118 node_name, name = self.split_name(attr_name)
119 if name is None or self.name != name:
122 matching_nodes = (n for n in self.nodes if n["name"] == node_name)
124 # A clone is created in order to avoid affecting the
126 node = dict(next(matching_nodes))
127 except StopIteration:
131 duplicate = next(matching_nodes)
132 except StopIteration:
135 raise ValueError("Duplicate nodes!!! Nodes: %s %s"
138 node["name"] = attr_name
141 def _get_network(self, attr_name):
142 if not isinstance(attr_name, collections.Mapping):
143 network = self.networks.get(attr_name)
146 # Don't generalize too much Just support vld_id
147 vld_id = attr_name.get('vld_id', {})
148 # for standalone context networks are dicts
149 iter1 = (n for n in self.networks.values() if n.get('vld_id') == vld_id)
150 network = next(iter1, None)
157 "name": network["name"],
158 "vld_id": network.get("vld_id"),
159 "segmentation_id": network.get("segmentation_id"),
160 "network_type": network.get("network_type"),
161 "physical_network": network.get("physical_network"),
165 def configure_nics_for_sriov(self):
166 vf_cmd = "ip link set {0} vf 0 mac {1}"
167 for ports in self.networks.values():
168 host_driver = ports.get('driver')
169 if host_driver not in self.drivers:
170 self.connection.execute("rmmod %svf" % host_driver)
171 self.drivers.append(host_driver)
173 # enable VFs for given...
174 build_vfs = "echo 1 > /sys/bus/pci/devices/{0}/sriov_numvfs"
175 self.connection.execute(build_vfs.format(ports.get('phy_port')))
178 mac = model.StandaloneContextHelper.get_mac_address()
179 interface = ports.get('interface')
180 if interface is not None:
181 self.connection.execute(vf_cmd.format(interface, mac))
183 vf_pci = self._get_vf_data(ports.get('phy_port'), mac, interface)
189 LOG.info('Ports %s', self.networks)
191 def _enable_interfaces(self, index, idx, vfs, cfg):
192 vf_spoofchk = "ip link set {0} vf 0 spoofchk off"
194 vf = self.networks[vfs[0]]
195 vpci = PciAddress(vf['vpci'].strip())
196 # Generate the vpci for the interfaces
197 slot = index + idx + 10
199 "{}:{}:{:02x}.{}".format(vpci.domain, vpci.bus, slot, vpci.function)
200 model.Libvirt.add_sriov_interfaces(
201 vf['vpci'], vf['vf_pci']['vf_pci'], vf['mac'], str(cfg))
202 self.connection.execute("ifconfig %s up" % vf['interface'])
203 self.connection.execute(vf_spoofchk.format(vf['interface']))
205 def setup_sriov_context(self):
208 # 1 : modprobe host_driver with num_vfs
209 self.configure_nics_for_sriov()
211 for index, (key, vnf) in enumerate(collections.OrderedDict(
212 self.servers).items()):
213 cfg = '/tmp/vm_sriov_%s.xml' % str(index)
214 vm_name = "vm_%s" % str(index)
216 # 1. Check and delete VM if already exists
217 model.Libvirt.check_if_vm_exists_and_delete(vm_name,
219 xml_str, mac = model.Libvirt.build_vm_xml(
220 self.connection, self.vm_flavor, vm_name, index)
222 # 2: Cleanup already available VMs
223 network_ports = collections.OrderedDict(
224 {k: v for k, v in vnf["network_ports"].items() if k != 'mgmt'})
225 for idx, vfs in enumerate(network_ports.values()):
226 self._enable_interfaces(index, idx, vfs, cfg)
228 # copy xml to target...
229 model.Libvirt.write_file(cfg, xml_str)
230 self.connection.put(cfg, cfg)
232 # NOTE: launch through libvirt
233 LOG.info("virsh create ...")
234 model.Libvirt.virsh_create_vm(self.connection, cfg)
236 self.vm_names.append(vm_name)
238 # build vnf node details
239 nodes.append(self.vnf_node.generate_vnf_instance(
240 self.vm_flavor, self.networks, self.host_mgmt.get('ip'),
245 def _get_vf_data(self, value, vfmac, pfif):
250 vfs = model.StandaloneContextHelper.get_virtual_devices(
251 self.connection, value)
252 for k, v in vfs.items():
253 m = PciAddress(k.strip())
254 m1 = PciAddress(value.strip())
256 vf_data.update({"vf_pci": str(v)})