1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-12 08:58:17 +03:00

F #5027: New address range types for IP6 no SLAAC. Basic lease

functionality
This commit is contained in:
Ruben S. Montero 2017-02-16 16:26:46 +01:00
parent 32576f61fc
commit 948d1d98c4
3 changed files with 381 additions and 107 deletions

View File

@ -39,18 +39,30 @@ public:
// *************************************************************************
// Address Range types
// *************************************************************************
/// *************************************************************************
/**
* Type of Addresses defined by this address range
* Constants are encoded as follows:
*
* option bits address family bits
* ---+----+----+
* ...*|0000|****|
* ----+----+||||+
* |||\___ AR with Ethernet addresses
* ||\____ AR with IPv4 addresses
* |\_____ AR with IPv6 addresses (SLAAC)
* \______ AR with IPv6 addresses (static, non-SLAAC)
*/
enum AddressType
{
NONE = 0x00000000, /** Undefined Address Type */
ETHER = 0x00000001, /** MAC address type */
IP4 = 0x00000003, /** IP version 4 address */
IP6 = 0x00000005, /** IP version 6 address */
IP4_6 = 0x00000007 /** IP dual stack version 4 & 6 addresses */
IP4 = 0x00000003, /** MAC + IP4 address */
IP6 = 0x00000005, /** MAC + IP6 address */
IP6_STATIC = 0x00000009, /** MAC + IP6 (no-SLAAC) address */
IP4_6 = 0x00000007, /** MAC + IP4 + IP6 addresses */
IP4_6_STATIC = 0x0000000B, /** MAC + IP4 + IP6 (no-SLAAC) addresses */
};
/**
@ -67,6 +79,31 @@ public:
*/
static AddressType str_to_type(string& str_type);
/**
* Return true if the address range includes IPv4 addresses
*/
bool is_ipv4() const
{
return (type & 0x00000002) != 0;
}
/**
* Return true if the address range includes IPv6 addresses
*/
bool is_ipv6() const
{
return (type & 0x00000004) != 0;
}
/**
* Return true if the address range includes static IPv6 addresses (host id
* is manually defined)
*/
bool is_ipv6_static() const
{
return (type & 0x00000008) != 0;
}
// *************************************************************************
// Address Range initialization functions
// *************************************************************************
@ -421,13 +458,13 @@ protected:
/* ---------------------------------------------------------------------- */
/**
* Sets the given range of addresses (by index) as used
* @param index the first address to set as used
* @param ix the first address to set as used
* @param sz number of addresses to set
* @param msg describing the error if any
* @param mg describing the error if any
*
* @return 0 if success
*/
virtual int allocate_addr(unsigned int index, unsigned int sz, string& msg) = 0;
virtual int allocate_addr(unsigned int ix, unsigned int sz, string& mg) = 0;
/**
* Gets a range of free addresses
* @param index the first address in the range
@ -487,6 +524,13 @@ private:
*/
int ip_to_i(const string& _ip, unsigned int& i_ip) const;
/**
* IP version 6 to binary (32 bits)
* @param ip string form 2a00:1bc0:b001:A::3
* @return 0 on success
*/
int ip6_to_i(const string& _ip, unsigned int i_ip[]) const;
/**
* IP version 4 to dot notation
*
@ -512,6 +556,14 @@ private:
int ip6_to_s(const unsigned int prefix[], const unsigned int mac[],
string& ip6_s) const;
/**
* IP version 6 to colon notation
*
* @param i_ip Numeric (128 bits) IP
* @return string in colon notation
*/
string ip6_to_s(const unsigned int i_ip6[]) const;
/* ---------------------------------------------------------------------- */
/* NIC setup functions */
/* ---------------------------------------------------------------------- */
@ -537,6 +589,13 @@ private:
*/
void set_ip6(unsigned int addr_index, VectorAttribute * nic) const;
/**
* Writes IPv6 address (no-slaac) to the given NIC attribute
* @param addr_index internal index for the lease
* @param nic attribute of a VMTemplate
*/
void set_ip6_static(unsigned int addr_index, VectorAttribute * nic) const;
/**
* Writes VNET configuration attributes to the given NIC attribute. It
* includes: BRIDGE, VLAN_ID, PHYDEV and INHERIT_VNET_ATTR in oned.conf
@ -605,6 +664,39 @@ private:
/* ---------------------------------------------------------------------- */
/* Restricted Attributes functions */
/* ---------------------------------------------------------------------- */
/**
* Function to parse the IPv4 attribute ("IP") for IP4 and IP4_6 ARs
* @param error_msg if any
* @return 0 on success
*/
int init_ipv4(string& error_msg);
/**
* Function to parse the IPv6 attributes ("GLOBAL_PREFIX" and "ULA_PREFIX")
* for IP6 and IP4_6 ARs
* @param error_msg if any
* @return 0 on success
*/
int init_ipv6(string& error_msg);
/**
* Function to parse the IPv6 attributes no slaac ("IP6") for IP6_STATIC
* and IP4_6_STATIC ARs
* @param error_msg if any
* @return 0 on success
*/
int init_ipv6_static(string& error_msg);
/**
* Function to parse the MAC attributes ("MAC") for all AR types
* @param error_msg if any
* @return 0 on success
*/
int init_mac(string& error_msg);
/**
* Checks for restricted attributes, returns the first one found
*/
bool check(string& rs_attr) const;
/**
@ -655,6 +747,11 @@ private:
*/
unsigned int ula6[2];
/**
* Binary representation of the first IPv6 address in the AR. No SLAAC ARs
*/
unsigned int ip6[4];
/**
* Security Group IDs for this Address Range
*/

