Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #3666: Clean VXLAN devices when no VM is left in the hypervisor #58

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/vnm_mad/remotes/802.1Q/clean

This file was deleted.

36 changes: 36 additions & 0 deletions src/vnm_mad/remotes/802.1Q/clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #

$: << File.dirname(__FILE__)
$: << File.join(File.dirname(__FILE__), "..")

require 'vnmmad'
require 'vlan_tag_driver'

template64 = ARGV[0]

begin
hm = VLANTagDriver.from_base64(template64)
hm.deactivate
filter_driver = VNMMAD::VNMDriver.filter_driver(template64)
filter_driver.deactivate
rescue Exception => e
OpenNebula.log_error(e.message)
OpenNebula.log_error(e.backtrace)
exit 1
end
14 changes: 7 additions & 7 deletions src/vnm_mad/remotes/802.1Q/vlan_tag_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ class VLANTagDriver < VNMMAD::VLANDriver
XPATH_FILTER = "TEMPLATE/NIC[VLAN='YES']"

############################################################################
# Creatges the driver device operations are not locked
# Create driver device operations are locked
############################################################################
def initialize(vm, deploy_id = nil, hypervisor = nil)
@locking = false
@locking = true

super(vm, XPATH_FILTER, deploy_id, hypervisor)
end

############################################################################
# This function creates and activate a VLAN device
############################################################################
def create_vlan_dev(options)
mtu = options[:mtu] ? "mtu #{options[:mtu]}" : ""
def create_vlan_dev
mtu = @nic[:mtu] ? "mtu #{@nic[:mtu]}" : ""

OpenNebula.exec_and_log("#{command(:ip)} link add link"\
" #{options[:phydev]} name #{options[:vlan_dev]} #{mtu} type vlan id"\
" #{options[:vlan_id]}")
" #{@nic[:phydev]} name #{@nic[:vlan_dev]} #{mtu} type vlan id"\
" #{@nic[:vlan_id]}")

OpenNebula.exec_and_log("#{command(:ip)} link set #{options[:vlan_dev]} up")
OpenNebula.exec_and_log("#{command(:ip)} link set #{@nic[:vlan_dev]} up")
end
end
114 changes: 67 additions & 47 deletions src/vnm_mad/remotes/lib/vlan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,89 +24,109 @@ module VNMMAD
class VLANDriver < VNMMAD::VNMDriver

def initialize(vm_tpl, xpath_filter, deploy_id = nil, hypervisor = nil)
super(vm_tpl, xpath_filter, deploy_id, hypervisor)
@locking = true

lock
@bridges = get_bridges
unlock
super(vm_tpl, xpath_filter, deploy_id, hypervisor)
end

# Activate the driver and creates bridges and tags devices as needed.
def activate
lock

options = Hash.new
@bridges = get_bridges

process do |nic|
@nic = nic

next if @nic[:phydev].nil?

options.clear
# Get the name of the vlan device.
get_vlan_dev_name

options[:bridge] = nic[:bridge]
options[:phydev] = nic[:phydev]
options[:vlan_id] = nic[:vlan_id]
options[:network_id] = nic[:network_id]
options[:mtu] = nic[:mtu]
# Create the bridge.
create_bridge

return if options[:phydev].nil?
# Return if vlan device is already in the bridge.
next if @bridges[@nic[:bridge]].include? @nic[:vlan_dev]

set_up_vlan(options)
# Create vlan device.
create_vlan_dev

# Add vlan device to the bridge.
OpenNebula.exec_and_log("#{command(:brctl)} addif"\
" #{@nic[:bridge]} #{@nic[:vlan_dev]}")

@bridges[@nic[:bridge]] << @nic[:vlan_dev]
end

unlock

return 0
end

# Set ups the VLAN for the VMs.
# @param options [Hash] including
# - :phydev Physical Device to bind the VLAN traffic to
# - :bridge Name of the bridge to attach the VMs and VLAN dev to
# - :network_id
def set_up_vlan(options)
# This function needs to be implemented by any VLAN driver to
# create the VLAN device. The device MUST be set up by this function
def create_vlan_dev
OpenNebula.log_error("create_vlan_dev function not implemented.")

if options[:vlan_id].nil?
options[:vlan_id] = CONF[:start_vlan] + options[:network_id].to_i
end
exit -1
end

options[:vlan_dev] = "#{options[:phydev]}.#{options[:vlan_id]}"
# Deactivate the driver and delete bridges and tags devices as needed.
def deactivate
lock

@bridges = get_bridges

create_bridge(options[:bridge])
attach_nic_id = @vm['TEMPLATE/NIC[ATTACH="YES"]/NIC_ID']

return if @bridges[options[:bridge]].include? options[:vlan_dev]
process do |nic|
next if attach_nic_id && attach_nic_id != nic[:nic_id]

create_vlan_dev(options)
@nic = nic

OpenNebula.exec_and_log("#{command(:brctl)} addif"\
" #{options[:bridge]} #{options[:vlan_dev]}")
next if @nic[:phydev].nil?

