Update of VPP_STABLE_VER files + quick fix for gre create tunnel
[csit.git] / resources / libraries / python / QemuManager.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """QEMU Manager library."""
15
16 from collections import OrderedDict
17
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
22
23 __all__ = ["QemuManager"]
24
25
26 class QemuManager(object):
27     """QEMU lifecycle management class"""
28
29     # Use one instance of class per tests.
30     ROBOT_LIBRARY_SCOPE = 'TEST CASE'
31
32     def __init__(self, nodes):
33         """Init QemuManager object."""
34         self.machines = None
35         self.machines_affinity = None
36         self.nodes = nodes
37
38     def initialize(self):
39         """Initialize QemuManager object."""
40         self.machines = OrderedDict()
41         self.machines_affinity = OrderedDict()
42
43     def construct_vms_on_node(self, **kwargs):
44         """Construct 1..Mx1..N VMs(s) on node with specified name.
45
46         :param kwargs: Named parameters.
47         :type kwargs: dict
48         """
49         node = kwargs['node']
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
56
57         img = Constants.QEMU_VM_KERNEL
58
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)
69
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)
74
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),
82                     vif1_mac=vif1_mac,
83                     vif2_mac=vif2_mac,
84                     queues=queues,
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'])
92
93     def construct_vms_on_all_nodes(self, **kwargs):
94         """Construct 1..Mx1..N VMs(s) with specified name on all nodes.
95
96         :param kwargs: Named parameters.
97         :type kwargs: dict
98         """
99         self.initialize()
100         for node in self.nodes:
101             if self.nodes[node]['type'] == NodeType.DUT:
102                 self.construct_vms_on_node(node=node, **kwargs)
103
104     def start_all_vms(self, pinning=False):
105         """Start all added VMs in manager.
106
107         :param pinning: If True, then do also QEMU process pinning.
108         :type pinning: bool
109         """
110         for machine, machine_affinity in zip(self.machines.values(),
111                                              self.machines_affinity.values()):
112             machine.qemu_start()
113             if pinning:
114                 machine.qemu_set_affinity(*machine_affinity)
115
116     def set_scheduler_all_vms(self):
117         """Set CFS scheduler policy on all VMs in manager."""
118         for machine in self.machines.values():
119             machine.qemu_set_scheduler_policy()
120
121     def kill_all_vms(self, force=False):
122         """Kill all added VMs in manager.
123
124         :param force: Force kill all Qemu instances by pkill qemu if True.
125         :type force: bool
126         """
127         for machine in self.machines.values():
128             if force:
129                 machine.qemu_kill_all()
130             else:
131                 machine.qemu_kill()