xen-vtx-unstable

view tools/python/xen/xend/XendVnet.py @ 6774:4d899a738d59

merge?
author cl349@firebug.cl.cam.ac.uk
date Tue Sep 13 15:05:49 2005 +0000 (2005-09-13)
parents dd668f7527cb 64d6de1ea743
children e7c7196fa329 8ca0f98ba8e2
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 #============================================================================
18 """Handler for vnet operations.
19 """
21 from xen.util import Brctl
22 from xen.xend import sxp
23 from xen.xend.XendError import XendError
24 from xen.xend.XendLogging import log
25 from xen.xend.xenstore import XenNode, DBMap, DBVar
27 def vnet_cmd(cmd):
28 out = None
29 try:
30 try:
31 out = file("/proc/vnet/policy", "wb")
32 sxp.show(cmd, out)
33 except IOError, ex:
34 raise XendError(str(ex))
35 finally:
36 if out: out.close()
38 class XendVnetInfo:
40 vifctl_ops = {'up': 'vif.add', 'down': 'vif.del'}
42 __exports__ = [
43 DBVar('id', ty='str'),
44 DBVar('dbid', ty='str'),
45 DBVar('config', ty='sxpr'),
46 ]
48 def __init__(self, db, config=None):
49 if config:
50 self.id = sxp.child_value(config, 'id')
51 self.id = str(self.id)
52 self.dbid = self.id.replace(':', '-')
53 self.db = db.addChild(self.dbid)
54 self.config = config
55 else:
56 self.db = db
57 self.importFromDB()
58 config = self.config
60 self.bridge = sxp.child_value(config, 'bridge')
61 if not self.bridge:
62 self.bridge = "vnet%s" % self.id
63 self.vnetif = sxp.child_value(config, 'vnetif')
64 if not self.vnetif:
65 self.vnetif = "vnif%s" % self.id
67 def saveToDB(self, save=False, sync=False):
68 self.db.saveDB(save=save, sync=sync)
70 def exportToDB(self, save=False, sync=False):
71 self.db.exportToDB(self, fields=self.__exports__, save=save, sync=sync)
73 def importFromDB(self):
74 self.db.importFromDB(self, fields=self.__exports__)
76 def sxpr(self):
77 return self.config
79 def configure(self):
80 log.info("Configuring vnet %s", self.id)
81 val = vnet_cmd(['vnet.add'] + sxp.children(self.config))
82 Brctl.bridge_create(self.bridge)
83 Brctl.vif_bridge_add({'bridge': self.bridge, 'vif': self.vnetif})
84 return val
86 def delete(self):
87 log.info("Deleting vnet %s", self.id)
88 Brctl.vif_bridge_rem({'bridge': self.bridge, 'vif': self.vnetif})
89 Brctl.bridge_del(self.bridge)
90 val = vnet_cmd(['vnet.del', self.id])
91 self.db.delete()
92 return val
94 def vifctl(self, op, vif, vmac):
95 try:
96 fn = self.vifctl_ops[op]
97 return vnet_cmd([fn, ['vnet', self.id], ['vif', vif], ['vmac', vmac]])
98 except XendError:
99 log.warning("vifctl failed: op=%s vif=%s mac=%s", op, vif, vmac)
101 class XendVnet:
102 """Index of all vnets. Singleton.
103 """
105 dbpath = "/vnet"
107 def __init__(self):
108 # Table of vnet info indexed by vnet id.
109 self.vnet = {}
110 self.db = DBMap(db=XenNode(self.dbpath))
111 self.db.readDB()
112 for vnetdb in self.db.values():
113 try:
114 info = XendVnetInfo(vnetdb)
115 self.vnet[info.id] = info
116 info.configure()
117 except XendError, ex:
118 log.warning("Failed to configure vnet %s: %s", str(info.id), str(ex))
119 except Exception, ex:
120 log.exception("Vnet error")
121 vnetdb.delete()
123 def vnet_of_bridge(self, bridge):
124 """Get the vnet for a bridge (if any).
126 @param bridge: bridge name
127 @return vnet or None
128 """
129 for v in self.vnet.values():
130 if v.bridge == bridge:
131 return v
132 else:
133 return None
135 def vnet_ls(self):
136 """List all vnet ids.
137 """
138 return self.vnet.keys()
140 def vnets(self):
141 """List all vnets.
142 """
143 return self.vnet.values()
145 def vnet_get(self, id):
146 """Get a vnet.
148 @param id vnet id
149 """
150 id = str(id)
151 return self.vnet.get(id)
153 def vnet_create(self, config):
154 """Create a vnet.
156 @param config: config
157 """
158 info = XendVnetInfo(self.db, config=config)
159 self.vnet[info.id] = info
160 info.saveToDB()
161 info.configure()
163 def vnet_delete(self, id):
164 """Delete a vnet.
166 @param id: vnet id
167 """
168 info = self.vnet_get(id)
169 if info:
170 del self.vnet[id]
171 info.delete()
173 def instance():
174 global inst
175 try:
176 inst
177 except:
178 inst = XendVnet()
179 return inst