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

« back to all changes in this revision

Viewing changes to tools/python/xen/xend/server/netif2.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
# Copyright (C) 2008 Citrix Systems Inc.
 
18
#============================================================================
 
19
#
 
20
# Based closely on netif.py.
 
21
#
 
22
 
 
23
"""Support for virtual network interfaces, version 2.
 
24
"""
 
25
 
 
26
import os
 
27
import random
 
28
import re
 
29
import time
 
30
 
 
31
from xen.xend import XendOptions
 
32
from xen.xend.server.DevController import DevController
 
33
from xen.xend.XendError import VmError
 
34
from xen.xend.XendXSPolicyAdmin import XSPolicyAdminInstance
 
35
from xen.xend.xenstore.xstransact import xstransact
 
36
import xen.util.xsm.xsm as security
 
37
 
 
38
from xen.xend.XendLogging import log
 
39
 
 
40
xoptions = XendOptions.instance()
 
41
 
 
42
def randomMAC():
 
43
    """Generate a random MAC address.
 
44
 
 
45
    Uses OUI (Organizationally Unique Identifier) 00-16-3E, allocated to
 
46
    Xensource, Inc. The OUI list is available at
 
47
    http://standards.ieee.org/regauth/oui/oui.txt.
 
48
 
 
49
    The remaining 3 fields are random, with the first bit of the first
 
50
    random field set 0.
 
51
 
 
52
    @return: MAC address string
 
53
    """
 
54
    mac = [ 0x00, 0x16, 0x3e,
 
55
            random.randint(0x00, 0x7f),
 
56
            random.randint(0x00, 0xff),
 
57
            random.randint(0x00, 0xff) ]
 
58
    return ':'.join(map(lambda x: "%02x" % x, mac))
 
59
 
 
60
class NetifController2(DevController):
 
61
    def __init__(self, vm):
 
62
        DevController.__init__(self, vm)
 
63
 
 
64
    def getDeviceDetails(self, config):
 
65
        """@see DevController.getDeviceDetails"""
 
66
 
 
67
        devid = self.allocateDeviceID()
 
68
 
 
69
        bridge = config.get('bridge')
 
70
        back_mac = config.get('back_mac')
 
71
        if not back_mac:
 
72
            if bridge:
 
73
                back_mac = "fe:ff:ff:ff:ff:ff"
 
74
            else:
 
75
                back_mac = randomMAC()
 
76
        front_mac = config.get('front_mac') or randomMAC()
 
77
        front_trust = config.get("trusted") or "0"
 
78
        back_trust = config.get("back_trusted") or "1"
 
79
        max_bypasses = config.get("max_bypasses") or "5"
 
80
        pdev = config.get('pdev')
 
81
        front_filter = config.get("front_filter_mac")
 
82
        if front_filter == None:
 
83
            if back_trust == "0":
 
84
                front_filter = "1"
 
85
            else:
 
86
                front_filter = "0"
 
87
        back_filter = config.get("filter_mac")
 
88
        if back_filter == None:
 
89
            if front_trust == "0":
 
90
                back_filter = "1"
 
91
            else:
 
92
                back_filter = "0"
 
93
        back = { 'mac': back_mac, 'remote-mac': front_mac,
 
94
                 'handle': "%i" % devid, 'local-trusted': back_trust,
 
95
                 'remote-trusted': front_trust, 'filter-mac': back_filter,
 
96
                 'max-bypasses': max_bypasses }
 
97
 
 
98
        front = { 'mac': front_mac, 'remote-mac': back_mac,
 
99
                  'local-trusted': front_trust, 'remote-trusted': back_trust,
 
100
                  'filter-mac': front_filter }
 
101
 
 
102
        if bridge:
 
103
            back['bridge'] = bridge
 
104
 
 
105
        if pdev:
 
106
            back['pdev'] = pdev
 
107
    
 
108
        return (devid, back, front)
 
109
 
 
110
    def getDeviceConfiguration(self, devid, transaction = None):
 
111
        """@see DevController.configuration"""
 
112
 
 
113
        if transaction is None:
 
114
            read_fn = xstransact.Read
 
115
        else:
 
116
            read_fn = transaction.read
 
117
        def front_read(x):
 
118
            return read_fn(frontpath + x)
 
119
        def back_read(x):
 
120
            return read_fn(backpath + x)
 
121
        
 
122
        result = DevController.getDeviceConfiguration(self, devid, transaction)
 
123
 
 
124
        dev = self.convertToDeviceNumber(devid)
 
125
        frontpath = self.frontendPath(dev) + "/"
 
126
 
 
127
        backpath = front_read("backend") + "/"
 
128
 
 
129
        front_mac = front_read("mac")
 
130
        back_mac = back_read("mac")
 
131
 
 
132
        front_trusted = back_read("remote-trusted")
 
133
        back_trusted = back_read("local-trusted")
 
134
        max_bypasses = back_read("max-bypasses")
 
135
 
 
136
        bridge = back_read("bridge")
 
137
 
 
138
        pdev = back_read("pdev")
 
139
 
 
140
        if front_mac:
 
141
            result["front_mac"] = front_mac
 
142
        if back_mac:
 
143
            result["back_mac"] = back_mac
 
144
        if front_trusted:
 
145
            result["front_trusted"] = front_trusted
 
146
        if back_trusted:
 
147
            result["back_trusted"] = back_trusted
 
148
        if bridge:
 
149
            result["bridge"] = bridge
 
150
        if pdev:
 
151
            result["pdev"] = pdev
 
152
        if max_bypasses:
 
153
            result["max-bypasses"] = max_bypasses
 
154
        return result
 
155
 
 
156
    def destroyDevice(self, devid, force):
 
157
        dev = self.convertToDeviceNumber(devid)
 
158
        self.writeBackend(dev, "online", "0")
 
159
        if force:
 
160
            self.writeBackend(dev, "shutdown-request", "force")
 
161
        else:
 
162
            self.writeBackend(dev, "shutdown-request", "normal")
 
163
        self.vm._removeVm("device/%s/%d" % (self.deviceClass, dev))