~ubuntu-branches/ubuntu/utopic/xen/utopic

« back to all changes in this revision

Viewing changes to tools/python/xen/xend/XendVnet.py

  • Committer: Bazaar Package Importer
  • Author(s): Bastian Blank
  • Date: 2010-05-06 15:47:38 UTC
  • mto: (1.3.1) (15.1.1 sid) (4.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20100506154738-agoz0rlafrh1fnq7
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#============================================================================
 
18
 
 
19
"""Handler for vnet operations.
 
20
"""
 
21
 
 
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
 
27
 
 
28
 
 
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()
 
39
 
 
40
class XendVnetInfo:
 
41
    
 
42
    vifctl_ops = {'up': 'vif.add', 'down': 'vif.del'}
 
43
 
 
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()
 
53
            
 
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
 
60
 
 
61
 
 
62
    def exportToDB(self, save=False, sync=False):
 
63
        to_store = {
 
64
            'id' : self.id,
 
65
            'dbid' : self.dbid,
 
66
            'config' : sxp.to_string(self.config)
 
67
            }
 
68
        xstransact.Write(self.dbpath, to_store)
 
69
 
 
70
 
 
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)
 
77
 
 
78
 
 
79
    def sxpr(self):
 
80
        return self.config
 
81
 
 
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
 
88
        
 
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
 
96
 
 
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)
 
103
 
 
104
class XendVnet:
 
105
    """Index of all vnets. Singleton.
 
106
    """
 
107
 
 
108
    dbpath = "/vnet"
 
109
 
 
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)
 
124
 
 
125
    def vnet_of_bridge(self, bridge):
 
126
        """Get the vnet for a bridge (if any).
 
127
 
 
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
 
136
 
 
137
    def vnet_ls(self):
 
138
        """List all vnet ids.
 
139
        """
 
140
        return self.vnet.keys()
 
141
 
 
142
    def vnets(self):
 
143
        """List all vnets.
 
144
        """
 
145
        return self.vnet.values()
 
146
 
 
147
    def vnet_get(self, id):
 
148
        """Get a vnet.
 
149
 
 
150
        @param id vnet id
 
151
        """
 
152
        id = str(id)
 
153
        return self.vnet.get(id)
 
154
 
 
155
    def vnet_create(self, config):
 
156
        """Create a vnet.
 
157
 
 
158
        @param config: config
 
159
        """
 
160
        info = XendVnetInfo(self.dbpath, config=config)
 
161
        self.vnet[info.id] = info
 
162
        info.exportToDB()
 
163
        info.configure()
 
164
 
 
165
    def vnet_delete(self, id):
 
166
        """Delete a vnet.
 
167
 
 
168
        @param id: vnet id
 
169
        """
 
170
        info = self.vnet_get(id)
 
171
        if info:
 
172
            del self.vnet[id]
 
173
            info.delete()
 
174
 
 
175
def instance():
 
176
    global inst
 
177
    try:
 
178
        inst
 
179
    except:
 
180
        inst = XendVnet()
 
181
    return inst