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