FIX: vpp_device image for 1908 branch
[csit.git] / resources / libraries / python / ContainerUtils.py
index 8c29924..1bd986c 100644 (file)
 
 """Library to manipulate Containers."""
 
+from string import Template
 from collections import OrderedDict, Counter
 
 from resources.libraries.python.ssh import SSH
-from resources.libraries.python.constants import Constants
+from resources.libraries.python.Constants import Constants
 from resources.libraries.python.topology import Topology
 from resources.libraries.python.VppConfigGenerator import VppConfigGenerator
 
@@ -135,19 +136,14 @@ class ContainerManager(object):
             self.engine.container = self.containers[container]
             self.engine.execute(command)
 
-    def install_vpp_in_all_containers(self):
-        """Install VPP into all containers."""
+    def start_vpp_in_all_containers(self):
+        """Start VPP in all containers."""
         for container in self.containers:
             self.engine.container = self.containers[container]
-            # We need to install supervisor client/server system to control VPP
-            # as a service
-            self.engine.execute('apt-get update')
-            self.engine.install_supervisor()
-            self.engine.install_vpp()
-            self.engine.restart_vpp()
+            self.engine.start_vpp()
 
     def restart_vpp_in_all_containers(self):
-        """Restart VPP on all containers."""
+        """Restart VPP in all containers."""
         for container in self.containers:
             self.engine.container = self.containers[container]
             self.engine.restart_vpp()
@@ -161,7 +157,7 @@ class ContainerManager(object):
             interface in container (only single container can be configured).
         :param kwargs: Named parameters.
         :type chain_topology: str
-        :param kwargs: dict
+        :type kwargs: dict
         """
         # Count number of DUTs based on node's host information
         dut_cnt = len(Counter([self.containers[container].node['host']
@@ -209,11 +205,11 @@ class ContainerManager(object):
         """Configure VPP in chain topology with l2xc.
 
         :param kwargs: Named parameters.
-        :param kwargs: dict
+        :type kwargs: dict
         """
         self.engine.create_vpp_startup_config()
         self.engine.create_vpp_exec_config(
-            'memif_create_chain_l2xc.vat',
+            'memif_create_chain_l2xc.exec',
             mid1=kwargs['mid1'], mid2=kwargs['mid2'],
             sid1=kwargs['sid1'], sid2=kwargs['sid2'],
             socket1='{guest_dir}/memif-{c.name}-{sid1}'.
@@ -225,7 +221,7 @@ class ContainerManager(object):
         """Configure VPP in cross horizontal topology (single memif).
 
         :param kwargs: Named parameters.
-        :param kwargs: dict
+        :type kwargs: dict
         """
         if 'DUT1' in self.engine.container.name:
             if_pci = Topology.get_interface_pci_addr(
@@ -239,7 +235,7 @@ class ContainerManager(object):
                 self.engine.container.node, kwargs['dut2_if'])
         self.engine.create_vpp_startup_config_dpdk_dev(if_pci)
         self.engine.create_vpp_exec_config(
-            'memif_create_cross_horizon.vat',
+            'memif_create_cross_horizon.exec',
             mid1=kwargs['mid1'], sid1=kwargs['sid1'], if_name=if_name,
             socket1='{guest_dir}/memif-{c.name}-{sid1}'.
             format(c=self.engine.container, **kwargs))
@@ -248,11 +244,11 @@ class ContainerManager(object):
         """Configure VPP in chain topology with l2xc (functional).
 
         :param kwargs: Named parameters.
-        :param kwargs: dict
+        :type kwargs: dict
         """
-        self.engine.create_vpp_startup_config_func_dev()
+        self.engine.create_vpp_startup_config()
         self.engine.create_vpp_exec_config(
-            'memif_create_chain_functional.vat',
+            'memif_create_chain_functional.exec',
             mid1=kwargs['mid1'], mid2=kwargs['mid2'],
             sid1=kwargs['sid1'], sid2=kwargs['sid2'],
             socket1='{guest_dir}/memif-{c.name}-{sid1}'.
@@ -265,7 +261,7 @@ class ContainerManager(object):
         """Configure VPP in chain topology with ip4.
 
         :param kwargs: Named parameters.
-        :param kwargs: dict
+        :type kwargs: dict
         """
         self.engine.create_vpp_startup_config()
 
@@ -276,7 +272,7 @@ class ContainerManager(object):
             if (kwargs['mid2'] - 1) % kwargs['nodes'] + 1 == kwargs['nodes'] \
             else '52:54:00:00:{0:02X}:01'.format(kwargs['mid2'] + 1)
         self.engine.create_vpp_exec_config(
-            'memif_create_chain_ip4.vat',
+            'memif_create_chain_ip4.exec',
             mid1=kwargs['mid1'], mid2=kwargs['mid2'],
             sid1=kwargs['sid1'], sid2=kwargs['sid2'],
             socket1='{guest_dir}/memif-{c.name}-{sid1}'.
@@ -291,7 +287,7 @@ class ContainerManager(object):
         """Configure VPP in pipeline topology with ip4.
 
         :param kwargs: Named parameters.