@bridges[options[:bridge]] << options[:vlan_dev]
end
# Get the name of the vlan device.
get_vlan_dev_name

# This function needs to be implemented by any VLAN driver to
# create the VLAN device. The device MUST be set up by this function
# Options is a driver specific hash. It includes
# :vlan_dev the name for the VLAN device
# :phydev Physical Device to bind the VLAN traffic to
# :vlan_id the VLAN ID
# : additional driver specific parameters
def create_vlan_dev(options)
OpenNebula.log_error("create_vlan_dev function not implemented.")
# Return if the vlan device is not the only left device in the bridge.
next if @bridges[@nic[:bridge]].length > 1 or !@bridges[@nic[:bridge]].include? @nic[:vlan_dev]

exit -1
# Delete the vlan device.
OpenNebula.exec_and_log("#{command(:ip)} link delete"\
" #{@nic[:vlan_dev]}")
@bridges[@nic[:bridge]].delete(@nic[:vlan_dev])

# Delete the bridge.
OpenNebula.exec_and_log("#{command(:ip)} link delete"\
" #{@nic[:bridge]}")
@bridges.delete(@nic[:bridge])
end

unlock
end

private
# Generate the name of the vlan device which will be added to the bridge.
def get_vlan_dev_name
if @nic[:vlan_id].nil?
@nic[:vlan_id] = CONF[:start_vlan] + @nic[:network_id].to_i
end

@nic[:vlan_dev] = "#{@nic[:phydev]}.#{@nic[:vlan_id]}"
end

# Creates a bridge if it does not exists, and brings it up.
# This function IS FINAL, exits if action cannot be completed
# @param bridge [String] the bridge name
def create_bridge(bridge)
return if @bridges.keys.include? bridge
def create_bridge
return if @bridges.keys.include? @nic[:bridge]

OpenNebula.exec_and_log("#{command(:brctl)} addbr #{bridge}")
OpenNebula.exec_and_log("#{command(:brctl)} addbr #{@nic[:bridge]}")

@bridges[bridge] = Array.new
@bridges[@nic[:bridge]] = Array.new

OpenNebula.exec_and_log("#{command(:ip)} link set #{bridge} up")
OpenNebula.exec_and_log("#{command(:ip)} link set #{@nic[:bridge]} up")
end

# Get hypervisor bridges
Expand Down
1 change: 0 additions & 1 deletion src/vnm_mad/remotes/vxlan/clean

This file was deleted.

36 changes: 36 additions & 0 deletions src/vnm_mad/remotes/vxlan/clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#--------------------------------------------------------------------------- #

$: << File.dirname(__FILE__)
$: << File.join(File.dirname(__FILE__), "..")

require 'vnmmad'
require 'vxlan_driver'

template64 = ARGV[0]

begin
hm = VXLANDriver.from_base64(template64)
hm.deactivate
filter_driver = VNMMAD::VNMDriver.filter_driver(template64)
filter_driver.deactivate
rescue Exception => e
OpenNebula.log_error(e.message)
OpenNebula.log_error(e.backtrace)
exit 1
end
18 changes: 9 additions & 9 deletions src/vnm_mad/remotes/vxlan/vxlan_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ class VXLANDriver < VNMMAD::VLANDriver
XPATH_FILTER = "TEMPLATE/NIC[VLAN='YES']"

############################################################################
# Creatges the driver device operations are not locked
# Create driver device operations are locked
############################################################################
def initialize(vm, deploy_id = nil, hypervisor = nil)
@locking = false
@locking = true

super(vm, XPATH_FILTER, deploy_id, hypervisor)
end

############################################################################
# This function creates and activate a VLAN device
############################################################################
def create_vlan_dev(options)
mc = VNMMAD::VNMNetwork::IPv4.to_i(CONF[:vxlan_mc]) + options[:vlan_id].to_i
def create_vlan_dev
mc = VNMMAD::VNMNetwork::IPv4.to_i(CONF[:vxlan_mc]) + @nic[:vlan_id].to_i
mcs = VNMMAD::VNMNetwork::IPv4.to_s(mc)
mtu = options[:mtu] ? "mtu #{options[:mtu]}" : ""
mtu = @nic[:mtu] ? "mtu #{@nic[:mtu]}" : ""

OpenNebula.exec_and_log("#{command(:ip)} link add #{options[:vlan_dev]}"\
" #{mtu} type vxlan id #{options[:vlan_id]} group #{mcs}"\
" dev #{options[:phydev]}")
OpenNebula.exec_and_log("#{command(:ip)} link add #{@nic[:vlan_dev]}"\
" #{mtu} type vxlan id #{@nic[:vlan_id]} group #{mcs}"\
" dev #{@nic[:phydev]}")

OpenNebula.exec_and_log("#{command(:ip)} link set #{options[:vlan_dev]} up")
OpenNebula.exec_and_log("#{command(:ip)} link set #{@nic[:vlan_dev]} up")
end
end