CSIT-312 VPP vhost-user - VPP vhost-user driver, virtio in VM
[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     @staticmethod
23     def dpdk_testpmd_start(node, **args):
24         """Start DPDK testpmd app on VM node.
25
26         :param node: VM Node to start testpmd on.
27         :param args: List of testpmd parameters.
28         :type node: dict
29         :type args: list
30         :return: nothing
31         """
32         # Set the hexadecimal bitmask of the cores to run on.
33         eal_coremask = '-c {} '.format(args['eal_coremask'])\
34             if args.get('eal_coremask', '') else ''
35         # Set the number of memory channels to use.
36         eal_mem_channels = '-n {} '.format(args['eal_mem_channels'])\
37             if args.get('eal_mem_channels', '') else ''
38         # Set the memory to allocate on specific sockets (comma separated).
39         eal_socket_mem = '--socket-mem {} '.format(args['eal_socket_mem'])\
40             if args.get('eal_socket_mem', '') else ''
41         # Load an external driver. Multiple -d options are allowed.
42         eal_driver = '-d /usr/lib/librte_pmd_virtio.so '
43         # Set the forwarding mode: io, mac, mac_retry, mac_swap, flowgen,
44         # rxonly, txonly, csum, icmpecho, ieee1588
45         pmd_fwd_mode = '--forward-mode={} '.format(args['pmd_fwd_mode'])\
46             if args.get('pmd_fwd_mode', '') else ''
47         # Set the number of packets per burst to N.
48         pmd_burst = '--burst=64 '
49         # Set the number of descriptors in the TX rings to N.
50         pmd_txd = '--txd=2048 '
51         # Set the number of descriptors in the RX rings to N.
52         pmd_rxd = '--rxd=2048 '
53         # Set the hexadecimal bitmask of TX queue flags.
54         pmd_txqflags = '--txqflags=0xf00 '
55         # Set the number of mbufs to be allocated in the mbuf pools.
56         pmd_total_num_mbufs = '--total-num-mbufs=65536 '
57         # Set the hexadecimal bitmask of the ports for forwarding.
58         pmd_portmask = '--portmask=0x3 '
59         # Disable hardware VLAN.
60         pmd_disable_hw_vlan = '--disable-hw-vlan '\
61             if args.get('pmd_disable_hw_vlan', '') else ''
62         # Disable RSS (Receive Side Scaling).
63         pmd_disable_rss = '--disable-rss '\
64             if args.get('pmd_disable_rss', '') else ''
65         # Set the hexadecimal bitmask of the cores running forwarding. Master
66         # lcore=0 is reserved, so highest bit is set to 0.
67         pmd_coremask = '--coremask={} '.format(\
68             hex(int(args['eal_coremask'], 0) & 0xFFFE))\
69             if args.get('eal_coremask', '') else ''
70         # Set the number of forwarding cores based on coremask.
71         pmd_nb_cores = '--nb-cores={} '.format(\
72             bin(int(args['eal_coremask'], 0) & 0xFFFE).count('1'))\
73             if args.get('eal_coremask', '') else ''
74         eal_options = '-v '\
75             + eal_coremask\
76             + eal_mem_channels\
77             + eal_socket_mem\
78             + eal_driver
79         pmd_options = '-- '\
80             + pmd_fwd_mode\
81             + pmd_burst\
82             + pmd_txd\
83             + pmd_rxd\
84             + pmd_txqflags\
85             + pmd_total_num_mbufs\
86             + pmd_portmask\
87             + pmd_disable_hw_vlan\
88             + pmd_disable_rss\
89             + pmd_coremask\
90             + pmd_nb_cores
91         ssh = SSH()
92         ssh.connect(node)
93         cmd = "/start-testpmd.sh {0} {1}".format(eal_options, pmd_options)
94         exec_cmd_no_error(node, cmd, sudo=True)
95         ssh.disconnect(node)
96
97     @staticmethod
98     def dpdk_testpmd_stop(node):
99         """Stop DPDK testpmd app on node.
100
101         :param node: Node to stop testpmd on.
102         :type node: dict
103         :return: nothing
104         """
105         ssh = SSH()
106         ssh.connect(node)
107         cmd = "/stop-testpmd.sh"
108         exec_cmd_no_error(node, cmd, sudo=True)
109         ssh.disconnect(node)