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.
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.
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.
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
33
out = file("/proc/vnet/policy", "wb")
36
raise XendError(str(ex))
42
vifctl_ops = {'up': 'vif.add', 'down': 'vif.del'}
44
def __init__(self, dbpath, config=None):
46
self.id = str(sxp.child_value(config, 'id'))
47
self.dbid = self.id.replace(':', '-')
48
self.dbpath = dbpath + '/' + self.dbid
54
self.bridge = sxp.child_value(self.config, 'bridge')
56
self.bridge = "vnet%s" % self.id
57
self.vnetif = sxp.child_value(self.config, 'vnetif')
59
self.vnetif = "vnif%s" % self.id
62
def exportToDB(self, save=False, sync=False):
66
'config' : sxp.to_string(self.config)
68
xstransact.Write(self.dbpath, to_store)
71
def importFromDB(self):
72
(self.id, self.dbid, c) = xstransact.Gather(self.dbpath,
76
self.config = sxp.from_string(c)
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})
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)
97
def vifctl(self, op, vif, vmac):
99
fn = self.vifctl_ops[op]
100
return vnet_cmd([fn, ['vnet', self.id], ['vif', vif], ['vmac', vmac]])
102
log.warning("vifctl failed: op=%s vif=%s mac=%s", op, vif, vmac)
105
"""Index of all vnets. Singleton.
111
# Table of vnet info indexed by vnet id.
113
listing = xstransact.List(self.dbpath)
114
for entry in listing:
116
info = XendVnetInfo(self.dbpath + '/' + entry)
117
self.vnet[info.id] = info
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
131
for v in self.vnet.values():
132
if v.bridge == bridge:
138
"""List all vnet ids.
140
return self.vnet.keys()
145
return self.vnet.values()
147
def vnet_get(self, id):
153
return self.vnet.get(id)
155
def vnet_create(self, config):
158
@param config: config
160
info = XendVnetInfo(self.dbpath, config=config)
161
self.vnet[info.id] = info
165
def vnet_delete(self, id):
170
info = self.vnet_get(id)