Skip to content

Commit

Permalink
F #911: IP(v6) alias(es) support
Browse files Browse the repository at this point in the history
   * Modify VM context generation.
   * Modify libvirt deployment file generation.
   * Modify detach nic functionality.
   * Modify atach nic to not call the driver.
   * Modify CLI nic-attach, so an alias can be attached.
  • Loading branch information
Alejandro Huertas committed Nov 19, 2018
1 parent 102a7e1 commit 7fa7ea0
Show file tree
Hide file tree
Showing 14 changed files with 517 additions and 136 deletions.
4 changes: 3 additions & 1 deletion include/History.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ class History:public ObjectSQL, public ObjectXML
RECOVER_ACTION = 42, // "one.vm.recover"
RETRY_ACTION = 43, // "one.vm.recover"
MONITOR_ACTION = 44, // internal, monitoring process
DISK_SNAPSHOT_RENAME_ACTION = 45 // "one.vm.disksnapshotrename"
DISK_SNAPSHOT_RENAME_ACTION = 45, // "one.vm.disksnapshotrename"
ALIAS_ATTACH_ACTION = 46, // "one.vm.attachnic"
ALIAS_DETACH_ACTION = 47 // "one.vm.detachnic"
};

static string action_to_str(VMAction action);
Expand Down
21 changes: 21 additions & 0 deletions include/VirtualMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,19 @@ class VirtualMachine : public PoolObjectSQL
return nic;
}

/**
* Deletes the alias of the NIC that was in the process of being attached/detached
*/
void delete_attach_alias(VirtualMachineNic *nic)
{
vector<string> alias_ids = one_util::split(nic->vector_value("ALIAS_IDS"), ',', true);

for(vector<string>::iterator it = alias_ids.begin(); it != alias_ids.end(); it++)
{
obj_template->remove(get_nic(std::stoi(*it))->vector_attribute());
}
}

// ------------------------------------------------------------------------
// Disk Snapshot related functions
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -1986,6 +1999,14 @@ class VirtualMachine : public PoolObjectSQL
*/
void clear_nic_context(int nicid);

/**
* Deletes the NETWORK ALIAS related CONTEXT section for the given nic, i.e.
* ETH_<id>_ALIAS<aliasid>
* @param nicid the id of the NIC
* @param aliasid the idx of the ALIAS
*/
void clear_nic_alias_context(int nicid, int aliasidx);

/**
* Generate the PCI related CONTEXT setions, i.e. PCI_*. This function
* is also adds basic network attributes for pass-through NICs
Expand Down
18 changes: 18 additions & 0 deletions include/VirtualMachineNic.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ class VirtualMachineNic : public VirtualMachineAttribute
*/
void to_xml_short(std::ostringstream& oss) const;

/**
* Check is a nic is alias or not
*/
bool is_alias() const
{
return name() == "NIC_ALIAS";
}

