trex: fix Trex makefile
[vswitchperf.git] / tools / pkt_fwd / testpmd.py
1 # Copyright 2016-2017 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 """VSPERF TestPMD implementation
16 """
17
18 import logging
19 import pexpect
20 from conf import settings
21 from src.dpdk import dpdk
22 from src.dpdk import TestPMDProcess
23 from tools.pkt_fwd.pkt_fwd import IPktFwd
24
25 _LOGGER = logging.getLogger(__name__)
26 _VSWITCHD_CONST_ARGS = ['--', '-i']
27 _TESTPMD_PVP_CONST_ARGS = ['--vdev', 'net_vhost0,iface=/tmp/dpdkvhostuser0',
28                            '--vdev', 'net_vhost1,iface=/tmp/dpdkvhostuser1',]
29
30 class TestPMD(IPktFwd):
31     """TestPMD implementation (only phy2phy deployment is supported)
32
33     This is wrapper for functionality implemented in ${DPDK}/app/test-pmd.
34
35     The method docstrings document only considerations specific to this
36     implementation. For generic information of the nature of the methods,
37     see the interface definition.
38     """
39
40     _logger = logging.getLogger()
41
42     def __init__(self, guest=False):
43         vswitchd_args = settings.getValue('VSWITCHD_DPDK_ARGS')
44
45         # override socket-mem settings
46         for tmp_arg in vswitchd_args:
47             if tmp_arg.startswith('--socket-mem'):
48                 vswitchd_args.remove(tmp_arg)
49         vswitchd_args += ['--socket-mem ' +
50                           ','.join(settings.getValue('DPDK_SOCKET_MEM'))]
51
52         if guest:
53             vswitchd_args += _TESTPMD_PVP_CONST_ARGS
54         vswitchd_args += _VSWITCHD_CONST_ARGS
55         vswitchd_args += settings.getValue('TESTPMD_ARGS')
56
57         # need to give mbufs a larger size for jumbo frames based on the setting
58         if settings.getValue('VSWITCH_JUMBO_FRAMES_ENABLED'):
59             vswitchd_args += ['--mbuf-size={}'.format(int(
60                 settings.getValue('VSWITCH_JUMBO_FRAMES_SIZE')) + 500)]
61
62         self._nports = len(settings.getValue('NICS'))
63         self._fwdmode = settings.getValue('TESTPMD_FWD_MODE')
64         self._csum_layer = settings.getValue('TESTPMD_CSUM_LAYER')
65         self._csum_calc = settings.getValue('TESTPMD_CSUM_CALC')
66         self._csum_tunnel = settings.getValue('TESTPMD_CSUM_PARSE_TUNNEL')
67
68         self._testpmd = TestPMDProcess(testpmd_args=vswitchd_args)
69
70     def start(self):
71         """See IPktFwd for general description
72
73         Activates testpmd.
74         """
75         self._logger.info("Starting TestPMD...")
76         dpdk.init()
77         self._testpmd.start()
78         self._logger.info("TestPMD...Started.")
79
80         self._testpmd.send('set fwd {}'.format(self._fwdmode), 1)
81
82         if settings.getValue('VSWITCH_JUMBO_FRAMES_ENABLED'):
83             self._testpmd.send('port stop all', 1)  # ports must be stopped to set mtu
84             self._testpmd.send('port config all max-pkt-len {}'.format(
85                 settings.getValue('VSWITCH_JUMBO_FRAMES_SIZE')), 1)
86             self._testpmd.send('port start all', 1)
87
88         for port in range(self._nports):
89             self._testpmd.send('csum set {} {} {}'.format(
90                 self._csum_layer, self._csum_calc, port), 1)
91             self._testpmd.send('csum parse_tunnel {} {}'.format(
92                 self._csum_tunnel, port), 1)
93
94         self._testpmd.send('start', 1)
95
96     def start_for_guest(self):
97         """See IPktFwd for general description
98
99         Activates testpmd for guest config
100         """
101         self._logger.info("Starting TestPMD for one guest...")
102         dpdk.init()
103         self._testpmd.start()
104         self._logger.info("TestPMD...Started.")
105
106         if settings.getValue('VSWITCH_JUMBO_FRAMES_ENABLED'):
107             self._testpmd.send('port stop all', 1)  # ports must be stopped to set mtu
108             self._testpmd.send('port config all max-pkt-len {}'.format(
109                 settings.getValue('VSWITCH_JUMBO_FRAMES_SIZE')), 1)
110             # conflicting info if scatter needs to be enabled or not
111             self._testpmd.send('port config all scatter on', 1)
112             self._testpmd.send('port start all', 1)
113             self._testpmd.wait(timeout=60)  # port startup can take a few seconds
114
115         self._testpmd.send('set portlist 0,2,1,3', 1)
116         self._testpmd.send('set fwd {}'.format(self._fwdmode), 1)
117
118         self._testpmd.send('start', 1)
119
120     def stop(self):
121         """See IPktFwd for general description
122
123         Kills testpmd.
124         """
125         try:
126             self._testpmd.send('stop')
127             self._testpmd.wait('Done.', 5)
128             self._testpmd.send('quit', 2)
129             self._testpmd.kill()
130         except pexpect.EOF:
131             pass
132         dpdk.cleanup()
133
134     # Method could be a function
135     # pylint: disable=no-self-use
136     def get_version(self):
137         """
138         Get product version
139         :return: None
140         """
141         # No way to read TestPMD version
142         return []