direct-io.hg

view tools/python/xen/xend/server/netif.py @ 7788:504111fd562a

Allocate MAC addresses using Xensource Inc's new OUI.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Mon Nov 14 11:06:25 2005 +0100 (2005-11-14)
parents 6a593de2f8ba
children 7c90df7df37e
line source
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 #============================================================================
20 """Support for virtual network interfaces.
21 """
23 import os
24 import random
26 from xen.xend import sxp
27 from xen.xend import XendRoot
29 from xen.xend.server.DevController import DevController
32 xroot = XendRoot.instance()
35 def randomMAC():
36 """Generate a random MAC address.
38 Uses OUI (Organizationally Unique Identifier) 00-16-3E, allocated to
39 Xensource, Inc. The OUI list is available at
40 http://standards.ieee.org/regauth/oui/oui.txt.
42 The remaining 3 fields are random, with the first bit of the first
43 random field set 0.
45 @return: MAC address string
46 """
47 mac = [ 0x00, 0x16, 0x3e,
48 random.randint(0x00, 0x7f),
49 random.randint(0x00, 0xff),
50 random.randint(0x00, 0xff) ]
51 return ':'.join(map(lambda x: "%02x" % x, mac))
54 class NetifController(DevController):
55 """Network interface controller. Handles all network devices for a domain.
56 """
58 def __init__(self, vm):
59 DevController.__init__(self, vm)
62 def getDeviceDetails(self, config):
63 """@see DevController.getDeviceDetails"""
65 def _get_config_ipaddr(config):
66 val = []
67 for ipaddr in sxp.children(config, elt='ip'):
68 val.append(sxp.child0(ipaddr))
69 return val
71 script = os.path.join(xroot.network_script_dir,
72 sxp.child_value(config, 'script',
73 xroot.get_vif_script()))
74 type = sxp.child_value(config, 'type')
75 if type == 'ioemu':
76 return (None,{},{})
77 bridge = sxp.child_value(config, 'bridge')
78 mac = sxp.child_value(config, 'mac')
79 ipaddr = _get_config_ipaddr(config)
81 devid = self.allocateDeviceID()
83 if not mac:
84 mac = randomMAC()
86 back = { 'script' : script,
87 'mac' : mac,
88 'handle' : "%i" % devid }
89 if ipaddr:
90 back['ip'] = ' '.join(ipaddr)
91 if bridge:
92 back['bridge'] = bridge
94 front = { 'handle' : "%i" % devid,
95 'mac' : mac }
97 return (devid, back, front)
100 def configuration(self, devid):
101 """@see DevController.configuration"""
103 result = DevController.configuration(self, devid)
105 (script, ip, bridge, mac) = self.readBackend(devid,
106 'script', 'ip', 'bridge',
107 'mac')
109 if script:
110 result.append(['script',
111 script.replace(xroot.network_script_dir + os.sep,
112 "")])
113 if ip:
114 result.append(['ip', ip.split(" ")])
115 if bridge:
116 result.append(['bridge', bridge])
117 if mac:
118 result.append(['mac', mac])
120 return result