Skip to content

Commit

Permalink
B #3029: Avoid running nic_query in network pre script. Make use of p…
Browse files Browse the repository at this point in the history
…re_action detection in other drivers.

Co-authored-by: Daniel Clavijo Coca <dclavijo@opennebula.systems>
  • Loading branch information
rsmontero and dann1 committed Mar 25, 2019
1 parent 2bb8957 commit d2a33ec
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 100 deletions.
41 changes: 21 additions & 20 deletions src/vnm_mad/remotes/ebtables/Ebtables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,36 @@ def ebtables(rule)

# Activates ebtables rules
#
def activate(pre_action=false)
if pre_action
def activate
if VNMMAD.pre_action?
super()
else
lock
return 0
end

process do |nic|
tap = nic[:tap]
if tap
iface_mac = nic[:mac]
lock

mac = iface_mac.split(':')
mac[-1] = '00'
process do |nic|
tap = nic[:tap]
if tap
iface_mac = nic[:mac]

net_mac = mac.join(':')
mac = iface_mac.split(':')
mac[-1] = '00'

in_rule="FORWARD -s ! #{net_mac}/ff:ff:ff:ff:ff:00 " <<
"-o #{tap} -j DROP"
out_rule="FORWARD -s ! #{iface_mac} -i #{tap} -j DROP"
net_mac = mac.join(':')

ebtables(in_rule) if nic[:filter_mac_spoofing] =~ /yes/i
ebtables(out_rule)
end
end
in_rule="FORWARD -s ! #{net_mac}/ff:ff:ff:ff:ff:00 " <<
"-o #{tap} -j DROP"
out_rule="FORWARD -s ! #{iface_mac} -i #{tap} -j DROP"

unlock
ebtables(in_rule) if nic[:filter_mac_spoofing] =~ /yes/i
ebtables(out_rule)
end
end

return 0
unlock

0
end

def deactivate
Expand Down
2 changes: 1 addition & 1 deletion src/vnm_mad/remotes/ebtables/pre
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ deploy_id = ARGV[0]
xpath_filter = EbtablesVLAN::XPATH_FILTER

onevlan = EbtablesVLAN.from_base64(template64, xpath_filter, deploy_id)
onevlan.activate(pre_action=true)
onevlan.activate
15 changes: 11 additions & 4 deletions src/vnm_mad/remotes/lib/nic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# limitations under the License. #
#--------------------------------------------------------------------------- #

require 'open3'

module VNMMAD

module VNMNetwork
Expand Down Expand Up @@ -110,12 +112,17 @@ def get_info(vm)
end

if deploy_id && vm.vm_info[:dumpxml].nil?
cmd = "lxc config show #{deploy_id} 2>/dev/null"
cmd = "lxc config show #{deploy_id}"

config, e, s = Open3.capture3(cmd)

config = YAML.safe_load(`#{cmd}`)
config = YAML.safe_load(`sudo #{cmd}`) if config.nil?
if s.exitstatus != 0 && e.include?('cannot create'\
'user data directory')
cmd.prepend('sudo')
config, _e, _s = Open3.capture3(cmd)
end

vm.vm_info[:dumpxml] = config
vm.vm_info[:dumpxml] = YAML.safe_load(config)

vm.vm_info.each_key do |k|
vm.vm_info[k] = nil if vm.vm_info[k].to_s.strip.empty?
Expand Down
134 changes: 69 additions & 65 deletions src/vnm_mad/remotes/lib/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,100 +16,104 @@

module VNMMAD

module VNMNetwork
module VNMNetwork

############################################################################
# This class represents the VM abstraction. It provides basic methods
# to interact with its network interfaces.
############################################################################
class VM
attr_accessor :nics, :vm_info, :deploy_id, :vm_root
########################################################################
# This class represents the VM abstraction. It provides basic methods
# to interact with its network interfaces.
########################################################################
class VM

attr_accessor :nics, :vm_info, :deploy_id, :vm_root

# Creates a new VM object, and bootstrap the NICs array
# @param vm_root [REXML] XML document representing the VM
# @param xpath_filer [String] to get the VM NICs
# @param deploy_id [String] refers to the VM in the hypervisor
def initialize(vm_root, xpath_filter, deploy_id)
@vm_root = vm_root
@deploy_id = deploy_id
# Creates a new VM object, and bootstrap the NICs array
# @param vm_root [REXML] XML document representing the VM
# @param xpath_filer [String] to get the VM NICs
# @param deploy_id [String] refers to the VM in the hypervisor
def initialize(vm_root, xpath_filter, deploy_id)
@vm_root = vm_root
@deploy_id = deploy_id

@vm_info = Hash.new
@vm_info = {}

@deploy_id = nil if deploy_id == "-"
@deploy_id = nil if deploy_id == '-'

