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
20 class DpdkUtil(object):
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='-')
34 # Set the hexadecimal bitmask of the cores to run on.
35 options.add_with_value_from_dict('l', 'eal_corelist', kwargs)
37 options.add_with_value('-master-lcore', '0')
38 # Load an external driver. Multiple -d options are allowed.
39 options.add_with_value_if_from_dict(
40 'd', '/usr/lib/librte_pmd_virtio.so', 'eal_driver', kwargs, True)
41 options.add_if_from_dict(
42 '-in-memory', 'eal_in_memory', kwargs, False)
46 def get_pmd_options(**kwargs):
47 """Create PMD parameters options (without --).
49 :param kwargs: List of testpmd parameters.
51 :returns: PMD parameters.
54 options = OptionString(prefix='--')
55 # Set the forwarding mode: io, mac, mac_retry, mac_swap, flowgen,
56 # rxonly, txonly, csum, icmpecho, ieee1588
57 options.add_equals_from_dict(
58 'forward-mode', 'pmd_fwd_mode', kwargs, 'io')
59 # Set the number of packets per burst to N.
60 options.add_equals('burst', 64)
61 # Set the number of descriptors in the TX rings to N.
62 options.add_equals_from_dict('txd', 'pmd_txd', kwargs, 1024)
63 # Set the number of descriptors in the RX rings to N.
64 options.add_equals_from_dict('rxd', 'pmd_rxd', kwargs, 1024)
65 # Set the number of queues in the TX to N.
66 options.add_equals_from_dict('txq', 'pmd_txq', kwargs, 1)
67 # Set the number of queues in the RX to N.
68 options.add_equals_from_dict('rxq', 'pmd_rxq', kwargs, 1)
69 # Set the hexadecimal bitmask of offloads.
70 options.add_equals_from_dict('tx-offloads', 'pmd_tx_offloads', kwargs)
71 # Set the number of mbufs to be allocated in the mbuf pools.
72 options.add_equals_from_dict('total-num-mbufs', 'pmd_num_mbufs', kwargs)
73 # Disable hardware VLAN.
74 options.add_if_from_dict(
75 'disable-hw-vlan', 'pmd_disable_hw_vlan', kwargs, True)
76 # Set the MAC address XX:XX:XX:XX:XX:XX of the peer port N
77 options.add_equals_from_dict('eth-peer', 'pmd_eth_peer_0', kwargs)
78 options.add_equals_from_dict('eth-peer', 'pmd_eth_peer_1', kwargs)
79 # Set the max packet length.
80 options.add_equals_from_dict('max-pkt-len', 'pmd_max_pkt_len', kwargs)
81 # Set the number of forwarding cores based on coremask.
82 options.add_equals_from_dict('nb-cores', 'pmd_nb_cores', kwargs)
86 def get_testpmd_cmdline(**kwargs):
87 """Get DPDK testpmd command line arguments.
89 :param args: Key-value testpmd parameters.
91 :returns: Command line string.
94 options = OptionString()
95 options.add('testpmd')
96 options.extend(DpdkUtil.get_eal_options(**kwargs))
98 options.extend(DpdkUtil.get_pmd_options(**kwargs))
102 def dpdk_testpmd_start(node, **kwargs):
103 """Start DPDK testpmd app on VM node.
105 :param node: VM Node to start testpmd on.
106 :param args: Key-value testpmd parameters.
110 cmd_options = OptionString()
111 cmd_options.add("/start-testpmd.sh")
112 cmd_options.extend(DpdkUtil.get_eal_options(**kwargs))
113 cmd_options.add('--')
114 cmd_options.extend(DpdkUtil.get_pmd_options(**kwargs))
115 exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True)
118 def dpdk_testpmd_stop(node):
119 """Stop DPDK testpmd app on node.
121 :param node: Node to stop testpmd on.
125 cmd = "/stop-testpmd.sh" # Completed string, simpler than OptionString.
126 exec_cmd_no_error(node, cmd, sudo=True, disconnect=True)