X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=xenserver%2Fetc_xapi.d_plugins_openvswitch-cfg-update;h=2df838a7e5efbb37124002c7ba3f0c95e0a44ad8;hb=HEAD;hp=a4d97f58e0161fba4a8a59a83e1afe41f7852359;hpb=939e5a1bbf47d2dc7ec5665906c441f6569ce9e0;p=sliver-openvswitch.git diff --git a/xenserver/etc_xapi.d_plugins_openvswitch-cfg-update b/xenserver/etc_xapi.d_plugins_openvswitch-cfg-update index a4d97f58e..2df838a7e 100755 --- a/xenserver/etc_xapi.d_plugins_openvswitch-cfg-update +++ b/xenserver/etc_xapi.d_plugins_openvswitch-cfg-update @@ -4,7 +4,7 @@ # ovs-vswitchd configuration that are managed in the xapi database when # integrated with Citrix management tools. -# Copyright (C) 2009, 2010, 2011 Nicira Networks, Inc. +# Copyright (C) 2009, 2010, 2011, 2012, 2013 Nicira, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -26,8 +26,10 @@ import XenAPI import os import subprocess import syslog +import re vsctl="/usr/bin/ovs-vsctl" +ofctl="/usr/bin/ovs-ofctl" cacert_filename="/etc/openvswitch/vswitchd.cacert" # Delete the CA certificate, so that we go back to boot-strapping mode @@ -54,16 +56,9 @@ def update(session, args): raise XenAPIPlugin.Failure("NO_POOL_FOR_HOST", []) if len(pools) > 1: raise XenAPIPlugin.Failure("MORE_THAN_ONE_POOL_FOR_HOST", []) + new_controller = False pool = session.xenapi.pool.get_record(pools[0]) - try: - try: - controller = pool["vswitch_controller"] - except KeyError: - # On systems older than XenServer 5.6.0, we needed to store - # the key in "other_config". - controller = pool["other_config"]["vSwitchController"] - except KeyError, e: - controller = "" + controller = pool.get("vswitch_controller", "") ret_str = "" currentController = vswitchCurrentController() if controller == "" and currentController != "": @@ -81,36 +76,112 @@ def update(session, args): except: pass setControllerCfg(controller) + new_controller = True ret_str += "Successfully set controller to %s. " % controller try: - fail_mode = pool["other_config"]["vswitch-controller-fail-mode"] + pool_fail_mode = pool["other_config"]["vswitch-controller-fail-mode"] except KeyError, e: - fail_mode = None + pool_fail_mode = None - if fail_mode != 'secure': - fail_mode = 'standalone' + bton = {} - fail_mode_changed = False - for (p, rec) in session.xenapi.PIF.get_all_records().items(): + for rec in session.xenapi.network.get_all_records().values(): try: - network = session.xenapi.network.get_record(rec['network']) - bridge = network['bridge'] - except Exception, e: - syslog.syslog("%s: failed to get bridge name (%s)" % - (script, str(e))) - continue + bton[rec['bridge']] = rec + except KeyError: + pass + # If new controller, get management MAC addresses from XAPI now + # in case fail_mode set to secure which may affect XAPI access + mgmt_bridge = None + host_mgmt_mac = None + host_mgmt_device = None + pool_mgmt_macs = {} + if new_controller: + recs = session.xenapi.PIF.get_all_records_where('field "management"="true"') + for rec in recs.itervalues(): + pool_mgmt_macs[rec.get('MAC')] = rec.get('device') + + dib_changed = False + fail_mode_changed = False + for bridge in vswitchCfgQuery(['list-br']).split(): + network = bton[bridge] + bridge = vswitchCfgQuery(['br-to-parent', bridge]) + + xapi_dib = network['other_config'].get('vswitch-disable-in-band') + if not xapi_dib: + xapi_dib = '' + + ovs_dib = vswitchCfgQuery(['--', '--if-exists', 'get', 'Bridge', + bridge, + 'other_config:disable-in-band']).strip('"') + + # Do nothing if setting is invalid, and warn the user. + if xapi_dib not in ['true', 'false', '']: + ret_str += '"' + xapi_dib + '"' + \ + ' is an invalid value for vswitch-disable-in-band on ' + \ + bridge + ' ' + + # Change bridge disable-in-band option if XAPI and OVS states differ. + elif xapi_dib != ovs_dib: + # 'true' or 'false' + if xapi_dib: + vswitchCfgMod(['--', 'set', 'Bridge', bridge, + 'other_config:disable-in-band=' + xapi_dib]) + # '' or None + else: + vswitchCfgMod(['--', 'remove', 'Bridge', bridge, + 'other_config', 'disable-in-band']) + dib_changed = True + + # Change bridge fail_mode if XAPI state differs from OVS state. bridge_fail_mode = vswitchCfgQuery(["get", "Bridge", bridge, "fail_mode"]).strip('[]"') + try: + fail_mode = bton[bridge]["other_config"]["vswitch-controller-fail-mode"] + except KeyError, e: + fail_mode = None + + if fail_mode not in ['secure', 'standalone']: + fail_mode = pool_fail_mode + + if fail_mode != 'secure': + fail_mode = 'standalone' + if bridge_fail_mode != fail_mode: vswitchCfgMod(['--', 'set', 'Bridge', bridge, "fail_mode=%s" % fail_mode]) fail_mode_changed = True + # Determine local mgmt MAC address if host being added to secure + # pool so we can add default flows to allow management traffic + if new_controller and fail_mode_changed and pool_fail_mode == "secure": + oc = vswitchCfgQuery(["get", "Bridge", bridge, "other-config"]) + m = re.match('.*hwaddr="([0-9a-fA-F:].*)".*', oc) + if m and m.group(1) in pool_mgmt_macs.keys(): + mgmt_bridge = bridge + host_mgmt_mac = m.group(1) + host_mgmt_device = pool_mgmt_macs[host_mgmt_mac] + + if host_mgmt_mac is not None and mgmt_bridge is not None and \ + host_mgmt_device is not None: + tp = "idle_timeout=0,priority=0" + port = vswitchCfgQuery(["get", "interface", host_mgmt_device, "ofport"]) + addFlow(mgmt_bridge, "%s,in_port=%s,arp,nw_proto=1,actions=local" % \ + (tp, port)) + addFlow(mgmt_bridge, "%s,in_port=local,arp,dl_src=%s,actions=%s" % \ + (tp, host_mgmt_mac, port)) + addFlow(mgmt_bridge, "%s,in_port=%s,dl_dst=%s,actions=local" % \ + (tp, port, host_mgmt_mac)) + addFlow(mgmt_bridge, "%s,in_port=local,dl_src=%s,actions=%s" % \ + (tp, host_mgmt_mac, port)) + + if dib_changed: + ret_str += "Updated in-band management. " if fail_mode_changed: - ret_str += "Set fail_mode to %s. " % fail_mode + ret_str += "Updated fail_mode. " if ret_str != '': return ret_str @@ -118,8 +189,7 @@ def update(session, args): return "No change to configuration" def vswitchCurrentController(): - controller = vswitchCfgQuery(["get", "Open_vSwitch", - ".", "managers"]).strip('[]"') + controller = vswitchCfgQuery(["get-manager"]) if controller == "": return controller if len(controller) < 4 or controller[0:4] != "ssl:": @@ -128,23 +198,22 @@ def vswitchCurrentController(): return controller.split(':')[1] def removeControllerCfg(): - vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers", + vswitchCfgMod(["--", "del-manager", "--", "del-ssl"]) def setControllerCfg(controller): # /etc/xensource/xapi-ssl.pem is mentioned twice below because it # contains both the private key and the certificate. - vswitchCfgMod(["--", "clear", "Open_vSwitch", ".", "managers", + vswitchCfgMod(["--", "del-manager", "--", "del-ssl", "--", "--bootstrap", "set-ssl", "/etc/xensource/xapi-ssl.pem", "/etc/xensource/xapi-ssl.pem", cacert_filename, - "--", "set", "Open_vSwitch", ".", - 'managers="ssl:' + controller + ':6632"']) + "--", "set-manager", 'ssl:' + controller + ':6632']) def vswitchCfgQuery(action_args): - cmd = [vsctl, "--timeout=5", "-vANY:console:emer"] + action_args + cmd = [vsctl, "-vconsole:off"] + action_args output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate() if len(output) == 0 or output[0] == None: output = "" @@ -153,7 +222,7 @@ def vswitchCfgQuery(action_args): return output def vswitchCfgMod(action_args): - cmd = [vsctl, "--timeout=5", "-vANY:console:emer"] + action_args + cmd = [vsctl, "--timeout=5", "-vconsole:off"] + action_args exitcode = subprocess.call(cmd) if exitcode != 0: raise XenAPIPlugin.Failure("VSWITCH_CONFIG_MOD_FAILURE", @@ -167,6 +236,13 @@ def emergency_reset(session, args): [ str(exitcode) ]) return "Successfully reset configuration" + +def addFlow(switch, flow): + cmd = [ofctl, "add-flow", switch, flow] + exitcode = subprocess.call(cmd) + if exitcode != 0: + raise XenAPIPlugin.Failure("VSWITCH_ADD_FLOW_FAILURE", + [ str(exitcode) , str(switch), str(flow) ]) if __name__ == "__main__": XenAPIPlugin.dispatch({"update": update,