ia64/xen-unstable

view tools/python/xen/xend/XendPIF.py @ 13192:159ae46d5e7f

Persist the VLAN setting.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Mon Dec 25 17:02:16 2006 +0000 (2006-12-25)
parents 8036efeefbd1
children a8e853aecb3e
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) 2006 Xensource Inc.
16 #============================================================================
18 import os
19 import commands
20 import re
21 import socket
23 from xen.xend.XendRoot import instance as xendroot
24 from xen.xend.XendLogging import log
26 MAC_RE = ':'.join(['[0-9a-f]{2}'] * 6)
27 IP_IFACE_RE = r'^\d+: (\w+):.*mtu (\d+) .* link/\w+ ([0-9a-f:]+)'
29 def linux_phy_to_virt(pif_name):
30 return 'eth' + re.sub(r'^[a-z]+', '', pif_name)
32 def linux_get_phy_ifaces():
33 """Returns a list of physical interfaces.
35 Identifies PIFs as those that have a interface name starting with 'p'
36 and have the fake 'fe:ff:ff:ff:ff:ff' MAC address.
38 See /etc/xen/scripts/network-bridge for how the devices are renamed.
40 @rtype: array of 3-element tuple (name, mtu, mac)
41 """
43 ip_cmd = '/sbin/ip -o link show'
44 rc, output = commands.getstatusoutput(ip_cmd)
45 ifaces = {}
46 phy_ifaces = []
47 if rc == 0:
48 # parse all interfaces into (name, mtu, mac)
49 for line in output.split('\n'):
50 has_if = re.search(IP_IFACE_RE, line)
51 if has_if:
52 ifaces[has_if.group(1)] = has_if.groups()
54 # resolve pifs' mac addresses
55 for name, mtu, mac in ifaces.values():
56 if name[0] == 'p' and mac == 'fe:ff:ff:ff:ff:ff':
57 bridged_ifname = linux_phy_to_virt(name)
58 bridged_if = ifaces.get(bridged_ifname)
59 if bridged_if:
60 bridged_mac = bridged_if[2]
61 phy_ifaces.append((name, int(mtu), bridged_mac))
63 return phy_ifaces
65 def linux_set_mac(iface, mac):
66 if not re.search(MAC_RE, mac):
67 return False
69 ip_mac_cmd = '/sbin/ip link set %s addr %s' % \
70 (linux_phy_to_virt(iface), mac)
71 rc, output = commands.getstatusoutput(ip_mac_cmd)
72 if rc == 0:
73 return True
75 return False
77 def linux_set_mtu(iface, mtu):
78 try:
79 ip_mtu_cmd = '/sbin/ip link set %s mtu %d' % \
80 (linux_phy_to_virt(iface), int(mtu))
81 rc, output = commands.getstatusoutput(ip_mtu_cmd)
82 if rc == 0:
83 return True
84 return False
85 except ValueError:
86 return False
88 def same_dir_rename(old_path, new_path):
89 """Ensure that the old_path and new_path refer to files in the same
90 directory."""
91 old_abs = os.path.normpath(old_path)
92 new_abs = os.path.normpath(new_path)
93 if os.path.dirname(old_abs) == os.path.dirname(new_abs):
94 os.rename(old_abs, new_abs)
95 else:
96 log.warning("Unable to ensure name is new name is safe: %s" % new_abs)
99 class XendPIF:
100 """Representation of a Physical Network Interface."""
102 def __init__(self, uuid, name, mtu, vlan, mac, network, host):
103 self.uuid = uuid
104 self.name = name
105 self.mac = mac
106 self.mtu = mtu
107 self.vlan = vlan
108 self.network = network
109 self.host = host
111 def set_name(self, new_name):
112 self.name = new_name
114 def set_mac(self, new_mac):
115 success = linux_set_mac(new_mac)
116 if success:
117 self.mac = new_mac
118 return success
120 def set_mtu(self, new_mtu):
121 success = linux_set_mtu(new_mtu)
122 if success:
123 self.mtu = new_mtu
124 return success
126 def get_io_read_kbs(self):
127 return 0.0
129 def get_io_write_kbs(self):
130 return 0.0
132 def get_record(self, transient = True):
133 result = {'name': self.name,
134 'MAC': self.mac,
135 'MTU': str(self.mtu),
136 'VLAN': self.vlan,
137 'host': self.host.uuid,
138 'network': self.network.uuid}
139 if transient:
140 result['io_read_kbs'] = str(self.get_io_read_kbs())
141 result['io_write_kbs'] = str(self.get_io_write_kbs())
142 return result