ia64/xen-unstable

view tools/python/xen/xend/XendVnet.py @ 7162:70d5bfbf23d4

Move XendVnet from using DBMap etc to using xstransact. This is the last
dependency upon xsnode, xsobj, and xsresource, so these can all go.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@ewan
date Sun Oct 02 18:22:22 2005 +0100 (2005-10-02)
parents 06d84bf87159
children 260a09e9a9c1
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 # Copyright (C) 2005 XenSource Ltd
17 #============================================================================
19 """Handler for vnet operations.
20 """
22 from xen.util import Brctl
23 from xen.xend import sxp
24 from xen.xend.XendError import XendError
25 from xen.xend.XendLogging import log
26 from xen.xend.xenstore.xstransact import xstransact
29 def vnet_cmd(cmd):
30 out = None
31 try:
32 try:
33 out = file("/proc/vnet/policy", "wb")
34 sxp.show(cmd, out)
35 except IOError, ex:
36 raise XendError(str(ex))
37 finally:
38 if out: out.close()
40 class XendVnetInfo:
42 vifctl_ops = {'up': 'vif.add', 'down': 'vif.del'}
44 def __init__(self, dbpath, config=None):
45 if config:
46 self.id = str(sxp.child_value(config, 'id'))
47 self.dbid = self.id.replace(':', '-')
48 self.dbpath = dbpath + '/' + self.dbid
49 self.config = config
50 else:
51 self.dbpath = dbpath
52 self.importFromDB()
54 self.bridge = sxp.child_value(self.config, 'bridge')
55 if not self.bridge:
56 self.bridge = "vnet%s" % self.id
57 self.vnetif = sxp.child_value(self.config, 'vnetif')
58 if not self.vnetif:
59 self.vnetif = "vnif%s" % self.id
62 def exportToDB(self, save=False, sync=False):
63 to_store = {
64 'id' : self.id,
65 'dbid' : self.dbid,
66 'config' : sxp.to_string
67 }
68 xstransact.Write(self.dbpath, to_store)
71 def importFromDB(self):
72 (self.id, self.dbid, c) = xstransact.Gather(self.dbpath,
73 ('id', str),
74 ('dbid', str),
75 ('config', str))
76 self.config = sxp.from_string(c)
79 def sxpr(self):
80 return self.config
82 def configure(self):
83 log.info("Configuring vnet %s", self.id)
84 val = vnet_cmd(['vnet.add'] + sxp.children(self.config))
85 Brctl.bridge_create(self.bridge)
86 Brctl.vif_bridge_add({'bridge': self.bridge, 'vif': self.vnetif})
87 return val
89 def delete(self):
90 log.info("Deleting vnet %s", self.id)
91 Brctl.vif_bridge_rem({'bridge': self.bridge, 'vif': self.vnetif})
92 Brctl.bridge_del(self.bridge)
93 val = vnet_cmd(['vnet.del', self.id])
94 xstransact.Remove(self.dbpath)
95 return val
97 def vifctl(self, op, vif, vmac):
98 try:
99 fn = self.vifctl_ops[op]
100 return vnet_cmd([fn, ['vnet', self.id], ['vif', vif], ['vmac', vmac]])
101 except XendError:
102 log.warning("vifctl failed: op=%s vif=%s mac=%s", op, vif, vmac)
104 class XendVnet:
105 """Index of all vnets. Singleton.
106 """
108 dbpath = "/vnet"
110 def __init__(self):
111 # Table of vnet info indexed by vnet id.
112 self.vnet = {}
113 listing = xstransact.List(self.dbpath)
114 for entry in listing:
115 try:
116 info = XendVnetInfo(self.dbpath + '/' + entry)
117 self.vnet[info.id] = info
118 info.configure()
119 except XendError, ex:
120 log.warning("Failed to configure vnet %s: %s", str(info.id), str(ex))
121 except Exception, ex:
122 log.exception("Vnet error")
123 xstransact.Remove(self.dbpath + '/' + entry)
125 def vnet_of_bridge(self, bridge):
126 """Get the vnet for a bridge (if any).
128 @param bridge: bridge name
129 @return vnet or None
130 """
131 for v in self.vnet.values():
132 if v.bridge == bridge:
133 return v
134 else:
135 return None
137 def vnet_ls(self):
138 """List all vnet ids.
139 """
140 return self.vnet.keys()
142 def vnets(self):
143 """List all vnets.
144 """
145 return self.vnet.values()
147 def vnet_get(self, id):
148 """Get a vnet.
150 @param id vnet id
151 """
152 id = str(id)
153 return self.vnet.get(id)
155 def vnet_create(self, config):
156 """Create a vnet.
158 @param config: config
159 """
160 info = XendVnetInfo(self.dbpath, config=config)
161 self.vnet[info.id] = info
162 info.exportToDB()
163 info.configure()
165 def vnet_delete(self, id):
166 """Delete a vnet.
168 @param id: vnet id
169 """
170 info = self.vnet_get(id)
171 if info:
172 del self.vnet[id]
173 info.delete()
175 def instance():
176 global inst
177 try:
178 inst
179 except:
180 inst = XendVnet()
181 return inst