-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimple-router.rb
180 lines (141 loc) · 4.18 KB
/
simple-router.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#
# A router implementation in Trema
#
# Copyright (C) 2012 NEC Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
require "arp-table"
require "interface"
require "router-utils"
require "routing-table"
class SimpleRouter < Controller
include RouterUtils
def start
load "simple_router.conf"
@interfaces = Interfaces.new( $interface )
@arp_table = ARPTable.new
@routing_table = RoutingTable.new( $route )
end
def packet_in( dpid, message )
return if not to_me?( message )
if message.arp_request?
handle_arp_request dpid, message
elsif message.arp_reply?
handle_arp_reply message
elsif message.ipv4?
handle_ipv4 dpid, message
else
# noop.
end
end
private
def to_me?( message )
return true if message.macda.broadcast?
interface = @interfaces.find_by_port( message.in_port )
if interface and interface.has?( message.macda )
return true
end
end
def handle_arp_request( dpid, message )
port = message.in_port
daddr = message.arp_tpa
interface = @interfaces.find_by_port_and_ipaddr( port, daddr )
if interface
arp_reply = create_arp_reply_from( message, interface.hwaddr )
packet_out dpid, arp_reply, SendOutPort.new( interface.port )
end
end
def handle_arp_reply( message )
@arp_table.update message.in_port, message.arp_spa, message.arp_sha
end
def handle_ipv4( dpid, message )
if should_forward?( message )
forward dpid, message
elsif message.icmpv4_echo_request?
handle_icmpv4_echo_request dpid, message
else
# noop.
end
end
def should_forward?( message )
not @interfaces.find_by_ipaddr( message.ipv4_daddr )
end
def handle_icmpv4_echo_request( dpid, message )
interface = @interfaces.find_by_port( message.in_port )
saddr = message.ipv4_saddr.value
arp_entry = @arp_table.lookup( saddr )
if arp_entry
icmpv4_reply = create_icmpv4_reply( arp_entry, interface, message )
packet_out dpid, icmpv4_reply, SendOutPort.new( interface.port )
else
handle_unresolved_packet dpid, message, interface, saddr
end
end
def forward( dpid, message )
next_hop = resolve_next_hop( message.ipv4_daddr )
interface = @interfaces.find_by_prefix( next_hop )
if not interface or interface.port == message.in_port
return
end
arp_entry = @arp_table.lookup( next_hop )
if arp_entry
macsa = interface.hwaddr
macda = arp_entry.hwaddr
action = create_action_from( macsa, macda, interface.port )
flow_mod dpid, message, action
packet_out dpid, message.data, action
else
handle_unresolved_packet dpid, message, interface, next_hop
end
end
def resolve_next_hop( daddr )
next_hop = @routing_table.lookup( daddr.value )
if next_hop
next_hop
else
daddr.value
end
end
def flow_mod( dpid, message, action )
send_flow_mod_add(
dpid,
:match => ExactMatch.from( message ),
:actions => action
)
end
def packet_out( dpid, packet, action )
send_packet_out(
dpid,
:data => packet,
:actions => action
)
end
def handle_unresolved_packet( dpid, message, interface, ipaddr )
arp_request = create_arp_request_from( interface, ipaddr )
packet_out dpid, arp_request, SendOutPort.new( interface.port )
end
def create_action_from( macsa, macda, port )
[
SetEthSrcAddr.new( macsa.to_s ),
SetEthDstAddr.new( macda.to_s ),
SendOutPort.new( port )
]
end
end
### Local variables:
### mode: Ruby
### coding: utf-8
### indent-tabs-mode: nil
### End: