Class: Trema::Controller

Inherits:
Object
  • Object
show all
Includes:
Pio
Defined in:
lib/trema/controller.rb

Overview

The base class of Trema controller. Subclass and override handlers to implement a custom OpenFlow controller.

rubocop:disable ClassLength

Defined Under Namespace

Classes: FlowModAddOption, FlowModDeleteOption, PacketOutOption

Constant Summary

SWITCH =
{}
DEFAULT_TCP_PORT =
6653

Class Attribute Summary collapse

Instance Attribute Summary collapse

OpenFlow Message collapse

Handlers collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port_number = DEFAULT_TCP_PORT) ⇒ Controller

Returns a new instance of Controller



110
111
112
113
114
115
# File 'lib/trema/controller.rb', line 110

def initialize(port_number = DEFAULT_TCP_PORT)
  @port_number = port_number
  @threads = []
  @logger = Logger.new(name)
  @logger.level = Controller.logging_level
end

Class Attribute Details

.logging_levelObject

Returns the value of attribute logging_level



79
80
81
# File 'lib/trema/controller.rb', line 79

def logging_level
  @logging_level
end

Instance Attribute Details

#loggerLogger (readonly)

Returns:



88
89
90
# File 'lib/trema/controller.rb', line 88

def logger
  @logger
end

Class Method Details

._handler(_name) ⇒ Object



168
169
170
# File 'lib/trema/controller.rb', line 168

def self._handler(_name)
  # Noop.
end

.create(port_number = DEFAULT_TCP_PORT) ⇒ Object



105
106
107
# File 'lib/trema/controller.rb', line 105

def self.create(port_number = DEFAULT_TCP_PORT)
  @controller_klass.new(port_number)
end

.inherited(subclass) ⇒ Object



100
101
102
# File 'lib/trema/controller.rb', line 100

def self.inherited(subclass)
  @controller_klass = subclass
end

.timer_event(handler, options) ⇒ Object



90
91
92
93
# File 'lib/trema/controller.rb', line 90

def self.timer_event(handler, options)
  @timer_handlers ||= {}
  @timer_handlers[handler] = options.fetch(:interval)
end

.timer_handlersObject



95
96
97
# File 'lib/trema/controller.rb', line 95

def self.timer_handlers
  @timer_handlers || {}
end

Instance Method Details

#echo_request(datapath_id, message) ⇒ Object

The default handler for echo request messages. Override this to implement a custom handler.



186
187
188
189
# File 'lib/trema/controller.rb', line 186

def echo_request(datapath_id, message)
  echo_reply = Echo::Reply.new(xid: message.xid)
  send_message datapath_id, echo_reply
end

#nameObject



131
132
133
# File 'lib/trema/controller.rb', line 131

def name
  self.class.name
end

#packet_in(datapath_id, message) ⇒ Object

Packet In message handler. Override this to implement a custom handler.



195
# File 'lib/trema/controller.rb', line 195

_handler :packet_in

#run(args) ⇒ Object

Starts this controller. Usually you do not need to invoke explicitly, because this is called implicitly by “trema run” command.



121
122
123
124
125
126
127
128
129
# File 'lib/trema/controller.rb', line 121

def run(args)
  drb_socket_file =
    File.expand_path(File.join(Phut.socket_dir, "#{name}.ctl"))
  @drb = DRb::DRbServer.new 'drbunix:' + drb_socket_file, self
  maybe_send_handler :start, args
  socket = TCPServer.open('<any>', @port_number)
  start_timers
  loop { start_switch_thread(socket.accept) }
end

#send_flow_mod_add(datapath_id, options) ⇒ Object



142
143
144
145
# File 'lib/trema/controller.rb', line 142

def send_flow_mod_add(datapath_id, options)
  flow_mod = FlowMod.new(FlowModAddOption.new(options).to_hash)
  send_message datapath_id, flow_mod
end

#send_flow_mod_delete(datapath_id, options) ⇒ Object



147
148
149
150
# File 'lib/trema/controller.rb', line 147

def send_flow_mod_delete(datapath_id, options)
  flow_mod = FlowMod.new(FlowModDeleteOption.new(options).to_hash)
  send_message datapath_id, flow_mod
end

#send_message(datapath_id, message) ⇒ Object



157
158
159
160
161
# File 'lib/trema/controller.rb', line 157

def send_message(datapath_id, message)
  SWITCH.fetch(datapath_id).write message
rescue KeyError, Errno::ECONNRESET, Errno::EPIPE
  logger.debug "Switch #{datapath_id} is disconnected."
end

#send_packet_out(datapath_id, options) ⇒ Object



152
153
154
155
# File 'lib/trema/controller.rb', line 152

def send_packet_out(datapath_id, options)
  packet_out = PacketOut.new(PacketOutOption.new(options).to_hash)
  send_message datapath_id, packet_out
end

#start(argv) ⇒ Object

Start event handler. Override this to implement a custom handler.



176
# File 'lib/trema/controller.rb', line 176

_handler :start

#stopObject



135
136
137
138
# File 'lib/trema/controller.rb', line 135

def stop
  @drb.stop_service if @drb
  @threads.map(&:kill)
end

#switch_ready(datapath_id) ⇒ Object

Switch Ready event handler. Override this to implement a custom handler.



182
# File 'lib/trema/controller.rb', line 182

_handler :switch_ready