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_if_from_dict(
71 'txqflags', '0xf00', 'pmd_tx_offloads', kwargs, True)
72 # Set the number of mbufs to be allocated in the mbuf pools.
73 options.add_equals_from_dict('total-num-mbufs', 'pmd_num_mbufs', kwargs)
74 # Disable hardware VLAN.
75 options.add_if_from_dict(
76 'disable-hw-vlan', 'pmd_disable_hw_vlan', kwargs, True)
77 # Set the MAC address XX:XX:XX:XX:XX:XX of the peer port N
78 options.add_equals_from_dict('eth-peer', 'pmd_eth_peer_0', kwargs)
79 options.add_equals_from_dict('eth-peer', 'pmd_eth_peer_1', kwargs)
80 # Set the max packet length.
81 options.add_equals_from_dict('max-pkt-len', 'pmd_max_pkt_len', kwargs)
82 # Set the number of forwarding cores based on coremask.
83 options.add_equals_from_dict('nb-cores', 'pmd_nb_cores', kwargs)
87 def get_testpmd_cmdline(**kwargs):
88 """Get DPDK testpmd command line arguments.
90 :param args: Key-value testpmd parameters.
92 :returns: Command line string.
95 options = OptionString()
96 options.add('testpmd')
97 options.extend(DpdkUtil.get_eal_options(**kwargs))
99 options.extend(DpdkUtil.get_pmd_options(**kwargs))
103 def dpdk_testpmd_start(node, **kwargs):
104 """Start DPDK testpmd app on VM node.
106 :param node: VM Node to start testpmd on.
107 :param args: Key-value testpmd parameters.
111 cmd_options = OptionString()
112 cmd_options.add("/start-testpmd.sh")
113 cmd_options.extend(DpdkUtil.get_eal_options(**kwargs))
114 cmd_options.add('--')
115 cmd_options.extend(DpdkUtil.get_pmd_options(**kwargs))
116 exec_cmd_no_error(node, cmd_options, sudo=True, disconnect=True)
119 def dpdk_testpmd_stop(node):
120 """Stop DPDK testpmd app on node.
122 :param node: Node to stop testpmd on.
126 cmd = "/stop-testpmd.sh" # Completed string, simpler than OptionString.
127 exec_cmd_no_error(node, cmd, sudo=True, disconnect=True)