Class: Phut::OpenVswitch

Inherits:
Object
  • Object
show all
Includes:
ShellRunner
Defined in:
lib/phut/open_vswitch.rb

Overview

Open vSwitch controller.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ShellRunner

#sh

Constructor Details

#initialize(dpid, port_number = 6653, name = nil, logger = NullLogger.new) ⇒ OpenVswitch



15
16
17
18
19
20
21
22
# File 'lib/phut/open_vswitch.rb', line 15

def initialize(dpid, port_number = 6653,
               name = nil, logger = NullLogger.new)
  @dpid = dpid
  @port_number = port_number
  @name = name
  @network_devices = []
  @logger = logger
end

Instance Attribute Details

#dpidObject (readonly) Also known as: datapath_id

Returns the value of attribute dpid.



11
12
13
# File 'lib/phut/open_vswitch.rb', line 11

def dpid
  @dpid
end

#network_devicesObject (readonly)

Returns the value of attribute network_devices.



13
14
15
# File 'lib/phut/open_vswitch.rb', line 13

def network_devices
  @network_devices
end

Instance Method Details

#add_network_device(network_device) ⇒ Object



81
82
83
84
# File 'lib/phut/open_vswitch.rb', line 81

def add_network_device(network_device)
  network_device.port_number = @network_devices.size + 1
  @network_devices << network_device
end

#bring_port_down(port_number) ⇒ Object



69
70
71
# File 'lib/phut/open_vswitch.rb', line 69

def bring_port_down(port_number)
  sh "sudo ovs-ofctl mod-port #{bridge_name} #{port_number} down"
end

#bring_port_up(port_number) ⇒ Object



65
66
67
# File 'lib/phut/open_vswitch.rb', line 65

def bring_port_up(port_number)
  sh "sudo ovs-ofctl mod-port #{bridge_name} #{port_number} up"
end

#dump_flowsObject



73
74
75
# File 'lib/phut/open_vswitch.rb', line 73

def dump_flows
  `sudo ovs-ofctl dump-flows #{bridge_name}`
end

#maybe_stopObject



60
61
62
63
# File 'lib/phut/open_vswitch.rb', line 60

def maybe_stop
  return unless running?
  stop
end

#nameObject



24
25
26
# File 'lib/phut/open_vswitch.rb', line 24

def name
  @name || format('%#x', @dpid)
end

#runObject Also known as: start

rubocop:disable MethodLength rubocop:disable AbcSize



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/phut/open_vswitch.rb', line 34

def run
  sh "sudo ovs-vsctl add-br #{bridge_name}"
  sh "sudo /sbin/sysctl -w net.ipv6.conf.#{bridge_name}.disable_ipv6=1 -q"
  @network_devices.each do |each|
    sh "sudo ovs-vsctl add-port #{bridge_name} #{each}"
  end
  sh "sudo ovs-vsctl set bridge #{bridge_name}" \
     " protocols=#{Pio::OpenFlow.version}" \
     " other-config:datapath-id=#{dpid_zero_filled}"
  sh "sudo ovs-vsctl set-controller #{bridge_name} "\
     "tcp:127.0.0.1:#{@port_number} "\
     "-- set controller #{bridge_name} connection-mode=out-of-band"
  sh "sudo ovs-vsctl set-fail-mode #{bridge_name} secure"
rescue
  raise "Open vSwitch (dpid = #{@dpid}) is already running!"
end

#running?Boolean



77
78
79
# File 'lib/phut/open_vswitch.rb', line 77

def running?
  system "sudo ovs-vsctl br-exists #{bridge_name}"
end

#stopObject Also known as: shutdown

rubocop:enable MethodLength rubocop:enable AbcSize



54
55
56
57
# File 'lib/phut/open_vswitch.rb', line 54

def stop
  fail "Open vSwitch (dpid = #{@dpid}) is not running!" unless running?
  sh "sudo ovs-vsctl del-br #{bridge_name}"
end

#to_sObject



28
29
30
# File 'lib/phut/open_vswitch.rb', line 28

def to_s
  "vswitch (name = #{name}, dpid = #{format('%#x', @dpid)})"
end