ia64/xen-unstable

view tools/python/xen/xend/XendVnet.py @ 6552:a9873d384da4

Merge.
author adsharma@los-vmm.sc.intel.com
date Thu Aug 25 12:24:48 2005 -0700 (2005-08-25)
parents 112d44270733 fa0754a9f64f
children dfaf788ab18c
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
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 def __init__(self, config):
43 self.config = config
44 self.id = sxp.child_value(config, 'id')
45 self.id = str(self.id)
46 self.bridge = sxp.child_value(config, 'bridge')
47 if not self.bridge:
48 self.bridge = "vnet%s" % self.id
49 self.vnetif = sxp.child_value(config, 'vnetif')
50 if not self.vnetif:
51 self.vnetif = "vnetif%s" % self.id
53 def sxpr(self):
54 return self.config
56 def configure(self):
57 log.info("Configuring vnet %s", self.id)
58 val = vnet_cmd(['vnet.add'] + sxp.children(self.config))
59 Brctl.bridge_create(self.bridge)
60 Brctl.vif_bridge_add({'bridge': self.bridge, 'vif': self.vnetif})
61 return val
63 def delete(self):
64 log.info("Deleting vnet %s", self.id)
65 Brctl.vif_bridge_rem({'bridge': self.bridge, 'vif': self.vnetif})
66 Brctl.bridge_del(self.bridge)
67 return vnet_cmd(['vnet.del', self.id])
69 def vifctl(self, op, vif, vmac):
70 try:
71 fn = self.vifctl_ops[op]
72 return vnet_cmd([fn, ['vnet', self.id], ['vif', vif], ['vmac', vmac]])
73 except XendError:
74 log.warning("vifctl failed: op=%s vif=%s mac=%s", op, vif, vmac)
76 class XendVnet:
77 """Index of all vnets. Singleton.
78 """
80 dbpath = "/vnet"
82 def __init__(self):
83 # Table of vnet info indexed by vnet id.
84 self.vnet = {}
85 self.dbmap = DBMap(db=XenNode(self.dbpath))
86 self.dbmap.readDB()
87 for vnetdb in self.dbmap.values():
88 config = vnetdb.config
89 info = XendVnetInfo(config)
90 self.vnet[info.id] = info
91 try:
92 info.configure()
93 except XendError, ex:
94 log.warning("Failed to configure vnet %s: %s", str(info.id), str(ex))
96 def vnet_of_bridge(self, bridge):
97 """Get the vnet for a bridge (if any).
99 @param bridge: bridge name
100 @return vnet or None
101 """
102 for v in self.vnet.values():
103 if v.bridge == bridge:
104 return v
105 else:
106 return None
108 def vnet_ls(self):
109 """List all vnet ids.
110 """
111 return self.vnet.keys()
113 def vnets(self):
114 """List all vnets.
115 """
116 return self.vnet.values()
118 def vnet_get(self, id):
119 """Get a vnet.
121 @param id: vnet id
122 """
123 id = str(id)
124 return self.vnet.get(id)
126 def vnet_create(self, config):
127 """Create a vnet.
129 @param config: config
130 """
131 info = XendVnetInfo(config)
132 self.vnet[info.id] = info
133 self.dbmap["%s/config" % info.id] = info.sxpr()
134 info.configure()
136 def vnet_delete(self, id):
137 """Delete a vnet.
139 @param id: vnet id
140 """
141 info = self.vnet_get(id)
142 if info:
143 del self.vnet[id]
144 self.dbmap.delete(id)
145 info.delete()
147 def instance():
148 global inst
149 try:
150 inst
151 except:
152 inst = XendVnet()
153 return inst