]> git.proxmox.com Git - mirror_ifupdown2.git/blob - addons/ethtool.py
Move ifupdown2addons into ifupdown2 pacakge
[mirror_ifupdown2.git] / 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 return
62
63 def _query_running(self, ifaceobjrunning):
64 return
65
66 _run_ops = {'post-up' : _post_up,
67 'query-checkcurr' : _query_check,
68 'query-running' : _query_running }
69
70 def get_ops(self):
71 """ returns list of ops supported by this module """
72 return self._run_ops.keys()
73
74 def _init_command_handlers(self):
75 if not self.ipcmd:
76 self.ipcmd = iproute2(**self.get_flags())
77
78 def run(self, ifaceobj, operation, query_ifaceobj=None):
79 """ run ethtool configuration on the interface object passed as
80 argument
81
82 Args:
83 **ifaceobj** (object): iface object
84
85 **operation** (str): any of 'post-up', 'query-checkcurr',
86 'query-running'
87 Kwargs:
88 **query_ifaceobj** (object): query check ifaceobject. This is only
89 valid when op is 'query-checkcurr'. It is an object same as
90 ifaceobj, but contains running attribute values and its config
91 status. The modules can use it to return queried running state
92 of interfaces. status is success if the running state is same
93 as user required state in ifaceobj. error otherwise.
94 """
95 op_handler = self._run_ops.get(operation)
96 if not op_handler:
97 return
98 self._init_command_handlers()
99 if operation == 'query-checkcurr':
100 op_handler(self, ifaceobj, query_ifaceobj)
101 else:
102 op_handler(self, ifaceobj)