ia64/xen-unstable

view tools/python/xen/xend/XendVnet.py @ 2398:4f6933bb7358

bitkeeper revision 1.1159.1.114 (4136e932LBZ1uaMs2kLCvyBHOfuxTQ)

Trap vifctl errors and warn.
author mjw@wray-m-3.hpl.hp.com
date Thu Sep 02 09:34:42 2004 +0000 (2004-09-02)
parents dae98734f12e
children 879d7c8f0d19
line source
1 # Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 """Handler for vnet operations.
4 """
6 from xen.util import Brctl
8 import sxp
9 import XendDB
10 from XendError import XendError
11 from XendLogging import log
13 def vnet_cmd(cmd):
14 out = None
15 try:
16 try:
17 out = file("/proc/vnet/policy", "wb")
18 sxp.show(cmd, out)
19 except IOError, ex:
20 raise XendError(str(ex))
21 finally:
22 if out: out.close()
24 class XendVnetInfo:
26 vifctl_ops = {'up': 'vif.add', 'down': 'vif.del'}
28 def __init__(self, config):
29 self.config = config
30 self.id = sxp.child_value(config, 'id')
31 self.id = str(self.id)
32 self.bridge = sxp.child_value(config, 'bridge')
33 if not self.bridge:
34 self.bridge = "vnet%s" % self.id
35 self.vnetif = sxp.child_value(config, 'vnetif')
36 if not self.vnetif:
37 self.vnetif = "vnetif%s" % self.id
39 def sxpr(self):
40 return self.config
42 def configure(self):
43 log.info("Configuring vnet %s", self.id)
44 val = vnet_cmd(['vnet.add'] + sxp.children(self.config))
45 Brctl.bridge_create(self.bridge)
46 Brctl.vif_bridge_add({'bridge': self.bridge, 'vif': self.vnetif})
47 return val
49 def delete(self):
50 log.info("Deleting vnet %s", self.id)
51 Brctl.vif_bridge_rem({'bridge': self.bridge, 'vif': self.vnetif})
52 Brctl.bridge_del(self.bridge)
53 return vnet_cmd(['vnet.del', self.id])
55 def vifctl(self, op, vif, vmac):
56 try:
57 fn = self.vifctl_ops[op]
58 return vnet_cmd([fn, ['vif', vif], ['vmac', vmac]])
59 except XendError:
60 log.warning("vifctl failed: op=%s vif=%s mac=%s", op, vif, vmac)
62 class XendVnet:
63 """Index of all vnets. Singleton.
64 """
66 dbpath = "vnet"
68 def __init__(self):
69 # Table of vnet info indexed by vnet id.
70 self.vnet = {}
71 self.db = XendDB.XendDB(self.dbpath)
72 vnets = self.db.fetchall("")
73 for config in vnets.values():
74 info = XendVnetInfo(config)
75 self.vnet[info.id] = info
76 try:
77 info.configure()
78 except XendError, ex:
79 log.warning("Failed to configure vnet %s: %s", str(info.id), str(ex))
81 def vnet_of_bridge(self, bridge):
82 """Get the vnet for a bridge (if any).
84 @param bridge: bridge name
85 @return vnet or None
86 """
87 for v in self.vnet.values():
88 if v.bridge == bridge:
89 return v
90 else:
91 return None
93 def vnet_ls(self):
94 """List all vnet ids.
95 """
96 return self.vnet.keys()
98 def vnets(self):
99 """List all vnets.
100 """
101 return self.vnet.values()
103 def vnet_get(self, id):
104 """Get a vnet.
106 @param id: vnet id
107 """
108 id = str(id)
109 return self.vnet.get(id)
111 def vnet_create(self, config):
112 """Create a vnet.
114 @param config: config
115 """
116 info = XendVnetInfo(config)
117 self.vnet[info.id] = info
118 self.db.save(info.id, info.sxpr())
119 info.configure()
121 def vnet_delete(self, id):
122 """Delete a vnet.
124 @param id: vnet id
125 """
126 info = self.vnet_get(id)
127 if info:
128 del self.vnet[id]
129 self.db.delete(id)
130 info.delete()
132 def instance():
133 global inst
134 try:
135 inst
136 except:
137 inst = XendVnet()
138 return inst