2 # Copyright (c) 2009, 2010, 2011, 2012 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.
34 from ovs.db import error
35 from ovs.db import types
39 vlog = ovs.vlog.Vlog("ovs-xapi-sync")
44 # Set up a session to interact with XAPI.
46 # On system start-up, OVS comes up before XAPI, so we can't log into the
47 # session until later. Try to do this on-demand, since we won't
48 # actually do anything interesting until XAPI is up.
51 if session is not None:
55 session = XenAPI.xapi_local()
56 session.xenapi.login_with_password("", "")
57 except XenAPI.Failure, e:
59 vlog.warn("Couldn't login to XAPI (%s)" % e)
65 def get_network_by_bridge(br_name):
66 if not init_session():
67 vlog.warn("Failed to get bridge id %s because"
68 " XAPI session could not be initialized" % br_name)
71 for n in session.xenapi.network.get_all():
72 rec = session.xenapi.network.get_record(n)
73 if rec['bridge'] == br_name:
79 # By default, the "bridge-id" external id in the Bridge table is the
80 # same as "xs-network-uuids". This may be overridden by defining a
81 # "nicira-bridge-id" key in the "other_config" field of the network
82 # record of XAPI. If nicira-bridge-id is undefined returns default.
83 # On error returns None.
84 def get_bridge_id(br_name, default=None):
85 rec = get_network_by_bridge(br_name)
87 return rec['other_config'].get('nicira-bridge-id', default)
91 # By default, the "iface-id" external id in the Interface table is the
92 # same as "xs-vif-uuid". This may be overridden by defining a
93 # "nicira-iface-id" key in the "other_config" field of the VIF
95 def get_iface_id(if_name, xs_vif_uuid):
96 if not if_name.startswith("vif") and not if_name.startswith("tap"):
97 # Treat whatever was passed into 'xs_vif_uuid' as a default
101 if not init_session():
102 vlog.warn("Failed to get interface id %s because"
103 " XAPI session could not be initialized" % if_name)
107 vif = session.xenapi.VIF.get_by_uuid(xs_vif_uuid)
108 rec = session.xenapi.VIF.get_record(vif)
109 return rec['other_config'].get('nicira-iface-id', xs_vif_uuid)
110 except XenAPI.Failure:
111 vlog.warn("Could not find XAPI entry for VIF %s" % if_name)
115 # By default, the "vm-id" external id in the Interface table is the
116 # same as "xs-vm-uuid". This may be overridden by defining a
117 # "nicira-vm-id" key in the "other_config" field of the VM
119 def get_vm_id(if_name, xs_vm_uuid):
120 if not if_name.startswith("vif") and not if_name.startswith("tap"):
121 # Treat whatever was passed into 'xs_vm_uuid' as a default
122 # value for non-VIFs.
125 if not init_session():
126 vlog.warn("Failed to get vm id for interface id %s because"
127 " XAPI session could not be initialized" % if_name)
131 vm = session.xenapi.VM.get_by_uuid(xs_vm_uuid)
132 rec = session.xenapi.VM.get_record(vm)
133 return rec['other_config'].get('nicira-vm-id', xs_vm_uuid)
134 except XenAPI.Failure:
135 vlog.warn("Could not find XAPI entry for VIF %s" % if_name)
139 def set_or_delete(d, key, value):
145 if d.get(key) != value:
151 def set_external_id(row, key, value):
152 external_ids = row.external_ids
153 if set_or_delete(external_ids, key, value):
154 row.external_ids = external_ids
157 # XenServer does not call interface-reconfigure on internal networks,
158 # which is where the fail-mode would normally be set.
159 def update_fail_mode(row):
160 rec = get_network_by_bridge(row.name)
164 fail_mode = rec['other_config'].get('vswitch-controller-fail-mode')
167 pools = session.xenapi.pool.get_all()
169 prec = session.xenapi.pool.get_record(pools[0])
170 fail_mode = prec['other_config'].get(
171 'vswitch-controller-fail-mode')
173 if fail_mode not in ['standalone', 'secure']:
174 fail_mode = 'standalone'
176 if row.fail_mode != fail_mode:
177 row.fail_mode = fail_mode
180 def update_in_band_mgmt(row):
181 rec = get_network_by_bridge(row.name)
185 dib = rec['other_config'].get('vswitch-disable-in-band')
187 other_config = row.other_config
188 if dib and dib not in ['true', 'false']:
189 vlog.warn('"%s" isn\'t a valid setting for '
190 "other_config:disable-in-band on %s" % (dib, row.name))
191 elif set_or_delete(other_config, 'disable-in-band', dib):
192 row.other_config = other_config
195 def keep_table_columns(schema, table_name, columns):
196 table = schema.tables.get(table_name)
198 raise error.Error("schema has no %s table" % table_name)
201 for column_name in columns:
202 column = table.columns.get(column_name)
204 raise error.Error("%s table schema lacks %s column"
205 % (table_name, column_name))
206 new_columns[column_name] = column
207 table.columns = new_columns
211 def prune_schema(schema):
213 new_tables["Bridge"] = keep_table_columns(
214 schema, "Bridge", ("name", "external_ids", "other_config",
216 new_tables["Interface"] = keep_table_columns(
217 schema, "Interface", ("name", "external_ids"))
218 schema.tables = new_tables
221 def handler(signum, _):
223 if (signum == signal.SIGHUP):
230 parser = argparse.ArgumentParser()
231 parser.add_argument("database", metavar="DATABASE",
232 help="A socket on which ovsdb-server is listening.")
233 parser.add_argument("--root-prefix", metavar="DIR", default='',
234 help="Use DIR as alternate root directory"
237 ovs.vlog.add_args(parser)
238 ovs.daemon.add_args(parser)
239 args = parser.parse_args()
240 ovs.vlog.handle_args(args)
241 ovs.daemon.handle_args(args)
243 remote = args.database
244 schema_file = "%s/vswitch.ovsschema" % ovs.dirs.PKGDATADIR
245 schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schema_file))
247 idl = ovs.db.idl.Idl(remote, schema)
249 ovs.daemon.daemonize()
251 # This daemon is usually started before XAPI, but to complete our
252 # tasks, we need it. Wait here until it's up.
253 cookie_file = args.root_prefix + "/var/run/xapi_init_complete.cookie"
254 while not os.path.exists(cookie_file):
257 signal.signal(signal.SIGHUP, handler)
259 bridges = {} # Map from bridge name to nicira-bridge-id
260 iface_ids = {} # Map from xs-vif-uuid to iface-id
261 vm_ids = {} # Map from xs-vm-uuid to vm-id
262 seqno = idl.change_seqno # Sequence number when we last processed the db
265 if not force_run and seqno == idl.change_seqno:
266 poller = ovs.poller.Poller()
272 vlog.info("Forced to re-run as the result of a SIGHUP")
277 seqno = idl.change_seqno
279 txn = ovs.db.idl.Transaction(idl)
282 for row in idl.tables["Bridge"].rows.itervalues():
283 if row.name in bridges:
284 nbd = bridges[row.name]
287 update_fail_mode(row)
288 update_in_band_mgmt(row)
289 nbd = get_bridge_id(row.name)
292 if bridge_id is None:
293 bridge_id = row.external_ids.get("xs-network-uuids")
295 if bridge_id is not None:
296 set_external_id(row, "bridge-id", bridge_id.split(";")[0])
298 new_bridges[row.name] = nbd
299 bridges = new_bridges
302 for row in idl.tables["Interface"].rows.itervalues():
303 iface_by_name[row.name] = row
307 for row in idl.tables["Interface"].rows.itervalues():
308 # Match up paired vif and tap devices.
309 if row.name.startswith("vif"):
311 tap = iface_by_name.get("tap%s" % row.name[3:])
312 elif row.name.startswith("tap"):
314 vif = iface_by_name.get("vif%s" % row.name[3:])
318 # Several tap external-ids need to be copied from the vif.
319 if row == tap and vif:
320 keys = ["attached-mac",
325 set_external_id(row, k, vif.external_ids.get(k))
327 # Map from xs-vif-uuid to iface-id.
329 # (A tap's xs-vif-uuid comes from its vif. That falls out
330 # naturally from the copy loop above.)
331 xvu = row.external_ids.get("xs-vif-uuid")
333 iface_id = (new_iface_ids.get(xvu)
334 or iface_ids.get(xvu)
335 or get_iface_id(row.name, xvu))
336 new_iface_ids[xvu] = iface_id
338 # No xs-vif-uuid therefore no iface-id.
340 set_external_id(row, "iface-id", iface_id)
342 # Map from xs-vm-uuid to vm-id.
343 xvmu = row.external_ids.get("xs-vm-uuid")
345 vm_id = (new_vm_ids.get(xvmu)
347 or get_vm_id(row.name, xvmu))
348 new_vm_ids[xvmu] = vm_id
351 set_external_id(row, "vm-id", vm_id)
353 # When there's a vif and a tap, the tap is active (used for
354 # traffic). When there's just a vif, the vif is active.
356 # A tap on its own shouldn't happen, and we don't know
357 # anything about other kinds of devices, so we don't use
358 # an iface-status for those devices at all.
360 set_external_id(tap, "iface-status", "active")
361 set_external_id(vif, "iface-status", "inactive")
363 set_external_id(vif, "iface-status", "active")
365 set_external_id(row, "iface-status", None)
366 iface_ids = new_iface_ids
372 if __name__ == '__main__':
376 # Let system.exit() calls complete normally
379 vlog.exception("traceback")
380 sys.exit(ovs.daemon.RESTART_EXIT_CODE)