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:
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
46 options.add_with_value(
47 u"-master-lcore", u"0"
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
53 options.add_if_from_dict(
54 u"-in-memory", u"eal_in_memory", kwargs, False
59 def get_testpmd_pmd_options(**kwargs):
60 """Create PMD parameters options for testpmd (without --).
62 :param kwargs: List of testpmd parameters.
64 :returns: PMD parameters.
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"
73 # Set the number of packets per burst to N.
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
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
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
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
93 # Set the hexadecimal bitmask of offloads.
94 options.add_equals_from_dict(
95 u"tx-offloads", u"pmd_tx_offloads", kwargs, u"0x0"
97 # Enables numa aware allocation of mbufs.
98 options.add_if_from_dict(
99 u"numa", u"pmd_numa", kwargs, True
102 options.add_if_from_dict(
103 u"auto-start", u"pmd_auto_start", kwargs, True
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
109 # Set the number of forwarding ports.
110 options.add_equals_from_dict(
111 u"nb-ports", u"pmd_nb_ports", kwargs
113 # Set the hexadecimal bitmask of the ports used by the packet
115 options.add_equals_from_dict(
116 u"portmask", u"pmd_portmask", kwargs
118 # Disable link status check.
119 options.add_if_from_dict(
120 u"disable-link-check", u"pmd_disable_link_check", kwargs, True
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
126 options.add_equals_from_dict(
127 u"eth-peer", u"pmd_eth_peer_1", kwargs
129 # Set the max packet length.
130 options.add_equals_from_dict(
131 u"max-pkt-len", u"pmd_max_pkt_len", kwargs
133 # Set the max packet length.
134 options.add_equals_from_dict(
135 u"mbuf-size", u"pmd_mbuf_size", kwargs
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
144 def get_testpmd_args(**kwargs):
145 """Get DPDK testpmd command line arguments.
147 :param kwargs: Key-value testpmd parameters.
149 :returns: Command line string.
152 options = OptionString()
153 options.extend(DpdkUtil.get_eal_options(**kwargs))
155 options.extend(DpdkUtil.get_testpmd_pmd_options(**kwargs))
159 def get_testpmd_cmdline(**kwargs):
160 """Get DPDK testpmd command line arguments with testpmd command.
162 :param kwargs: Key-value testpmd parameters.
164 :returns: Command line string.
167 options = OptionString()
168 options.add(u"testpmd")
169 options.extend(DpdkUtil.get_eal_options(**kwargs))
171 options.extend(DpdkUtil.get_testpmd_pmd_options(**kwargs))
175 def dpdk_testpmd_start(node, **kwargs):
176 """Start DPDK testpmd app on VM node.
178 :param node: VM Node to start testpmd on.
179 :param kwargs: Key-value testpmd parameters.
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)
191 def dpdk_testpmd_stop(node):
192 """Stop DPDK testpmd app on node.
194 :param node: Node to stop testpmd on.
198 cmd = u"/stop-testpmd.sh" # Completed string, simple one.
199 exec_cmd_no_error(node, cmd, sudo=True, disconnect=True)
202 def get_l3fwd_pmd_options(**kwargs):
203 """Create PMD parameters options for l3fwd (without --).
205 :param kwargs: List of l3fwd parameters.
207 :returns: PMD parameters.
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
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
219 options.add_equals_from_dict(
220 u"eth-dest", u"pmd_eth_dest_1", kwargs
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
226 # Enables jumbo frames.
227 options.add_if_from_dict(
228 u"enable-jumbo", u"pmd_enable_jumbo", kwargs, False
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
237 def get_l3fwd_args(**kwargs):
238 """Get DPDK l3fwd command line arguments.
240 :param kwargs: Key-value l3fwd parameters.
242 :returns: Command line string.
245 options = OptionString()
246 options.extend(DpdkUtil.get_eal_options(**kwargs))
248 options.extend(DpdkUtil.get_l3fwd_pmd_options(**kwargs))