1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """QEMU Manager library."""
16 from collections import OrderedDict
18 from resources.libraries.python.Constants import Constants
19 from resources.libraries.python.CpuUtils import CpuUtils
20 from resources.libraries.python.QemuUtils import QemuUtils
21 from resources.libraries.python.topology import NodeType
23 __all__ = ["QemuManager"]
26 class QemuManager(object):
27 """QEMU lifecycle management class"""
29 # Use one instance of class per tests.
30 ROBOT_LIBRARY_SCOPE = 'TEST CASE'
32 def __init__(self, nodes):
33 """Init QemuManager object."""
35 self.machines_affinity = None
39 """Initialize QemuManager object."""
40 self.machines = OrderedDict()
41 self.machines_affinity = OrderedDict()
43 def construct_vms_on_node(self, **kwargs):
44 """Construct 1..Mx1..N VMs(s) on node with specified name.
46 :param kwargs: Named parameters.
50 nf_chains = int(kwargs['nf_chains'])
51 nf_nodes = int(kwargs['nf_nodes'])
52 queues = kwargs['rxq_count_int'] if kwargs['auto_scale'] else 1
53 vs_dtc = kwargs['vs_dtc']
54 nf_dtc = kwargs['vs_dtc'] if kwargs['auto_scale'] else kwargs['nf_dtc']
55 nf_dtcr = kwargs['nf_dtcr'] if isinstance(kwargs['nf_dtcr'], int) else 2
57 img = Constants.QEMU_VM_KERNEL
59 for nf_chain in range(1, nf_chains + 1):
60 for nf_node in range(1, nf_nodes + 1):
61 qemu_id = (nf_chain - 1) * nf_nodes + nf_node
62 name = '{node}_{qemu_id}'.format(node=node, qemu_id=qemu_id)
63 sock1 = '/var/run/vpp/sock-{qemu_id}-1'.format(qemu_id=qemu_id)
64 sock2 = '/var/run/vpp/sock-{qemu_id}-2'.format(qemu_id=qemu_id)
65 vif1_mac = kwargs['tg_if1_mac'] if nf_node == 1 \
66 else '52:54:00:00:{id:02x}:02'.format(id=qemu_id - 1)
67 vif2_mac = kwargs['tg_if2_mac'] if nf_node == nf_nodes \
68 else '52:54:00:00:{id:02x}:01'.format(id=qemu_id + 1)
70 self.machines_affinity[name] = CpuUtils.get_affinity_nf(
71 nodes=self.nodes, node=node, nf_chains=nf_chains,
72 nf_nodes=nf_nodes, nf_chain=nf_chain, nf_node=nf_node,
73 vs_dtc=vs_dtc, nf_dtc=nf_dtc, nf_dtcr=nf_dtcr)
75 self.machines[name] = QemuUtils(
76 node=self.nodes[node], qemu_id=qemu_id,
77 smp=len(self.machines_affinity[name]), mem=4096,
78 vnf=kwargs['vnf'], img=img)
79 self.machines[name].configure_kernelvm_vnf(
80 mac1='52:54:00:00:{id:02x}:01'.format(id=qemu_id),
81 mac2='52:54:00:00:{id:02x}:02'.format(id=qemu_id),
85 jumbo_frames=kwargs['jumbo'])
86 self.machines[name].qemu_add_vhost_user_if(
87 sock1, jumbo_frames=kwargs['jumbo'], queues=queues,
88 queue_size=kwargs['perf_qemu_qsz'])
89 self.machines[name].qemu_add_vhost_user_if(
90 sock2, jumbo_frames=kwargs['jumbo'], queues=queues,
91 queue_size=kwargs['perf_qemu_qsz'])
93 def construct_vms_on_all_nodes(self, **kwargs):
94 """Construct 1..Mx1..N VMs(s) with specified name on all nodes.
96 :param kwargs: Named parameters.
100 for node in self.nodes:
101 if self.nodes[node]['type'] == NodeType.DUT:
102 self.construct_vms_on_node(node=node, **kwargs)
104 def start_all_vms(self, pinning=False):
105 """Start all added VMs in manager.
107 :param pinning: If True, then do also QEMU process pinning.
110 for machine, machine_affinity in zip(self.machines.values(),
111 self.machines_affinity.values()):
114 machine.qemu_set_affinity(*machine_affinity)
116 def kill_all_vms(self, force=False):
117 """Kill all added VMs in manager.
119 :param force: Force kill all Qemu instances by pkill qemu if True.
122 for machine in self.machines.values():
124 machine.qemu_kill_all()