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

Returns a new instance of OpenVswitch.



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

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.



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

def dpid
  @dpid
end

#network_devicesObject (readonly)

Returns the value of attribute network_devices.



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

def network_devices
  @network_devices
end

Instance Method Details

#bring_port_down(port_number) ⇒ Object



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

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

#bring_port_up(port_number) ⇒ Object



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

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

#dump_flowsObject



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

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

#maybe_stopObject



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

def maybe_stop
  return unless running?
  stop
end

#nameObject



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

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

#runObject Also known as: start

rubocop:disable MethodLength rubocop:disable AbcSize



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

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=#{open_flow_protocol}" \
     " 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

Returns:

  • (Boolean)


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

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

#stopObject Also known as: shutdown

rubocop:enable MethodLength rubocop:enable AbcSize



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

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

#to_sObject



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

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