CSIT-425: HC Test: NSH-SFC test suite
[csit.git] / resources / libraries / python / DpdkUtil.py
1 # Copyright (c) 2016 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 """Dpdk Utilities Library."""
15
16 from resources.libraries.python.ssh import SSH, exec_cmd_no_error
17
18
19 class DpdkUtil(object):
20     """Utilities for DPDK."""
21
22     #pylint: disable=too-many-locals
23     @staticmethod
24     def dpdk_testpmd_start(node, **args):
25         """Start DPDK testpmd app on VM node.
26
27         :param node: VM Node to start testpmd on.
28         :param args: List of testpmd parameters.
29         :type node: dict
30         :type args: list
31         :return: nothing
32         """
33         # Set the hexadecimal bitmask of the cores to run on.
34         eal_coremask = '-c {} '.format(args['eal_coremask'])\
35             if args.get('eal_coremask', '') else ''
36         # Set master core.
37         eal_master_core = '--master-lcore 0 '
38         # Set the number of memory channels to use.
39         eal_mem_channels = '-n {} '.format(args['eal_mem_channels'])\
40             if args.get('eal_mem_channels', '') else ''
41         # Set the memory to allocate on specific sockets (comma separated).
42         eal_socket_mem = '--socket-mem {} '.format(args['eal_socket_mem'])\
43             if args.get('eal_socket_mem', '') else ''
44         # Load an external driver. Multiple -d options are allowed.
45         eal_driver = '-d /usr/lib/librte_pmd_virtio.so '
46         # Set the forwarding mode: io, mac, mac_retry, mac_swap, flowgen,
47         # rxonly, txonly, csum, icmpecho, ieee1588
48         pmd_fwd_mode = '--forward-mode={} '.format(args['pmd_fwd_mode'])\
49             if args.get('pmd_fwd_mode', '') else ''
50         # Set the number of packets per burst to N.
51         pmd_burst = '--burst=64 '
52         # Set the number of descriptors in the TX rings to N.
53         pmd_txd = '--txd=256 '
54         # Set the number of descriptors in the RX rings to N.
55         pmd_rxd = '--rxd=256 '
56         # Set the number of queues in the TX to N.
57         pmd_txq = '--txq=1 '
58         # Set the number of queues in the RX to N.
59         pmd_rxq = '--rxq=1 '
60         # Set the hexadecimal bitmask of TX queue flags.
61         pmd_txqflags = '--txqflags=0xf00 '
62         # Set the number of mbufs to be allocated in the mbuf pools.
63         pmd_total_num_mbufs = '--total-num-mbufs={} '.format(\
64             args['pmd_num_mbufs']) if args.get('pmd_num_mbufs', '') else ''
65         # Set the hexadecimal bitmask of the ports for forwarding.
66         pmd_portmask = '--portmask={} '.format(args['pmd_portmask'])\
67             if args.get('pmd_portmask', '') else ''
68         # Disable hardware VLAN.
69         pmd_disable_hw_vlan = '--disable-hw-vlan '\
70             if args.get('pmd_disable_hw_vlan', '') else ''
71         # Disable RSS (Receive Side Scaling).
72         pmd_disable_rss = '--disable-rss '\
73             if args.get('pmd_disable_rss', '') else ''
74         # Set the MAC address XX:XX:XX:XX:XX:XX of the peer port N
75         pmd_eth_peer_0 = '--eth-peer={} '.format(args['pmd_eth_peer_0'])\
76             if args.get('pmd_eth_peer_0', '') else ''
77         pmd_eth_peer_1 = '--eth-peer={} '.format(args['pmd_eth_peer_1'])\
78             if args.get('pmd_eth_peer_1', '') else ''
79         # Set the number of forwarding cores based on coremask.
80         pmd_nb_cores = '--nb-cores={} '.format(\
81             bin(int(args['eal_coremask'], 0)).count('1')-1)\
82             if args.get('eal_coremask', '') else ''
83         eal_options = '-v '\
84             + eal_coremask\
85             + eal_master_core\
86             + eal_mem_channels\
87             + eal_socket_mem\
88             + eal_driver
89         pmd_options = '-- '\
90             + pmd_fwd_mode\
91             + pmd_burst\
92             + pmd_txd\
93             + pmd_rxd\
94             + pmd_txq\
95             + pmd_rxq\
96             + pmd_txqflags\
97             + pmd_total_num_mbufs\
98             + pmd_portmask\
99             + pmd_disable_hw_vlan\
100             + pmd_disable_rss\
101             + pmd_eth_peer_0\
102             + pmd_eth_peer_1\
103             + pmd_nb_cores
104         ssh = SSH()
105         ssh.connect(node)
106         cmd = "/start-testpmd.sh {0} {1}".format(eal_options, pmd_options)
107         exec_cmd_no_error(node, cmd, sudo=True)
108         ssh.disconnect(node)
109
110     @staticmethod
111     def dpdk_testpmd_stop(node):
112         """Stop DPDK testpmd app on node.
113
114         :param node: Node to stop testpmd on.
115         :type node: dict
116         :return: nothing
117         """
118         ssh = SSH()
119         ssh.connect(node)
120         cmd = "/stop-testpmd.sh"
121         exec_cmd_no_error(node, cmd, sudo=True)
122         ssh.disconnect(node)