Add output.xml with only INFO logging leve
[csit.git] / resources / libraries / python / DpdkUtil.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """Dpdk Utilities Library."""
15
16 from resources.libraries.python.ssh import SSH, exec_cmd_no_error
17
18
19 class DpdkUtil(object):
20     """Utilities for DPDK."""
21
22     @staticmethod
23     def get_eal_options(**args):
24         """Create EAL parameters string.
25
26         :param args: List of testpmd parameters.
27         :type args: dict
28         :returns: EAL parameters string.
29         :rtype: str
30         """
31         eal_version = '-v'
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 ''
36         # Set master core.
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 ''
41         # Run in memory.
42         eal_in_memory = '--in-memory'\
43             if args.get('eal_in_memory', False) else ''
44
45         return ' '.join([eal_version,
46                          eal_corelist,
47                          eal_master_core,
48                          eal_driver,
49                          eal_in_memory])
50
51     @staticmethod
52     def get_pmd_options(**args):
53         """Create PMD parameters string.
54
55         :param args: List of testpmd parameters.
56         :type args: dict
57         :returns: PMD parameters string.
58         :rtype: str
59         """
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 ''
108
109         return ' '.join([pmd_fwd_mode,
110                          pmd_burst,
111                          pmd_txd,
112                          pmd_rxd,
113                          pmd_txq,
114                          pmd_rxq,
115                          pmd_tx_offloads,
116                          pmd_total_num_mbufs,
117                          pmd_disable_hw_vlan,
118                          pmd_eth_peer_0,
119                          pmd_eth_peer_1,
120                          pmd_max_pkt_len,
121                          pmd_nb_cores])
122
123     @staticmethod
124     def get_testpmd_cmdline(**kwargs):
125         """Get DPDK testpmd command line arguments.
126
127         :param args: Key-value testpmd parameters.
128         :type args: dict
129         :returns: Command line string.
130         :rtype: str
131         """
132         eal_options = DpdkUtil.get_eal_options(**kwargs)
133         pmd_options = DpdkUtil.get_pmd_options(**kwargs)
134
135         return 'testpmd {0} -- {1}'.format(eal_options, pmd_options)
136
137     @staticmethod
138     def dpdk_testpmd_start(node, **kwargs):
139         """Start DPDK testpmd app on VM node.
140
141         :param node: VM Node to start testpmd on.
142         :param args: Key-value testpmd parameters.
143         :type node: dict
144         :type args: dict
145         :returns: nothing
146         """
147         eal_options = DpdkUtil.get_eal_options(**kwargs)
148         pmd_options = DpdkUtil.get_pmd_options(**kwargs)
149
150         ssh = SSH()
151         ssh.connect(node)
152         cmd = "/start-testpmd.sh {0} -- {1}".format(eal_options, pmd_options)
153         exec_cmd_no_error(node, cmd, sudo=True)
154         ssh.disconnect(node)
155
156     @staticmethod
157     def dpdk_testpmd_stop(node):
158         """Stop DPDK testpmd app on node.
159
160         :param node: Node to stop testpmd on.
161         :type node: dict
162         :returns: nothing
163         """
164         ssh = SSH()
165         ssh.connect(node)
166         cmd = "/stop-testpmd.sh"
167         exec_cmd_no_error(node, cmd, sudo=True)
168         ssh.disconnect(node)