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