Performance: DPDK refactor
[csit.git] / resources / libraries / python / DpdkUtil.py
1 # Copyright (c) 2020 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(
36             u"l", u"eal_corelist", kwargs
37         )
38         # Add a PCI device in white list.
39         options.add_with_value_from_dict(
40             u"w", u"eal_pci_whitelist0", kwargs
41         )
42         options.add_with_value_from_dict(
43             u"w", u"eal_pci_whitelist1", kwargs
44         )
45         # Set master core.
46         options.add_with_value(
47             u"-master-lcore", u"0"
48         )
49         # Load an external driver. Multiple -d options are allowed.
50         options.add_with_value_if_from_dict(
51             u"d", u"/usr/lib/librte_pmd_virtio.so", u"eal_driver", kwargs, True
52         )
53         options.add_if_from_dict(
54             u"-in-memory", u"eal_in_memory", kwargs, False
55         )
56         return options
57
58     @staticmethod
59     def get_testpmd_pmd_options(**kwargs):
60         """Create PMD parameters options for testpmd (without --).
61
62         :param kwargs: List of testpmd parameters.
63         :type kwargs: dict
64         :returns: PMD parameters.
65         :rtype: OptionString
66         """
67         options = OptionString(prefix=u"--")
68         # Set the forwarding mode: io, mac, mac_retry, mac_swap, flowgen,
69         # rxonly, txonly, csum, icmpecho, ieee1588
70         options.add_equals_from_dict(
71             u"forward-mode", u"pmd_fwd_mode", kwargs, u"io"
72         )
73         # Set the number of packets per burst to N.
74         options.add_equals(
75             u"burst", 64
76         )
77         # Set the number of descriptors in the TX rings to N.
78         options.add_equals_from_dict(
79             u"txd", u"pmd_txd", kwargs, 1024
80         )
81         # Set the number of descriptors in the RX rings to N.
82         options.add_equals_from_dict(
83             u"rxd", u"pmd_rxd", kwargs, 1024
84         )
85         # Set the number of queues in the TX to N.
86         options.add_equals_from_dict(
87             u"txq", u"pmd_txq", kwargs, 1
88         )
89         # Set the number of queues in the RX to N.
90         options.add_equals_from_dict(
91             u"rxq", u"pmd_rxq", kwargs, 1
92         )
93         # Set the hexadecimal bitmask of offloads.
94         options.add_equals_from_dict(
95             u"tx-offloads", u"pmd_tx_offloads", kwargs, u"0x0"
96         )
97         # Enables numa aware allocation of mbufs.
98         options.add_if_from_dict(
99             u"numa", u"pmd_numa", kwargs, True
100         )
101         # Run by default.
102         options.add_if_from_dict(
103             u"auto-start", u"pmd_auto_start", kwargs, True
104         )
105         # Set the number of mbufs to be allocated in the mbuf pools.
106         options.add_equals_from_dict(
107             u"total-num-mbufs", u"pmd_num_mbufs", kwargs
108         )
109         # Set the number of forwarding ports.
110         options.add_equals_from_dict(
111             u"nb-ports", u"pmd_nb_ports", kwargs
112         )
113         # Set the hexadecimal bitmask of the ports used by the packet
114         # forwarding test.
115         options.add_equals_from_dict(
116             u"portmask", u"pmd_portmask", kwargs
117         )
118         # Disable link status check.
119         options.add_if_from_dict(
120             u"disable-link-check", u"pmd_disable_link_check", kwargs, True
121         )
122         # Set the MAC address XX:XX:XX:XX:XX:XX of the peer port N
123         options.add_equals_from_dict(
124             u"eth-peer", u"pmd_eth_peer_0", kwargs
125         )
126         options.add_equals_from_dict(
127             u"eth-peer", u"pmd_eth_peer_1", kwargs
128         )
129         # Set the max packet length.
130         options.add_equals_from_dict(
131             u"max-pkt-len", u"pmd_max_pkt_len", kwargs
132         )
133         # Set the max packet length.
134         options.add_equals_from_dict(
135             u"mbuf-size", u"pmd_mbuf_size", kwargs
136         )
137         # Set the number of forwarding cores based on coremask.
138         options.add_equals_from_dict(
139             u"nb-cores", u"pmd_nb_cores", kwargs
140         )
141         return options
142
143     @staticmethod
144     def get_testpmd_args(**kwargs):
145         """Get DPDK testpmd command line arguments.
146
147         :param kwargs: Key-value testpmd parameters.
148         :type kwargs: dict
149         :returns: Command line string.
150         :rtype: OptionString
151         """
152         options = OptionString()
153         options.extend(DpdkUtil.get_eal_options(**kwargs))
154         options.add(u"--")
155         options.extend(DpdkUtil.get_testpmd_pmd_options(**kwargs))
156         return options
157
158     @staticmethod
159     def get_testpmd_cmdline(**kwargs):
160         """Get DPDK testpmd command line arguments with testpmd command.
161
162         :param kwargs: Key-value testpmd parameters.
163         :type kwargs: dict
164         :returns: Command line string.
165         :rtype: OptionString
166         """
167         options = OptionString()
168         options.add(u"testpmd")
169         options.extend(DpdkUtil.get_eal_options(**kwargs))
170         options.add(u"--")
171         options.extend(DpdkUtil.get_testpmd_pmd_options(**kwargs))
172         return options
173
174     @staticmethod
175     def dpdk_testpmd_start(node, **kwargs):
176         """Start DPDK testpmd app on VM node.
177
178         :param node: VM Node to start testpmd on.
179         :param kwargs: Key-value testpmd parameters.
180         :type node: dict
181         :type kwargs: dict
182         """
183         cmd_options = OptionString()
184         cmd_options.add(u"/start-testpmd.sh")
185         cmd_options.extend(DpdkUtil.get_eal_options(**kwargs))
186         cmd_options.add(u"--")
187         cmd_options.extend(DpdkUtil.get_testpmd_pmd_options(**kwargs))
188         exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True)
189
190     @staticmethod
191     def dpdk_testpmd_stop(node):
192         """Stop DPDK testpmd app on node.
193
194         :param node: Node to stop testpmd on.
195         :type node: dict
196         :returns: nothing
197         """
198         cmd = u"/stop-testpmd.sh"  # Completed string, simple one.
199         exec_cmd_no_error(node, cmd, sudo=True, disconnect=True)
200
201     @staticmethod
202     def get_l3fwd_pmd_options(**kwargs):
203         """Create PMD parameters options for l3fwd (without --).
204
205         :param kwargs: List of l3fwd parameters.
206         :type kwargs: dict
207         :returns: PMD parameters.
208         :rtype: OptionString
209         """
210         options = OptionString(prefix=u"--")
211         # Set to use software to analyze packet type.
212         options.add_if_from_dict(
213             u"parse-ptype", u"pmd_parse_ptype", kwargs, True
214         )
215         # Set the MAC address XX:XX:XX:XX:XX:XX of the peer port N.
216         options.add_equals_from_dict(
217             u"eth-dest", u"pmd_eth_dest_0", kwargs
218         )
219         options.add_equals_from_dict(
220             u"eth-dest", u"pmd_eth_dest_1", kwargs
221         )
222         # Determines which queues from which ports are mapped to which cores.
223         options.add_equals_from_dict(
224             u"config", u"pmd_config", kwargs
225         )
226         # Enables jumbo frames.
227         options.add_if_from_dict(
228             u"enable-jumbo", u"pmd_enable_jumbo", kwargs, False
229         )
230         # Set the max packet length.
231         options.add_with_value_if_from_dict(
232             u"max-pkt-len", u"9200", u"pmd_max_pkt_len", kwargs, False
233         )
234         return options
235
236     @staticmethod
237     def get_l3fwd_args(**kwargs):
238         """Get DPDK l3fwd command line arguments.
239
240         :param kwargs: Key-value l3fwd parameters.
241         :type kwargs: dict
242         :returns: Command line string.
243         :rtype: OptionString
244         """
245         options = OptionString()
246         options.extend(DpdkUtil.get_eal_options(**kwargs))
247         options.add(u"--")
248         options.extend(DpdkUtil.get_l3fwd_pmd_options(**kwargs))
249         return options