887eae156e9872b21ca55f89f4d245a4fe2d52a2
[csit.git] / resources / libraries / python / DpdkUtil.py
1 # Copyright (c) 2019 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.OptionString import OptionString
17 from resources.libraries.python.ssh import exec_cmd_no_error
18
19
20 class DpdkUtil(object):
21     """Utilities for DPDK."""
22
23     @staticmethod
24     def get_eal_options(**kwargs):
25         """Create EAL parameters options (including -v).
26
27         :param kwargs: Dict of testpmd parameters.
28         :type kwargs: dict
29         :returns: EAL parameters.
30         :rtype: OptionString
31         """
32         options = OptionString(prefix='-')
33         options.add('v')
34         # Set the hexadecimal bitmask of the cores to run on.
35         options.add_with_value_from_dict('l', 'eal_corelist', kwargs)
36         # Set master core.
37         options.add_with_value('-master-lcore', '0')
38         # Load an external driver. Multiple -d options are allowed.
39         options.add_with_value_if_from_dict(
40             'd', '/usr/lib/librte_pmd_virtio.so', 'eal_driver', kwargs, True)
41         options.add_if_from_dict(
42             '-in-memory', 'eal_in_memory', kwargs, False)
43         return options
44
45     @staticmethod
46     def get_pmd_options(**kwargs):
47         """Create PMD parameters options (without --).
48
49         :param kwargs: List of testpmd parameters.
50         :type kwargs: dict
51         :returns: PMD parameters.
52         :rtype: OptionString
53         """
54         options = OptionString(prefix='--')
55         # Set the forwarding mode: io, mac, mac_retry, mac_swap, flowgen,
56         # rxonly, txonly, csum, icmpecho, ieee1588
57         options.add_equals_from_dict(
58             'forward-mode', 'pmd_fwd_mode', kwargs, 'io')
59         # Set the number of packets per burst to N.
60         options.add_equals('burst', 64)
61         # Set the number of descriptors in the TX rings to N.
62         options.add_equals_from_dict('txd', 'pmd_txd', kwargs, 1024)
63         # Set the number of descriptors in the RX rings to N.
64         options.add_equals_from_dict('rxd', 'pmd_rxd', kwargs, 1024)
65         # Set the number of queues in the TX to N.
66         options.add_equals_from_dict('txq', 'pmd_txq', kwargs, 1)
67         # Set the number of queues in the RX to N.
68         options.add_equals_from_dict('rxq', 'pmd_rxq', kwargs, 1)
69         # Set the hexadecimal bitmask of offloads.
70         options.add_equals_if_from_dict(
71             'txqflags', '0xf00', 'pmd_tx_offloads', kwargs, True)
72         # Set the number of mbufs to be allocated in the mbuf pools.
73         options.add_equals_from_dict('total-num-mbufs', 'pmd_num_mbufs', kwargs)
74         # Disable hardware VLAN.
75         options.add_if_from_dict(
76             'disable-hw-vlan', 'pmd_disable_hw_vlan', kwargs, True)
77         # Set the MAC address XX:XX:XX:XX:XX:XX of the peer port N
78         options.add_equals_from_dict('eth-peer', 'pmd_eth-peer_0', kwargs)
79         options.add_equals_from_dict('eth-peer', 'pmd_eth-peer_1', kwargs)
80         # Set the max packet length.
81         options.add_equals_from_dict('max-pkt-len', 'pmd_max_pkt_len', kwargs)
82         # Set the number of forwarding cores based on coremask.
83         options.add_equals_from_dict('nb-cores', 'pmd_nb_cores', kwargs)
84         return options
85
86     @staticmethod
87     def get_testpmd_cmdline(**kwargs):
88         """Get DPDK testpmd command line arguments.
89
90         :param args: Key-value testpmd parameters.
91         :type args: dict
92         :returns: Command line string.
93         :rtype: OptionString
94         """
95         options = OptionString()
96         options.add('testpmd')
97         options.extend(DpdkUtil.get_eal_options(**kwargs))
98         options.add('--')
99         options.extend(DpdkUtil.get_pmd_options(**kwargs))
100         return options
101
102     @staticmethod
103     def dpdk_testpmd_start(node, **kwargs):
104         """Start DPDK testpmd app on VM node.
105
106         :param node: VM Node to start testpmd on.
107         :param args: Key-value testpmd parameters.
108         :type node: dict
109         :type kwargs: dict
110         """
111         cmd_options = OptionString()
112         cmd_options.add("/start-testpmd.sh")
113         cmd_options.extend(DpdkUtil.get_eal_options(**kwargs))
114         cmd_options.add('--')
115         cmd_options.extend(DpdkUtil.get_pmd_options(**kwargs))
116         exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True)
117
118     @staticmethod
119     def dpdk_testpmd_stop(node):
120         """Stop DPDK testpmd app on node.
121
122         :param node: Node to stop testpmd on.
123         :type node: dict
124         :returns: nothing
125         """
126         cmd = "/stop-testpmd.sh"  # Completed string, simpler than OptionString.
127         exec_cmd_no_error(node, cmd, sudo=True, disconnect=True)