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