vswitch: Allow user to set Ethernet address of any internal interface.
[sliver-openvswitch.git] / xenserver / root_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         try: 
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"])
34
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"]))
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 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))
60         finally:
61                 session.xenapi.session.logout()
62         
63 if __name__ == '__main__':
64         if (len(sys.argv) != 3):
65                 sys.stderr.write("ERROR: %s <domid> <devid>\n")
66                 sys.exit(1)
67
68         domid = sys.argv[1]
69         devid = sys.argv[2]
70
71         vif_ref = get_vif_ref(domid, devid)
72         if not vif_ref:
73                 sys.stderr.write("ERROR: Could not find interface vif%s.%s\n" 
74                                 % (domid, devid))
75                 sys.exit(1)
76
77         dump_vif_info(domid, devid, vif_ref)
78         sys.exit(0)