Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:trema/learning_switch into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Dec 21, 2015
2 parents e8e2246 + 6631200 commit 24a5676
Showing 1 changed file with 62 additions and 13 deletions.
75 changes: 62 additions & 13 deletions lib/learning_switch13.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,73 @@
require 'fdb'

# An OpenFlow controller that emulates a layer-2 switch.
class LearningSwitch13 < Trema::Controller
timer_event :age_fdb, interval: 5.sec

INGRESS_FILTERING_TABLE_ID = 0
FORWARDING_TABLE_ID = 1

AGING_TIME = 180

def start(_args)
@fdb = FDB.new
logger.info "#{name} started."
end

def switch_ready(datapath_id)
add_multicast_mac_drop_flow_entry datapath_id
add_default_forwarding_flow_entry datapath_id
add_default_flooding_flow_entry datapath_id
add_multicast_mac_drop_flow_entry(datapath_id)
add_ipv6_multicast_mac_drop_flow_entry(datapath_id)
add_default_broadcast_flow_entry(datapath_id)
add_default_flooding_flow_entry(datapath_id)
add_default_forwarding_flow_entry(datapath_id)
end

def packet_in(_datapath_id, message)
@fdb.learn(message.source_mac, message.in_port)
add_forwarding_flow_and_packet_out(message)
end

def packet_in(datapath_id, message)
add_forwarding_flow_entry(datapath_id, message)
def age_fdb
@fdb.age
end

private

def add_forwarding_flow_entry(datapath_id, message)
def add_forwarding_flow_and_packet_out(message)
port_no = @fdb.lookup(message.destination_mac)
add_forwarding_flow_entry(message, port_no) if port_no
packet_out(message, port_no || :flood)
end

def add_forwarding_flow_entry(message, port_no)
send_flow_mod_add(
datapath_id,
message.datapath_id,
table_id: FORWARDING_TABLE_ID,
idle_timeout: AGING_TIME,
priority: 2,
match: Match.new(ether_destination_address: message.source_mac),
instructions: Apply.new(SendOutPort.new(message.in_port))
match: Match.new(in_port: message.in_port,
ether_destination_address: message.destination_mac,
ether_source_address: message.source_mac),
instructions: Apply.new(SendOutPort.new(port_no))
)
end

def packet_out(message, port_no)
send_packet_out(
message.datapath_id,
packet_in: message,
actions: SendOutPort.new(port_no)
)
end

def add_default_broadcast_flow_entry(datapath_id)
send_flow_mod_add(
datapath_id,
table_id: FORWARDING_TABLE_ID,
idle_timeout: 0,
priority: 3,
match: Match.new(ether_destination_address: 'ff:ff:ff:ff:ff:ff'),
instructions: Apply.new(SendOutPort.new(:flood))
)
end

Expand All @@ -39,8 +78,7 @@ def add_default_flooding_flow_entry(datapath_id)
idle_timeout: 0,
priority: 1,
match: Match.new,
instructions: Apply.new([SendOutPort.new(:controller),
SendOutPort.new(:flood)])
instructions: Apply.new(SendOutPort.new(:controller))
)
end

Expand All @@ -50,8 +88,19 @@ def add_multicast_mac_drop_flow_entry(datapath_id)
table_id: INGRESS_FILTERING_TABLE_ID,
idle_timeout: 0,
priority: 2,
match: Match.new(ether_destination_address: '01:00:5e:00:00:00',
ether_destination_address_mask: 'ff:ff:ff:00:00:00')
match: Match.new(ether_destination_address: '01:00:00:00:00:00',
ether_destination_address_mask: 'ff:00:00:00:00:00')
)
end

def add_ipv6_multicast_mac_drop_flow_entry(datapath_id)
send_flow_mod_add(
datapath_id,
table_id: INGRESS_FILTERING_TABLE_ID,
idle_timeout: 0,
priority: 2,
match: Match.new(ether_destination_address: '33:33:00:00:00:00',
ether_destination_address_mask: 'ff:ff:00:00:00:00')
)
end

Expand Down

0 comments on commit 24a5676

Please sign in to comment.