3 # Script to retrieve extended information about VIFs that are
4 # needed by the controller. This is called by the "vif" script,
5 # which is run when virtual interfaces are added and removed.
7 # Copyright (C) 2009 Nicira Networks, Inc.
9 # Copying and distribution of this file, with or without modification,
10 # are permitted in any medium without royalty provided the copyright
11 # notice and this notice are preserved. This file is offered as-is,
12 # without warranty of any kind.
16 import xen.lowlevel.xs
18 # Query XenStore for the opaque reference of this vif
19 def get_vif_ref(domid, devid):
20 xenstore = xen.lowlevel.xs.xs()
21 t = xenstore.transaction_start()
22 vif_ref = xenstore.read(t, '/xapi/%s/private/vif/%s/ref' % (domid, devid))
23 xenstore.transaction_end(t)
26 # Query XAPI for the information we need using the vif's opaque reference
27 def dump_vif_info(domid, devid, vif_ref):
28 session = XenAPI.xapi_local()
29 session.xenapi.login_with_password("root", "")
31 vif_rec = session.xenapi.VIF.get_record(vif_ref)
32 net_rec = session.xenapi.network.get_record(vif_rec["network"])
33 vm_uuid = session.xenapi.VM.get_uuid(vif_rec["VM"])
35 # Data to allow vNetManager to associate VIFs with xapi data
37 vif_info.append(('xs-network-uuid', net_rec["uuid"]))
38 vif_info.append(('xs-vif-mac', vif_rec["MAC"]))
39 vif_info.append(('xs-vif-uuid', vif_rec["uuid"]))
40 vif_info.append(('xs-vm-uuid', vm_uuid))
41 for key, value in vif_info:
42 print("-- iface-set-external-id vif%s.%s %s %s"
43 % (domid, devid, key, value))
45 # vNetManager needs to know the network UUID(s) associated with
46 # each datapath. Normally interface-reconfigure adds them, but
47 # interface-reconfigure never gets called for internal networks
48 # (xapi does the addbr ioctl internally), so we have to do it
49 # here instead for internal networks. This is only acceptable
50 # because xapi is lazy about creating internal networks: it
51 # only creates one just before it adds the first vif to it.
52 # There may still be a brief delay between the initial
53 # ovs-vswitchd connection to vNetManager and setting this
54 # configuration variable, but vNetManager can tolerate that.
55 if not net_rec['PIFs']:
56 bridge = net_rec['bridge']
58 xs_network_uuid = net_rec['uuid']
59 print("-- br-set-external-id %s %s %s"
60 % (bridge, "xs-network-uuids", xs_network_uuid))
62 xs_network_name = net_rec['name_label']
63 print("-- br-set-external-id %s %s %s"
64 % (bridge, "xs-network-names", xs_network_name))
66 session.xenapi.session.logout()
68 if __name__ == '__main__':
69 if len(sys.argv) != 3:
70 sys.stderr.write("ERROR: %s <domid> <devid>\n" % sys.argv[0])
76 vif_ref = get_vif_ref(domid, devid)
78 sys.stderr.write("ERROR: Could not find interface vif%s.%s\n"
82 dump_vif_info(domid, devid, vif_ref)