]> git.proxmox.com Git - mirror_ifupdown2.git/blame - pkg/scheduler.py
prefix ethtool attributes with "link-" to be compatible with
[mirror_ifupdown2.git] / pkg / scheduler.py
CommitLineData
a6f80f0e 1#!/usr/bin/python
3e8ee54f 2#
3# Copyright 2013. Cumulus Networks, Inc.
4# Author: Roopa Prabhu, roopa@cumulusnetworks.com
5#
6# ifaceScheduler --
7# interface scheduler
8#
a6f80f0e 9
a6f80f0e 10from statemanager import *
11from iface import *
12from graph import *
13from collections import deque
14from collections import OrderedDict
a6f80f0e 15import logging
d08d5f54 16import traceback
69f58278 17import sys
a6f80f0e 18from graph import *
19from collections import deque
20from threading import *
21from ifupdownbase import *
22
d08d5f54 23class ifaceSchedulerFlags():
c798b0f4 24 INORDER = 0x1
25 POSTORDER = 0x2
d08d5f54 26
be0b20f2 27class ifaceScheduler():
28 """ scheduler functions to schedule configuration of interfaces.
a6f80f0e 29
a6f80f0e 30 supports scheduling of interfaces serially in plain interface list
31 or dependency graph format.
99b212b0 32
33 Algo:
34 - run topological sort on the iface objects
35 - In the sorted iface object list, pick up interfaces with no parents
36 and run ops on them and their children.
37 - If operation is up and user gave interface list (ie not all)
38 option, also see if there were upper-devices and run ops on them.
39 - if operation is down, dont down the interface if it still
40 has upperifaces present. The down operation is executed when the
41 last upperiface goes away. If force option is set, this rule does not
42 apply.
43 - run ops calls addon modules run operation passing the iface
44 object and op to each module.
45 - ops are [pre-up, up, post-up, pre-down, down,
46 post-down, query-running, query-check]
a6f80f0e 47 """
48
c798b0f4 49 _STATE_CHECK = True
50
be0b20f2 51 @classmethod
44a6ca06 52 def run_iface_op(cls, ifupdownobj, ifaceobj, op, cenv=None):
a6f80f0e 53 """ Runs sub operation on an interface """
62ddec8b 54 ifacename = ifaceobj.name
a6f80f0e 55
c798b0f4 56 if (cls._STATE_CHECK and
62ddec8b 57 (ifaceobj.state >= ifaceState.from_str(op)) and
58 (ifaceobj.status == ifaceStatus.SUCCESS)):
be0b20f2 59 ifupdownobj.logger.debug('%s: already in state %s' %(ifacename, op))
d08d5f54 60 return
20dd6242 61 if not ifupdownobj.ADDONS_ENABLE: return
44a6ca06
RP
62 if op == 'query-checkcurr':
63 query_ifaceobj=ifupdownobj.create_n_save_ifaceobjcurr(ifaceobj)
be0b20f2 64 for mname in ifupdownobj.module_ops.get(op):
37c0543d 65 m = ifupdownobj.modules.get(mname)
a6f80f0e 66 err = 0
67 try:
d08d5f54 68 if hasattr(m, 'run'):
a690dfae 69 msg = ('%s: %s : running module %s' %(ifacename, op, mname))
739f665b 70 if op == 'query-checkcurr':
d08d5f54 71 # Dont check curr if the interface object was
37c0543d 72 # auto generated
d08d5f54 73 if (ifaceobj.priv_flags & ifupdownobj.NOCONFIG):
37c0543d 74 continue
a690dfae 75 ifupdownobj.logger.debug(msg)
44a6ca06 76 m.run(ifaceobj, op, query_ifaceobj)
a6f80f0e 77 else:
a690dfae 78 ifupdownobj.logger.debug(msg)
d08d5f54 79 m.run(ifaceobj, op)
a6f80f0e 80 except Exception, e:
81 err = 1
be0b20f2 82 ifupdownobj.log_error(str(e))
a6f80f0e 83 finally:
31a5f4c3 84 if err:
85 ifaceobj.set_state_n_status(ifaceState.from_str(op),
86 ifaceStatus.ERROR)
d08d5f54 87 else:
31a5f4c3 88 ifaceobj.set_state_n_status(ifaceState.from_str(op),
89 ifaceStatus.SUCCESS)
6bd7fc74 90
91 if ifupdownobj.COMPAT_EXEC_SCRIPTS:
92 # execute /etc/network/ scripts
93 for mname in ifupdownobj.script_ops.get(op, []):
94 ifupdownobj.logger.debug('%s: %s : running script %s'
d08d5f54 95 %(ifacename, op, mname))
6bd7fc74 96 try:
97 ifupdownobj.exec_command(mname, cmdenv=cenv)
98 except Exception, e:
99 ifupdownobj.log_error(str(e))
37c0543d 100
be0b20f2 101 @classmethod
923290bd 102 def run_iface_list_ops(cls, ifupdownobj, ifaceobjs, ops):
103 """ Runs all operations on a list of interface
104 configurations for the same interface
105 """
a690dfae 106 # minor optimization. If operation is 'down', proceed only
107 # if interface exists in the system
923290bd 108 ifacename = ifaceobjs[0].name
109 if ('down' in ops[0] and
110 not ifupdownobj.link_exists(ifacename)):
525f0a30 111 ifupdownobj.logger.debug('%s: does not exist' %ifacename)
923290bd 112 # run posthook before you get out of here, so that
113 # appropriate cleanup is done
114 posthookfunc = ifupdownobj.sched_hooks.get('posthook')
115 if posthookfunc:
116 for ifaceobj in ifaceobjs:
117 ifaceobj.status = ifaceStatus.SUCCESS
118 posthookfunc(ifupdownobj, ifaceobj, 'down')
a690dfae 119 return
923290bd 120 for op in ops:
121 # first run ifupdownobj handlers. This is good enough
122 # for the first object in the list
123 handler = ifupdownobj.ops_handlers.get(op)
124 if handler:
125 if (not ifaceobjs[0].addr_method or
126 (ifaceobjs[0].addr_method and
127 ifaceobjs[0].addr_method != 'manual')):
128 handler(ifupdownobj, ifaceobjs[0])
129 for ifaceobj in ifaceobjs:
130 cls.run_iface_op(ifupdownobj, ifaceobj, op,
923290bd 131 cenv=ifupdownobj.generate_running_env(ifaceobj, op)
132 if ifupdownobj.COMPAT_EXEC_SCRIPTS else None)
133 posthookfunc = ifupdownobj.sched_hooks.get('posthook')
134 if posthookfunc:
135 posthookfunc(ifupdownobj, ifaceobj, op)
21c7daa7 136
137 @classmethod
fa3da4be 138 def _check_upperifaces(cls, ifupdownobj, ifaceobj, ops, parent,
139 followdependents=False):
99b212b0 140 """ Check if upperifaces are hanging off us and help caller decide
141 if he can proceed with the ops on this device
21c7daa7 142
99b212b0 143 Returns True or False indicating the caller to proceed with the
144 operation.
145 """
86fc62e2 146 # proceed only for down operation
147 if 'down' not in ops[0]:
148 return True
149
65c48517 150 if (ifupdownobj.FORCE or
151 not ifupdownobj.ADDONS_ENABLE or
86fc62e2 152 (not ifupdownobj.is_ifaceobj_noconfig(ifaceobj) and
153 ifupdownobj.config.get('warn_on_ifdown', '0') == '0')):
99b212b0 154 return True
65c48517 155
62ddec8b 156 ulist = ifaceobj.upperifaces
99b212b0 157 if not ulist:
158 return True
159 # Get the list of upper ifaces other than the parent
160 tmpulist = ([u for u in ulist if u != parent] if parent
161 else ulist)
162 if not tmpulist:
163 return True
164 # XXX: This is expensive. Find a cheaper way to do this.
165 # if any of the upperdevs are present,
166 # return false to the caller to skip this interface
167 for u in tmpulist:
168 if ifupdownobj.link_exists(u):
169 if not ifupdownobj.ALL:
86fc62e2 170 if ifupdownobj.is_ifaceobj_noconfig(ifaceobj):
171 ifupdownobj.logger.info('%s: skipping interface down,'
172 %ifaceobj.name + ' upperiface %s still around ' %u)
173 else:
174 ifupdownobj.logger.warn('%s: skipping interface down,'
175 %ifaceobj.name + ' upperiface %s still around ' %u)
99b212b0 176 return False
21c7daa7 177 return True
178
be0b20f2 179 @classmethod
180 def run_iface_graph(cls, ifupdownobj, ifacename, ops, parent=None,
d08d5f54 181 order=ifaceSchedulerFlags.POSTORDER,
182 followdependents=True):
6ef5bfa2 183 """ runs interface by traversing all nodes rooted at itself """
184
d08d5f54 185 # Each ifacename can have a list of iface objects
31a5f4c3 186 ifaceobjs = ifupdownobj.get_ifaceobjs(ifacename)
187 if not ifaceobjs:
d08d5f54 188 raise Exception('%s: not found' %ifacename)
a6f80f0e 189
d08d5f54 190 for ifaceobj in ifaceobjs:
ca3f4fc7 191 if not cls._check_upperifaces(ifupdownobj, ifaceobj,
192 ops, parent, followdependents):
21c7daa7 193 return
f3215127 194
923290bd 195 # If inorder, run the iface first and then its dependents
196 if order == ifaceSchedulerFlags.INORDER:
197 cls.run_iface_list_ops(ifupdownobj, ifaceobjs, ops)
198
199 for ifaceobj in ifaceobjs:
f3215127 200 # Run lowerifaces or dependents
62ddec8b 201 dlist = ifaceobj.lowerifaces
f3215127 202 if dlist:
fa3da4be 203 ifupdownobj.logger.debug('%s: found dependents %s'
204 %(ifacename, str(dlist)))
d08d5f54 205 try:
206 if not followdependents:
207 # XXX: this is yet another extra step,
208 # but is needed for interfaces that are
f3215127 209 # implicit dependents. even though we are asked to
210 # not follow dependents, we must follow the ones
211 # that dont have user given config. Because we own them
d08d5f54 212 new_dlist = [d for d in dlist
923290bd 213 if ifupdownobj.is_iface_noconfig(d)]
6ef5bfa2 214 if new_dlist:
be0b20f2 215 cls.run_iface_list(ifupdownobj, new_dlist, ops,
923290bd 216 ifacename, order, followdependents,
217 continueonfailure=False)
d08d5f54 218 else:
be0b20f2 219 cls.run_iface_list(ifupdownobj, dlist, ops,
f3215127 220 ifacename, order,
221 followdependents,
6ef5bfa2 222 continueonfailure=False)
d08d5f54 223 except Exception, e:
be0b20f2 224 if (ifupdownobj.ignore_error(str(e))):
d08d5f54 225 pass
226 else:
227 # Dont bring the iface up if children did not come up
a690dfae 228 ifaceobj.set_state_n_status(ifaceState.NEW,
923290bd 229 ifaceStatus.ERROR)
d08d5f54 230 raise
923290bd 231 if order == ifaceSchedulerFlags.POSTORDER:
232 cls.run_iface_list_ops(ifupdownobj, ifaceobjs, ops)
a6f80f0e 233
be0b20f2 234 @classmethod
235 def run_iface_list(cls, ifupdownobj, ifacenames,
f3215127 236 ops, parent=None, order=ifaceSchedulerFlags.POSTORDER,
6ef5bfa2 237 followdependents=True, continueonfailure=True):
d08d5f54 238 """ Runs interface list """
a6f80f0e 239
d08d5f54 240 for ifacename in ifacenames:
a6f80f0e 241 try:
be0b20f2 242 cls.run_iface_graph(ifupdownobj, ifacename, ops, parent,
d08d5f54 243 order, followdependents)
a6f80f0e 244 except Exception, e:
6ef5bfa2 245 if continueonfailure:
69f58278 246 if ifupdownobj.logger.isEnabledFor(logging.DEBUG):
247 traceback.print_tb(sys.exc_info()[2])
be0b20f2 248 ifupdownobj.logger.error('%s : %s' %(ifacename, str(e)))
d08d5f54 249 pass
250 else:
be0b20f2 251 if (ifupdownobj.ignore_error(str(e))):
6ef5bfa2 252 pass
253 else:
9dce3561 254 raise Exception('%s : (%s)' %(ifacename, str(e)))
d08d5f54 255
be0b20f2 256 @classmethod
c798b0f4 257 def run_iface_graph_upper(cls, ifupdownobj, ifacename, ops, parent=None,
258 followdependents=True, skip_root=False):
259 """ runs interface by traversing all nodes rooted at itself """
260
261 # Each ifacename can have a list of iface objects
262 ifaceobjs = ifupdownobj.get_ifaceobjs(ifacename)
263 if not ifaceobjs:
264 raise Exception('%s: not found' %ifacename)
265
923290bd 266 if not skip_root:
267 # run the iface first and then its upperifaces
268 cls.run_iface_list_ops(ifupdownobj, ifaceobjs, ops)
c798b0f4 269 for ifaceobj in ifaceobjs:
c798b0f4 270 # Run upperifaces
62ddec8b 271 ulist = ifaceobj.upperifaces
c798b0f4 272 if ulist:
fa3da4be 273 ifupdownobj.logger.debug('%s: found upperifaces %s'
274 %(ifacename, str(ulist)))
c798b0f4 275 try:
276 cls.run_iface_list_upper(ifupdownobj, ulist, ops,
277 ifacename,
278 followdependents,
279 continueonfailure=True)
280 except Exception, e:
281 if (ifupdownobj.ignore_error(str(e))):
282 pass
283 else:
284 raise
285
286 @classmethod
287 def run_iface_list_upper(cls, ifupdownobj, ifacenames,
288 ops, parent=None, followdependents=True,
289 continueonfailure=True, skip_root=False):
290 """ Runs interface list """
291
292 for ifacename in ifacenames:
293 try:
294 cls.run_iface_graph_upper(ifupdownobj, ifacename, ops, parent,
295 followdependents, skip_root)
296 except Exception, e:
297 if continueonfailure:
298 if ifupdownobj.logger.isEnabledFor(logging.DEBUG):
299 traceback.print_tb(sys.exc_info()[2])
300 ifupdownobj.logger.error('%s : %s' %(ifacename, str(e)))
301 pass
302 else:
303 if (ifupdownobj.ignore_error(str(e))):
304 pass
305 else:
9dce3561 306 raise Exception('%s : (%s)' %(ifacename, str(e)))
c798b0f4 307
308 @classmethod
309 def sched_ifaces(cls, ifupdownobj, ifacenames, ops,
310 dependency_graph=None, indegrees=None,
d08d5f54 311 order=ifaceSchedulerFlags.POSTORDER,
312 followdependents=True):
313 """ Runs iface dependeny graph by visiting all the nodes
314
315 Parameters:
316 -----------
317 ifupdownobj : ifupdown object (used for getting and updating iface
318 object state)
319 dependency_graph : dependency graph in adjacency list
320 format (contains more than one dependency graph)
321 ops : list of operations to perform eg ['pre-up', 'up', 'post-up']
322
323 indegrees : indegree array if present is used to determine roots
324 of the graphs in the dependency_graph
325 """
c798b0f4 326
327 if not ifupdownobj.ALL or not followdependents or len(ifacenames) == 1:
ca3f4fc7 328 # If there is any interface that does exist, maybe it is a
329 # logical interface and we have to followupperifaces
330 followupperifaces = (True if
331 [i for i in ifacenames
923290bd 332 if not ifupdownobj.link_exists(i)]
333 else False)
c798b0f4 334 cls.run_iface_list(ifupdownobj, ifacenames, ops,
335 parent=None,order=order,
336 followdependents=followdependents)
ca3f4fc7 337 if (not ifupdownobj.ALL and
923290bd 338 (followdependents or followupperifaces) and
339 'up' in ops[0]):
c798b0f4 340 # If user had given a set of interfaces to bring up
341 # try and execute 'up' on the upperifaces
342 ifupdownobj.logger.info('running upperifaces if available')
343 cls._STATE_CHECK = False
344 cls.run_iface_list_upper(ifupdownobj, ifacenames, ops,
345 skip_root=True)
346 cls._STATE_CHECK = True
347 return
f3215127 348
44a6ca06
RP
349 if ifupdownobj.config.get('skip_ifacesort', '0') == '1':
350 # This is a backdoor to skip sorting of interfaces, if required
351 cls.run_iface_list(ifupdownobj, ifacenames, ops,
352 parent=None,order=order,
353 followdependents=followdependents)
354 return
355
356 run_queue = []
c798b0f4 357 # Get a sorted list of all interfaces
20dd6242 358 if not indegrees:
f3215127 359 indegrees = OrderedDict()
d08d5f54 360 for ifacename in dependency_graph.keys():
f3215127 361 indegrees[ifacename] = ifupdownobj.get_iface_refcnt(ifacename)
f3215127 362 sorted_ifacenames = graph.topological_sort_graphs_all(dependency_graph,
f90dd038 363 indegrees)
be0b20f2 364 ifupdownobj.logger.debug('sorted ifacenames %s : '
365 %str(sorted_ifacenames))
f3215127 366
c798b0f4 367 # From the sorted list, pick interfaces that user asked
368 # and those that dont have any dependents first
369 [run_queue.append(ifacename)
370 for ifacename in sorted_ifacenames
371 if ifacename in ifacenames and
372 not indegrees.get(ifacename)]
d08d5f54 373
20dd6242 374 ifupdownobj.logger.debug('graph roots (interfaces that dont have '
375 'dependents):' + ' %s' %str(run_queue))
c798b0f4 376 cls.run_iface_list(ifupdownobj, run_queue, ops,
be0b20f2 377 parent=None,order=order,
378 followdependents=followdependents)