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
19 from collections import OrderedDict
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
30 LOG = logging.getLogger(__name__)
33 class SriovContext(Context):
34 """ This class handles SRIOV standalone nodes - VM running on Non-Managed NFVi
38 __context_type__ = "StandaloneSriov"
44 self.dpdk_nic_bind = ""
53 self.helper = StandaloneContextHelper()
54 self.vnf_node = Server()
56 super(SriovContext, self).__init__()
58 def init(self, attrs):
59 """initializes itself from the supplied arguments"""
61 self.name = attrs["name"]
62 self.file_path = attrs.get("file", "pod.yaml")
64 self.nodes, self.nfvi_host, self.host_mgmt = \
65 self.helper.parse_pod_file(self.file_path, 'Sriov')
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", {})
74 LOG.debug("Nodes: %r", self.nodes)
75 LOG.debug("NFVi Node: %r", self.nfvi_host)
76 LOG.debug("Networks: %r", self.networks)
79 """don't need to deploy"""
81 # Todo: NFVi deploy (sriov, vswitch, ovs etc) based on the config.
82 if not self.vm_deploy:
85 self.connection = ssh.SSH.from_node(self.host_mgmt)
86 self.dpdk_nic_bind = provision_tool(
88 os.path.join(get_nsb_option("bin_path"), "dpdk_nic_bind.py"))
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,
95 self.nodes = self.setup_sriov_context()
97 LOG.debug("Waiting for VM to come up...")
98 self.nodes = StandaloneContextHelper.wait_for_vnfs_to_start(self.connection,
103 """don't need to undeploy"""
105 if not self.vm_deploy:
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)
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')))
118 def _get_server(self, attr_name):
119 """lookup server info by name from context
122 attr_name -- A name for a server listed in nodes config file
124 node_name, name = self.split_name(attr_name)
125 if name is None or self.name != name:
128 matching_nodes = (n for n in self.nodes if n["name"] == node_name)
130 # A clone is created in order to avoid affecting the
132 node = dict(next(matching_nodes))
133 except StopIteration:
137 duplicate = next(matching_nodes)
138 except StopIteration:
141 raise ValueError("Duplicate nodes!!! Nodes: %s %s",
144 node["name"] = attr_name
147 def _get_network(self, attr_name):
148 if not isinstance(attr_name, collections.Mapping):
149 network = self.networks.get(attr_name)
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)
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"),
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():
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)
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')))
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))
190 vf_pci = self.get_vf_data('vf_pci', ports.get('phy_port'), mac, interface)
196 LOG.info("Ports %s" % self.networks)
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
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'])
209 def setup_sriov_context(self):
212 # 1 : modprobe host_driver with num_vfs
213 self.configure_nics_for_sriov()
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)
219 # 1. Check and delete VM if already exists
220 Libvirt.check_if_vm_exists_and_delete(vm_name, self.connection)
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()):
227 self._enable_interfaces(index, idx, vfs, cfg)
229 # copy xml to target...
230 self.connection.put(cfg, cfg)
232 # FIXME: launch through libvirt
233 LOG.info("virsh create ...")
234 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(self.vm_flavor,
241 self.host_mgmt.get('ip'),
246 def get_vf_data(self, key, value, vfmac, pfif):
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)
256 vf_data.update({"vf_pci": str(v)})