]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - addons/addressvirtual.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
14 class addressvirtual(moduleBase
):
15 """ ifupdown2 addon module to configure virtual addresses """
17 _modinfo
= {'mhelp' : 'address module configures virtual addresses for ' +
18 'interfaces. It creates a macvlan interface for ' +
19 'every mac ip address-virtual line',
22 { 'help' : 'bridge router virtual mac and ip',
23 'example' : ['address-virtual 00:11:22:33:44:01 11.0.1.254/24 11.0.1.254/24']}
26 def __init__(self
, *args
, **kargs
):
27 moduleBase
.__init
__(self
, *args
, **kargs
)
30 def _is_supported(self
, ifaceobj
):
31 if ifaceobj
.get_attr_value_first('address-virtual'):
35 def _apply_address_config(self
, ifaceobj
, realifacename
, address_virtual_list
):
36 purge_existing
= False if self
.PERFMODE
else True
38 self
.ipcmd
.batch_start()
40 macvlan_prefix
= '%s-virt' %ifaceobj
.name
.replace('.', '-')
41 for av
in address_virtual_list
:
44 self
.logger
.warn("%s: incorrect address-virtual attrs '%s'"
49 # Create a macvlan device on this device and set the virtual
50 # router mac and ip on it
51 macvlan_ifacename
= '%s-%d' %(macvlan_prefix
, av_idx
)
52 self
.ipcmd
.link_create_macvlan(macvlan_ifacename
, realifacename
)
53 self
.ipcmd
.link_set_hwaddress(macvlan_ifacename
, av_attrs
[0])
54 self
.ipcmd
.addr_add_multiple(macvlan_ifacename
, av_attrs
[1:],
57 self
.ipcmd
.batch_commit()
59 def _remove_address_config(self
, ifaceobj
, ifacename
):
60 if not self
.ipcmd
.link_exists(ifacename
):
62 self
.ipcmd
.batch_start()
63 macvlan_prefix
= '%s-virt' %ifacename
.replace('.', '-')
64 for macvlan_ifacename
in glob
.glob("/sys/class/net/%s-*" %macvlan_prefix
):
65 self
.ipcmd
.link_delete(os
.path
.basename(macvlan_ifacename
))
66 self
.ipcmd
.batch_commit()
68 def _get_real_ifacename(self
, ifaceobj
):
69 realifacename
= ifaceobj
.name
70 if ifaceobj
.type == ifaceType
.BRIDGE_VLAN
:
71 bridgename
= ifaceobj
.get_attr_value_first('bridge')
73 realifacename
= '%s.%s' %(bridgename
, ifaceobj
.priv_data
)
76 def _up(self
, ifaceobj
):
77 realifacename
= self
._get
_real
_ifacename
(ifaceobj
)
78 address_virtual_list
= ifaceobj
.get_attr_value('address-virtual')
79 if not address_virtual_list
:
80 # XXX: address virtual is not present. In which case,
81 # delete stale any macvlan devices.
82 self
._remove
_address
_config
(ifaceobj
, realifacename
)
85 if not self
.ipcmd
.link_exists(realifacename
):
86 self
.log_warn('%s: target link %s does not exist'
87 %(ifaceobj
.name
, realifacename
))
89 self
._apply
_address
_config
(ifaceobj
, realifacename
, address_virtual_list
)
91 def _down(self
, ifaceobj
):
92 realifacename
= self
._get
_real
_ifacename
(ifaceobj
)
94 self
._remove
_address
_config
(ifaceobj
, realifacename
)
98 def _query_check(self
, ifaceobj
, ifaceobjcurr
):
99 address_virtual_list
= ifaceobj
.get_attr_value('address-virtual')
100 if not address_virtual_list
:
102 realifacename
= self
._get
_real
_ifacename
(ifaceobj
)
104 macvlan_prefix
= '%s-virt' %realifacename
.replace('.', '-')
105 for address_virtual
in address_virtual_list
:
106 av_attrs
= address_virtual
.split()
107 if len(av_attrs
) < 2:
108 self
.logger
.warn("%s: incorrect address-virtual attrs '%s'"
109 %(ifaceobj
.name
, address_virtual
))
113 # Check if the macvlan device on this interface
114 macvlan_ifacename
= '%s-%d' %(macvlan_prefix
, av_idx
)
115 if self
.ipcmd
.link_exists(macvlan_ifacename
):
116 # XXX Check mac and ip address
117 rhwaddress
= self
.ipcmd
.link_get_hwaddress(macvlan_ifacename
)
118 raddrs
= self
.ipcmd
.addr_get(macvlan_ifacename
)
119 if rhwaddress
== av_attrs
[0] and raddrs
== av_attrs
[1:]:
120 ifaceobjcurr
.update_config_with_status('address-virtual',
123 raddress_virtual
= '%s %s' %(rhwaddress
, ' '.join(raddrs
))
124 ifaceobjcurr
.update_config_with_status('address-virtual',
129 def _query_running(self
, ifaceobjrunning
):
133 _run_ops
= {'up' : _up
,
135 'query-checkcurr' : _query_check
,
136 'query-running' : _query_running
}
139 """ returns list of ops supported by this module """
140 return self
._run
_ops
.keys()
142 def _init_command_handlers(self
):
144 self
.ipcmd
= iproute2(**self
.get_flags())
146 def run(self
, ifaceobj
, operation
, query_ifaceobj
=None):
147 """ run vlan configuration on the interface object passed as argument
150 **ifaceobj** (object): iface object
152 **operation** (str): any of 'pre-up', 'post-down', 'query-checkcurr',
155 **query_ifaceobj** (object): query check ifaceobject. This is only
156 valid when op is 'query-checkcurr'. It is an object same as
157 ifaceobj, but contains running attribute values and its config
158 status. The modules can use it to return queried running state
159 of interfaces. status is success if the running state is same
160 as user required state in ifaceobj. error otherwise.
162 op_handler
= self
._run
_ops
.get(operation
)
165 self
._init
_command
_handlers
()
166 if operation
== 'query-checkcurr':
167 op_handler(self
, ifaceobj
, query_ifaceobj
)
169 op_handler(self
, ifaceobj
)