Class: Phut::OpenVswitch

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

Overview

Open vSwitch controller.

Defined Under Namespace

Classes: AlreadyRunning

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.



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

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.



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

def dpid
  @dpid
end

#network_devicesObject (readonly)

Returns the value of attribute network_devices.



15
16
17
# File 'lib/phut/open_vswitch.rb', line 15

def network_devices
  @network_devices
end

Instance Method Details

#add_network_device(network_device) ⇒ Object



83
84
85
86
# File 'lib/phut/open_vswitch.rb', line 83

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



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

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

#bring_port_up(port_number) ⇒ Object



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

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

#dump_flowsObject



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

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

#maybe_stopObject



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

def maybe_stop
  return unless running?
  stop
end

#nameObject



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

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

#runObject Also known as: start

rubocop:disable MethodLength rubocop:disable AbcSize



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

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 AlreadyRunning, "Open vSwitch (dpid = #{@dpid}) is already running!"
end

#running?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/phut/open_vswitch.rb', line 79

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

#stopObject Also known as: shutdown

rubocop:enable MethodLength rubocop:enable AbcSize



56
57
58
59
# File 'lib/phut/open_vswitch.rb', line 56

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

#to_sObject



30
31
32
# File 'lib/phut/open_vswitch.rb', line 30

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