Merge citrix branch into master.
[openvswitch] / xenserver / usr_share_vswitch_scripts_dump-vif-details
1 #!/usr/bin/python
2 #
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.
6
7 # Copyright (C) 2009 Nicira Networks, Inc.
8 #
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.
13
14 import sys
15 import XenAPI
16 import xen.lowlevel.xs
17
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)
24         return vif_ref
25
26 # Query XAPI for the information we need using the vif's opaque reference
27 def dump_vif_info(domid, devid, vif_ref):
28         vif_info = []
29         session = XenAPI.xapi_local()
30         session.xenapi.login_with_password("root", "")
31         try: 
32                 vif_rec = session.xenapi.VIF.get_record(vif_ref)
33                 net_rec = session.xenapi.network.get_record(vif_rec["network"])
34                 vm_uuid = session.xenapi.VM.get_uuid(vif_rec["VM"])
35
36                 # Data to allow vNetManager to associate VIFs with xapi data
37                 add_port = '--add=port.vif%s.%s' % (domid, devid)
38                 vif_info.append('%s.net-uuid=%s' % (add_port, net_rec["uuid"]))
39                 vif_info.append('%s.vif-mac=%s' % (add_port, vif_rec["MAC"]))
40                 vif_info.append('%s.vif-uuid=%s' % (add_port, vif_rec["uuid"]))
41                 vif_info.append('%s.vm-uuid=%s' % (add_port, vm_uuid))
42
43                 # vNetManager needs to know the network UUID(s) associated with
44                 # each datapath.  Normally interface-reconfigure adds them, but
45                 # interface-reconfigure never gets called for internal networks
46                 # (xapi does the addbr ioctl internally), so we have to do it
47                 # here instead for internal networks.  This is only acceptable
48                 # because xapi is lazy about creating internal networks: it
49                 # only creates one just before it adds the first vif to it.
50                 # There may still be a brief delay between the initial
51                 # ovs-vswitchd connection to vNetManager and setting this
52                 # configuration variable, but vNetManager can tolerate that.
53                 if not net_rec['PIFs']:
54                         key = 'bridge.%s.xs-network-uuids' % net_rec['bridge']
55                         value = net_rec['uuid']
56                         vif_info.append('--del-match=%s=*' % key)
57                         vif_info.append('--add=%s=%s' % (key, value))
58         finally:
59                 session.xenapi.session.logout()
60         print ' '.join(vif_info)
61         
62 if __name__ == '__main__':
63         if len(sys.argv) != 3:
64                 sys.stderr.write("ERROR: %s <domid> <devid>\n" % sys.argv[0])
65                 sys.exit(1)
66
67         domid = sys.argv[1]
68         devid = sys.argv[2]
69
70         vif_ref = get_vif_ref(domid, devid)
71         if not vif_ref:
72                 sys.stderr.write("ERROR: Could not find interface vif%s.%s\n" 
73                                  % (domid, devid))
74                 sys.exit(1)
75
76         dump_vif_info(domid, devid, vif_ref)
77         sys.exit(0)