2 # Copyright (c) 2009, 2010, 2011 Nicira Networks
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 # A daemon to monitor the external_ids columns of the Bridge and
18 # Interface OVSDB tables for changes that require interrogating XAPI.
19 # Its responsibilities include:
21 # - Set the "bridge-id" key in the Bridge table.
22 # - Set the "iface-id" key in the Interface table.
23 # - Set the fail-mode on internal bridges.
26 import logging, logging.handlers
35 from ovs.db import error
36 from ovs.db import types
41 s_log = logging.getLogger("ovs-xapi-sync")
42 l_handler = logging.handlers.RotatingFileHandler(
43 "/var/log/openvswitch/ovs-xapi-sync.log")
44 l_formatter = logging.Formatter('%(filename)s: %(levelname)s: %(message)s')
45 l_handler.setFormatter(l_formatter)
46 s_log.addHandler(l_handler)
47 s_log.setLevel(logging.INFO)
49 vsctl="/usr/bin/ovs-vsctl"
53 # Set up a session to interact with XAPI.
55 # On system start-up, OVS comes up before XAPI, so we can't log into the
56 # session until later. Try to do this on-demand, since we won't
57 # actually do anything interesting until XAPI is up.
60 if session is not None:
64 session = XenAPI.xapi_local()
65 session.xenapi.login_with_password("", "")
68 s_log.warning("Couldn't login to XAPI")
73 def get_network_by_bridge(br_name):
74 if not init_session():
75 s_log.warning("Failed to get bridge id %s because"
76 " XAPI session could not be initialized" % br_name)
79 for n in session.xenapi.network.get_all():
80 rec = session.xenapi.network.get_record(n)
81 if rec['bridge'] == br_name:
86 # By default, the "bridge-id" external id in the Bridge table is the
87 # same as "xs-network-uuids". This may be overridden by defining a
88 # "nicira-bridge-id" key in the "other_config" field of the network
89 # record of XAPI. If nicira-bridge-id is undefined returns default.
90 # On error returns None.
91 def get_bridge_id(br_name, default=None):
92 rec = get_network_by_bridge(br_name)
94 return rec['other_config'].get('nicira-bridge-id', default)
97 # By default, the "iface-id" external id in the Interface table is the
98 # same as "xs-vif-uuid". This may be overridden by defining a
99 # "nicira-iface-id" key in the "other_config" field of the VIF
101 def get_iface_id(if_name, xs_vif_uuid):
102 if not if_name.startswith("vif"):
103 # Treat whatever was passed into 'xs_vif_uuid' as a default
104 # value for non-VIFs.
107 if not init_session():
108 s_log.warning("Failed to get interface id %s because"
109 " XAPI session could not be initialized" % if_name)
113 vif = session.xenapi.VIF.get_by_uuid(xs_vif_uuid)
114 rec = session.xenapi.VIF.get_record(vif)
115 return rec['other_config'].get('nicira-iface-id', xs_vif_uuid)
116 except XenAPI.Failure:
117 s_log.warning("Could not find XAPI entry for VIF %s" % if_name)
120 def call_vsctl(args):
121 cmd = [vsctl, "--timeout=30", "-vANY:console:emer"] + args
122 exitcode = subprocess.call(cmd)
124 s_log.warning("Couldn't call ovs-vsctl")
126 def set_external_id(table, record, key, value):
127 col = 'external-ids:"' + key + '"="' + value + '"'
128 call_vsctl(["set", table, record, col])
130 # XenServer does not call interface-reconfigure on internal networks,
131 # which is where the fail-mode would normally be set.
132 def update_fail_mode(name):
133 rec = get_network_by_bridge(name)
138 fail_mode = rec['other_config'].get('vswitch-controller-fail-mode')
141 pools = session.xenapi.pool.get_all()
143 prec = session.xenapi.pool.get_record(pools[0])
144 fail_mode = prec['other_config'].get('vswitch-controller-fail-mode')
146 if fail_mode not in ['standalone', 'secure']:
147 fail_mode = 'standalone'
149 call_vsctl(["set", "bridge", name, "fail_mode=" + fail_mode])
151 def update_in_band_mgmt(name):
152 rec = get_network_by_bridge(name)
157 dib = rec['other_config'].get('vswitch-disable-in-band')
159 call_vsctl(['remove', 'bridge', name, 'other_config',
161 elif dib in ['true', 'false']:
162 call_vsctl(['set', 'bridge', name,
163 'other_config:disable-in-band=' + dib])
165 s_log.warning('"' + dib + '"'
166 "isn't a valid setting for other_config:disable-in-band on " +
169 def update_bridge_id(name, ids):
170 id = get_bridge_id(name, ids.get("xs-network-uuids"))
175 primary_id = id.split(";")[0]
177 if ids.get("bridge-id") != primary_id:
178 set_external_id("Bridge", name, "bridge-id", primary_id)
179 ids["bridge-id"] = primary_id
181 def update_iface_id(name, ids):
182 id = get_iface_id(name, ids.get("xs-vif-uuid"))
183 if ids.get("iface-id") != id and id:
184 set_external_id("Interface", name, "iface-id", id)
187 def keep_table_columns(schema, table_name, column_types):
188 table = schema.tables.get(table_name)
190 raise error.Error("schema has no %s table" % table_name)
193 for column_name, column_type in column_types.iteritems():
194 column = table.columns.get(column_name)
196 raise error.Error("%s table schema lacks %s column"
197 % (table_name, column_name))
198 if column.type != column_type:
199 raise error.Error("%s column in %s table has type \"%s\", "
200 "expected type \"%s\""
201 % (column_name, table_name,
202 column.type.toEnglish(),
203 column_type.toEnglish()))
204 new_columns[column_name] = column
205 table.columns = new_columns
208 def monitor_uuid_schema_cb(schema):
209 string_type = types.Type(types.BaseType(types.StringType))
210 string_map_type = types.Type(types.BaseType(types.StringType),
211 types.BaseType(types.StringType),
215 for table_name in ("Bridge", "Interface"):
216 new_tables[table_name] = keep_table_columns(
217 schema, table_name, {"name": string_type,
218 "external_ids": string_map_type})
219 schema.tables = new_tables
222 print "usage: %s [OPTIONS] DATABASE" % sys.argv[0]
223 print "where DATABASE is a socket on which ovsdb-server is listening."
225 print "Other options:"
226 print " -h, --help display this help message"
229 def handler(signum, frame):
231 if (signum == signal.SIGHUP):
238 options, args = getopt.gnu_getopt(
239 argv[1:], 'h', ['help'] + ovs.daemon.LONG_OPTIONS)
240 except getopt.GetoptError, geo:
241 sys.stderr.write("%s: %s\n" % (ovs.util.PROGRAM_NAME, geo.msg))
244 for key, value in options:
245 if key in ['-h', '--help']:
247 elif not ovs.daemon.parse_opt(key, value):
248 sys.stderr.write("%s: unhandled option %s\n"
249 % (ovs.util.PROGRAM_NAME, key))
253 sys.stderr.write("%s: exactly one nonoption argument is required "
254 "(use --help for help)\n" % ovs.util.PROGRAM_NAME)
258 idl = ovs.db.idl.Idl(remote, "Open_vSwitch", monitor_uuid_schema_cb)
260 ovs.daemon.daemonize()
262 # This daemon is usually started before XAPI, but to complete our
263 # tasks, we need it. Wait here until it's up.
264 while not os.path.exists("/var/run/xapi_init_complete.cookie"):
267 signal.signal(signal.SIGHUP, handler)
272 if not force_run and not idl.run():
273 poller = ovs.poller.Poller()
279 s_log.info("Forced to re-run as the result of a SIGHUP")
285 for rec in idl.data["Bridge"].itervalues():
286 name = rec.name.as_scalar()
287 xs_network_uuids = rec.external_ids.get("xs-network-uuids")
288 bridge_id = rec.external_ids.get("bridge-id")
289 new_bridges[name] = {"xs-network-uuids": xs_network_uuids,
290 "bridge-id": bridge_id}
293 for rec in idl.data["Interface"].itervalues():
294 name = rec.name.as_scalar()
295 xs_vif_uuid = rec.external_ids.get("xs-vif-uuid")
296 iface_id = rec.external_ids.get("iface-id")
297 new_interfaces[name] = {"xs-vif-uuid": xs_vif_uuid,
298 "iface-id": iface_id}
300 if bridges != new_bridges:
301 for name,ids in new_bridges.items():
302 if name not in bridges:
303 update_fail_mode(name)
304 update_in_band_mgmt(name)
306 if (name not in bridges) or (bridges[name] != ids):
307 update_bridge_id(name, ids)
309 bridges = new_bridges
311 if interfaces != new_interfaces:
312 for name,ids in new_interfaces.items():
313 if (name not in interfaces) or (interfaces[name] != ids):
314 update_iface_id(name, ids)
315 interfaces = new_interfaces
317 if __name__ == '__main__':
321 # Let system.exit() calls complete normally
324 s_log.exception("traceback")
325 sys.exit(ovs.daemon.RESTART_EXIT_CODE)