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.ssh import SSH, exec_cmd_no_error
19 class DpdkUtil(object):
20 """Utilities for DPDK."""
23 def get_eal_options(**args):
24 """Create EAL parameters string.
26 :param args: List of testpmd parameters.
28 :returns: EAL parameters string.
32 # Set the hexadecimal bitmask of the cores to run on.
33 eal_corelist = '-l {}'.format(
34 args.get('eal_corelist', ''))\
35 if args.get('eal_corelist', '') else ''
37 eal_master_core = '--master-lcore 0'
38 # Load an external driver. Multiple -d options are allowed.
39 eal_driver = '-d /usr/lib/librte_pmd_virtio.so'\
40 if args.get('eal_driver', True) else ''
42 eal_in_memory = '--in-memory'\
43 if args.get('eal_in_memory', False) else ''
45 return ' '.join([eal_version,
52 def get_pmd_options(**args):
53 """Create PMD parameters string.
55 :param args: List of testpmd parameters.
57 :returns: PMD parameters string.
60 # Set the forwarding mode: io, mac, mac_retry, mac_swap, flowgen,
61 # rxonly, txonly, csum, icmpecho, ieee1588
62 pmd_fwd_mode = '--forward-mode={}'.format(
63 args.get('pmd_fwd_mode', 'io')) \
64 if args.get('pmd_fwd_mode', 'io') else ''
65 # Set the number of packets per burst to N.
66 pmd_burst = '--burst=64'
67 # Set the number of descriptors in the TX rings to N.
68 pmd_txd = '--txd={}'.format(
69 args.get('pmd_txd', '1024')) \
70 if args.get('pmd_txd', '1024') else ''
71 # Set the number of descriptors in the RX rings to N.
72 pmd_rxd = '--rxd={}'.format(
73 args.get('pmd_rxd', '1024')) \
74 if args.get('pmd_rxd', '1024') else ''
75 # Set the number of queues in the TX to N.
76 pmd_txq = '--txq={}'.format(
77 args.get('pmd_txq', '1')) \
78 if args.get('pmd_txq', '1') else ''
79 # Set the number of queues in the RX to N.
80 pmd_rxq = '--rxq={}'.format(
81 args.get('pmd_rxq', '1')) \
82 if args.get('pmd_rxq', '1') else ''
83 # Set the hexadecimal bitmask of TX offloads.
84 pmd_tx_offloads = '--txqflags=0xf00'\
85 if args.get('pmd_tx_offloads', True) else ''
86 # Set the number of mbufs to be allocated in the mbuf pools.
87 pmd_total_num_mbufs = '--total-num-mbufs={}'.format(
88 args.get('pmd_num_mbufs', '')) \
89 if args.get('pmd_num_mbufs', '') else ''
90 # Set the max packet length.
91 pmd_max_pkt_len = '--max-pkt-len={}'.format(
92 args.get("pmd_max_pkt_len", "")) \
93 if args.get("pmd_max_pkt_len", "") else ""
94 # Disable hardware VLAN.
95 pmd_disable_hw_vlan = '--disable-hw-vlan'\
96 if args.get('pmd_disable_hw_vlan', True) else ''
97 # Set the MAC address XX:XX:XX:XX:XX:XX of the peer port N
98 pmd_eth_peer_0 = '--eth-peer={}'.format(
99 args.get('pmd_eth_peer_0', ''))\
100 if args.get('pmd_eth_peer_0', '') else ''
101 pmd_eth_peer_1 = '--eth-peer={}'.format(
102 args.get('pmd_eth_peer_1', ''))\
103 if args.get('pmd_eth_peer_1', '') else ''
104 # Set the number of forwarding cores based on coremask.
105 pmd_nb_cores = '--nb-cores={}'.format(
106 args.get('pmd_nb_cores', ''))\
107 if args.get('pmd_nb_cores', '') else ''
109 return ' '.join([pmd_fwd_mode,
124 def get_testpmd_cmdline(**kwargs):
125 """Get DPDK testpmd command line arguments.
127 :param args: Key-value testpmd parameters.
129 :returns: Command line string.
132 eal_options = DpdkUtil.get_eal_options(**kwargs)
133 pmd_options = DpdkUtil.get_pmd_options(**kwargs)
135 return 'testpmd {0} -- {1}'.format(eal_options, pmd_options)
138 def dpdk_testpmd_start(node, **kwargs):
139 """Start DPDK testpmd app on VM node.
141 :param node: VM Node to start testpmd on.
142 :param args: Key-value testpmd parameters.
147 eal_options = DpdkUtil.get_eal_options(**kwargs)
148 pmd_options = DpdkUtil.get_pmd_options(**kwargs)
152 cmd = "/start-testpmd.sh {0} -- {1}".format(eal_options, pmd_options)
153 exec_cmd_no_error(node, cmd, sudo=True)
157 def dpdk_testpmd_stop(node):
158 """Stop DPDK testpmd app on node.
160 :param node: Node to stop testpmd on.
166 cmd = "/stop-testpmd.sh"
167 exec_cmd_no_error(node, cmd, sudo=True)