Class: Phut::OpenVswitch

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

Overview

Open vSwitch controller. rubocop:disable ClassLength

Defined Under Namespace

Classes: AlreadyRunning

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellRunner

#sh

Constructor Details

#initialize(dpid, port_number, name, logger) ⇒ OpenVswitch



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

def initialize(dpid, port_number, name, logger)
  @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.



50
51
52
# File 'lib/phut/open_vswitch.rb', line 50

def dpid
  @dpid
end

#network_devicesObject (readonly)

Returns the value of attribute network_devices.



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

def network_devices
  @network_devices
end

Class Method Details

.create(dpid, port_number = 6653, name = nil, logger = NullLogger.new) ⇒ Object



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

def self.create(dpid, port_number = 6653, name = nil,
                logger = NullLogger.new)
  new(dpid, port_number, name, logger).tap do |vswitch|
    conflict = find_by(name: vswitch.name)
    fail "The name #{vswitch.name} conflicts with #{conflict}." if conflict
    all << vswitch
  end
end

.dump_flows(dpid, port_number = 6653, name = nil, logger = NullLogger.new) ⇒ Object



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

def self.dump_flows(dpid, port_number = 6653, name = nil,
                    logger = NullLogger.new)
  OpenVswitch.new(dpid, port_number, name, logger).dump_flows
end

.each(&block) ⇒ Object



38
39
40
# File 'lib/phut/open_vswitch.rb', line 38

def self.each(&block)
  all.each(&block)
end

.find_by(queries) ⇒ Object



32
33
34
35
36
# File 'lib/phut/open_vswitch.rb', line 32

def self.find_by(queries)
  queries.inject(all) do |memo, (attr, value)|
    memo.find_all { |vswitch| vswitch.__send__(attr) == value }
  end.first
end

.select(&block) ⇒ Object



42
43
44
# File 'lib/phut/open_vswitch.rb', line 42

def self.select(&block)
  all.select(&block)
end

.shutdown(dpid, port_number = 6653, name = nil, logger = NullLogger.new) ⇒ Object



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

def self.shutdown(dpid, port_number = 6653, name = nil,
                  logger = NullLogger.new)
  OpenVswitch.new(dpid, port_number, name, logger).stop!
end

Instance Method Details

#add_network_device(network_device) ⇒ Object



123
124
125
126
# File 'lib/phut/open_vswitch.rb', line 123

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



107
108
109
# File 'lib/phut/open_vswitch.rb', line 107

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

#bring_port_up(port_number) ⇒ Object



103
104
105
# File 'lib/phut/open_vswitch.rb', line 103

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

#dump_flowsObject



111
112
113
114
115
116
117
# File 'lib/phut/open_vswitch.rb', line 111

def dump_flows
  output =
    `sudo ovs-ofctl dump-flows #{bridge_name} -O #{Pio::OpenFlow.version}`
  output.split("\n").inject('') do |memo, each|
    memo + ((/^(NXST|OFPST)_FLOW reply/=~ each) ? '' : each.lstrip + "\n")
  end
end

#nameObject



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

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

#runObject Also known as: start

rubocop:disable MethodLength rubocop:disable AbcSize



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/phut/open_vswitch.rb', line 72

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



119
120
121
# File 'lib/phut/open_vswitch.rb', line 119

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

#stopObject

rubocop:enable MethodLength rubocop:enable AbcSize



92
93
94
95
# File 'lib/phut/open_vswitch.rb', line 92

def stop
  return unless running?
  stop!
end

#stop!Object Also known as: shutdown



97
98
99
100
# File 'lib/phut/open_vswitch.rb', line 97

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

#to_sObject



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

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