]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/addons/vxlan.py
Don't allow IP addresses on ports enslaved in bonds or bridges
[mirror_ifupdown2.git] / ifupdown2 / addons / vxlan.py
1 #!/usr/bin/python
2
3 from ifupdown.iface import *
4 from ifupdownaddons.modulebase import moduleBase
5 from ifupdownaddons.iproute2 import iproute2
6 import ifupdown.rtnetlink_api as rtnetlink_api
7 import logging
8 from sets import Set
9
10 class vxlan(moduleBase):
11 _modinfo = {'mhelp' : 'vxlan module configures vxlan interfaces.',
12 'attrs' : {
13 'vxlan-id' :
14 {'help' : 'vxlan id',
15 'required' : True,
16 'example': ['vxlan-id 100']},
17 'vxlan-local-tunnelip' :
18 {'help' : 'vxlan local tunnel ip',
19 'example': ['vxlan-local-tunnelip 172.16.20.103']},
20 'vxlan-svcnodeip' :
21 {'help' : 'vxlan id',
22 'example': ['vxlan-svcnodeip 172.16.22.125']},
23 'vxlan-remoteip' :
24 {'help' : 'vxlan remote ip',
25 'example': ['vxlan-remoteip 172.16.22.127']},
26 'vxlan-learning' :
27 {'help' : 'vxlan learning on/off',
28 'example': ['vxlan-learning off'],
29 'default': 'on'},
30 }}
31
32 def __init__(self, *args, **kargs):
33 moduleBase.__init__(self, *args, **kargs)
34 self.ipcmd = None
35
36 def get_dependent_ifacenames(self, ifaceobj, ifaceobjs_all=None):
37 if not self._is_vxlan_device(ifaceobj):
38 return None
39 ifaceobj.link_kind |= ifaceLinkKind.VXLAN
40 return None
41
42 def _is_vxlan_device(self, ifaceobj):
43 if ifaceobj.get_attr_value_first('vxlan-id'):
44 return True
45 return False
46
47 def _up(self, ifaceobj):
48 vxlanid = ifaceobj.get_attr_value_first('vxlan-id')
49 if vxlanid:
50 self.ipcmd.link_create_vxlan(ifaceobj.name, vxlanid,
51 localtunnelip=ifaceobj.get_attr_value_first('vxlan-local-tunnelip'),
52 svcnodeips=ifaceobj.get_attr_value('vxlan-svcnodeip'),
53 remoteips=ifaceobj.get_attr_value('vxlan-remoteip'),
54 learning=ifaceobj.get_attr_value_first('vxlan-learning'),
55 ageing=ifaceobj.get_attr_value_first('vxlan-ageing'))
56 if ifaceobj.addr_method == 'manual':
57 rtnetlink_api.rtnl_api.link_set(ifaceobj.name, "up")
58
59 def _down(self, ifaceobj):
60 try:
61 self.ipcmd.link_delete(ifaceobj.name)
62 except Exception, e:
63 self.log_warn(str(e))
64
65 def _query_check_n_update(self, ifaceobjcurr, attrname, attrval,
66 running_attrval):
67 if running_attrval and attrval == running_attrval:
68 ifaceobjcurr.update_config_with_status(attrname, attrval, 0)
69 else:
70 ifaceobjcurr.update_config_with_status(attrname, running_attrval, 1)
71
72 def _query_check_n_update_addresses(self, ifaceobjcurr, attrname,
73 addresses, running_addresses):
74 if addresses:
75 for a in addresses:
76 if a in running_addresses:
77 ifaceobjcurr.update_config_with_status(attrname, a, 0)
78 else:
79 ifaceobjcurr.update_config_with_status(attrname, a, 1)
80 running_addresses = Set(running_addresses).difference(
81 Set(addresses))
82 [ifaceobjcurr.update_config_with_status(attrname, a, 1)
83 for a in running_addresses]
84
85 def _query_check(self, ifaceobj, ifaceobjcurr):
86 if not self.ipcmd.link_exists(ifaceobj.name):
87 return
88 # Update vxlan object
89 vxlanattrs = self.ipcmd.get_vxlandev_attrs(ifaceobj.name)
90 if not vxlanattrs:
91 ifaceobjcurr.check_n_update_config_with_status_many(ifaceobj,
92 self.get_mod_attrs(), -1)
93 return
94 self._query_check_n_update(ifaceobjcurr, 'vxlan-id',
95 ifaceobj.get_attr_value_first('vxlan-id'),
96 vxlanattrs.get('vxlanid'))
97
98 self._query_check_n_update(ifaceobjcurr, 'vxlan-local-tunnelip',
99 ifaceobj.get_attr_value_first('vxlan-local-tunnelip'),
100 vxlanattrs.get('local'))
101
102 self._query_check_n_update_addresses(ifaceobjcurr, 'vxlan-svcnodeip',
103 ifaceobj.get_attr_value('vxlan-svcnodeip'),
104 vxlanattrs.get('svcnode', []))
105
106 self._query_check_n_update_addresses(ifaceobjcurr, 'vxlan-remoteip',
107 ifaceobj.get_attr_value('vxlan-remoteip'),
108 vxlanattrs.get('remote', []))
109
110 learning = ifaceobj.get_attr_value_first('vxlan-learning')
111 if not learning:
112 learning = 'on'
113 running_learning = vxlanattrs.get('learning')
114 if learning == running_learning:
115 ifaceobjcurr.update_config_with_status('vxlan-learning',
116 running_learning, 0)
117 else:
118 ifaceobjcurr.update_config_with_status('vxlan-learning',
119 running_learning, 1)
120
121 def _query_running(self, ifaceobjrunning):
122 vxlanattrs = self.ipcmd.get_vxlandev_attrs(ifaceobjrunning.name)
123 if not vxlanattrs:
124 return
125 attrval = vxlanattrs.get('vxlanid')
126 if attrval:
127 ifaceobjrunning.update_config('vxlan-id', vxlanattrs.get('vxlanid'))
128 attrval = vxlanattrs.get('local')
129 if attrval:
130 ifaceobjrunning.update_config('vxlan-local-tunnelip', attrval)
131 attrval = vxlanattrs.get('svcnode')
132 if attrval:
133 [ifaceobjrunning.update_config('vxlan-svcnode', a)
134 for a in attrval]
135 attrval = vxlanattrs.get('remote')
136 if attrval:
137 [ifaceobjrunning.update_config('vxlan-remoteip', a)
138 for a in attrval]
139 attrval = vxlanattrs.get('learning')
140 if attrval and attrval == 'on':
141 ifaceobjrunning.update_config('vxlan-learning', 'on')
142
143
144 _run_ops = {'pre-up' : _up,
145 'post-down' : _down,
146 'query-checkcurr' : _query_check,
147 'query-running' : _query_running}
148
149 def get_ops(self):
150 return self._run_ops.keys()
151
152 def _init_command_handlers(self):
153 if not self.ipcmd:
154 self.ipcmd = iproute2(**self.get_flags())
155
156 def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args):
157 op_handler = self._run_ops.get(operation)
158 if not op_handler:
159 return
160 if (operation != 'query-running' and
161 not self._is_vxlan_device(ifaceobj)):
162 return
163 self._init_command_handlers()
164 if operation == 'query-checkcurr':
165 op_handler(self, ifaceobj, query_ifaceobj)
166 else:
167 op_handler(self, ifaceobj)