1
0
mirror of https://github.com/CumulusNetworks/ifupdown2.git synced 2024-05-06 15:54:50 +00:00
Roopa Prabhu 2ec2da0cf9 new ifupdown2 link module to create dummy devices
Ticket: CM-3525
Reviewed By: CCR-3326
Testing Done: Tested creating dummy devices using ifupdown2

This is modification to gospos loopback module. It solves the same
purpose ie using linux dummy device like a loopback device but there were
objections on calling it loopback so i have renamed it to link and i have changed it
into a generic module that can do any 'ip link'. Can be extended for
link args in the future.

below example creates a loopy device

$ifquery loopy
auto loopy
iface loopy
link-type dummy

$ifup loopy
$ifquery -c loopy
auto loopy
iface loopy [pass]
link-type dummy [pass]

(cherry picked from commit 1151420408a53c106d29183a1e0da5562c8b03a3)
2015-08-29 07:02:50 -07:00

77 lines
2.8 KiB
Python

#!/usr/bin/python
# This should be pretty simple and might not really even need to exist.
# The key is that we need to call link_create with a type of "dummy"
# since that will translate to 'ip link add loopbackX type dummy'
# The config file should probably just indicate that the type is
# loopback or dummy.
from ifupdown.iface import *
from ifupdownaddons.modulebase import moduleBase
from ifupdownaddons.iproute2 import iproute2
import logging
class link(moduleBase):
_modinfo = {'mhelp' : 'create/configure link types. similar to ip-link',
'attrs' : {
'link-type' :
{'help' : 'type of link as in \'ip link\' command.',
'example' : ['link-type <dummy|veth>']}}}
def __init__(self, *args, **kargs):
moduleBase.__init__(self, *args, **kargs)
self.ipcmd = None
def _is_my_interface(self, ifaceobj):
if ifaceobj.get_attr_value_first('link-type'):
return True
return False
def _up(self, ifaceobj):
self.ipcmd.link_create(ifaceobj.name,
ifaceobj.get_attr_value_first('link-type'))
def _down(self, ifaceobj):
if not self.PERFMODE and not self.ipcmd.link_exists(ifaceobj.name):
return
try:
self.ipcmd.link_delete(ifaceobj.name)
except Exception, e:
self.log_warn(str(e))
def _query_check(self, ifaceobj, ifaceobjcurr):
if not self.ipcmd.link_exists(ifaceobj.name):
ifaceobjcurr.update_config_with_status('link-type', 'None', 1)
else:
link_type = ifaceobj.get_attr_value_first('link-type')
if self.ipcmd.link_get_kind(ifaceobj.name) == link_type:
ifaceobjcurr.update_config_with_status('link-type',
link_type, 0)
else:
ifaceobjcurr.update_config_with_status('link-type',
link_type, 1)
_run_ops = {'pre-up' : _up,
'post-down' : _down,
'query-checkcurr' : _query_check}
def get_ops(self):
return self._run_ops.keys()
def _init_command_handlers(self):
if not self.ipcmd:
self.ipcmd = iproute2(**self.get_flags())
def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args):
op_handler = self._run_ops.get(operation)
if not op_handler:
return
if (operation != 'query-running' and
not self._is_my_interface(ifaceobj)):
return
self._init_command_handlers()
if operation == 'query-checkcurr':
op_handler(self, ifaceobj, query_ifaceobj)
else:
op_handler(self, ifaceobj)