bugfix: mount hugepages for PVP and PVVP scenarios
[vswitchperf.git] / src / dpdk / testpmd_proc.py
1 # Copyright 2015 Intel Corporation.
2 #
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
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 """Class wrapper for controlling a TestPMD instance.
16
17 Wraps ``testpmd`` process.
18 """
19
20 import os
21 import logging
22 import time
23 import pexpect
24
25 from conf import settings
26 from tools import tasks
27
28 _TESTPMD_PROMPT = 'Done'
29
30 _TESTPMD_BIN = os.path.join(
31     settings.getValue('RTE_SDK'), settings.getValue('RTE_TARGET'),
32     'app', 'testpmd')
33
34 _LOG_FILE_VSWITCHD = os.path.join(
35     settings.getValue('LOG_DIR'), settings.getValue('LOG_FILE_VSWITCHD'))
36
37 class TestPMDProcess(tasks.Process):
38     """Class wrapper for controlling a TestPMD instance.
39
40     Wraps ``testpmd`` process.
41     """
42     _logfile = _LOG_FILE_VSWITCHD
43     _proc_name = 'testpmd'
44
45     def __init__(self, timeout=30, testpmd_args=None, expected_cmd=None):
46         """Initialise the wrapper with a specific start timeout and extra
47         parameters.
48
49         :param timeout: Timeout to wait for application to start.
50         :param testpmd_args: Command line parameters for testpmd.
51
52         :returns: None
53         """
54         self._logger = logging.getLogger(__name__)
55         self._timeout = timeout
56         self._expect = expected_cmd
57         if not self._expect:
58             self._expect = _TESTPMD_PROMPT
59         testpmd_args = testpmd_args or []
60         self._cmd = ['sudo', '-E', _TESTPMD_BIN] + testpmd_args
61
62     # startup/shutdown
63
64     def start(self):
65         """ Start ``testpmd`` instance.
66
67         :returns: None
68         :raises: pexpect.EOF, pexpect.TIMEOUT
69         """
70
71         try:
72             super(TestPMDProcess, self).start()
73             self.relinquish()
74         except (pexpect.EOF, pexpect.TIMEOUT) as exc:
75             logging.error("TestPMD start failed.")
76             raise exc
77
78     def kill(self, signal='-15', sleep=2):
79         """Kill ``testpmd`` instance if it is alive.
80
81         :returns: None
82         """
83         self._logger.info('Killing testpmd...')
84
85         super(TestPMDProcess, self).kill(signal, sleep)
86
87     # helper functions
88
89     def send(self, cmd, delay=0):
90         """
91         Send ``cmd`` with no wait.
92
93         Useful for asynchronous commands.
94
95         :param cmd: Command to send to guest.
96         :param timeout: Delay to wait after sending command before returning.
97
98         :returns: None
99         """
100         self._logger.debug(cmd)
101         self._child.sendline(cmd)
102         time.sleep(delay)
103
104     def wait(self, msg=_TESTPMD_PROMPT, timeout=30):
105         """
106         Wait for ``msg``.
107
108         :param msg: Message to wait for from guest.
109         :param timeout: Time to wait for message.
110
111         :returns: None
112         """
113         self._child.expect(msg, timeout=timeout)