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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Dpdk Utilities Library."""
16 from resources.libraries.python.OptionString import OptionString
17 from resources.libraries.python.ssh import exec_cmd_no_error
21 """Utilities for DPDK."""
24 def get_eal_options(**kwargs):
25 """Create EAL parameters options (including -v).
27 :param kwargs: Dict of testpmd parameters.
29 :returns: EAL parameters.
32 options = OptionString(prefix=u"-")
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
38 # Add a PCI device in white list.
39 options.add_with_value_from_dict(
40 u"w", u"eal_pci_whitelist0", kwargs
42 options.add_with_value_from_dict(
43 u"w", u"eal_pci_whitelist1", kwargs
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
49 options.add_if_from_dict(
50 u"-in-memory", u"eal_in_memory", kwargs, False
55 def get_testpmd_pmd_options(**kwargs):
56 """Create PMD parameters options for testpmd (without --).
58 :param kwargs: List of testpmd parameters.
60 :returns: PMD parameters.
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"
69 # Set the number of packets per burst to N.
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
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
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
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
89 # Set the hexadecimal bitmask of offloads.
90 options.add_equals_from_dict(
91 u"tx-offloads", u"pmd_tx_offloads", kwargs, u"0x0"
93 # Enables numa aware allocation of mbufs.
94 options.add_if_from_dict(
95 u"numa", u"pmd_numa", kwargs, True
98 options.add_if_from_dict(
99 u"auto-start", u"pmd_auto_start", kwargs, True
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
105 # Set the number of forwarding ports.
106 options.add_equals_from_dict(
107 u"nb-ports", u"pmd_nb_ports", kwargs
109 # Set the hexadecimal bitmask of the ports used by the packet
111 options.add_equals_from_dict(
112 u"portmask", u"pmd_portmask", kwargs
114 # Disable link status check.
115 options.add_if_from_dict(
116 u"disable-link-check", u"pmd_disable_link_check", kwargs, True
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
122 options.add_equals_from_dict(
123 u"eth-peer", u"pmd_eth_peer_1", kwargs
125 # Set the max packet length.
126 options.add_equals_from_dict(
127 u"max-pkt-len", u"pmd_max_pkt_len", kwargs
130 options.add_equals_from_dict(
131 u"mbuf-size", u"pmd_mbuf_size", kwargs
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
140 def get_testpmd_args(**kwargs):
141 """Get DPDK testpmd command line arguments.
143 :param kwargs: Key-value testpmd parameters.
145 :returns: Command line string.
148 options = OptionString()
149 options.extend(DpdkUtil.get_eal_options(**kwargs))
151 options.extend(DpdkUtil.get_testpmd_pmd_options(**kwargs))
155 def get_testpmd_cmdline(**kwargs):
156 """Get DPDK testpmd command line arguments with testpmd command.
158 :param kwargs: Key-value testpmd parameters.
160 :returns: Command line string.
163 options = OptionString()
164 options.add(u"dpdk-testpmd")
165 options.extend(DpdkUtil.get_eal_options(**kwargs))
167 options.extend(DpdkUtil.get_testpmd_pmd_options(**kwargs))
171 def dpdk_testpmd_start(node, **kwargs):
172 """Start DPDK testpmd app on VM node.
174 :param node: VM Node to start testpmd on.
175 :param kwargs: Key-value testpmd parameters.
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)
187 def dpdk_testpmd_stop(node):
188 """Stop DPDK testpmd app on node.
190 :param node: Node to stop testpmd on.
194 cmd = u"/stop-testpmd.sh" # Completed string, simple one.
195 exec_cmd_no_error(node, cmd, sudo=True, disconnect=True)
198 def get_l3fwd_pmd_options(**kwargs):
199 """Create PMD parameters options for l3fwd (without --).
201 :param kwargs: List of l3fwd parameters.
203 :returns: PMD parameters.
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
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
215 options.add_equals_from_dict(
216 u"eth-dest", u"pmd_eth_dest_1", kwargs
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
222 # Enables jumbo frames.
223 options.add_if_from_dict(
224 u"enable-jumbo", u"pmd_enable_jumbo", kwargs, False
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
233 def get_l3fwd_args(**kwargs):
234 """Get DPDK l3fwd command line arguments.
236 :param kwargs: Key-value l3fwd parameters.
238 :returns: Command line string.
241 options = OptionString()
242 options.extend(DpdkUtil.get_eal_options(**kwargs))
244 options.extend(DpdkUtil.get_l3fwd_pmd_options(**kwargs))