diff options
author | Andy Smith <code@term.ie> | 2011-01-14 17:57:42 -0800 |
---|---|---|
committer | Andy Smith <code@term.ie> | 2011-01-14 17:57:42 -0800 |
commit | 9750e4ab3e41d3f4205b0df56ef8200744c327a0 (patch) | |
tree | be886950dae9603636e0d3bc08e85937621d6990 /nova/utils.py | |
parent | eb6021ad9489185418f545a54e1d415ba6c3429d (diff) | |
parent | 34ceed1ce114ab01eca06eced00a204ae71dc3db (diff) | |
download | nova-9750e4ab3e41d3f4205b0df56ef8200744c327a0.tar.gz nova-9750e4ab3e41d3f4205b0df56ef8200744c327a0.tar.xz nova-9750e4ab3e41d3f4205b0df56ef8200744c327a0.zip |
merge from upstream
Diffstat (limited to 'nova/utils.py')
-rw-r--r-- | nova/utils.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/nova/utils.py b/nova/utils.py index 45adb7b38..27589c30c 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -30,6 +30,8 @@ import struct import sys import time from xml.sax import saxutils +import re +import netaddr from eventlet import event from eventlet import greenthread @@ -200,6 +202,40 @@ def last_octet(address): return int(address.split(".")[-1]) +def get_my_linklocal(interface): + try: + if_str = execute("ip -f inet6 -o addr show %s" % interface) + condition = "\s+inet6\s+([0-9a-f:]+/\d+)\s+scope\s+link" + links = [re.search(condition, x) for x in if_str[0].split('\n')] + address = [w.group(1) for w in links if w is not None] + if address[0] is not None: + return address[0] + else: + return 'fe00::' + except IndexError as ex: + LOG.warn(_("Couldn't get Link Local IP of %s :%s"), interface, ex) + except ProcessExecutionError as ex: + LOG.warn(_("Couldn't get Link Local IP of %s :%s"), interface, ex) + except: + return 'fe00::' + + +def to_global_ipv6(prefix, mac): + mac64 = netaddr.EUI(mac).eui64().words + int_addr = int(''.join(['%02x' % i for i in mac64]), 16) + mac64_addr = netaddr.IPAddress(int_addr) + maskIP = netaddr.IPNetwork(prefix).ip + return (mac64_addr ^ netaddr.IPAddress('::0200:0:0:0') | maskIP).format() + + +def to_mac(ipv6_address): + address = netaddr.IPAddress(ipv6_address) + mask1 = netaddr.IPAddress("::ffff:ffff:ffff:ffff") + mask2 = netaddr.IPAddress("::0200:0:0:0") + mac64 = netaddr.EUI(int(address & mask1 ^ mask2)).words + return ":".join(["%02x" % i for i in mac64[0:3] + mac64[5:8]]) + + def utcnow(): """Overridable version of datetime.datetime.utcnow.""" if utcnow.override_time: |