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