ovs-vsctl: Drop redundant {port,iface}-{set,get}-external-ids commands.
[sliver-openvswitch.git] / 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, 2010 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         session = XenAPI.xapi_local()
29         session.xenapi.login_with_password("root", "")
30         try: 
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"])
34
35                 # Data to allow vNetManager to associate VIFs with xapi data
36                 vif_info = []
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("-- set interface vif%s.%s external-ids:\"%s\"=\"%s\""
43                               % (domid, devid, key, value))
44
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']
57
58                         xs_network_uuid = net_rec['uuid']
59                         print("-- br-set-external-id %s %s %s"
60                               % (bridge, "xs-network-uuids", xs_network_uuid))
61
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))
65         finally:
66                 session.xenapi.session.logout()
67         
68 if __name__ == '__main__':
69         if len(sys.argv) != 3:
70                 sys.stderr.write("ERROR: %s <domid> <devid>\n" % sys.argv[0])
71                 sys.exit(1)
72
73         domid = sys.argv[1]
74         devid = sys.argv[2]
75
76         vif_ref = get_vif_ref(domid, devid)
77         if not vif_ref:
78                 sys.stderr.write("ERROR: Could not find interface vif%s.%s\n" 
79                                  % (domid, devid))
80                 sys.exit(1)
81
82         dump_vif_info(domid, devid, vif_ref)
83         sys.exit(0)