Class: Phut::Vsctl

Inherits:
Object
  • Object
show all
Extended by:
ShellRunner
Includes:
ShellRunner
Defined in:
lib/phut/vsctl.rb

Overview

ovs-vsctl wrapper

Constant Summary collapse

MAX_BRIDGE_NAME_LENGTH =
15

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellRunner

sh, sudo

Constructor Details

#initialize(name:, bridge:) ⇒ Vsctl

Returns a new instance of Vsctl.



31
32
33
34
# File 'lib/phut/vsctl.rb', line 31

def initialize(name:, bridge:)
  @name = name
  @bridge = bridge
end

Instance Attribute Details

#bridgeObject (readonly)

Returns the value of attribute bridge.



29
30
31
# File 'lib/phut/vsctl.rb', line 29

def bridge
  @bridge
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.list_br(prefix) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/phut/vsctl.rb', line 13

def self.list_br(prefix)
  sudo('ovs-vsctl list-br').split.each_with_object([]) do |each, list|
    match = /^#{prefix}(\S+)/.match(each)
    next unless match
    name = match[1]
    vsctl = new(name: name, bridge: prefix + name)
    list << { name: vsctl.name,
              dpid: vsctl.dpid,
              openflow_version: vsctl.openflow_version,
              tcp_port: vsctl.tcp_port }
  end
end

Instance Method Details

#add_bridgeObject



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

def add_bridge
  sudo "ovs-vsctl add-br #{@bridge}"
  sudo "/sbin/sysctl -w net.ipv6.conf.#{@bridge}.disable_ipv6=1 -q"
end

#add_numbered_port(port_number, device) ⇒ Object



99
100
101
102
103
104
# File 'lib/phut/vsctl.rb', line 99

def add_numbered_port(port_number, device)
  add_port device
  sudo "ovs-vsctl set Interface #{device} "\
       "ofport_request=#{port_number}"
  nil
end

#add_port(device) ⇒ Object



94
95
96
97
# File 'lib/phut/vsctl.rb', line 94

def add_port(device)
  sudo "ovs-vsctl add-port #{@bridge} #{device}"
  nil
end

#bring_port_down(port_number) ⇒ Object



110
111
112
# File 'lib/phut/vsctl.rb', line 110

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

#bring_port_up(port_number) ⇒ Object



106
107
108
# File 'lib/phut/vsctl.rb', line 106

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

#delete_bridgeObject



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

def delete_bridge
  sudo "ovs-vsctl del-br #{@bridge}"
end

#delete_controllerObject



86
87
88
# File 'lib/phut/vsctl.rb', line 86

def delete_controller
  sudo "ovs-vsctl del-controller #{@bridge}"
end

#dpidObject



36
37
38
39
# File 'lib/phut/vsctl.rb', line 36

def dpid
  ('0x' +
   sudo("ovs-vsctl get bridge #{@bridge} datapath-id").delete('"')).hex
end

#dpid=(dpid) ⇒ Object



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

def dpid=(dpid)
  sudo "ovs-vsctl set bridge #{@bridge} "\
       "other-config:datapath-id=#{fill_zero(dpid)}"
end

#openflow_versionObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/phut/vsctl.rb', line 41

def openflow_version
  case sudo("ovs-ofctl show #{@bridge} -O OpenFlow10,OpenFlow13")
  when /^OFPT_FEATURES_REPLY \(xid=0x[^\)]+\):/
    1.0
  when /^OFPT_FEATURES_REPLY \(OF(\d\.\d)\)/
    Regexp.last_match(1).to_f
  else
    raise "Couldn't identify the OpenFlow version of #{@bridge}"
  end
end

#openflow_version=(version) ⇒ Object



61
62
63
64
65
66
# File 'lib/phut/vsctl.rb', line 61

def openflow_version=(version)
  sudo "ovs-vsctl set bridge #{@bridge} "\
       "protocols=OpenFlow#{version.to_s.delete('.')} "
rescue
  raise "Invalid openflow_version: #{version}"
end

#portsObject



114
115
116
# File 'lib/phut/vsctl.rb', line 114

def ports
  sudo("ovs-vsctl list-ports #{@bridge}").split
end

#set_fail_mode_secureObject



90
91
92
# File 'lib/phut/vsctl.rb', line 90

def set_fail_mode_secure
  sudo "ovs-vsctl set-fail-mode #{@bridge} secure"
end

#tcp_portObject



73
74
75
76
77
# File 'lib/phut/vsctl.rb', line 73

def tcp_port
  controller_info = sudo("ovs-vsctl get-controller #{@bridge}")
  return if controller_info.empty?
  controller_info.chomp.split(':').last.to_i
end

#tcp_port=(tcp_port) ⇒ Object



79
80
81
82
83
84
# File 'lib/phut/vsctl.rb', line 79

def tcp_port=(tcp_port)
  raise "Invalid tcp_port: #{tcp_port}" if tcp_port.negative?
  sudo "ovs-vsctl set-controller #{@bridge} "\
       "tcp:127.0.0.1:#{tcp_port} "\
       "-- set controller #{@bridge} connection-mode=out-of-band"
end