c214a5687a88617d9753cfd579fd9d5f46c9dbaa
[bottlenecks.git] / vstf / vstf / agent / perf / ethtool.py
1 #!/usr/bin/python
2 # -*- coding: utf8 -*-
3 # author: wly
4 # date: 2015/11/12
5 # see license for license details
6
7 import vstf.common.utils as utils
8
9 __all__ = ["autoneg_on", "autoneg_off", "autoneg_query"]
10
11 _para_map = {
12     "Autonegotiate": ("-A", "-a", "autoneg"),
13     "RX": ("-A", "-a", "rx"),
14     "TX": ("-A", "-a", "tx"),
15 }
16
17
18 def autoneg_on(iface, nspace=None):
19     return _set(nspace, iface, Autonegotiate="on", RX="on", TX="on")
20
21
22 def autoneg_off(iface, nspace=None):
23     return _set(nspace, iface, Autonegotiate="off", RX="off", TX="off")
24
25
26 def autoneg_query(iface, nspace=None):
27     return _query(nspace, iface, "-a")
28
29
30 def _set(nspace, iface, **kwargs):
31     cmds = {}
32     for item, value in kwargs.items():
33         opt, _, key = _para_map[item]
34         cmds.setdefault(opt, [])
35         cmds[opt].append(key)
36         cmds[opt].append(value)
37
38     for key, value in cmds.items():
39         cmd = _namespace(nspace)
40         cmd += ["ethtool", key, iface] + value
41         utils.call(cmd)
42
43     return True
44
45
46 def _query(nspace, iface, item):
47     cmd = _namespace(nspace)
48     cmd += ["ethtool", item, iface]
49     return utils.check_output(cmd)
50
51
52 def _namespace(nspace):
53     result = ""
54     if nspace:
55         result = "ip netns exec %(namespace)s " % {"namespace": nspace}
56     return result.split()