]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - addons/vlan.py
3 # Copyright 2014 Cumulus Networks, Inc. All rights reserved.
4 # Author: Roopa Prabhu, roopa@cumulusnetworks.com
7 from ifupdown
.iface
import *
8 from ifupdownaddons
.modulebase
import moduleBase
9 from ifupdownaddons
.iproute2
import iproute2
12 class vlan(moduleBase
):
13 """ ifupdown2 addon module to configure vlans """
15 _modinfo
= {'mhelp' : 'vlan module configures vlan interfaces.' +
16 'This module understands vlan interfaces with dot ' +
17 'notations. eg swp1.100. Vlan interfaces with any ' +
18 'other names need to have raw device and vlan id ' +
22 {'help' : 'vlan raw device'},
24 {'help' : 'vlan id'}}}
26 def __init__(self
, *args
, **kargs
):
27 moduleBase
.__init
__(self
, *args
, **kargs
)
30 def _is_vlan_device(self
, ifaceobj
):
31 vlan_raw_device
= ifaceobj
.get_attr_value_first('vlan-raw-device')
34 elif '.' in ifaceobj
.name
:
38 def _get_vlan_id(self
, ifaceobj
):
39 """ Derives vlanid from iface name
42 Returns 1 for ifname vlan0001 returns 1
43 Returns 1 for ifname vlan1
44 Returns 1 for ifname eth0.1
46 Returns -1 if vlan id cannot be determined
48 vid_str
= ifaceobj
.get_attr_value_first('vlan-id')
50 if vid_str
: return int(vid_str
)
54 if ifaceobj
.name
.startswith('vlan'):
55 vid_str
= ifaceobj
.name
[4:]
56 elif '.' in ifaceobj
.name
:
57 vid_str
= ifaceobj
.name
.split('.', 1)[1]
66 def _is_vlan_by_name(self
, ifacename
):
67 return '.' in ifacename
69 def _get_vlan_raw_device_from_ifacename(self
, ifacename
):
70 """ Returns vlan raw device from ifname
72 Returns eth0 for ifname eth0.100
74 Returns None if vlan raw device name cannot
77 vlist
= ifacename
.split('.', 1)
82 def _get_vlan_raw_device(self
, ifaceobj
):
83 vlan_raw_device
= ifaceobj
.get_attr_value_first('vlan-raw-device')
85 return vlan_raw_device
86 return self
._get
_vlan
_raw
_device
_from
_ifacename
(ifaceobj
.name
)
88 def get_dependent_ifacenames(self
, ifaceobj
, ifaceobjs_all
=None):
89 if not self
._is
_vlan
_device
(ifaceobj
):
91 return [self
._get
_vlan
_raw
_device
(ifaceobj
)]
93 def _up(self
, ifaceobj
):
94 vlanid
= self
._get
_vlan
_id
(ifaceobj
)
96 raise Exception('could not determine vlanid')
97 vlanrawdevice
= self
._get
_vlan
_raw
_device
(ifaceobj
)
99 raise Exception('could not determine vlan raw device')
100 self
.ipcmd
.link_create_vlan(ifaceobj
.name
,
101 vlanrawdevice
, vlanid
)
103 def _down(self
, ifaceobj
):
104 vlanid
= self
._get
_vlan
_id
(ifaceobj
)
106 raise Exception('could not determine vlanid')
107 vlan_raw_device
= self
._get
_vlan
_raw
_device
(ifaceobj
)
108 if not vlan_raw_device
:
109 raise Exception('could not determine vlan raw device')
110 if not self
.PERFMODE
and not self
.ipcmd
.link_exists(ifaceobj
.name
):
113 self
.ipcmd
.link_delete(ifaceobj
.name
)
115 self
.log_warn(str(e
))
117 def _query_check(self
, ifaceobj
, ifaceobjcurr
):
118 if not self
.ipcmd
.link_exists(ifaceobj
.name
):
119 ifaceobjcurr
.status
= ifaceStatus
.NOTFOUND
121 if not '.' in ifaceobj
.name
:
122 # if vlan name is not in the dot format, check its running state
123 (vlanrawdev
, vlanid
) = self
.ipcmd
.get_vlandev_attrs(ifaceobj
.name
)
124 if vlanrawdev
!= ifaceobj
.get_attr_value_first('vlan-raw-device'):
125 ifaceobjcurr
.update_config_with_status('vlan-raw-device',
128 ifaceobjcurr
.update_config_with_status('vlan-raw-device',
130 if vlanid
!= ifaceobj
.get_attr_value_first('vlan-id'):
131 ifaceobjcurr
.update_config_with_status('vlan-id', vlanid
, 1)
133 ifaceobjcurr
.update_config_with_status('vlan-id',
136 def _query_running(self
, ifaceobjrunning
):
137 if not self
.ipcmd
.link_exists(ifaceobjrunning
.name
):
138 if self
._is
_vlan
_by
_name
(ifaceobjrunning
.name
):
139 ifaceobjcurr
.status
= ifaceStatus
.NOTFOUND
141 if not self
.ipcmd
.get_vlandev_attrs(ifaceobjrunning
.name
):
143 # If vlan name is not in the dot format, get the
144 # vlan dev and vlan id
145 if not '.' in ifaceobjrunning
.name
:
146 (vlanrawdev
, vlanid
) = self
.ipcmd
.get_vlandev_attrs(ifaceobjrunning
.name
)
147 ifaceobjrunning
.update_config_dict({(k
, v
) for k
, v
in
148 {'vlan-raw-device' : vlanrawdev
,
149 'vlan-id' : vlanid
}.items()
152 _run_ops
= {'pre-up' : _up
,
154 'query-checkcurr' : _query_check
,
155 'query-running' : _query_running
}
158 """ returns list of ops supported by this module """
159 return self
._run
_ops
.keys()
161 def _init_command_handlers(self
):
163 self
.ipcmd
= iproute2(**self
.get_flags())
165 def run(self
, ifaceobj
, operation
, query_ifaceobj
=None):
166 """ run vlan configuration on the interface object passed as argument
169 **ifaceobj** (object): iface object
171 **operation** (str): any of 'pre-up', 'post-down', 'query-checkcurr',
174 **query_ifaceobj** (object): query check ifaceobject. This is only
175 valid when op is 'query-checkcurr'. It is an object same as
176 ifaceobj, but contains running attribute values and its config
177 status. The modules can use it to return queried running state
178 of interfaces. status is success if the running state is same
179 as user required state in ifaceobj. error otherwise.
181 op_handler
= self
._run
_ops
.get(operation
)
184 if (operation
!= 'query-running' and
185 not self
._is
_vlan
_device
(ifaceobj
)):
187 self
._init
_command
_handlers
()
188 if operation
== 'query-checkcurr':
189 op_handler(self
, ifaceobj
, query_ifaceobj
)
191 op_handler(self
, ifaceobj
)