Use the IANA-assigned ports for OpenFlow and OVSDB.
[cascardo/ovs.git] / xenserver / etc_xapi.d_plugins_openvswitch-cfg-update
1 #!/usr/bin/env python
2 #
3 # xapi plugin script to update the cache of configuration items in the
4 # ovs-vswitchd configuration that are managed in the xapi database when
5 # integrated with Citrix management tools.
6
7 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
8 #
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at:
12 #
13 #     http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20
21 # TBD: - error handling needs to be improved.  Currently this can leave
22 # TBD:   the system in a bad state if anything goes wrong.
23
24 import XenAPIPlugin
25 import os
26 import subprocess
27 import syslog
28 import re
29
30 vsctl = '/usr/bin/ovs-vsctl'
31 ofctl = '/usr/bin/ovs-ofctl'
32 cacert_filename = '/etc/openvswitch/vswitchd.cacert'
33
34
35 # Delete the CA certificate, so that we go back to boot-strapping mode
36 def delete_cacert():
37     try:
38         os.remove(cacert_filename)
39     except OSError:
40         # Ignore error if file doesn't exist
41         pass
42
43
44 def update(session, args):
45     # Refresh bridge network UUIDs in case this host joined or left a pool.
46     script = '/opt/xensource/libexec/interface-reconfigure'
47     try:
48         retval = subprocess.call([script, 'rewrite'])
49         if retval != 0:
50             syslog.syslog('%s exited with status %d' % (script, retval))
51     except OSError, e:
52         syslog.syslog('%s: failed to execute (%s)' % (script, e.strerror))
53
54     pools = session.xenapi.pool.get_all()
55     # We assume there is only ever one pool...
56     if len(pools) == 0:
57         raise XenAPIPlugin.Failure('NO_POOL_FOR_HOST', [])
58     if len(pools) > 1:
59         raise XenAPIPlugin.Failure('MORE_THAN_ONE_POOL_FOR_HOST', [])
60     new_controller = False
61     pool = session.xenapi.pool.get_record(pools[0])
62     controller = pool.get('vswitch_controller')
63     ret_str = ''
64     currentControllers = vswitchCurrentControllers()
65
66     if not controller and currentControllers:
67         delete_cacert()
68         try:
69             emergency_reset(session, None)
70         except:
71             pass
72         removeControllerCfg()
73         ret_str += 'Successfully removed controller config.  '
74     # controller cannot be empty, otherwise, this will always be True.
75     elif controller and controller not in currentControllers:
76         delete_cacert()
77         try:
78             emergency_reset(session, None)
79         except:
80             pass
81         setControllerCfg(controller)
82         new_controller = True
83         ret_str += 'Successfully set controller to %s.  ' % controller
84
85     try:
86         pool_fail_mode = pool['other_config']['vswitch-controller-fail-mode']
87     except KeyError, e:
88         pool_fail_mode = None
89
90     bton = {}
91
92     for rec in session.xenapi.network.get_all_records().values():
93         try:
94             bton[rec['bridge']] = rec
95         except KeyError:
96             pass
97
98     # If new controller, get management MAC addresses from XAPI now
99     # in case fail_mode set to secure which may affect XAPI access
100     mgmt_bridge = None
101     host_mgmt_mac = None
102     host_mgmt_device = None
103     pool_mgmt_macs = {}
104     if new_controller:
105         query = 'field "management"="true"'
106         recs = session.xenapi.PIF.get_all_records_where(query)
107         for rec in recs.itervalues():
108             pool_mgmt_macs[rec.get('MAC')] = rec.get('device')
109
110     dib_changed = False
111     fail_mode_changed = False
112     for bridge in vswitchCfgQuery(['list-br']).split():
113         network = bton[bridge]
114         bridge = vswitchCfgQuery(['br-to-parent', bridge])
115
116         xapi_dib = network['other_config'].get('vswitch-disable-in-band')
117         if not xapi_dib:
118             xapi_dib = ''
119
120         ovs_dib = vswitchCfgQuery(['--', '--if-exists', 'get', 'Bridge',
121                                    bridge,
122                                    'other_config:disable-in-band']).strip('"')
123
124         # Do nothing if setting is invalid, and warn the user.
125         if xapi_dib not in ['true', 'false', '']:
126             ret_str += '"' + xapi_dib + '"' + \
127                 ' is an invalid value for vswitch-disable-in-band on ' + \
128                 bridge + '  '
129
130         # Change bridge disable-in-band option if XAPI and OVS states differ.
131         elif xapi_dib != ovs_dib:
132             # 'true' or 'false'
133             if xapi_dib:
134                 vswitchCfgMod(['--', 'set', 'Bridge', bridge,
135                                'other_config:disable-in-band=' + xapi_dib])
136             # '' or None
137             else:
138                 vswitchCfgMod(['--', 'remove', 'Bridge', bridge,
139                                'other_config', 'disable-in-band'])
140             dib_changed = True
141
142         # Change bridge fail_mode if XAPI state differs from OVS state.
143         bridge_fail_mode = vswitchCfgQuery(['get', 'Bridge',
144                                             bridge, 'fail_mode']).strip('[]"')
145
146         try:
147             other_config = bton[bridge]['other_config']
148             fail_mode = other_config['vswitch-controller-fail-mode']
149         except KeyError, e:
150             fail_mode = None
151
152         if fail_mode not in ['secure', 'standalone']:
153             fail_mode = pool_fail_mode
154
155         if fail_mode != 'secure':
156             fail_mode = 'standalone'
157
158         if bridge_fail_mode != fail_mode:
159             vswitchCfgMod(['--', 'set', 'Bridge', bridge,
160                            'fail_mode=%s' % fail_mode])
161             fail_mode_changed = True
162
163         # Determine local mgmt MAC address if host being added to secure
164         # pool so we can add default flows to allow management traffic
165         if new_controller and fail_mode_changed and pool_fail_mode == 'secure':
166             oc = vswitchCfgQuery(['get', 'Bridge', bridge, 'other-config'])
167             m = re.match('.*hwaddr="([0-9a-fA-F:].*)".*', oc)
168             if m and m.group(1) in pool_mgmt_macs.keys():
169                 mgmt_bridge = bridge
170                 host_mgmt_mac = m.group(1)
171                 host_mgmt_device = pool_mgmt_macs[host_mgmt_mac]
172
173     if (host_mgmt_mac is not None and mgmt_bridge is not None and
174             host_mgmt_device is not None):
175         tp = 'idle_timeout=0,priority=0'
176         port = vswitchCfgQuery(['get', 'interface', host_mgmt_device,
177                                 'ofport'])
178
179         addFlow(mgmt_bridge, '%s,in_port=%s,arp,nw_proto=1,actions=local' %
180                 (tp, port))
181         addFlow(mgmt_bridge, '%s,in_port=local,arp,dl_src=%s,actions=%s' %
182                 (tp, host_mgmt_mac, port))
183         addFlow(mgmt_bridge, '%s,in_port=%s,dl_dst=%s,actions=local' %
184                 (tp, port, host_mgmt_mac))
185         addFlow(mgmt_bridge, '%s,in_port=local,dl_src=%s,actions=%s' %
186                 (tp, host_mgmt_mac, port))
187
188     if dib_changed:
189         ret_str += 'Updated in-band management.  '
190     if fail_mode_changed:
191         ret_str += 'Updated fail_mode.  '
192
193     if ret_str != '':
194         return ret_str
195     else:
196         return 'No change to configuration'
197
198
199 def vswitchCurrentControllers():
200     controllers = vswitchCfgQuery(['get-manager'])
201
202     def parse_controller(controller):
203         if controller.startswith('ssl:'):
204             return controller.split(':')[1]
205
206         return controller.split(':')[0]
207
208     return [parse_controller(controller)
209             for controller in controllers.split('\n')
210             if controller]
211
212
213 def removeControllerCfg():
214     vswitchCfgMod(['--', 'del-manager',
215                    '--', 'del-ssl'])
216
217
218 def setControllerCfg(controller):
219     # /etc/xensource/xapi-ssl.pem is mentioned twice below because it
220     # contains both the private key and the certificate.
221     vswitchCfgMod(['--', 'del-manager',
222                    '--', 'del-ssl',
223                    '--', '--bootstrap', 'set-ssl',
224                    '/etc/xensource/xapi-ssl.pem',
225                    '/etc/xensource/xapi-ssl.pem',
226                    cacert_filename,
227                    '--', 'set-manager', 'ssl:' + controller + ':6640'])
228
229
230 def vswitchCfgQuery(action_args):
231     cmd = [vsctl, '-vconsole:off'] + action_args
232     output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
233     if len(output) == 0 or output[0] is None:
234         output = ''
235     else:
236         output = output[0].strip()
237     return output
238
239
240 def vswitchCfgMod(action_args):
241     cmd = [vsctl, '--timeout=5', '-vconsole:off'] + action_args
242     exitcode = subprocess.call(cmd)
243     if exitcode != 0:
244         raise XenAPIPlugin.Failure('VSWITCH_CONFIG_MOD_FAILURE',
245                                    [str(exitcode), str(action_args)])
246
247
248 def emergency_reset(session, args):
249     cmd = [vsctl, '--timeout=5', 'emer-reset']
250     exitcode = subprocess.call(cmd)
251     if exitcode != 0:
252         raise XenAPIPlugin.Failure('VSWITCH_EMER_RESET_FAILURE',
253                                    [str(exitcode)])
254
255     return 'Successfully reset configuration'
256
257
258 def addFlow(switch, flow):
259     cmd = [ofctl, 'add-flow', switch, flow]
260     exitcode = subprocess.call(cmd)
261     if exitcode != 0:
262         raise XenAPIPlugin.Failure('VSWITCH_ADD_FLOW_FAILURE',
263                                    [str(exitcode), str(switch), str(flow)])
264
265
266 if __name__ == '__main__':
267     XenAPIPlugin.dispatch({'update': update,
268                            'emergency_reset': emergency_reset})