-        :param kwargs: dict
+        :type kwargs: dict
         """
         self.engine.create_vpp_startup_config()
         node = (kwargs['mid1'] - 1) % kwargs['nodes'] + 1
@@ -321,7 +317,7 @@ class ContainerManager(object):
             format(c=self.engine.container, **kwargs)
 
         self.engine.create_vpp_exec_config(
-            'memif_create_pipeline_ip4.vat',
+            'memif_create_pipeline_ip4.exec',
             mid1=kwargs['mid1'], mid2=kwargs['mid2'],
             sid1=kwargs['sid1'], sid2=kwargs['sid2'],
             socket1=socket1, socket2=socket2, role1=role1, role2=role2,
@@ -393,84 +389,48 @@ class ContainerEngine(object):
         """System info."""
         raise NotImplementedError
 
-    def install_supervisor(self):
-        """Install supervisord inside a container."""
-        self.execute('apt-get install -y supervisor')
-        self.execute('echo "{config}" > {config_file} && '
-                     'supervisord -c {config_file}'.
-                     format(
-                         config='[unix_http_server]\n'
-                         'file  = /tmp/supervisor.sock\n\n'
-                         '[rpcinterface:supervisor]\n'
-                         'supervisor.rpcinterface_factory = '
-                         'supervisor.rpcinterface:make_main_rpcinterface\n\n'
-                         '[supervisorctl]\n'
-                         'serverurl = unix:///tmp/supervisor.sock\n\n'
-                         '[supervisord]\n'
-                         'pidfile = /tmp/supervisord.pid\n'
-                         'identifier = supervisor\n'
-                         'directory = /tmp\n'
-                         'logfile=/tmp/supervisord.log\n'
-                         'loglevel=debug\n'
-                         'nodaemon=false\n\n',
-                         config_file=SUPERVISOR_CONF))
-
-    def install_vpp(self):
-        """Install VPP inside a container."""
-        self.execute('ln -s /dev/null /etc/sysctl.d/80-vpp.conf')
-        # Workaround for install xenial vpp build on bionic ubuntu.
-        self.execute('apt-get install -y wget')
-        self.execute('deb=$(mktemp) && wget -O "${deb}" '
-                     'http://launchpadlibrarian.net/336117627/'
-                     'libmbedcrypto0_2.5.1-1ubuntu1_amd64.deb && '
-                     'dpkg -i "${deb}" && '
-                     'rm -f "${deb}"')
-        self.execute('deb=$(mktemp) && wget -O "${deb}" '
-                     'http://launchpadlibrarian.net/252876048/'
-                     'libboost-system1.58.0_1.58.0+dfsg-5ubuntu3_amd64.deb && '
-                     'dpkg -i "${deb}" && '
-                     'rm -f "${deb}"')
+    def start_vpp(self):
+        """Start VPP inside a container."""
         self.execute(
-            'dpkg -i --force-all '
-            '{guest_dir}/openvpp-testing/download_dir/*.deb'.
-            format(guest_dir=self.container.mnt[0].split(':')[1]))
-        self.execute('apt-get -f install -y')
-        self.execute('apt-get install -y ca-certificates')
-        self.execute('echo "{config}" >> {config_file}'.
-                     format(
-                         config='[program:vpp]\n'
-                         'command=/usr/bin/vpp -c /etc/vpp/startup.conf\n'
-                         'autorestart=false\n'
-                         'redirect_stderr=true\n'
-                         'priority=1',
-                         config_file=SUPERVISOR_CONF))
-        self.execute('supervisorctl reload')
+            u"setsid /usr/bin/vpp -c /etc/vpp/startup.conf "
+            u">/tmp/vppd.log 2>&1 < /dev/null &")
 
     def restart_vpp(self):
         """Restart VPP service inside a container."""
-        self.execute('supervisorctl restart vpp')
-        self.execute('cat /tmp/supervisord.log')
+        self.execute(u"pkill vpp")
+        self.start_vpp()
+        self.execute(u"cat /tmp/vppd.log")
 
-    def create_base_vpp_startup_config(self):
+    def create_base_vpp_startup_config(self, cpuset_cpus=None):
         """Create base startup configuration of VPP on container.
 
+        :param cpuset_cpus: List of CPU cores to allocate.
+
+        :type cpuset_cpus: list.
         :returns: Base VPP startup configuration.
         :rtype: VppConfigGenerator
         """
-        cpuset_cpus = self.container.cpuset_cpus
-
+        if cpuset_cpus is None:
+            cpuset_cpus = self.container.cpuset_cpus
         # Create config instance
         vpp_config = VppConfigGenerator()
         vpp_config.set_node(self.container.node)
         vpp_config.add_unix_cli_listen()
         vpp_config.add_unix_nodaemon()
-        vpp_config.add_unix_exec('/tmp/running.exec')
-        # We will pop the first core from the list to be a main core
-        vpp_config.add_cpu_main_core(str(cpuset_cpus.pop(0)))
-        # If more cores in the list, the rest will be used as workers.
+        vpp_config.add_unix_exec(u"/tmp/running.exec")
+        vpp_config.add_statseg_per_node_counters(value=u"on")
         if cpuset_cpus:
-            corelist_workers = ','.join(str(cpu) for cpu in cpuset_cpus)
+            # We will pop the first core from the list to be a main core
+            vpp_config.add_cpu_main_core(str(cpuset_cpus.pop(0)))
+            # If more cores in the list, the rest will be used as workers.
+            corelist_workers = u",".join(str(cpu) for cpu in cpuset_cpus)
             vpp_config.add_cpu_corelist_workers(corelist_workers)
+        vpp_config.add_buffers_per_numa(215040)
+        vpp_config.add_plugin(u"disable", u"default")
+        vpp_config.add_plugin(u"enable", u"memif_plugin.so")
+        vpp_config.add_heapsize(u"4G")
+        vpp_config.add_ip_heap_size(u"4G")
+        vpp_config.add_statseg_size(u"4G")
 
         return vpp_config
 
@@ -478,66 +438,28 @@ class ContainerEngine(object):
         """Create startup configuration of VPP without DPDK on container.
         """
         vpp_config = self.create_base_vpp_startup_config()
-        vpp_config.add_plugin('disable', 'dpdk_plugin.so')
 
         # Apply configuration
         self.execute('mkdir -p /etc/vpp/')
         self.execute('echo "{config}" | tee /etc/vpp/startup.conf'
                      .format(config=vpp_config.get_config_str()))
 
-    def create_vpp_startup_config_dpdk_dev(self, *devices):
-        """Create startup configuration of VPP with DPDK on container.
-
-        :param devices: List of PCI devices to add.
-        :type devices: list
-        """
-        vpp_config = self.create_base_vpp_startup_config()
-        vpp_config.add_dpdk_dev(*devices)
-        vpp_config.add_dpdk_no_tx_checksum_offload()
-        vpp_config.add_dpdk_log_level('debug')
-        vpp_config.add_plugin('disable', 'default')
-        vpp_config.add_plugin('enable', 'dpdk_plugin.so')
-        vpp_config.add_plugin('enable', 'memif_plugin.so')
-
-        # Apply configuration
-        self.execute('mkdir -p /etc/vpp/')
-        self.execute('echo "{config}" | tee /etc/vpp/startup.conf'
-                     .format(config=vpp_config.get_config_str()))
-
-    def create_vpp_startup_config_func_dev(self):
-        """Create startup configuration of VPP on container for functional
-        vpp_device tests.
-        """
-        # Create config instance
-        vpp_config = VppConfigGenerator()
-        vpp_config.set_node(self.container.node)
-        vpp_config.add_unix_cli_listen()
-        vpp_config.add_unix_nodaemon()
-        vpp_config.add_unix_exec('/tmp/running.exec')
-        vpp_config.add_plugin('disable', 'dpdk_plugin.so')
-
-        # Apply configuration
-        self.execute('mkdir -p /etc/vpp/')
-        self.execute('echo "{config}" | tee /etc/vpp/startup.conf'
-                     .format(config=vpp_config.get_config_str()))
-
-    def create_vpp_exec_config(self, vat_template_file, **kwargs):
+    def create_vpp_exec_config(self, template_file, **kwargs):
         """Create VPP exec configuration on container.
 
