Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / agent / perf / ethtool.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import vstf.common.utils as utils
11
12 __all__ = ["autoneg_on", "autoneg_off", "autoneg_query"]
13
14 _para_map = {
15     "Autonegotiate": ("-A", "-a", "autoneg"),
16     "RX": ("-A", "-a", "rx"),
17     "TX": ("-A", "-a", "tx"),
18 }
19
20
21 def autoneg_on(iface, nspace=None):
22     return _set(nspace, iface, Autonegotiate="on", RX="on", TX="on")
23
24
25 def autoneg_off(iface, nspace=None):
26     return _set(nspace, iface, Autonegotiate="off", RX="off", TX="off")
27
28
29 def autoneg_query(iface, nspace=None):
30     return _query(nspace, iface, "-a")
31
32
33 def _set(nspace, iface, **kwargs):
34     cmds = {}
35     for item, value in kwargs.items():
36         opt, _, key = _para_map[item]
37         cmds.setdefault(opt, [])
38         cmds[opt].append(key)
39         cmds[opt].append(value)
40
41     for key, value in cmds.items():
42         cmd = _namespace(nspace)
43         cmd += ["ethtool", key, iface] + value
44         utils.call(cmd)
45
46     return True
47
48
49 def _query(nspace, iface, item):
50     cmd = _namespace(nspace)
51     cmd += ["ethtool", item, iface]
52     return utils.check_output(cmd)
53
54
55 def _namespace(nspace):
56     result = ""
57     if nspace:
58         result = "ip netns exec %(namespace)s " % {"namespace": nspace}
59     return result.split()