Skip to content

Instantly share code, notes, and snippets.

@shun159
Last active December 22, 2015 17:19
Show Gist options
  • Save shun159/6505485 to your computer and use it in GitHub Desktop.
Save shun159/6505485 to your computer and use it in GitHub Desktop.
require "rubygems"
require "bundler/setup"
require "trema"
require "pp"
class ArpTest < Controller
periodic_time_event :flood_arp_request_frames, 5
def start
puts "starting #{ self }"
@featured_switches = []
end
def switch_ready( datapath_id )
send_message datapath_id, FeaturesRequest.new
end
def features_reply( datapath_id, features_reply )
@featured_switches << datapath_id
end
def packet_in( datapath_id, message )
if featured_switch.include?( datapath_id )
if message.arp_request?
puts "received arp request"
arp_frame = Pio::Arp.read( message.data )
pp arp_frame
handle_arp_request(
datapath_id,
message.in_port,
arp_frame.sender_protocol_address,
arp_frame.sender_hardware_address
)
elsif message.arp_reply?
puts "received arp request"
arp_frame = Pio::Arp.read( message.data )
pp arp_frame
else
raise "Not an ARP packet!!!!!!"
end
end
end
######################################################
private
######################################################
def handle_arp_request( datapath_id, out_port, target_proto_addr, target_hw_addr )
send_arp_reply( datapath_id, out_port, target_proto_addr, target_hw_addr )
end
def send_arp_reply( datapath_id, out_port, target_proto_addr, target_hw_addr )
send_out_packet( datapath_id,
:actions => SendOutPort.new( out_port ),
:data => arp_reply_frames( [
:source_mac => "00:00:00:00:00:00",
:source_protocol_address => "192.168.0.254",
:target_hardware_address => target_hw_addr
:target_protocol_address => target_proto_addr
] )
)
end
def flood_arp_request_frames
@featured_switches.each do | dpid |
send_arp_request( dpid )
end
end
def send_arp_request( datapath_id )
send_out_packet( datapath_id,
:actions => SendOutPort.new( OFPP_FLOOD ),
:data => arp_request_frames( [
:source_mac => "00:00:00:00:00:00",
:source_protocol_address => "192.168.0.254",
:target_protocol_address => "192.168.0.1"
] )
)
end
def arp_reply_frames_binary_string
Pio::ArpReply.new( options )
end
def arp_request_frames_binary_string
Pio::ArpRequest.new( options )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment