FIX: Use vhost macs when testpmd_mac forwarding used in VM
[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, Topology
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 = Topology.get_interface_mac(
66                     self.nodes[node], 'vhost{idx}'.format(
67                         idx=(nf_chain - 1) * nf_nodes * 2 + nf_node * 2 - 1)) \
68                     if kwargs['vnf'] == 'testpmd_mac' \
69                     else kwargs['tg_if1_mac'] if nf_node == 1 \
70                     else '52:54:00:00:{id:02x}:02'.format(id=qemu_id - 1)
71                 vif2_mac = Topology.get_interface_mac(
72                     self.nodes[node], 'vhost{idx}'.format(
73                         idx=(nf_chain - 1) * nf_nodes * 2 + nf_node * 2)) \
74                     if kwargs['vnf'] == 'testpmd_mac' \
75                     else kwargs['tg_if2_mac'] if nf_node == nf_nodes \
76                     else '52:54:00:00:{id:02x}:01'.format(id=qemu_id + 1)
77
78                 self.machines_affinity[name] = CpuUtils.get_affinity_nf(
79                     nodes=self.nodes, node=node, nf_chains=nf_chains,
80                     nf_nodes=nf_nodes, nf_chain=nf_chain, nf_node=nf_node,
81                     vs_dtc=vs_dtc, nf_dtc=nf_dtc, nf_dtcr=nf_dtcr)
82
83                 self.machines[name] = QemuUtils(
84                     node=self.nodes[node], qemu_id=qemu_id,
85                     smp=len(self.machines_affinity[name]), mem=4096,
86                     vnf=kwargs['vnf'], img=img)
87                 self.machines[name].configure_kernelvm_vnf(
88                     mac1='52:54:00:00:{id:02x}:01'.format(id=qemu_id),
89                     mac2='52:54:00:00:{id:02x}:02'.format(id=qemu_id),
90                     vif1_mac=vif1_mac,
91                     vif2_mac=vif2_mac,
92                     queues=queues,
93                     jumbo_frames=kwargs['jumbo'])
94                 self.machines[name].qemu_add_vhost_user_if(
95                     sock1, jumbo_frames=kwargs['jumbo'], queues=queues,
96                     queue_size=kwargs['perf_qemu_qsz'])
97                 self.machines[name].qemu_add_vhost_user_if(
98                     sock2, jumbo_frames=kwargs['jumbo'], queues=queues,
99                     queue_size=kwargs['perf_qemu_qsz'])
100
101     def construct_vms_on_all_nodes(self, **kwargs):
102         """Construct 1..Mx1..N VMs(s) with specified name on all nodes.
103
104         :param kwargs: Named parameters.
105         :type kwargs: dict
106         """
107         self.initialize()
108         for node in self.nodes:
109             if self.nodes[node]['type'] == NodeType.DUT:
110                 self.construct_vms_on_node(node=node, **kwargs)
111
112     def start_all_vms(self, pinning=False):
113         """Start all added VMs in manager.
114
115         :param pinning: If True, then do also QEMU process pinning.
116         :type pinning: bool
117         """
118         for machine, machine_affinity in zip(self.machines.values(),
119                                              self.machines_affinity.values()):
120             machine.qemu_start()
121             if pinning:
122                 machine.qemu_set_affinity(*machine_affinity)
123
124     def kill_all_vms(self, force=False):
125         """Kill all added VMs in manager.
126
127         :param force: Force kill all Qemu instances by pkill qemu if True.
128         :type force: bool
129         """
130         for machine in self.machines.values():
131             if force:
132                 machine.qemu_kill_all()
133             else:
134                 machine.qemu_kill()