]> git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/addons/vxlan.py
Modified ifupdown support for vxlan head end replication
[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 _is_vxlan_device(self, ifaceobj):
37 if ifaceobj.get_attr_value_first('vxlan-id'):
38 return True
39 return False
40
41 def _up(self, ifaceobj):
42 vxlanid = ifaceobj.get_attr_value_first('vxlan-id')
43 if vxlanid:
44 self.ipcmd.link_create_vxlan(ifaceobj.name, vxlanid,
45 localtunnelip=ifaceobj.get_attr_value_first('vxlan-local-tunnelip'),
46 svcnodeips=ifaceobj.get_attr_value('vxlan-svcnodeip'),
47 remoteips=ifaceobj.get_attr_value('vxlan-remoteip'),
48 learning=ifaceobj.get_attr_value_first('vxlan-learning'),
49 ageing=ifaceobj.get_attr_value_first('vxlan-ageing'))
50 if ifaceobj.addr_method == 'manual':
51 rtnetlink_api.rtnl_api.link_set(ifaceobj.name, "up")
52
53 def _down(self, ifaceobj):
54 try:
55 self.ipcmd.link_delete(ifaceobj.name)
56 except Exception, e:
57 self.log_warn(str(e))
58
59 def _query_check_n_update(self, ifaceobjcurr, attrname, attrval,
60 running_attrval):
61 if running_attrval and attrval == running_attrval:
62 ifaceobjcurr.update_config_with_status(attrname, attrval, 0)
63 else:
64 ifaceobjcurr.update_config_with_status(attrname, running_attrval, 1)
65
66 def _query_check_n_update_addresses(self, ifaceobjcurr, attrname,
67 addresses, running_addresses):
68 if addresses:
69 for a in addresses:
70 if a in running_addresses:
71 ifaceobjcurr.update_config_with_status(attrname, a, 0)
72 else:
73 ifaceobjcurr.update_config_with_status(attrname, a, 1)
74 running_addresses = Set(running_addresses).difference(
75 Set(addresses))
76 [ifaceobjcurr.update_config_with_status(attrname, a, 1)
77 for a in running_addresses]
78
79 def _query_check(self, ifaceobj, ifaceobjcurr):
80 if not self.ipcmd.link_exists(ifaceobj.name):
81 return
82 # Update vxlan object
83 vxlanattrs = self.ipcmd.get_vxlandev_attrs(ifaceobj.name)
84 if not vxlanattrs:
85 ifaceobjcurr.check_n_update_config_with_status_many(ifaceobj,
86 self.get_mod_attrs(), -1)
87 return
88 self._query_check_n_update(ifaceobjcurr, 'vxlan-id',
89 ifaceobj.get_attr_value_first('vxlan-id'),
90 vxlanattrs.get('vxlanid'))
91
92 self._query_check_n_update(ifaceobjcurr, 'vxlan-local-tunnelip',
93 ifaceobj.get_attr_value_first('vxlan-local-tunnelip'),
94 vxlanattrs.get('local'))
95
96 self._query_check_n_update_addresses(ifaceobjcurr, 'vxlan-svcnodeip',
97 ifaceobj.get_attr_value('vxlan-svcnodeip'),
98 vxlanattrs.get('svcnode', []))
99
100 self._query_check_n_update_addresses(ifaceobjcurr, 'vxlan-remoteip',
101 ifaceobj.get_attr_value('vxlan-remoteip'),
102 vxlanattrs.get('remote', []))
103
104 learning = ifaceobj.get_attr_value_first('vxlan-learning')
105 if not learning:
106 learning = 'on'
107 running_learning = vxlanattrs.get('learning')
108 if learning == running_learning:
109 ifaceobjcurr.update_config_with_status('vxlan-learning',
110 running_learning, 0)
111 else:
112 ifaceobjcurr.update_config_with_status('vxlan-learning',
113 running_learning, 1)
114
115 def _query_running(self, ifaceobjrunning):
116 vxlanattrs = self.ipcmd.get_vxlandev_attrs(ifaceobjrunning.name)
117 if not vxlanattrs:
118 return
119 attrval = vxlanattrs.get('vxlanid')
120 if attrval:
121 ifaceobjrunning.update_config('vxlan-id', vxlanattrs.get('vxlanid'))
122 attrval = vxlanattrs.get('local')
123 if attrval:
124 ifaceobjrunning.update_config('vxlan-local-tunnelip', attrval)
125 attrval = vxlanattrs.get('svcnode')
126 if attrval:
127 [ifaceobjrunning.update_config('vxlan-svcnode', a)
128 for a in attrval]
129 attrval = vxlanattrs.get('remote')
130 if attrval:
131 [ifaceobjrunning.update_config('vxlan-remoteip', a)
132 for a in attrval]
133 attrval = vxlanattrs.get('learning')
134 if attrval and attrval == 'on':
135 ifaceobjrunning.update_config('vxlan-learning', 'on')
136
137
138 _run_ops = {'pre-up' : _up,
139 'post-down' : _down,
140 'query-checkcurr' : _query_check,
141 'query-running' : _query_running}
142
143 def get_ops(self):
144 return self._run_ops.keys()
145
146 def _init_command_handlers(self):
147 if not self.ipcmd:
148 self.ipcmd = iproute2(**self.get_flags())
149
150 def run(self, ifaceobj, operation, query_ifaceobj=None, **extra_args):
151 op_handler = self._run_ops.get(operation)
152 if not op_handler:
153 return
154 if (operation != 'query-running' and
155 not self._is_vxlan_device(ifaceobj)):
156 return
157 self._init_command_handlers()
158 if operation == 'query-checkcurr':
159 op_handler(self, ifaceobj, query_ifaceobj)
160 else:
161 op_handler(self, ifaceobj)