View File

@ -169,7 +169,7 @@ class OneVMHelper < OpenNebulaHelper::OneHelper
end
vm_nics.each do |nic|
["IP", "IP6_GLOBAL", "IP6_ULA",
["IP", "IP6_GLOBAL", "IP6_ULA", "IP6",
"VROUTER_IP", "VROUTER_IP6_GLOBAL", "VROUTER_IP6_ULA"].each do |attr|
if nic.has_key?(attr)
ips.push(nic[attr])
@ -752,7 +752,7 @@ in the frontend machine.
next if nic.has_key?("CLI_DONE")
["IP6_LINK", "IP6_ULA", "IP6_GLOBAL"].each do |attr|
["IP6_LINK", "IP6_ULA", "IP6_GLOBAL", "IP6"].each do |attr|
if nic.has_key?(attr)
shown_ips << nic[attr]

View File

@ -32,10 +32,12 @@ string AddressRange::type_to_str(AddressType ob)
{
switch (ob)
{
case ETHER: return "ETHER"; break;
case IP4: return "IP4" ; break;
case IP6: return "IP6" ; break;
case IP4_6: return "IP4_6"; break;
case ETHER: return "ETHER" ;
case IP4: return "IP4" ;
case IP6: return "IP6" ;
case IP6_STATIC: return "IP6_STATIC" ;
case IP4_6: return "IP4_6" ;
case IP4_6_STATIC: return "IP4_6_STATIC";
default: return "";
}
};
@ -62,6 +64,14 @@ AddressRange::AddressType AddressRange::str_to_type(string& str_type)
{
return IP4_6;
}
else if (str_type == "IP4_6_STATIC")
{
return IP4_6_STATIC;
}
else if (str_type == "IP6_STATIC")
{
return IP6_STATIC;
}
else
{
return NONE;
@ -71,6 +81,116 @@ AddressRange::AddressType AddressRange::str_to_type(string& str_type)
/* ************************************************************************** */
/* ************************************************************************** */
int AddressRange::init_ipv4(string& error_msg)
{
if (!is_ipv4())
{
attr->remove("IP");
return 0;
}
string value = attr->vector_value("IP");
if (value.empty() || ip_to_i(value, ip) == -1)
{
error_msg = "Wrong or empty IP attribute";
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
int AddressRange::init_ipv6(string& error_msg)
{
if (!is_ipv6())
{
attr->remove("GLOBAL_PREFIX");
attr->remove("ULA_PREFIX");
return 0;
}
string value = attr->vector_value("GLOBAL_PREFIX");
if (prefix6_to_i(value, global6) != 0 )
{
error_msg = "Wrong format for IP6 global address prefix";
return -1;
}
value = attr->vector_value("ULA_PREFIX");
if (prefix6_to_i(value, ula6) != 0 )
{
error_msg = "Wrong format for IP6 unique local address prefix";
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
int AddressRange::init_ipv6_static(string& error_msg)
{
if (!is_ipv6_static())
{
attr->remove("IP6");
return 0;
}
string value = attr->vector_value("IP6");
if (value.empty() || ip6_to_i(value, ip6) == -1)
{
error_msg = "Wrong or empty IP6 attribute";
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
int AddressRange::init_mac(string& error_msg)
{
string value = attr->vector_value("MAC");
if (value.empty())
{
mac[1] = VirtualNetworkPool::mac_prefix();
if ( is_ipv4() )
{
mac[0] = ip;
}
else
{
srand(time(0));
mac[0] = rand() & 0x0000FFFF;
mac[0]+= (rand()<<16) & 0xFFFF0000;
}
set_mac(0, attr);
}
else
{
if (mac_to_i(value, mac) == -1)
{
error_msg = "Wrong format for MAC attribute";
return -1;
};
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int AddressRange::from_attr(VectorAttribute *vattr, string& error_msg)
{
string value;
@ -94,76 +214,25 @@ int AddressRange::from_attr(VectorAttribute *vattr, string& error_msg)
return -1;
}
/* -------------------- MAC & IPv4 start addresses ---------------------- */
/* ---------------------- L3 & L2 start addresses ---------------------- */
bool do_mac = false;
value = vattr->vector_value("MAC");
if (value.empty())
if ( init_ipv4(error_msg) != 0 )
{
do_mac = true;
mac[1] = VirtualNetworkPool::mac_prefix();
}
else
{
if (mac_to_i(value, mac) == -1)
{
error_msg = "Wrong format for MAC attribute";
return -1;
};
}
switch(type)
{
case ETHER:
case IP6:
vattr->remove("IP");
if (do_mac)
{
srand(time(0));
mac[0] = rand() & 0x0000FFFF;
mac[0]+= (rand()<<16) & 0xFFFF0000;
}
break;
case IP4:
case IP4_6:
value = vattr->vector_value("IP");
if (value.empty() || ip_to_i(value, ip) == -1)
{
error_msg = "Wrong or empty IP attribute";
return -1;
}
if (do_mac)
if ( init_ipv6(error_msg) != 0 )
{
mac[0] = ip;
}
break;
default:
return -1;
}
/* -------------------------- IP6 prefixes ------------------------------ */
value = vattr->vector_value("GLOBAL_PREFIX");
if (prefix6_to_i(value, global6) != 0 )
if ( init_ipv6_static(error_msg) != 0 )
{
error_msg = "Wrong format for IP6 global address prefix";
return -1;
}
value = vattr->vector_value("ULA_PREFIX");
if (prefix6_to_i(value, ula6) != 0 )
if ( init_mac(error_msg) != 0 )
{
error_msg = "Wrong format for IP6 unique local address prefix";
return -1;
}
@ -194,11 +263,6 @@ int AddressRange::from_attr(VectorAttribute *vattr, string& error_msg)
vattr->remove("PARENT_NETWORK");
if (do_mac) //Need to add MAC to the attribute
{
set_mac(0, attr);
}
return 0;
}
@ -237,7 +301,14 @@ int AddressRange::update_attributes(
vup->remove("IP");
if (type & 0x00000002)
if (is_ipv4())
{
vup->replace("IP", attr->vector_value("IP"));
}
vup->remove("IP6");
if (is_ipv6_static())
{
vup->replace("IP", attr->vector_value("IP"));
}
@ -266,6 +337,8 @@ int AddressRange::update_attributes(
vup->remove("IP_END");
vup->remove("IP6_END");
vup->remove("IP6_ULA");
vup->remove("IP6_ULA_END");
@ -367,11 +440,16 @@ int AddressRange::from_vattr_db(VectorAttribute *vattr)
rc += mac_to_i(vattr->vector_value("MAC"), mac);
if (type & 0x00000002)
if (is_ipv4())
{
rc += ip_to_i(vattr->vector_value("IP"), ip);
}
if (is_ipv6_static())
{
rc += ip6_to_i(vattr->vector_value("IP6"), ip6);
}
rc += prefix6_to_i(vattr->vector_value("GLOBAL_PREFIX"), global6);
rc += prefix6_to_i(vattr->vector_value("ULA_PREFIX"), ula6);
@ -426,6 +504,16 @@ void AddressRange::addr_to_xml(unsigned int index, unsigned int rsize,
<< "</IP6_GLOBAL>";
}
if ( ip6[0] != 0 || ip6[1] != 0 || ip6[2] != 0 || ip6[3] != 0 )
{
unsigned int ip_low[2];
ip_low[0] = ip6[0] + index;
ip_low[1] = ip6[1];
oss << "<IP6>" << ip6_to_s(&(ip6[2]), ip_low, ip6_s) << "</IP6>";
}
oss << "<SIZE>" << rsize << "</SIZE>"
<< "</ADDRESS>";
}
@ -438,7 +526,6 @@ void AddressRange::to_xml(ostringstream &oss) const
const map<string,string>& ar_attrs = attr->value();
map<string,string>::const_iterator it;
string aux_st;
unsigned int mac_end[2];
oss << "<AR>";
@ -460,12 +547,13 @@ void AddressRange::to_xml(ostringstream &oss) const
oss << "<MAC_END>" << one_util::escape_xml(mac_to_s(mac_end))<<"</MAC_END>";
aux_st = attr->vector_value("IP");
if (aux_st != "")
if (is_ipv4())
{
string aux_st;
unsigned int ip_i;
aux_st = attr->vector_value("IP");
if (ip_to_i(aux_st, ip_i) == 0)
{
oss << "<IP_END>" << one_util::escape_xml(ip_to_s(ip_i + size - 1))
@ -473,7 +561,7 @@ void AddressRange::to_xml(ostringstream &oss) const
}
}
if (type & 0x00000004)
if (is_ipv6())
{
string ip6_s;
@ -499,6 +587,22 @@ void AddressRange::to_xml(ostringstream &oss) const
}
}
if (is_ipv6_static())
{
string ip6_s;
ip6_to_s(&(ip6[2]), ip6, ip6_s);
oss << "<IP6>" << one_util::escape_xml(ip6_s) << "</IP6>";
unsigned int ip_low[2];
ip_low[1] = ip6[1];
ip_low[0] = ip6[0] + size - 1;
ip6_to_s(&(ip6[2]), ip_low, ip6_s);
oss << "<IP6_END>" << one_util::escape_xml(ip6_s) << "</IP6_END>";
}
oss << "<USED_LEASES>" << get_used_addr() << "</USED_LEASES>";
oss << "</AR>";
}
@ -539,11 +643,10 @@ void AddressRange::to_xml(ostringstream &oss, const vector<int>& vms,
oss << "<MAC_END>" << one_util::escape_xml(mac_to_s(mac_end))<<"</MAC_END>";
aux_st = attr->vector_value("IP");
if (aux_st != "")
if (is_ipv4())
{
unsigned int ip_i;
string aux_st = attr->vector_value("IP");
rc = ip_to_i(aux_st, ip_i);
@ -554,7 +657,7 @@ void AddressRange::to_xml(ostringstream &oss, const vector<int>& vms,
}
}
if (type & 0x00000004)
if (is_ipv6())
{
string ip6_s;
@ -564,7 +667,8 @@ void AddressRange::to_xml(ostringstream &oss, const vector<int>& vms,
oss << "<IP6_ULA>" << one_util::escape_xml(ip6_s) << "</IP6_ULA>";
ip6_to_s(ula6, mac_end, ip6_s);
oss << "<IP6_ULA_END>" << one_util::escape_xml(ip6_s) << "</IP6_ULA_END>";
oss << "<IP6_ULA_END>" << one_util::escape_xml(ip6_s)
<< "</IP6_ULA_END>";
}
if (global6[1] != 0 || global6[0] != 0 ) /* Glocal Unicast */
@ -573,10 +677,27 @@ void AddressRange::to_xml(ostringstream &oss, const vector<int>& vms,
oss << "<IP6_GLOBAL>" << one_util::escape_xml(ip6_s) << "</IP6_GLOBAL>";
ip6_to_s(global6, mac_end, ip6_s);
oss << "<IP6_GLOBAL_END>" << one_util::escape_xml(ip6_s) << "</IP6_GLOBAL_END>";
oss << "<IP6_GLOBAL_END>" << one_util::escape_xml(ip6_s)
<< "</IP6_GLOBAL_END>";
}
}
if (is_ipv6_static())
{
string ip6_s;
ip6_to_s(&(ip6[2]), ip6, ip6_s);
oss << "<IP6>" << one_util::escape_xml(ip6_s) << "</IP6>";
unsigned int ip_low[2];
ip_low[1] = ip6[1];
ip_low[0] = ip6[0] + size - 1;
ip6_to_s(&(ip6[2]), ip_low, ip6_s);
oss << "<IP6_END>" << one_util::escape_xml(ip6_s) << "</IP6_END>";
}
oss << "<USED_LEASES>" << get_used_addr() << "</USED_LEASES>";
if (allocated.empty())
@ -636,16 +757,21 @@ void AddressRange::to_xml(ostringstream &oss, const vector<int>& vms,
set_mac(it->first, &lease);
if (type & 0x00000002 )
if (is_ipv4())
{
set_ip(it->first, &lease);
}
if (type & 0x00000004)
if (is_ipv6())
{
set_ip6(it->first, &lease);
}
if (is_ipv6_static())
{
set_ip6_static(it->first, &lease);
}
lease.to_xml(oss);
}
@ -810,6 +936,34 @@ string AddressRange::ip_to_s(unsigned int i_ip) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int AddressRange::ip6_to_i(const string& _ip, unsigned int i_ip[]) const
{
struct in6_addr s6;
if (_ip.empty())
{
return -1;
}
int rc = inet_pton(AF_INET6, _ip.c_str(), &s6);
if ( rc != 1 )
{
return -1;
}
i_ip[3] = ntohl(s6.s6_addr32[0]);
i_ip[2] = ntohl(s6.s6_addr32[1]);
i_ip[1] = ntohl(s6.s6_addr32[2]);
i_ip[0] = ntohl(s6.s6_addr32[3]);
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int AddressRange::prefix6_to_i(const string& prefix, unsigned int ip[]) const
{
struct in6_addr s6;
@ -983,6 +1137,24 @@ void AddressRange::set_ip6(unsigned int addr_index, VectorAttribute * nic) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void AddressRange::set_ip6_static(unsigned int addr_index,
VectorAttribute * nic) const
{
unsigned int low_ip[2];
string ip6_s;
low_ip[0] = ip6[0] + addr_index;
low_ip[1] = ip6[1];
if ( ip6_to_s(&(ip6[2]), low_ip, ip6_s) == 0 )
{
nic->replace("IP6", ip6_s);
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void AddressRange::set_vnet(VectorAttribute *nic, const vector<string> &inherit) const
{
nic->replace("AR_ID", id);
@ -1127,16 +1299,21 @@ void AddressRange::allocate_by_index(unsigned int index,
{
set_mac(index, nic);
if (type & 0x00000002 )
if (is_ipv4())
{
set_ip(index, nic);
}
if (type & 0x00000004)
if (is_ipv6())
{
set_ip6(index, nic);
}
if (is_ipv6_static())
{
set_ip6_static(index, nic);
}
set_vnet(nic, inherit);
set_allocated_addr(ot, obid, index);
@ -1263,7 +1440,7 @@ int AddressRange::free_addr_by_ip(PoolObjectSQL::ObjectType ot, int obid,
{
string error_msg;
if (!(type & 0x00000002))//Not of type IP4 or IP4_6
if (!is_ipv4())
{
return -1;
}