b8c1d15a9fc5ea55ee5d4d73a97e09aaa4c27238
[csit.git] / resources / libraries / python / DpdkUtil.py
1 # Copyright (c) 2016 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         # Set the hexadecimal bitmask of the cores to run on.
32         eal_coremask = '-c {} '.format(args['eal_coremask'])\
33             if args.get('eal_coremask', '') else ''
34         # Set master core.
35         eal_master_core = '--master-lcore 0 '
36         # Set the number of memory channels to use.
37         eal_mem_channels = '-n {} '.format(args['eal_mem_channels'])\
38             if args.get('eal_mem_channels', '') else ''
39         # Set the memory to allocate on specific sockets (comma separated).
40         eal_socket_mem = '--socket-mem {} '.format(args['eal_socket_mem'])\
41             if args.get('eal_socket_mem', '') else ''
42         # Load an external driver. Multiple -d options are allowed.
43         eal_driver = '-d /usr/lib/librte_pmd_virtio.so '
44         eal_options = '-v '\
45             + eal_coremask\
46             + eal_master_core\
47             + eal_mem_channels\
48             + eal_socket_mem\
49             + eal_driver
50         return eal_options
51
52     @staticmethod
53     def get_pmd_options(**args):
54         """Create PMD parameters string.
55
56         :param args: List of testpmd parameters.
57         :type args: dict
58         :returns: PMD parameters string.
59         :rtype: str
60         """
61         # Set the forwarding mode: io, mac, mac_retry, mac_swap, flowgen,
62         # rxonly, txonly, csum, icmpecho, ieee1588
63         pmd_fwd_mode = '--forward-mode={} '.format(args['pmd_fwd_mode'])\
64             if args.get('pmd_fwd_mode', '') 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(args.get('pmd_txd', '256')) \
69             if args.get('pmd_txd', '256') else ''
70         # Set the number of descriptors in the RX rings to N.
71         pmd_rxd = '--rxd={} '.format(args.get('pmd_rxd', '256')) \
72             if args.get('pmd_rxd', '256') else ''
73         # Set the number of queues in the TX to N.
74         pmd_txq = '--txq={} '.format(args.get('pmd_txq', '1')) \
75             if args.get('pmd_txq', '1') else ''
76         # Set the number of queues in the RX to N.
77         pmd_rxq = '--rxq={} '.format(args.get('pmd_rxq', '1')) \
78             if args.get('pmd_rxq', '1') else ''
79         # Set the hexadecimal bitmask of TX queue flags.
80         pmd_txqflags = '--txqflags=0xf00 '
81         # Set the number of mbufs to be allocated in the mbuf pools.
82         pmd_total_num_mbufs = '--total-num-mbufs={} '.format(
83             args['pmd_num_mbufs']) if args.get('pmd_num_mbufs', '') else ''
84         # Set the max packet length.
85         pmd_max_pkt_len = "--max-pkt-len={0}".format(args["pmd_max_pkt_len"]) \
86             if args.get("pmd_max_pkt_len", "") else ""
87         # Set the hexadecimal bitmask of the ports for forwarding.
88         pmd_portmask = '--portmask={} '.format(args['pmd_portmask'])\
89             if args.get('pmd_portmask', '') else ''
90         # Disable hardware VLAN.
91         pmd_disable_hw_vlan = '--disable-hw-vlan '\
92             if args.get('pmd_disable_hw_vlan', '') else ''
93         # Disable RSS (Receive Side Scaling).
94         pmd_disable_rss = '--disable-rss '\
95             if args.get('pmd_disable_rss', '') else ''
96         # Set the MAC address XX:XX:XX:XX:XX:XX of the peer port N
97         pmd_eth_peer_0 = '--eth-peer={} '.format(args['pmd_eth_peer_0'])\
98             if args.get('pmd_eth_peer_0', '') else ''
99         pmd_eth_peer_1 = '--eth-peer={} '.format(args['pmd_eth_peer_1'])\
100             if args.get('pmd_eth_peer_1', '') else ''
101         # Set the number of forwarding cores based on coremask.
102         pmd_nb_cores = '--nb-cores={} '.format(
103             bin(int(args['eal_coremask'], 0)).count('1')-1)\
104             if args.get('eal_coremask', '') else ''
105         pmd_options = '-- '\
106             + pmd_fwd_mode\
107             + pmd_burst\
108             + pmd_txd\
109             + pmd_rxd\
110             + pmd_txq\
111             + pmd_rxq\
112             + pmd_txqflags\
113             + pmd_total_num_mbufs\
114             + pmd_portmask\
115             + pmd_disable_hw_vlan\
116             + pmd_disable_rss\
117             + pmd_eth_peer_0\
118             + pmd_eth_peer_1\
119             + pmd_nb_cores\
120             + pmd_max_pkt_len
121         return pmd_options
122
123     @staticmethod
124     def dpdk_testpmd_start(node, **args):
125         """Start DPDK testpmd app on VM node.
126
127         :param node: VM Node to start testpmd on.
128         :param args: List of testpmd parameters.
129         :type node: dict
130         :type args: dict
131         :return: nothing
132         """
133         eal_options = DpdkUtil.get_eal_options(**args)
134         pmd_options = DpdkUtil.get_pmd_options(**args)
135
136         ssh = SSH()
137         ssh.connect(node)
138         cmd = "/start-testpmd.sh {0} {1}".format(eal_options, pmd_options)
139         exec_cmd_no_error(node, cmd, sudo=True)
140         ssh.disconnect(node)
141
142     @staticmethod
143     def dpdk_testpmd_stop(node):
144         """Stop DPDK testpmd app on node.
145
146         :param node: Node to stop testpmd on.
147         :type node: dict
148         :return: nothing
149         """
150         ssh = SSH()
151         ssh.connect(node)
152         cmd = "/stop-testpmd.sh"
153         exec_cmd_no_error(node, cmd, sudo=True)
154         ssh.disconnect(node)