private:
/**
* Fills the authorization request for this NIC based on the VNET and SG
Expand Down Expand Up @@ -133,11 +141,14 @@ class VirtualMachineNics : public VirtualMachineAttributeSet
VirtualMachineAttributeSet(false)
{
std::vector<VectorAttribute *> vas;
std::vector<VectorAttribute *> alias;
std::vector<VectorAttribute *> pcis;
std::vector<VectorAttribute *>::iterator it;

tmpl->get(NIC_NAME, vas);

tmpl->get(NIC_ALIAS_NAME, alias);

tmpl->get("PCI", pcis);

for ( it=pcis.begin(); it != pcis.end() ; ++it)
Expand All @@ -148,6 +159,11 @@ class VirtualMachineNics : public VirtualMachineAttributeSet
}
}

for ( it=alias.begin(); it != alias.end(); ++it)
{
vas.push_back(*it);
}

init(vas, false);
};

Expand Down Expand Up @@ -324,6 +340,8 @@ class VirtualMachineNics : public VirtualMachineAttributeSet
private:
static const char * NIC_NAME; //"NIC"

static const char * NIC_ALIAS_NAME; //"NIC_ALIAS"

static const char * NIC_ID_NAME; //"NIC_ID"
};

Expand Down
1 change: 0 additions & 1 deletion src/cli/one_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#--------------------------------------------------------------------------- #

require 'cli_helper'

require 'open3'
require 'io/console'

Expand Down
196 changes: 105 additions & 91 deletions src/cli/one_helper/onevm_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ class OneVMHelper < OpenNebulaHelper::OneHelper
:description=> "Does not communicate with the guest OS"
}

ALIAS = {
:name => "alias",
:short => "-a alias",
:large => "--alias alias",
:description=> "Attach the NIC as an ALIAS",
:format => String
}

def self.rname
"VM"
end
Expand Down Expand Up @@ -800,132 +808,138 @@ def format_resource(vm, options = {})

extra_ips.uniq!

if vm.has_elements?("/VM/TEMPLATE/NIC") ||
vm.has_elements?("/VM/TEMPLATE/PCI[NIC_ID>-1]") || !extra_ips.empty?
['NIC', 'NIC_ALIAS'].each do |type|
if vm.has_elements?("/VM/TEMPLATE/#{type}") ||
vm.has_elements?("/VM/TEMPLATE/PCI[NIC_ID>-1]") || !extra_ips.empty?

puts
CLIHelper.print_header(str_h1 % "VM NICS",false)
puts
CLIHelper.print_header(str_h1 % "VM #{type == 'NIC' ? 'NICS' : 'ALIAS'}",false)

nic_default = {"NETWORK" => "-",
"IP" => "-",
"MAC"=> "-",
"BRIDGE"=>"-"}
nic_default = {"NETWORK" => "-",
"IP" => "-",
"MAC"=> "-",
"BRIDGE"=>"-"}

shown_ips = []
shown_ips = []

array_id = 0
vm_nics = [vm_hash['VM']['TEMPLATE']['NIC']]
array_id = 0
vm_nics = [vm_hash['VM']['TEMPLATE'][type]]

vm_pcis = [vm_hash['VM']['TEMPLATE']['PCI']].flatten.compact
if type == 'NIC'
vm_pcis = [vm_hash['VM']['TEMPLATE']['PCI']].flatten.compact

vm_pcis.each do |pci|
if !pci['NIC_ID'].nil?
vm_nics << pci
vm_pcis.each do |pci|
if !pci['NIC_ID'].nil?
vm_nics << pci
end
end
end
end

vm_nics.flatten!
vm_nics.compact!
vm_nics.flatten!
vm_nics.compact!

vm_nics.each {|nic|
vm_nics.each {|nic|

next if nic.has_key?("CLI_DONE")
next if nic.has_key?("CLI_DONE")

["IP6_LINK", "IP6_ULA", "IP6_GLOBAL", "IP6"].each do |attr|
if nic.has_key?(attr)
shown_ips << nic[attr]
["IP6_LINK", "IP6_ULA", "IP6_GLOBAL", "IP6"].each do |attr|
if nic.has_key?(attr)
shown_ips << nic[attr]

ipstr = {"IP" => nic.delete(attr),
"CLI_DONE" => true,
"DOUBLE_ENTRY" => true}
vm_nics.insert(array_id+1,ipstr)
ipstr = {"IP" => nic.delete(attr),
"CLI_DONE" => true,
"DOUBLE_ENTRY" => true}
vm_nics.insert(array_id+1,ipstr)

array_id += 1
array_id += 1
end
end
end

["VROUTER_IP", "VROUTER_IP6_LINK",
"VROUTER_IP6_ULA", "VROUTER_IP6_GLOBAL"].each do |attr|
if nic.has_key?(attr)
shown_ips << nic[attr]
["VROUTER_IP", "VROUTER_IP6_LINK",
"VROUTER_IP6_ULA", "VROUTER_IP6_GLOBAL"].each do |attr|
if nic.has_key?(attr)
shown_ips << nic[attr]

ipstr = {"IP" => nic.delete(attr) + " (VRouter)",
"CLI_DONE" => true,
"DOUBLE_ENTRY" => true}
vm_nics.insert(array_id+1,ipstr)
ipstr = {"IP" => nic.delete(attr) + " (VRouter)",
"CLI_DONE" => true,
"DOUBLE_ENTRY" => true}
vm_nics.insert(array_id+1,ipstr)

array_id += 1
array_id += 1
end
end
end

shown_ips << nic["IP"] if nic.has_key?("IP")
shown_ips << nic["IP"] if nic.has_key?("IP")

nic.merge!(nic_default) {|k,v1,v2| v1}
array_id += 1
}
nic.merge!(nic_default) {|k,v1,v2| v1}
array_id += 1
}

extra_ips -= shown_ips
extra_ips -= shown_ips

# Add extra IPs to the VM NICS table
extra_ips.each do |ip|
vm_nics << {
"NIC_ID" => "-",
"IP" => ip,
"NETWORK" => "Additional IP",
"BRIDGE" => "-"
}
end
# Add extra IPs to the VM NICS table
extra_ips.each do |ip|
vm_nics << {
"NIC_ID" => "-",
"IP" => ip,
"NETWORK" => "Additional IP",
"BRIDGE" => "-"
}
end

CLIHelper::ShowTable.new(nil, self) do
column :ID, "", :size=>3 do |d|
if d["DOUBLE_ENTRY"]
""
else
d["NIC_ID"]
CLIHelper::ShowTable.new(nil, self) do
column :ID, "", :size=>3 do |d|
if d["DOUBLE_ENTRY"]
""
else
d["NIC_ID"]
end
end
end

column :NETWORK, "", :left, :size=>20 do |d|
if d["DOUBLE_ENTRY"]
""
else
d["NETWORK"]
column :NETWORK, "", :left, :size=>20 do |d|
if d["DOUBLE_ENTRY"]
""
else
d["NETWORK"]
end
end
end

column :BRIDGE, "", :left, :size=>12 do |d|
if d["DOUBLE_ENTRY"]
""
else
d["BRIDGE"]
column :BRIDGE, "", :left, :size=>12 do |d|
if d["DOUBLE_ENTRY"]
""
else
d["BRIDGE"]
end
end
end

column :IP, "",:left, :donottruncate, :size=>15 do |d|
d["IP"]
end
column :IP, "",:left, :donottruncate, :size=>15 do |d|
d["IP"]
end

column :MAC, "", :left, :size=>17 do |d|
if d["DOUBLE_ENTRY"]
""
else
d["MAC"]
column :MAC, "", :left, :size=>17 do |d|
if d["DOUBLE_ENTRY"]
""
else
d["MAC"]
end
end
end

column :PCI_ID, "", :left, :size=>8 do |d|
if d["DOUBLE_ENTRY"]
""
else
d["PCI_ID"]
if type == 'NIC'
column :PCI_ID, "", :left, :size=>8 do |d|
if d["DOUBLE_ENTRY"]
""
else
d["PCI_ID"]
end
end
end
end

end.show(vm_nics,{})
end.show(vm_nics,{})

while vm.has_elements?("/VM/TEMPLATE/NIC")
vm.delete_element("/VM/TEMPLATE/NIC")
end if !options[:all]
while vm.has_elements?("/VM/TEMPLATE/#{type}")
vm.delete_element("/VM/TEMPLATE/#{type}")
end if !options[:all]
end
end

while vm.has_elements?("/VM/TEMPLATE/NIC")
Expand Down
22 changes: 19 additions & 3 deletions src/cli/onevm
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,8 @@ CommandParser::CmdParser.new(ARGV) do
command :"nic-attach", nic_attach_desc, :vmid,
:options => [OneVMHelper::FILE,
OneVMHelper::NETWORK,
OneVMHelper::IP] do
OneVMHelper::IP,
OneVMHelper::ALIAS] do

if options[:file].nil? && options[:network].nil?
STDERR.puts 'Provide a template file or a network:'
Expand All @@ -784,10 +785,25 @@ CommandParser::CmdParser.new(ARGV) do
else
network_id = options[:network]
ip = options[:ip]
nic_alias = options[:alias]

if ip
template = "NIC = [ NETWORK_ID = #{network_id}, IP = #{ip} ]"
if !nic_alias
template = "NIC = [ NETWORK_ID = #{network_id}, IP = #{ip} ]"
else
template = "NIC_ALIAS = \
[ NETWORK_ID = #{network_id},\
IP = #{ip},\
NAME = #{nic_alias} ]"
end
else
template = "NIC = [ NETWORK_ID = #{network_id} ]"
if !nic_alias
template = "NIC = [ NETWORK_ID = #{network_id} ]"
else
template = "NIC_ALIAS = \
[ NETWORK_ID = #{network_id},\
NAME = #{nic_alias} ]"
end
end
end

Expand Down
Loading

0 comments on commit 7fa7ea0

Please sign in to comment.