mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-20 20:02:38 +09:00
feature #476: refactor network classes
This commit is contained in:
1 parent
0a78dd76f3
commit
e5b069d846
13 files changed
+605
-557
No files matched your search
@@ -0,0 +1,79 @@
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
|
||||
# #
|
||||
# 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. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
class EbtablesVLAN < OpenNebulaNetwork
|
||||
def initialize(vm, hypervisor = nil)
|
||||
super(vm,hypervisor)
|
||||
end
|
||||
|
||||
def ebtables(rule)
|
||||
system("#{COMMANDS[:ebtables]} -A #{rule}")
|
||||
end
|
||||
|
||||
def activate
|
||||
process do |nic|
|
||||
tap = nic[:tap]
|
||||
if tap
|
||||
iface_mac = nic[:mac]
|
||||
|
||||
mac = iface_mac.split(':')
|
||||
mac[-1] = '00'
|
||||
|
||||
net_mac = mac.join(':')
|
||||
|
||||
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"
|
||||
|
||||
ebtables(in_rule)
|
||||
ebtables(out_rule)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def deactivate
|
||||
process do |nic|
|
||||
mac = nic[:mac]
|
||||
# remove 0-padding
|
||||
mac = mac.split(":").collect{|e| e.hex.to_s(16)}.join(":")
|
||||
|
||||
tap = ""
|
||||
rules.each do |rule|
|
||||
if m = rule.match(/#{mac} -i (\w+)/)
|
||||
tap = m[1]
|
||||
break
|
||||
end
|
||||
end
|
||||
remove_rules(tap)
|
||||
end
|
||||
end
|
||||
|
||||
def rules
|
||||
`#{COMMANDS[:ebtables]} -L FORWARD`.split("\n")[3..-1]
|
||||
end
|
||||
|
||||
def remove_rules(tap)
|
||||
rules.each do |rule|
|
||||
if rule.match(tap)
|
||||
remove_rule(rule)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def remove_rule(rule)
|
||||
system("#{COMMANDS[:ebtables]} -D FORWARD #{rule}")
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,138 @@
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
|
||||
# #
|
||||
# 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. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
class OpenNebulaFirewall < OpenNebulaNetwork
|
||||
def initialize(vm, hypervisor = nil)
|
||||
super(vm,hypervisor)
|
||||
end
|
||||
def activate
|
||||
vm_id = @vm['ID']
|
||||
process do |nic|
|
||||
#:white_ports_tcp => iptables_range
|
||||
#:white_ports_udp => iptables_range
|
||||
#:black_ports_tcp => iptables_range
|
||||
#:black_ports_udp => iptables_range
|
||||
#:icmp => 'DROP' or 'NO'
|
||||
|
||||
nic_rules = Array.new
|
||||
|
||||
chain = "one-#{vm_id}-#{nic[:network_id]}"
|
||||
tap = nic[:tap]
|
||||
|
||||
if tap
|
||||
#TCP
|
||||
if range = nic[:white_ports_tcp]
|
||||
nic_rules << filter_established(chain, :tcp, :accept)
|
||||
nic_rules << filter_ports(chain, :tcp, range, :accept)
|
||||
nic_rules << filter_protocol(chain, :tcp, :drop)
|
||||
elsif range = nic[:black_ports_tcp]
|
||||
nic_rules << filter_ports(chain, :tcp, range, :drop)
|
||||
end
|
||||
|
||||
#UDP
|
||||
if range = nic[:white_ports_udp]
|
||||
nic_rules << filter_established(chain, :udp, :accept)
|
||||
nic_rules << filter_ports(chain, :udp, range, :accept)
|
||||
nic_rules << filter_protocol(chain, :udp, :drop)
|
||||
elsif range = nic[:black_ports_udp]
|
||||
nic_rules << filter_ports(chain, :udp, range, :drop)
|
||||
end
|
||||
|
||||
#ICMP
|
||||
if nic[:icmp]
|
||||
if %w(no drop).include? nic[:icmp].downcase
|
||||
nic_rules << filter_established(chain, :icmp, :accept)
|
||||
nic_rules << filter_protocol(chain, :icmp, :drop)
|
||||
end
|
||||
end
|
||||
|
||||
process_chain(chain, tap, nic_rules)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def deactivate
|
||||
vm_id = @vm['ID']
|
||||
process do |nic|
|
||||
chain = "one-#{vm_id}-#{nic[:network_id]}"
|
||||
iptables_out = `#{COMMANDS[:iptables]} -n -v --line-numbers -L FORWARD`
|
||||
if m = iptables_out.match(/.*#{chain}.*/)
|
||||
rule_num = m[0].split(/\s+/)[0]
|
||||
purge_chain(chain, rule_num)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def purge_chain(chain, rule_num)
|
||||
rules = Array.new
|
||||
rules << rule("-D FORWARD #{rule_num}")
|
||||
rules << rule("-F #{chain}")
|
||||
rules << rule("-X #{chain}")
|
||||
run_rules rules
|
||||
end
|
||||
|
||||
def process_chain(chain, tap, nic_rules)
|
||||
rules = Array.new
|
||||
if !nic_rules.empty?
|
||||
# new chain
|
||||
rules << new_chain(chain)
|
||||
# move tap traffic to chain
|
||||
rules << tap_to_chain(tap, chain)
|
||||
|
||||
rules << nic_rules
|
||||
end
|
||||
run_rules rules
|
||||
end
|
||||
|
||||
def filter_established(chain, protocol, policy)
|
||||
policy = policy.to_s.upcase
|
||||
rule "-A #{chain} -p #{protocol} -m state --state ESTABLISHED -j #{policy}"
|
||||
end
|
||||
|
||||
def run_rules(rules)
|
||||
rules.flatten.each do |rule|
|
||||
system(rule)
|
||||
end
|
||||
end
|
||||
|
||||
def range?(range)
|
||||
range.match(/^(?:(?:\d+|\d+:\d+),)*(?:\d+|\d+:\d+)$/)
|
||||
end
|
||||
|
||||
def filter_protocol(chain, protocol, policy)
|
||||
policy = policy.to_s.upcase
|
||||
rule "-A #{chain} -p #{protocol} -j #{policy}"
|
||||
end
|
||||
|
||||
def filter_ports(chain, protocol, range, policy)
|
||||
policy = policy.to_s.upcase
|
||||
if range? range
|
||||
rule "-A #{chain} -p #{protocol} -m multiport --dports #{range} -j #{policy}"
|
||||
end
|
||||
end
|
||||
|
||||
def tap_to_chain(tap, chain)
|
||||
rule "-A FORWARD -m physdev --physdev-out #{tap} -j #{chain}"
|
||||
end
|
||||
|
||||
def new_chain(chain)
|
||||
rule "-N #{chain}"
|
||||
end
|
||||
|
||||
def rule(rule)
|
||||
"#{COMMANDS[:iptables]} #{rule}"
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,73 @@
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
|
||||
# #
|
||||
# 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. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
class OpenNebulaHM < OpenNebulaNetwork
|
||||
def initialize(vm, hypervisor = nil)
|
||||
super(vm,hypervisor)
|
||||
@bridges = get_interfaces
|
||||
end
|
||||
|
||||
def activate
|
||||
vm_id = @vm['ID']
|
||||
process do |nic|
|
||||
bridge = nic[:bridge]
|
||||
dev = nic[:phydev]
|
||||
if dev
|
||||
vlan = CONF[:start_vlan] + nic[:network_id].to_i
|
||||
|
||||
create_bridge bridge if !bridge_exists? bridge
|
||||
create_dev_vlan(dev, vlan) if !device_exists?(dev, vlan)
|
||||
if !attached_bridge_dev?(bridge, dev, vlan)
|
||||
attach_brigde_dev(bridge, dev, vlan)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def deactivate
|
||||
vm_id = @vm['ID']
|
||||
process do |nic|
|
||||
end
|
||||
end
|
||||
|
||||
def bridge_exists?(bridge)
|
||||
@bridges.keys.include? bridge
|
||||
end
|
||||
|
||||
def create_bridge(bridge)
|
||||
system("#{COMMANDS[:brctl]} addbr #{bridge}")
|
||||
end
|
||||
|
||||
def device_exists?(dev, vlan=nil)
|
||||
dev = "#{dev}.#{vlan}" if vlan
|
||||
system("#{COMMANDS[:ip]} link show #{dev}")
|
||||
end
|
||||
|
||||
def create_dev_vlan(dev, vlan)
|
||||
system("#{COMMANDS[:vconfig]} add #{dev} #{vlan}")
|
||||
end
|
||||
|
||||
def attached_bridge_dev?(bridge, dev, vlan=nil)
|
||||
return false if !bridge_exists? bridge
|
||||
dev = "#{dev}.#{vlan}" if vlan
|
||||
@bridges[bridge].include? dev
|
||||
end
|
||||
|
||||
def attach_brigde_dev(bridge, dev, vlan=nil)
|
||||
dev = "#{dev}.#{vlan}" if vlan
|
||||
system("#{COMMANDS[:brctl]} addif #{bridge} #{dev}")
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,145 @@
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
|
||||
# #
|
||||
# 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. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
class Nics < Array
|
||||
def initialize(hypervisor)
|
||||
case hypervisor
|
||||
when "kvm"
|
||||
@nicClass = NicKVM
|
||||
when "xen"
|
||||
@nicClass = NicXen
|
||||
end
|
||||
end
|
||||
|
||||
def new_nic
|
||||
@nicClass.new
|
||||
end
|
||||
|
||||
# finds nics that match 'args'
|
||||
# 'args' can be a Hash, or an array
|
||||
# args example:
|
||||
# {:mac => "02:00:C0:A8:01:01", :bridge => "br0"}
|
||||
# :mac, "02:00:C0:A8:01:01"
|
||||
# key values may also be an array:
|
||||
# {:mac => "02:00:C0:A8:01:01", :bridge => ["br0","br1"]}
|
||||
def get(*args)
|
||||
if args.length == 2
|
||||
dict = Hash.new
|
||||
dict[args[0]] = args[1]
|
||||
elsif args.length == 1
|
||||
dict = args[0]
|
||||
else
|
||||
return nil
|
||||
end
|
||||
|
||||
matching = Array.new
|
||||
self.each do |e|
|
||||
e_filter = Hash.new
|
||||
dict.each_key{|k| e_filter[k] = e[k]}
|
||||
if compare(e_filter,dict)
|
||||
matching << e
|
||||
end
|
||||
end
|
||||
|
||||
if matching.empty?
|
||||
nil
|
||||
else
|
||||
matching
|
||||
end
|
||||
end
|
||||
|
||||
def compare(hash1, hash2)
|
||||
#hash1 has a single value per key
|
||||
#hash2 may contain an array of values
|
||||
hash1.each do |k,v|
|
||||
return false if !hash2[k]
|
||||
v2 = hash2[k]
|
||||
if hash2[k].kind_of?(Array)
|
||||
return false if !v2.include? v
|
||||
else
|
||||
return false if v != v2
|
||||
end
|
||||
end
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
class NicKVM < Hash
|
||||
def initialize
|
||||
super(nil)
|
||||
end
|
||||
|
||||
def get_info(vm)
|
||||
@vm_info = Hash.new
|
||||
deploy_id = vm['DEPLOY_ID']
|
||||
if deploy_id
|
||||
@vm_info[:dumpxml] = `#{COMMANDS[:virsh]} dumpxml #{deploy_id} 2>/dev/null`
|
||||
@vm_info.each_key{|k| @vm_info[k] = nil if @vm_info[k].to_s.strip.empty?}
|
||||
end
|
||||
end
|
||||
|
||||
def get_tap
|
||||
dumpxml = @vm_info[:dumpxml]
|
||||
if dumpxml
|
||||
dumpxml_root = REXML::Document.new(dumpxml).root
|
||||
|
||||
xpath = "devices/interface[@type='bridge']/"
|
||||
xpath << "mac[@address='#{self[:mac]}']/../target"
|
||||
tap = dumpxml_root.elements[xpath]
|
||||
|
||||
if tap
|
||||
self[:tap] = tap.attributes['dev']
|
||||
end
|
||||
end
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
class NicXen < Hash
|
||||
def initialize
|
||||
super(nil)
|
||||
end
|
||||
|
||||
def get_info(vm)
|
||||
@vm_info = Hash.new
|
||||
deploy_id = vm['DEPLOY_ID']
|
||||
if deploy_id
|
||||
@vm_info[:domid] =`#{COMMANDS[:xm]} domid #{deploy_id}`.strip
|
||||
@vm_info[:networks] =`#{COMMANDS[:xm]} network-list #{deploy_id}`
|
||||
@vm_info.each_key{|k| @vm_info[k] = nil if @vm_info[k].to_s.strip.empty?}
|
||||
end
|
||||
@vm_info
|
||||
end
|
||||
|
||||
def get_tap
|
||||
domid = @vm_info[:domid]
|
||||
if domid
|
||||
networks = @vm_info[:networks].split("\n")[1..-1]
|
||||
networks.each do |net|
|
||||
n = net.split
|
||||
|
||||
iface_id = n[0]
|
||||
iface_mac = n[2]
|
||||
|
||||
if iface_mac == self[:mac]
|
||||
self[:tap] = "vif#{domid}.#{iface_id}"
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
self
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,145 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
|
||||
# #
|
||||
# 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__)
|
||||
|
||||
require 'rexml/document'
|
||||
require 'Nic'
|
||||
|
||||
CONF = {
|
||||
:start_vlan => 2
|
||||
}
|
||||
|
||||
COMMANDS = {
|
||||
:ebtables => "sudo /sbin/ebtables",
|
||||
:iptables => "sudo /sbin/iptables",
|
||||
:brctl => "sudo /usr/sbin/brctl",
|
||||
:ip => "sudo /sbin/ip",
|
||||
:vconfig => "sudo /sbin/vconfig",
|
||||
:virsh => "virsh -c qemu:///system",
|
||||
:xm => "sudo /usr/sbin/xm",
|
||||
:ovs_vsctl=> "sudo /usr/local/bin/ovs-vsctl",
|
||||
:lsmod => "/sbin/lsmod"
|
||||
}
|
||||
|
||||
class VM
|
||||
def initialize(vm_root)
|
||||
@vm_root = vm_root
|
||||
end
|
||||
|
||||
def [](element)
|
||||
if @vm_root
|
||||
val = @vm_root.elements[element]
|
||||
if val.text
|
||||
return val.text
|
||||
end
|
||||
end
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
class OpenNebulaNetwork
|
||||
attr_reader :vm_info, :hypervisor, :nics
|
||||
|
||||
def initialize(vm_tpl, hypervisor=nil)
|
||||
@vm_root = REXML::Document.new(vm_tpl).root
|
||||
@vm = VM.new(@vm_root)
|
||||
@vm_info = Hash.new
|
||||
|
||||
if !hypervisor
|
||||
hypervisor = detect_hypervisor
|
||||
end
|
||||
@hypervisor = hypervisor
|
||||
|
||||
@nics = get_nics
|
||||
@filtered_nics = @nics
|
||||
end
|
||||
|
||||
def filter(*filter)
|
||||
@filtered_nics = @nics.get(*filter)
|
||||
self
|
||||
end
|
||||
|
||||
def unfilter
|
||||
@filtered_nics = @nics
|
||||
self
|
||||
end
|
||||
|
||||
def process(&block)
|
||||
if @filtered_nics
|
||||
@filtered_nics.each do |n|
|
||||
yield(n)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def detect_hypervisor
|
||||
uname_a = `uname -a`
|
||||
lsmod = `#{COMMANDS[:lsmod]}`
|
||||
|
||||
if uname_a.match(/xen/i)
|
||||
"xen"
|
||||
elsif lsmod.match(/kvm/)
|
||||
"kvm"
|
||||
end
|
||||
end
|
||||
|
||||
def get_nics
|
||||
nics = Nics.new(@hypervisor)
|
||||
|
||||
@vm_root.elements.each("TEMPLATE/NIC") do |nic_element|
|
||||
nic = nics.new_nic
|
||||
nic_element.elements.each('*') do |nic_attribute|
|
||||
key = nic_attribute.xpath.split('/')[-1].downcase.to_sym
|
||||
nic[key] = nic_attribute.text
|
||||
end
|
||||
nic.get_info(@vm)
|
||||
nic.get_tap
|
||||
nics << nic
|
||||
end
|
||||
nics
|
||||
end
|
||||
|
||||
def get_interfaces
|
||||
bridges = Hash.new
|
||||
brctl_exit =`#{COMMANDS[:brctl]} show`
|
||||
|
||||
cur_bridge = ""
|
||||
|
||||
brctl_exit.split("\n")[1..-1].each do |l|
|
||||
l = l.split
|
||||
|
||||
if l.length > 1
|
||||
cur_bridge = l[0]
|
||||
|
||||
bridges[cur_bridge] = Array.new
|
||||
bridges[cur_bridge] << l[3]
|
||||
else
|
||||
bridges[cur_bridge] << l[0]
|
||||
end
|
||||
end
|
||||
|
||||
bridges
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,461 +0,0 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
|
||||
# #
|
||||
# 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__)
|
||||
|
||||
require 'rexml/document'
|
||||
|
||||
CONF = {
|
||||
:start_vlan => 2
|
||||
}
|
||||
|
||||
COMMANDS = {
|
||||
:ebtables => "sudo /sbin/ebtables",
|
||||
:iptables => "sudo /sbin/iptables",
|
||||
:brctl => "sudo /usr/sbin/brctl",
|
||||
:ip => "sudo /sbin/ip",
|
||||
:vconfig => "sudo /sbin/vconfig",
|
||||
:virsh => "virsh -c qemu:///system",
|
||||
:xm => "sudo /usr/sbin/xm",
|
||||
:ovs_vsctl=> "sudo /usr/local/bin/ovs-vsctl",
|
||||
:lsmod => "/sbin/lsmod"
|
||||
}
|
||||
|
||||
class Nics < Array
|
||||
# finds nics that match 'args'
|
||||
# 'args' can be a Hash, or an array
|
||||
# args example:
|
||||
# {:mac => "02:00:C0:A8:01:01", :bridge => "br0"}
|
||||
# :mac, "02:00:C0:A8:01:01"
|
||||
# key values may also be an array:
|
||||
# {:mac => "02:00:C0:A8:01:01", :bridge => ["br0","br1"]}
|
||||
def get(*args)
|
||||
if args.length == 2
|
||||
dict = Hash.new
|
||||
dict[args[0]] = args[1]
|
||||
elsif args.length == 1
|
||||
dict = args[0]
|
||||
else
|
||||
return nil
|
||||
end
|
||||
|
||||
matching = Array.new
|
||||
self.each do |e|
|
||||
e_filter = Hash.new
|
||||
dict.each_key{|k| e_filter[k] = e[k]}
|
||||
if compare(e_filter,dict)
|
||||
matching << e
|
||||
end
|
||||
end
|
||||
|
||||
if matching.empty?
|
||||
nil
|
||||
else
|
||||
matching
|
||||
end
|
||||
end
|
||||
|
||||
def compare(hash1, hash2)
|
||||
#hash1 has a single value per key
|
||||
#hash2 may contain an array of values
|
||||
hash1.each do |k,v|
|
||||
return false if !hash2[k]
|
||||
v2 = hash2[k]
|
||||
if hash2[k].kind_of?(Array)
|
||||
return false if !v2.include? v
|
||||
else
|
||||
return false if v != v2
|
||||
end
|
||||
end
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
class VM
|
||||
def initialize(vm_root)
|
||||
@vm_root = vm_root
|
||||
end
|
||||
|
||||
def [](element)
|
||||
if @vm_root
|
||||
val = @vm_root.elements[element]
|
||||
if val.text
|
||||
return val.text
|
||||
end
|
||||
end
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
class OpenNebulaVLAN
|
||||
attr_reader :vm_info, :hypervisor, :nics
|
||||
|
||||
def initialize(vm_tpl, hypervisor=nil)
|
||||
@vm_root = REXML::Document.new(vm_tpl).root
|
||||
@vm = VM.new(@vm_root)
|
||||
@vm_info = Hash.new
|
||||
|
||||
if !hypervisor
|
||||
hypervisor = detect_hypervisor
|
||||
end
|
||||
@hypervisor = hypervisor
|
||||
|
||||
case hypervisor
|
||||
when "kvm"
|
||||
require 'KVMVLAN'
|
||||
class <<self
|
||||
include OpenNebulaVLANKVM
|
||||
end
|
||||
when "xen"
|
||||
require 'XenVLAN'
|
||||
class <<self
|
||||
include OpenNebulaVLANXen
|
||||
end
|
||||
end
|
||||
|
||||
@vm_info = get_info
|
||||
|
||||
@nics = get_nics
|
||||
@filtered_nics = @nics
|
||||
end
|
||||
|
||||
def filter(*filter)
|
||||
@filtered_nics = @nics.get(*filter)
|
||||
self
|
||||
end
|
||||
|
||||
def unfilter
|
||||
@filtered_nics = @nics
|
||||
self
|
||||
end
|
||||
|
||||
def process(&block)
|
||||
if @filtered_nics
|
||||
@filtered_nics.each do |n|
|
||||
yield(n)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def detect_hypervisor
|
||||
uname_a = `uname -a`
|
||||
lsmod = `#{COMMANDS[:lsmod]}`
|
||||
|
||||
if uname_a.match(/xen/i)
|
||||
"xen"
|
||||
elsif lsmod.match(/kvm/)
|
||||
"kvm"
|
||||
end
|
||||
end
|
||||
|
||||
def get_nics
|
||||
nics = Nics.new
|
||||
@vm_root.elements.each("TEMPLATE/NIC") do |nic_element|
|
||||
nic = new_nic(@hypervisor)
|
||||
nic_element.elements.each('*') do |nic_attribute|
|
||||
key = nic_attribute.xpath.split('/')[-1].downcase.to_sym
|
||||
nic[key] = nic_attribute.text
|
||||
end
|
||||
nic.get_tap(self)
|
||||
nics << nic
|
||||
end
|
||||
nics
|
||||
end
|
||||
|
||||
def get_interfaces
|
||||
bridges = Hash.new
|
||||
brctl_exit =`#{COMMANDS[:brctl]} show`
|
||||
|
||||
cur_bridge = ""
|
||||
|
||||
brctl_exit.split("\n")[1..-1].each do |l|
|
||||
l = l.split
|
||||
|
||||
if l.length > 1
|
||||
cur_bridge = l[0]
|
||||
|
||||
bridges[cur_bridge] = Array.new
|
||||
bridges[cur_bridge] << l[3]
|
||||
else
|
||||
bridges[cur_bridge] << l[0]
|
||||
end
|
||||
end
|
||||
|
||||
bridges
|
||||
end
|
||||
end
|
||||
|
||||
class OpenvSwitchVLAN < OpenNebulaVLAN
|
||||
def initialize(vm, hypervisor = nil)
|
||||
super(vm,hypervisor)
|
||||
end
|
||||
|
||||
def activate
|
||||
process do |nic|
|
||||
cmd = "#{COMMANDS[:ovs_vsctl]} set Port #{nic[:tap]} "
|
||||
cmd << "tag=#{nic[:network_id].to_i + CONF[:start_vlan]}"
|
||||
|
||||
system(cmd)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class EbtablesVLAN < OpenNebulaVLAN
|
||||
def initialize(vm, hypervisor = nil)
|
||||
super(vm,hypervisor)
|
||||
end
|
||||
|
||||
def ebtables(rule)
|
||||
system("#{COMMANDS[:ebtables]} -A #{rule}")
|
||||
end
|
||||
|
||||
def activate
|
||||
process do |nic|
|
||||
tap = nic[:tap]
|
||||
if tap
|
||||
iface_mac = nic[:mac]
|
||||
|
||||
mac = iface_mac.split(':')
|
||||
mac[-1] = '00'
|
||||
|
||||
net_mac = mac.join(':')
|
||||
|
||||
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"
|
||||
|
||||
ebtables(in_rule)
|
||||
ebtables(out_rule)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def deactivate
|
||||
process do |nic|
|
||||
mac = nic[:mac]
|
||||
# remove 0-padding
|
||||
mac = mac.split(":").collect{|e| e.hex.to_s(16)}.join(":")
|
||||
|
||||
tap = ""
|
||||
rules.each do |rule|
|
||||
if m = rule.match(/#{mac} -i (\w+)/)
|
||||
tap = m[1]
|
||||
break
|
||||
end
|
||||
end
|
||||
remove_rules(tap)
|
||||
end
|
||||
end
|
||||
|
||||
def rules
|
||||
`#{COMMANDS[:ebtables]} -L FORWARD`.split("\n")[3..-1]
|
||||
end
|
||||
|
||||
def remove_rules(tap)
|
||||
rules.each do |rule|
|
||||
if rule.match(tap)
|
||||
remove_rule(rule)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def remove_rule(rule)
|
||||
system("#{COMMANDS[:ebtables]} -D FORWARD #{rule}")
|
||||
end
|
||||
end
|
||||
|
||||
class OpenNebulaFirewall < OpenNebulaVLAN
|
||||
def initialize(vm, hypervisor = nil)
|
||||
super(vm,hypervisor)
|
||||
end
|
||||
def activate
|
||||
vm_id = @vm['ID']
|
||||
process do |nic|
|
||||
#:white_ports_tcp => iptables_range
|
||||
#:white_ports_udp => iptables_range
|
||||
#:black_ports_tcp => iptables_range
|
||||
#:black_ports_udp => iptables_range
|
||||
#:icmp => 'DROP' or 'NO'
|
||||
|
||||
nic_rules = Array.new
|
||||
|
||||
chain = "one-#{vm_id}-#{nic[:network_id]}"
|
||||
tap = nic[:tap]
|
||||
|
||||
if tap
|
||||
#TCP
|
||||
if range = nic[:white_ports_tcp]
|
||||
nic_rules << filter_established(chain, :tcp, :accept)
|
||||
nic_rules << filter_ports(chain, :tcp, range, :accept)
|
||||
nic_rules << filter_protocol(chain, :tcp, :drop)
|
||||
elsif range = nic[:black_ports_tcp]
|
||||
nic_rules << filter_ports(chain, :tcp, range, :drop)
|
||||
end
|
||||
|
||||
#UDP
|
||||
if range = nic[:white_ports_udp]
|
||||
nic_rules << filter_established(chain, :udp, :accept)
|
||||
nic_rules << filter_ports(chain, :udp, range, :accept)
|
||||
nic_rules << filter_protocol(chain, :udp, :drop)
|
||||
elsif range = nic[:black_ports_udp]
|
||||
nic_rules << filter_ports(chain, :udp, range, :drop)
|
||||
end
|
||||
|
||||
#ICMP
|
||||
if nic[:icmp]
|
||||
if %w(no drop).include? nic[:icmp].downcase
|
||||
nic_rules << filter_established(chain, :icmp, :accept)
|
||||
nic_rules << filter_protocol(chain, :icmp, :drop)
|
||||
end
|
||||
end
|
||||
|
||||
process_chain(chain, tap, nic_rules)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def deactivate
|
||||
vm_id = @vm['ID']
|
||||
process do |nic|
|
||||
chain = "one-#{vm_id}-#{nic[:network_id]}"
|
||||
iptables_out = `#{COMMANDS[:iptables]} -n -v --line-numbers -L FORWARD`
|
||||
if m = iptables_out.match(/.*#{chain}.*/)
|
||||
rule_num = m[0].split(/\s+/)[0]
|
||||
purge_chain(chain, rule_num)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def purge_chain(chain, rule_num)
|
||||
rules = Array.new
|
||||
rules << rule("-D FORWARD #{rule_num}")
|
||||
rules << rule("-F #{chain}")
|
||||
rules << rule("-X #{chain}")
|
||||
run_rules rules
|
||||
end
|
||||
|
||||
def process_chain(chain, tap, nic_rules)
|
||||
rules = Array.new
|
||||
if !nic_rules.empty?
|
||||
# new chain
|
||||
rules << new_chain(chain)
|
||||
# move tap traffic to chain
|
||||
rules << tap_to_chain(tap, chain)
|
||||
|
||||
rules << nic_rules
|
||||
end
|
||||
run_rules rules
|
||||
end
|
||||
|
||||
def filter_established(chain, protocol, policy)
|
||||
policy = policy.to_s.upcase
|
||||
rule "-A #{chain} -p #{protocol} -m state --state ESTABLISHED -j #{policy}"
|
||||
end
|
||||
|
||||
def run_rules(rules)
|
||||
rules.flatten.each do |rule|
|
||||
system(rule)
|
||||
end
|
||||
end
|
||||
|
||||
def range?(range)
|
||||
range.match(/^(?:(?:\d+|\d+:\d+),)*(?:\d+|\d+:\d+)$/)
|
||||
end
|
||||
|
||||
def filter_protocol(chain, protocol, policy)
|
||||
policy = policy.to_s.upcase
|
||||
rule "-A #{chain} -p #{protocol} -j #{policy}"
|
||||
end
|
||||
|
||||
def filter_ports(chain, protocol, range, policy)
|
||||
policy = policy.to_s.upcase
|
||||
if range? range
|
||||
rule "-A #{chain} -p #{protocol} -m multiport --dports #{range} -j #{policy}"
|
||||
end
|
||||
end
|
||||
|
||||
def tap_to_chain(tap, chain)
|
||||
rule "-A FORWARD -m physdev --physdev-out #{tap} -j #{chain}"
|
||||
end
|
||||
|
||||
def new_chain(chain)
|
||||
rule "-N #{chain}"
|
||||
end
|
||||
|
||||
def rule(rule)
|
||||
"#{COMMANDS[:iptables]} #{rule}"
|
||||
end
|
||||
end
|
||||
|
||||
class OpenNebulaHM < OpenNebulaVLAN
|
||||
def initialize(vm, hypervisor = nil)
|
||||
super(vm,hypervisor)
|
||||
@bridges = get_interfaces
|
||||
end
|
||||
|
||||
def activate
|
||||
vm_id = @vm['ID']
|
||||
process do |nic|
|
||||
bridge = nic[:bridge]
|
||||
dev = nic[:phydev]
|
||||
if dev
|
||||
vlan = CONF[:start_vlan] + nic[:network_id].to_i
|
||||
|
||||
create_bridge bridge if !bridge_exists? bridge
|
||||
create_dev_vlan(dev, vlan) if !device_exists?(dev, vlan)
|
||||
if !attached_bridge_dev?(bridge, dev, vlan)
|
||||
attach_brigde_dev(bridge, dev, vlan)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def deactivate
|
||||
vm_id = @vm['ID']
|
||||
process do |nic|
|
||||
end
|
||||
end
|
||||
|
||||
def bridge_exists?(bridge)
|
||||
@bridges.keys.include? bridge
|
||||
end
|
||||
|
||||
def create_bridge(bridge)
|
||||
system("#{COMMANDS[:brctl]} addbr #{bridge}")
|
||||
end
|
||||
|
||||
def device_exists?(dev, vlan=nil)
|
||||
dev = "#{dev}.#{vlan}" if vlan
|
||||
system("#{COMMANDS[:ip]} link show #{dev}")
|
||||
end
|
||||
|
||||
def create_dev_vlan(dev, vlan)
|
||||
system("#{COMMANDS[:vconfig]} add #{dev} #{vlan}")
|
||||
end
|
||||
|
||||
def attached_bridge_dev?(bridge, dev, vlan=nil)
|
||||
return false if !bridge_exists? bridge
|
||||
dev = "#{dev}.#{vlan}" if vlan
|
||||
@bridges[bridge].include? dev
|
||||
end
|
||||
|
||||
def attach_brigde_dev(bridge, dev, vlan=nil)
|
||||
dev = "#{dev}.#{vlan}" if vlan
|
||||
system("#{COMMANDS[:brctl]} addif #{bridge} #{dev}")
|
||||
end
|
||||
end
|
||||
@@ -14,40 +14,17 @@
|
||||
# limitations under the License. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
module OpenNebulaVLANKVM
|
||||
def new_nic(hypervisor)
|
||||
NicKVM.new(hypervisor)
|
||||
class OpenvSwitchVLAN < OpenNebulaNetwork
|
||||
def initialize(vm, hypervisor = nil)
|
||||
super(vm,hypervisor)
|
||||
end
|
||||
|
||||
def get_info
|
||||
vminfo = Hash.new
|
||||
deploy_id = @vm['DEPLOY_ID']
|
||||
if deploy_id
|
||||
vminfo[:dumpxml] = `#{COMMANDS[:virsh]} dumpxml #{deploy_id} 2>/dev/null`
|
||||
vminfo.each_key{|k| vminfo[k] = nil if vminfo[k].to_s.strip.empty?}
|
||||
def activate
|
||||
process do |nic|
|
||||
cmd = "#{COMMANDS[:ovs_vsctl]} set Port #{nic[:tap]} "
|
||||
cmd << "tag=#{nic[:network_id].to_i + CONF[:start_vlan]}"
|
||||
|
||||
system(cmd)
|
||||
end
|
||||
vminfo
|
||||
end
|
||||
end
|
||||
|
||||
class NicKVM < Hash
|
||||
def initialize(hash=nil)
|
||||
super(nil)
|
||||
end
|
||||
def get_tap(vm)
|
||||
dumpxml = vm.vm_info[:dumpxml]
|
||||
if dumpxml
|
||||
dumpxml_root = REXML::Document.new(dumpxml).root
|
||||
|
||||
xpath = "devices/interface[@type='bridge']/"
|
||||
xpath << "mac[@address='#{self[:mac]}']/../target"
|
||||
tap = dumpxml_root.elements[xpath]
|
||||
|
||||
if tap
|
||||
self[:tap] = tap.attributes['dev']
|
||||
end
|
||||
end
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
# -------------------------------------------------------------------------- #
|
||||
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) #
|
||||
# #
|
||||
# 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. #
|
||||
#--------------------------------------------------------------------------- #
|
||||
|
||||
module OpenNebulaVLANXen
|
||||
def new_nic(hypervisor)
|
||||
NicXen.new(hypervisor)
|
||||
end
|
||||
|
||||
def get_info
|
||||
vminfo = Hash.new
|
||||
deploy_id = @vm['DEPLOY_ID']
|
||||
if deploy_id
|
||||
vminfo[:domid] =`#{COMMANDS[:xm]} domid #{deploy_id}`.strip
|
||||
vminfo[:networks] =`#{COMMANDS[:xm]} network-list #{deploy_id}`
|
||||
vminfo.each_key{|k| vminfo[k] = nil if vminfo[k].to_s.strip.empty?}
|
||||
end
|
||||
vminfo
|
||||
end
|
||||
end
|
||||
|
||||
class NicXen < Hash
|
||||
def initialize(hash=nil)
|
||||
super(nil)
|
||||
end
|
||||
|
||||
def get_tap(vm)
|
||||
domid = vm.vm_info[:domid]
|
||||
if domid
|
||||
networks = vm.vm_info[:networks].split("\n")[1..-1]
|
||||
networks.each do |net|
|
||||
n = net.split
|
||||
|
||||
iface_id = n[0]
|
||||
iface_mac = n[2]
|
||||
|
||||
if iface_mac == self[:mac]
|
||||
self[:tap] = "vif#{domid}.#{iface_id}"
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
self
|
||||
end
|
||||
end
|
||||
@@ -19,7 +19,8 @@
|
||||
$: << File.dirname(__FILE__)
|
||||
|
||||
require 'base64'
|
||||
require 'OpenNebulaVLAN'
|
||||
require 'OpenNebulaNetwork'
|
||||
require 'Ebtables'
|
||||
|
||||
action = ARGV[0]
|
||||
template = ARGV[1]
|
||||
|
||||
@@ -19,7 +19,8 @@
|
||||
$: << File.dirname(__FILE__)
|
||||
|
||||
require 'base64'
|
||||
require 'OpenNebulaVLAN'
|
||||
require 'OpenNebulaNetwork'
|
||||
require 'Firewall'
|
||||
|
||||
action = ARGV[0]
|
||||
template = ARGV[1]
|
||||
|
||||
+2
-1
@@ -19,7 +19,8 @@
|
||||
$: << File.dirname(__FILE__)
|
||||
|
||||
require 'base64'
|
||||
require 'OpenNebulaVLAN'
|
||||
require 'OpenNebulaNetwork'
|
||||
require 'HostManaged'
|
||||
|
||||
template = ARGV[0]
|
||||
vm_xml = Base64::decode64(template)
|
||||
|
||||
@@ -19,7 +19,8 @@
|
||||
$: << File.dirname(__FILE__)
|
||||
|
||||
require 'base64'
|
||||
require 'OpenNebulaVLAN'
|
||||
require 'OpenNebulaNetwork'
|
||||
require 'OpenvSwitch'
|
||||
|
||||
template = ARGV[0]
|
||||
vm_xml = Base64::decode64(template)
|
||||
|
||||
+8
-3
@@ -1,11 +1,17 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
$: << File.dirname(__FILE__) + '/..'
|
||||
|
||||
require 'rubygems'
|
||||
require 'rspec'
|
||||
require 'SystemMock'
|
||||
require 'pp'
|
||||
|
||||
require File.expand_path(File.dirname(__FILE__) + '/../OpenNebulaVLAN')
|
||||
require 'OpenNebulaNetwork'
|
||||
require 'Ebtables'
|
||||
require 'Firewall'
|
||||
require 'HostManaged'
|
||||
require 'OpenvSwitch'
|
||||
|
||||
OUTPUT = Hash.new
|
||||
Dir[File.dirname(__FILE__) + "/output/**"].each do |f|
|
||||
@@ -22,13 +28,12 @@ RSpec.configure do |config|
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe 'networking' do
|
||||
it "get all nics in kvm" do
|
||||
$capture_commands = {
|
||||
/virsh.*dumpxml/ => OUTPUT[:virsh_dumpxml]
|
||||
}
|
||||
onevlan = OpenNebulaVLAN.new(OUTPUT[:onevm_show],"kvm")
|
||||
onevlan = OpenNebulaNetwork.new(OUTPUT[:onevm_show],"kvm")
|
||||
nics_expected = [{:bridge=>"br0",
|
||||
:ip=>"172.16.0.100",
|
||||
:mac=>"02:00:ac:10:00:64",
|
||||
Reference in new issue
Block a user