-        :param vat_template_file: File name of a VAT template script.
-        :param kwargs: Parameters for VAT script.
-        :type vat_template_file: str
+        :param template_file: File name of a template script.
+        :param kwargs: Parameters for script.
+        :type template_file: str
         :type kwargs: dict
         """
-        vat_file_path = '{p}/{f}'.format(p=Constants.RESOURCES_TPL_VAT,
-                                         f=vat_template_file)
+        running = '/tmp/running.exec'
+        template = '{res}/{tpl}'.format(
+            res=Constants.RESOURCES_TPL_CONTAINER, tpl=template_file)
 
-        with open(vat_file_path, 'r') as template_file:
-            cmd_template = template_file.readlines()
-            for line_tmpl in cmd_template:
-                vat_cmd = line_tmpl.format(**kwargs)
-                self.execute('echo "{c}" >> /tmp/running.exec'
-                             .format(c=vat_cmd.replace('\n', '')))
+        with open(template, 'r') as src_file:
+            src = Template(src_file.read())
+            self.execute('echo "{out}" > {running}'.format(
+                out=src.safe_substitute(**kwargs), running=running))
 
     def is_container_running(self):
         """Check if container is running."""
@@ -607,8 +529,12 @@ class LXC(ContainerEngine):
             else:
                 return
 
+        target_arch = 'arm64' \
+            if Topology.get_node_arch(self.container.node) == 'aarch64' \
+            else 'amd64'
+
         image = self.container.image if self.container.image else\
-            "-d ubuntu -r xenial -a amd64"
+            "-d ubuntu -r bionic -a {arch}".format(arch=target_arch)
 
         cmd = 'lxc-create -t download --name {c.name} -- {image} '\
             '--no-validate'.format(c=self.container, image=image)
@@ -619,6 +545,10 @@ class LXC(ContainerEngine):
 
         self._configure_cgroup('lxc')
 
+    def build(self):
+        """Build container (compile)."""
+        raise NotImplementedError
+
     def create(self):
         """Create/deploy an application inside a container on system.
 
@@ -627,11 +557,14 @@ class LXC(ContainerEngine):
         if self.container.mnt:
             for mount in self.container.mnt:
                 host_dir, guest_dir = mount.split(':')
+                options = 'bind,create=dir' \
+                    if guest_dir.endswith('/') else 'bind,create=file'
                 entry = 'lxc.mount.entry = {host_dir} '\
                     '/var/lib/lxc/{c.name}/rootfs{guest_dir} none ' \
-                    'bind,create=dir 0 0'.format(c=self.container,
-                                                 host_dir=host_dir,
-                                                 guest_dir=guest_dir)
+                    '{options} 0 0'.format(c=self.container,
+                                           host_dir=host_dir,
+                                           guest_dir=guest_dir,
+                                           options=options)
                 ret, _, _ = self.container.ssh.exec_command_sudo(
                     "sh -c 'echo \"{e}\" >> /var/lib/lxc/{c.name}/config'".
                     format(e=entry, c=self.container))
@@ -678,8 +611,8 @@ class LXC(ContainerEngine):
             ' '.join('--set-var %s' % env for env in self.container.env))\
             if self.container.env else ''
 
-        cmd = "lxc-attach {env} --name {c.name} -- /bin/sh -c '{command}"\
-            "exit $?'".format(env=env, c=self.container, command=command)
+        cmd = "lxc-attach {env} --name {c.name} -- /bin/sh -c '{command}'"\
+            .format(env=env, c=self.container, command=command)
 
         ret, _, _ = self.container.ssh.exec_command_sudo(cmd, timeout=180)
         if int(ret) != 0:
@@ -797,7 +730,10 @@ class Docker(ContainerEngine):
                 return
 
         if not self.container.image:
-            setattr(self.container, 'image', 'snergster/csit-sut:latest')
+            img = Constants.DOCKER_SUT_IMAGE_UBUNTU_ARM \
+                if Topology.get_node_arch(self.container.node) == 'aarch64' \
+                else Constants.DOCKER_SUT_IMAGE_UBUNTU
+            setattr(self.container, 'image', img)
 
         cmd = 'docker pull {image}'.format(image=self.container.image)
 
@@ -805,6 +741,7 @@ class Docker(ContainerEngine):
         if int(ret) != 0:
             raise RuntimeError('Failed to create container {c.name}.'
                                .format(c=self.container))
+
         if self.container.cpuset_cpus:
             self._configure_cgroup('docker')
 
@@ -860,10 +797,10 @@ class Docker(ContainerEngine):
 
         :param command: Command to run inside container.
         :type command: str
-        :raises RuntimeError: If runnig the command in a container failed.
+        :raises RuntimeError: If running the command in a container failed.
         """
-        cmd = "docker exec --interactive {c.name} /bin/sh -c '{command}"\
-            "exit $?'".format(c=self.container, command=command)
+        cmd = "docker exec --interactive {c.name} /bin/sh -c '{command}'"\
+            .format(c=self.container, command=command)
 
         ret, _, _ = self.container.ssh.exec_command_sudo(cmd, timeout=180)
         if int(ret) != 0: