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:
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(u"l", u"eal_corelist", kwargs)
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
42 options.add_if_from_dict(
43 u"-in-memory", u"eal_in_memory", kwargs, False
48 def get_pmd_options(**kwargs):
49 """Create PMD parameters options (without --).
51 :param kwargs: List of testpmd parameters.
53 :returns: PMD parameters.
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"
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
78 # Disable hardware VLAN.
79 options.add_if_from_dict(
80 u"disable-hw-vlan", u"pmd_disable_hw_vlan", kwargs, True
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)
92 def get_testpmd_cmdline(**kwargs):
93 """Get DPDK testpmd command line arguments.
95 :param kwargs: Key-value testpmd parameters.
97 :returns: Command line string.
100 options = OptionString()
101 options.add(u"testpmd")
102 options.extend(DpdkUtil.get_eal_options(**kwargs))
104 options.extend(DpdkUtil.get_pmd_options(**kwargs))
108 def dpdk_testpmd_start(node, **kwargs):
109 """Start DPDK testpmd app on VM node.
111 :param node: VM Node to start testpmd on.
112 :param kwargs: Key-value testpmd parameters.
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)
124 def dpdk_testpmd_stop(node):
125 """Stop DPDK testpmd app on node.
127 :param node: Node to stop testpmd on.
131 cmd = u"/stop-testpmd.sh" # Completed string, simple one.
132 exec_cmd_no_error(node, cmd, sudo=True, disconnect=True)