Skip to content

Commit

Permalink
Merge pull request #200 from trema/feature/refactor_packetin
Browse files Browse the repository at this point in the history
Refactor PacketIn messages.
  • Loading branch information
yasuhito committed Jul 9, 2015
2 parents 3812a02 + 0073c72 commit 23e0026
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'bundler/gem_tasks'

RELISH_PROJECT = 'trema/pio'
FLAY_THRESHOLD = 1030
FLAY_THRESHOLD = 757

task default: :travis
task test: [:spec, :cucumber]
Expand Down
7 changes: 0 additions & 7 deletions lib/pio/open_flow10/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,6 @@ def physical_ports
body_option :capabilities
body_option :actions
body_option :ports

def initialize(user_options = {})
validate_user_options user_options
header_options = parse_header_options(user_options)
body_options = parse_body_options(user_options)
@format = Format.new(header: header_options, body: body_options)
end
end
end
end
29 changes: 10 additions & 19 deletions lib/pio/open_flow10/packet_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ def method_missing(method, *args)
end
end

attr_accessor :datapath_id
alias_method :dpid, :datapath_id
alias_method :dpid=, :datapath_id=

# OpenFlow 1.0 Flow Mod message format.
class Format < BinData::Record
extend OpenFlow::Format
Expand All @@ -69,20 +65,15 @@ class Format < BinData::Record
body :body
end

# rubocop:disable MethodLength
def initialize(user_options = {})
header_options = parse_header_options(user_options)
body_options = if user_options.respond_to?(:fetch)
user_options.delete :transaction_id
user_options.delete :xid
dpid = user_options[:dpid]
user_options[:datapath_id] = dpid if dpid
user_options
else
''
end
@format = Format.new(header: header_options, body: body_options)
end
# rubocop:enable MethodLength
attr_accessor :datapath_id
alias_method :dpid, :datapath_id
alias_method :dpid=, :datapath_id=

body_option :transaction_id
body_option :xid
body_option :buffer_id
body_option :in_port
body_option :reason
body_option :raw_data
end
end
87 changes: 44 additions & 43 deletions lib/pio/open_flow13/packet_in.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,55 @@ module Pio

# OpenFlow 1.3 PacketIn message parser and generator
class PacketIn < OpenFlow::Message
# OpenFlow 1.3 PacketIn message format
class Format < BinData::Record
# OpenFlow 1.3 PacketIn message body
class Body < BinData::Record
# Why is this packet being sent to the controller?
# (enum ofp_packet_in_reason)
class Reason < BinData::Primitive
REASONS = { no_match: 0, action: 1, invalid_ttl: 2 }
# Why is this packet being sent to the controller?
# (enum ofp_packet_in_reason)
class Reason < BinData::Primitive
REASONS = { no_match: 0, action: 1, invalid_ttl: 2 }

uint8 :reason
uint8 :reason

def get
REASONS.invert.fetch(reason)
end
def get
REASONS.invert.fetch(reason)
end

def set(value)
self.reason = REASONS.fetch(value)
end
end
def set(value)
self.reason = REASONS.fetch(value)
end
end

endian :big
# OpenFlow 1.3 PacketIn message body
class Body < BinData::Record
endian :big

uint32 :buffer_id
uint16 :total_len, initial_value: -> { raw_data.length }
reason :reason
uint8 :table_id
uint64 :cookie
oxm :match
string :padding, length: 2
hide :padding
string :raw_data, read_length: :total_len
uint32 :buffer_id
uint16 :total_len, initial_value: -> { raw_data.length }
reason :reason
uint8 :table_id
uint64 :cookie
oxm :match
string :padding, length: 2
hide :padding
string :raw_data, read_length: :total_len

def length
16 + match.length + padding.length + raw_data.length
end
def length
16 + match.length + padding.length + raw_data.length
end

def data
@data ||= Pio::Parser.read(raw_data)
end
def data
@data ||= Pio::Parser.read(raw_data)
end

def in_port
match.in_port
end
def in_port
match.in_port
end

def method_missing(method, *args)
data.__send__(method, *args).snapshot
end
def method_missing(method, *args)
data.__send__(method, *args).snapshot
end
end

# OpenFlow 1.3 PacketIn message format
class Format < BinData::Record
extend OpenFlow::Format

header version: 4, message_type: 10
Expand All @@ -67,10 +67,11 @@ def method_missing(method, *args)
alias_method :dpid=, :datapath_id=
end

def initialize(user_attrs = {})
header_attrs = parse_header_options(user_attrs)
body_attrs = { raw_data: user_attrs[:raw_data] }
@format = Format.new(header: header_attrs, body: body_attrs)
end
body_option :transaction_id
body_option :xid
body_option :buffer_id
body_option :in_port
body_option :reason
body_option :raw_data
end
end
14 changes: 14 additions & 0 deletions spec/pio/open_flow10/packet_in_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'pio/open_flow10/packet_in'

describe Pio::PacketIn do
Given(:packet_in) { Pio::PacketIn.new }

describe '.new' do
it_should_behave_like('an OpenFlow message', Pio::PacketIn)
end

describe 'datapath_id=' do
When { packet_in.datapath_id = 1 }
Then { packet_in.datapath_id == 1 }
end
end
4 changes: 4 additions & 0 deletions spec/pio/open_flow13/packet_in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
describe Pio::PacketIn do
Given(:packet_in) { Pio::PacketIn.new }

describe '.new' do
it_should_behave_like('an OpenFlow message', Pio::PacketIn)
end

describe 'datapath_id=' do
When { packet_in.datapath_id = 1 }
Then { packet_in.datapath_id == 1 }
Expand Down

0 comments on commit 23e0026

Please sign in to comment.