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):
29 session = XenAPI.xapi_local()
30 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_rec = session.xenapi.VM.get_record(vif_rec["VM"])
35 # Data to allow vNetManager to associate VIFs with xapi data
36 sys.stdout.write('--add=port.vif%s.%s.net-uuid=%s '
37 % (domid, devid, net_rec["uuid"]))
38 sys.stdout.write('--add=port.vif%s.%s.vif-mac=%s '
39 % (domid, devid, vif_rec["MAC"]))
40 sys.stdout.write('--add=port.vif%s.%s.vif-uuid=%s '
41 % (domid, devid, vif_rec["uuid"]))
42 sys.stdout.write('--add=port.vif%s.%s.vm-uuid=%s '
43 % (domid, devid, vm_rec["uuid"]))
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 len(net_rec['PIFs']) == 0:
56 key = 'bridge.%s.xs-network-uuids' % net_rec['bridge']
57 value = net_rec['uuid']
58 sys.stdout.write('--del-match=%s=* ' % key)
59 sys.stdout.write('--add=%s=%s ' % (key, value))
61 session.xenapi.session.logout()
63 if __name__ == '__main__':
64 if (len(sys.argv) != 3):
65 sys.stderr.write("ERROR: %s <domid> <devid>\n" % sys.argv[0])
71 vif_ref = get_vif_ref(domid, devid)
73 sys.stderr.write("ERROR: Could not find interface vif%s.%s\n"
77 dump_vif_info(domid, devid, vif_ref)