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
# Copyright (C) 2008 Citrix Systems Inc.
18
#============================================================================
20
# Based closely on netif.py.
23
"""Support for virtual network interfaces, version 2.
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
38
from xen.xend.XendLogging import log
40
xoptions = XendOptions.instance()
43
"""Generate a random MAC address.
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.
49
The remaining 3 fields are random, with the first bit of the first
52
@return: MAC address string
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))
60
class NetifController2(DevController):
61
def __init__(self, vm):
62
DevController.__init__(self, vm)
64
def getDeviceDetails(self, config):
65
"""@see DevController.getDeviceDetails"""
67
devid = self.allocateDeviceID()
69
bridge = config.get('bridge')
70
back_mac = config.get('back_mac')
73
back_mac = "fe:ff:ff:ff:ff:ff"
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:
87
back_filter = config.get("filter_mac")
88
if back_filter == None:
89
if front_trust == "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 }
98
front = { 'mac': front_mac, 'remote-mac': back_mac,
99
'local-trusted': front_trust, 'remote-trusted': back_trust,
100
'filter-mac': front_filter }
103
back['bridge'] = bridge
108
return (devid, back, front)
110
def getDeviceConfiguration(self, devid, transaction = None):
111
"""@see DevController.configuration"""
113
if transaction is None:
114
read_fn = xstransact.Read
116
read_fn = transaction.read
118
return read_fn(frontpath + x)
120
return read_fn(backpath + x)
122
result = DevController.getDeviceConfiguration(self, devid, transaction)
124
dev = self.convertToDeviceNumber(devid)
125
frontpath = self.frontendPath(dev) + "/"
127
backpath = front_read("backend") + "/"
129
front_mac = front_read("mac")
130
back_mac = back_read("mac")
132
front_trusted = back_read("remote-trusted")
133
back_trusted = back_read("local-trusted")
134
max_bypasses = back_read("max-bypasses")
136
bridge = back_read("bridge")
138
pdev = back_read("pdev")
141
result["front_mac"] = front_mac
143
result["back_mac"] = back_mac
145
result["front_trusted"] = front_trusted
147
result["back_trusted"] = back_trusted
149
result["bridge"] = bridge
151
result["pdev"] = pdev
153
result["max-bypasses"] = max_bypasses
156
def destroyDevice(self, devid, force):
157
dev = self.convertToDeviceNumber(devid)
158
self.writeBackend(dev, "online", "0")
160
self.writeBackend(dev, "shutdown-request", "force")
162
self.writeBackend(dev, "shutdown-request", "normal")
163
self.vm._removeVm("device/%s/%d" % (self.deviceClass, dev))