]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/addons/ethtool.py
579fdfae7c4a58b445219cf89f46ab6c4b53d262
[mirror_ifupdown2.git] / ifupdown2 / addons / ethtool.py
1 #!/usr/bin/python
2 #
3 # Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
5 #
6
7 try:
8 from ipaddr import IPNetwork
9 from sets import Set
10 from ifupdown.iface import *
11 from ifupdownaddons.modulebase import moduleBase
12 from ifupdownaddons.iproute2 import iproute2
13 except ImportError, e:
14 raise ImportError (str(e) + "- required module not found")
15
16 class ethtool(moduleBase):
17 """ ifupdown2 addon module to configure ethtool attributes """
18
19 _modinfo = {'mhelp' : 'ethtool configuration module for interfaces',
20 'attrs': {
21 'link-speed' :
22 {'help' : 'set link speed',
23 'example' : ['link-speed 1000']},
24 'link-duplex' :
25 {'help': 'set link duplex',
26 'example' : ['link-duplex full'],
27 'validvals' : ['half', 'full'],
28 'default' : 'half'},
29 'link-autoneg' :
30 {'help': 'set autonegotiation',
31 'example' : ['link-autoneg on'],
32 'validvals' : ['on', 'off'],
33 'default' : 'off'}}}
34
35 def __init__(self, *args, **kargs):
36 moduleBase.__init__(self, *args, **kargs)
37 self.ipcmd = None
38
39 def _post_up(self, ifaceobj):
40 if not self.ipcmd.link_exists(ifaceobj.name):
41 return
42 cmd = ''
43 attrval = ifaceobj.get_attr_value_first('link-speed')
44 if attrval:
45 cmd += ' speed %s' %attrval
46 attrval = ifaceobj.get_attr_value_first('link-duplex')
47 if attrval:
48 cmd += ' duplex %s' %attrval
49 attrval = ifaceobj.get_attr_value_first('link-autoneg')
50 if attrval:
51 cmd += ' autoneg %s' %attrval
52 if cmd:
53 try:
54 cmd = 'ethtool -s %s %s' %(ifaceobj.name, cmd)
55 self.exec_command(cmd)
56 except Exception, e:
57 ifaceobj.status = ifaceStatus.ERROR
58 self.log_warn('%s: %s' %(ifaceobj.name, str(e)))
59
60 def _query_check(self, ifaceobj, ifaceobjcurr):
61 """
62 Advertised auto-negotiation: No
63 Speed: 1000Mb/s
64 Duplex: Full"""
65 ethtool_attrs = self.dict_key_subset(ifaceobj.config,
66 self.get_mod_attrs())
67 if not ethtool_attrs:
68 return
69 try:
70 speed = ifaceobj.get_attr_value_first('link-speed')
71 if speed:
72 running_speed = self.read_file_oneline(
73 '/sys/class/net/%s/speed' %ifaceobj.name)
74 if running_speed and running_speed != speed:
75 ifaceobjcurr.update_config_with_status('link-speed',
76 running_speed, 1)
77 else:
78 ifaceobjcurr.update_config_with_status('link-speed',
79 running_speed, 0)
80 duplex = ifaceobj.get_attr_value_first('link-duplex')
81 if duplex:
82 running_duplex = self.read_file_oneline(
83 '/sys/class/net/%s/duplex' %ifaceobj.name)
84 if running_duplex and running_duplex != duplex:
85 ifaceobjcurr.update_config_with_status('link-duplex',
86 running_duplex, 1)
87 else:
88 ifaceobjcurr.update_config_with_status('link-duplex',
89 running_duplex, 0)
90 except Exception:
91 pass
92 return
93
94 def _query_running(self, ifaceobjrunning):
95 return
96
97 _run_ops = {'post-up' : _post_up,
98 'query-checkcurr' : _query_check,
99 'query-running' : _query_running }
100
101 def get_ops(self):
102 """ returns list of ops supported by this module """
103 return self._run_ops.keys()
104
105 def _init_command_handlers(self):
106 if not self.ipcmd:
107 self.ipcmd = iproute2(**self.get_flags())
108
109 def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args):
110 """ run ethtool configuration on the interface object passed as
111 argument
112
113 Args:
114 **ifaceobj** (object): iface object
115
116 **operation** (str): any of 'post-up', 'query-checkcurr',
117 'query-running'
118 Kwargs:
119 **query_ifaceobj** (object): query check ifaceobject. This is only
120 valid when op is 'query-checkcurr'. It is an object same as
121 ifaceobj, but contains running attribute values and its config
122 status. The modules can use it to return queried running state
123 of interfaces. status is success if the running state is same
124 as user required state in ifaceobj. error otherwise.
125 """
126 op_handler = self._run_ops.get(operation)
127 if not op_handler:
128 return
129 self._init_command_handlers()
130 if operation == 'query-checkcurr':
131 op_handler(self, ifaceobj, query_ifaceobj)
132 else:
133 op_handler(self, ifaceobj)