nics = VNMNetwork::Nics.new(hypervisor)
nics = VNMNetwork::Nics.new(hypervisor)

@vm_root.elements.each(xpath_filter) do |nic_element|
nic = nics.new_nic
@vm_root.elements.each(xpath_filter) do |nic_element|
nic = nics.new_nic

nic_build_hash(nic_element,nic)
nic_build_hash(nic_element, nic)

nic.get_info(self)
nic.get_tap(self)
if !VNMMAD.pre_action?
nic.get_info(self)
nic.get_tap(self)
end

nics << nic
end

nics << nic
@nics = nics
end

@nics = nics
end
# Iterator on each NIC of the VM
def each_nic(block)
return if @nics.nil?

# Iterator on each NIC of the VM
def each_nic(block)
if @nics != nil
@nics.each do |the_nic|
block.call(the_nic)
end
end
end

# Access an XML Element of the VM
# @param element [String] element name
# @return [String] value of the element or nil if not found
def [](element)
if @vm_root
val = @vm_root.elements[element]
return val.text if !val.nil? && val.text
end
# Access an XML Element of the VM
# @param element [String] element name
# @return [String] value of the element or nil if not found
def [](element)
if @vm_root
val = @vm_root.elements[element]
return val.text if !val.nil? && val.text
end

nil
end
nil
end

# Gets the Hypervisor VM_MAD from the Template
# @return [String] name of the hypervisor driver
def hypervisor
xpath = 'HISTORY_RECORDS/HISTORY/VM_MAD'
@vm_root.root.elements[xpath].text
end
# Gets the Hypervisor VM_MAD from the Template
# @return [String] name of the hypervisor driver
def hypervisor
xpath = 'HISTORY_RECORDS/HISTORY/VM_MAD'
@vm_root.root.elements[xpath].text
end

private
private

# Method to build the associated Hash from a NIC
# @param nic_element [REXML] for the NIC
# @param nic [Nic] class representation
def nic_build_hash(nic_element,nic)
nic_element.elements.each('*') do |nic_attribute|
key = nic_attribute.name.downcase.to_sym
# Method to build the associated Hash from a NIC
# @param nic_element [REXML] for the NIC
# @param nic [Nic] class representation
def nic_build_hash(nic_element, nic)
nic_element.elements.each('*') do |nic_attribute|
key = nic_attribute.name.downcase.to_sym

if nic_attribute.has_elements?
data = {}
nic_build_hash(nic_attribute,data)
else
data = nic_attribute.text
end
if nic_attribute.has_elements?
data = {}
nic_build_hash(nic_attribute, data)
else
data = nic_attribute.text
end

if nic[key]
if nic[key].instance_of?(Array)
nic[key] << data
if nic[key]
if nic[key].instance_of?(Array)
nic[key] << data
else
nic[key] = [nic[key], data]
end
else
nic[key] = [nic[key], data]
nic[key] = data
end
else
nic[key] = data
end
end

end

end
end

end
7 changes: 7 additions & 0 deletions src/vnm_mad/remotes/lib/vnm_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,12 @@ def self.parse_options(string)

options
end

# Returns true if the driver is executing action pre
def self.pre_action?
File.basename($PROGRAM_NAME) == 'pre'
end

end

end
14 changes: 6 additions & 8 deletions src/vnm_mad/remotes/ovswitch/OpenvSwitch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def initialize(vm, xpath_filter = nil, deploy_id = nil)
end
end

def activate(pre_action=false)
def activate
lock

@bridges = get_bridges
Expand Down Expand Up @@ -65,13 +65,11 @@ def activate(pre_action=false)
# In net/pre action, we just need to ensure the bridge is
# created so the libvirt/QEMU can add VM interfaces into that.
# Any other driver actions are done in net/post action.
if pre_action
next
else
STDERR.puts "No tap device found for nic #{@nic[:nic_id]}"
unlock
exit 1
end
next if VNMMAD.pre_action?

STDERR.puts "No tap device found for nic #{@nic[:nic_id]}"
unlock
exit 1
end

# Apply VLAN
Expand Down
2 changes: 1 addition & 1 deletion src/vnm_mad/remotes/ovswitch/pre
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ xpath_filter = OpenvSwitchVLAN::XPATH_FILTER

ovs = OpenvSwitchVLAN.from_base64(template64, xpath_filter, deploy_id)

ovs.activate(pre_action=true)
ovs.activate
2 changes: 1 addition & 1 deletion src/vnm_mad/remotes/ovswitch_vxlan/pre
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ xpath_filter = OpenvSwitchVXLAN::XPATH_FILTER

ovs = OpenvSwitchVXLAN.from_base64(template64, xpath_filter, deploy_id)

ovs.activate(pre_action=true)
ovs.activate

0 comments on commit d2a33ec

Please sign in to comment.