1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-04-01 06:50:25 +03:00

F #5027: Generate IP6 context variables for IP6 AR

This commit is contained in:
Ruben S. Montero 2017-03-03 18:41:23 +01:00
parent 1fdf5127b7
commit 34ea719974
4 changed files with 140 additions and 90 deletions

View File

@ -1935,34 +1935,6 @@ private:
// -------------------------------------------------------------------------
// Context related functions
// -------------------------------------------------------------------------
/**
* Known attributes for network contextualization rendered as:
* ETH_<nicid>_<context[0]> = $NETWORK[context[1], vnet_name]
*
* The context[1] values in the map are searched in the NIC and
* if not found in the AR and VNET. They can be also set in the
* CONTEXT section it self using full name (ETH_).
*
* IPv4 context variables:
* {"IP", "IP"},
* {"MAC", "MAC"},
* {"MASK", "NETWORK_MASK"},
* {"NETWORK", "NETWORK_ADDRESS"},
* {"GATEWAY", "GATEWAY"},
* {"DNS", "DNS"},
* {"SEARCH_DOMAIN", "SEARCH_DOMAIN"}
*
* IPv6 context:
* {"IP6", "IP6_GLOBAL"},
* {"GATEWAY6", "GATEWAY6"},
* {"CONTEXT_FORCE_IPV4", "CONTEXT_FORCE_IPV4"}
*/
static const char* NETWORK_CONTEXT[][2];
static const int NUM_NETWORK_CONTEXT;
static const char* NETWORK6_CONTEXT[][2];
static const int NUM_NETWORK6_CONTEXT;
/**
* Generate the NETWORK related CONTEXT setions, i.e. ETH_*. This function
* is invoked when ever the context is prepared for the VM to capture

View File

@ -143,6 +143,39 @@ cmd=CommandParser::CmdParser.new(ARGV) do
exit -1
end
if options[:ip6]
m = /([\h:]*)\/(\d.*)$/.match(options[:ip6])
if m.nil? || m[1].nil?
STDERR.puts "Missing or wrong IP6"
exit -1
else
begin
require 'ipaddr'
ip = IPAddr.new(m[1])
if !ip.ipv6?
STDERR.puts "Wrong IP6 format address"
exit -1
end
rescue
STDERR.puts "Wrong IP6 format address"
exit -1
end
end
if m[2].nil?
STDERR.puts "IP6 address need to set the prefix length"
exit -1
end
ar << ", PREFIX_LENGTH=\"#{m[2]}\""
options[:ip6] = m[1]
end
ar << ", IP = " << options[:ip] if options[:ip]
ar << ", IP6 = " << options[:ip6] if options[:ip6]
ar << ", MAC = " << options[:mac] if options[:mac]

View File

@ -40,28 +40,38 @@
/* -------------------------------------------------------------------------- */
/* Context constants */
/* -------------------------------------------------------------------------- */
const char * VirtualMachine::NETWORK_CONTEXT[][2] = {
{"IP", "IP"},
{"MAC", "MAC"},
{"MASK", "NETWORK_MASK"},
{"NETWORK", "NETWORK_ADDRESS"},
{"GATEWAY", "GATEWAY"},
{"DNS", "DNS"},
{"SEARCH_DOMAIN", "SEARCH_DOMAIN"},
{"MTU", "GUEST_MTU"},
{"VLAN_ID", "VLAN_ID"},
{"VROUTER_IP", "VROUTER_IP"},
{"VROUTER_MANAGEMENT", "VROUTER_MANAGEMENT"}};
const int VirtualMachine::NUM_NETWORK_CONTEXT = 11;
const char* VirtualMachine::NETWORK6_CONTEXT[][2] = {
{"IP6", "IP6_GLOBAL"},
{"IP6_ULA", "IP6_ULA"},
{"GATEWAY6", "GATEWAY6"},
{"CONTEXT_FORCE_IPV4", "CONTEXT_FORCE_IPV4"},
{"VROUTER_IP6", "VROUTER_IP6_GLOBAL"}};
struct ContextVariable
{
std::string context_name;
std::string nic_name;
std::string nic_name_alt;
const int VirtualMachine::NUM_NETWORK6_CONTEXT = 5;
bool ar_lookup;
};
const std::vector<ContextVariable> NETWORK_CONTEXT = {
{"IP", "IP", "", false},
{"MAC", "MAC", "", false},
{"MASK", "NETWORK_MASK", "", true},
{"NETWORK", "NETWORK_ADDRESS", "", true},
{"GATEWAY", "GATEWAY", "", true},
{"DNS", "DNS", "", true},
{"SEARCH_DOMAIN", "SEARCH_DOMAIN", "", true},
{"MTU", "GUEST_MTU", "", true},
{"VLAN_ID", "VLAN_ID", "", true},
{"VROUTER_IP", "VROUTER_IP", "", false},
{"VROUTER_MANAGEMENT", "VROUTER_MANAGEMENT", "", false}
};
const std::vector<ContextVariable> NETWORK6_CONTEXT = {
{"IP6", "IP6_GLOBAL", "IP6", false},
{"IP6_ULA", "IP6_ULA", "", false},
{"GATEWAY6", "GATEWAY6", "", true},
{"CONTEXT_FORCE_IPV4", "CONTEXT_FORCE_IPV4", "", true},
{"IP6_PREFIX_LENGTH", "PREFIX_LENGTH", "", true},
{"VROUTER_IP6", "VROUTER_IP6_GLOBAL", "VROUTER_IP6", false},
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -229,25 +239,34 @@ int VirtualMachine::get_created_by_uid() const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
static void parse_context_network(const char* vars[][2], int num_vars,
static void parse_context_network(const std::vector<ContextVariable>& cvars,
VectorAttribute * context, VectorAttribute * nic)
{
string nic_id = nic->vector_value("NIC_ID");
for (int i=0; i < num_vars; i++)
std::vector<ContextVariable>::const_iterator it;
for (it = cvars.begin(); it != cvars.end() ; ++it)
{
ostringstream cvar;
string cval;
cvar << "ETH" << nic_id << "_" << vars[i][0];
cvar << "ETH" << nic_id << "_" << (*it).context_name;
cval = nic->vector_value(vars[i][1]); //Check the NIC
cval = nic->vector_value((*it).nic_name); //Check the NIC
if (cval.empty()) //Will check the AR and VNET
if ( cval.empty() && !((*it).nic_name_alt).empty() )
{
cval = nic->vector_value((*it).nic_name_alt);
}
if ( cval.empty() && (*it).ar_lookup ) //Will check the AR and VNET
{
ostringstream cval_ss;
cval_ss << "$NETWORK["<< vars[i][1] <<", NIC_ID=\""<< nic_id <<"\"]";
cval_ss << "$NETWORK["<< (*it).nic_name << ", NIC_ID=\""
<< nic_id <<"\"]";
cval = cval_ss.str();
}
@ -285,10 +304,8 @@ int VirtualMachine::generate_network_context(VectorAttribute* context,
for(int i=0; i<num_vatts; i++)
{
parse_context_network(NETWORK_CONTEXT, NUM_NETWORK_CONTEXT,
&tmp_context, vatts[i]);
parse_context_network(NETWORK6_CONTEXT, NUM_NETWORK6_CONTEXT,
&tmp_context, vatts[i]);
parse_context_network(NETWORK_CONTEXT, &tmp_context, vatts[i]);
parse_context_network(NETWORK6_CONTEXT, &tmp_context, vatts[i]);
}
str = tmp_context.marshall();
@ -320,25 +337,31 @@ int VirtualMachine::generate_network_context(VectorAttribute* context,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
static void parse_pci_context_network(const char* vars[][2], int num_vars,
static void parse_pci_context_network(const std::vector<ContextVariable>& cvars,
VectorAttribute * context, VectorAttribute * nic)
{
string pci_id = nic->vector_value("PCI_ID");
for (int i=0; i < num_vars; i++)
std::vector<ContextVariable>::const_iterator it;
for (it = cvars.begin(); it != cvars.end() ; ++it)
{
ostringstream cvar;
cvar << "PCI" << pci_id << "_" << vars[i][0];
cvar << "PCI" << pci_id << "_" << (*it).context_name;
string cval = nic->vector_value(vars[i][1]);
string cval = nic->vector_value((*it).nic_name);
if ( cval.empty() )
{
cval = nic->vector_value((*it).nic_name_alt);
}
if (!cval.empty())
{
context->replace(cvar.str(), cval);
}
}
}
/**
@ -360,10 +383,8 @@ bool VirtualMachine::generate_pci_context(VectorAttribute * context)
{
if ( net_context && vatts[i]->vector_value("TYPE") == "NIC" )
{
parse_pci_context_network(NETWORK_CONTEXT, NUM_NETWORK_CONTEXT,
context, vatts[i]);
parse_pci_context_network(NETWORK6_CONTEXT, NUM_NETWORK6_CONTEXT,
context, vatts[i]);
parse_pci_context_network(NETWORK_CONTEXT, context, vatts[i]);
parse_pci_context_network(NETWORK6_CONTEXT, context, vatts[i]);
}
ostringstream cvar;
@ -577,16 +598,18 @@ int VirtualMachine::parse_context_variables(VectorAttribute ** context,
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
static void clear_context_network(const char* vars[][2], int num_vars,
static void clear_context_network(const std::vector<ContextVariable>& cvars,
VectorAttribute * context, int nic_id)
{
ostringstream att_name;
std::vector<ContextVariable>::const_iterator it;
for (int i=0; i < num_vars; i++)
for (it = cvars.begin(); it != cvars.end() ; ++it)
{
att_name.str("");
att_name << "ETH" << nic_id << "_" << vars[i][0];
att_name << "ETH" << nic_id << "_" << (*it).context_name;
context->remove(att_name.str());
}
@ -603,8 +626,8 @@ void VirtualMachine::clear_nic_context(int nicid)
return;
}
clear_context_network(NETWORK_CONTEXT, NUM_NETWORK_CONTEXT, context, nicid);
clear_context_network(NETWORK6_CONTEXT,NUM_NETWORK6_CONTEXT, context,nicid);
clear_context_network(NETWORK_CONTEXT, context, nicid);
clear_context_network(NETWORK6_CONTEXT, context, nicid);
}
/* ------------------------------------------------------------------------ */

View File

@ -149,6 +149,14 @@ int AddressRange::init_ipv6_static(string& error_msg)
return -1;
}
int pl;
if ( attr->vector_value("PREFIX_LENGTH", pl) != 0 )
{
error_msg = "Wrong or empty PREFIX_LENGTH";
return -1;
}
return 0;
}
@ -382,28 +390,42 @@ int AddressRange::update_attributes(
new_size = size;
}
string new_global = vup->vector_value("GLOBAL_PREFIX");
if (prefix6_to_i(new_global, global6) != 0 )
{
error_msg = "Wrong format for IP6 global address prefix";
return -1;
}
string new_ula = vup->vector_value("ULA_PREFIX");
if (prefix6_to_i(new_ula, ula6) != 0 )
{
error_msg = "Wrong format for IP6 unique local address prefix";
return -1;
}
size = new_size;
vup->replace("SIZE", size);
vup->replace("GLOBAL_PREFIX", new_global);
vup->replace("ULA_PREFIX", new_ula);
if ( is_ipv6_static() )
{
int new_pl;
if (vup->vector_value("PREFIX_LENGTH", new_pl) == -1 )
{
vup->replace("PREFIX_LENGTH", attr->vector_value("PREFIX_LENGTH"));
}
}
if ( is_ipv6() )
{
string new_global = vup->vector_value("GLOBAL_PREFIX");
if (prefix6_to_i(new_global, global6) != 0 )
{
error_msg = "Wrong format for IP6 global address prefix";
return -1;
}
string new_ula = vup->vector_value("ULA_PREFIX");
if (prefix6_to_i(new_ula, ula6) != 0 )
{
error_msg = "Wrong format for IP6 unique local address prefix";
return -1;
}
vup->replace("GLOBAL_PREFIX", new_global);
vup->replace("ULA_PREFIX", new_ula);
}
string value = vup->vector_value("SECURITY_GROUPS");