/
of_tutorial_router.py
executable file
·368 lines (313 loc) · 14.3 KB
/
of_tutorial_router.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
from pox.core import core
import pox
log = core.getLogger()
import pox.lib.packet as pkt
from pox.lib.packet.ethernet import ethernet, ETHER_BROADCAST
from pox.lib.packet.ipv4 import ipv4
#from pox.lib.packet.icmp import icmp
from pox.lib.packet.arp import arp
from pox.lib.addresses import IPAddr, EthAddr
from pox.lib.util import str_to_bool, dpidToStr
from pox.lib.recoco import Timer
import pox.openflow.libopenflow_01 as of
from pox.lib.revent import *
import time
class Entry (object):
def __init__ (self, port, mac):
self.port = port
self.mac = mac
def __eq__ (self, other):
if type(other) == tuple:
return (self.port,self.mac)==other
else:
return (self.port,self.mac)==(other.port,other.mac)
def __ne__ (self, other):
return not self.__eq__(other)
def dpid_to_mac (dpid):
return EthAddr("%012x" % (dpid & 0xffFFffFFffFF,))
class router (EventMixin):
def __init__ (self, arp_for_unknowns = False):
self.arp_for_unknowns = arp_for_unknowns
self.message_queue = {}
self.arpTable= {}
"""
self.arpTable[1]= {}
self.arpTable[2]= {}
"""
self.ip_to_port = {}
self.routingTable={}
# Creating two routing table for both switches
self.routingTable[1] = [ ['10.0.1.2', '10.0.1.2', 's1-eth1', '10.0.1.1', 1],['10.0.1.3', '10.0.1.3','s1-eth2', '10.0.1.1', 2],['10.0.2.2', '10.0.2.1', 's1-eth3', '10.0.2.1', 3],['10.0.2.3', '10.0.2.1', 's1-eth3', '10.0.2.1',3],['10.0.2.4','10.0.2.1','s1-eth3','10.0.2.1',3]]
self.routingTable[2] = [ ['10.0.2.2', '10.0.2.2', 's2-eth1', '10.0.2.1', 1],['10.0.2.3', '10.0.2.3', 's2-eth2', '10.0.2.1', 2],['10.0.2.4', '10.0.2.4', 's2-eth3', '10.0.2.1', 3],['10.0.1.2', '10.0.1.1', 's2-eth4', '10.0.1.1', 4],['10.0.1.3','10.0.1.1','s2-eth4','10.0.1.1',4]]
"""
self.arpTable[1] = [ ['10.0.1.1', '92:66:3c:5f:00:4f', 1],['10.0.2.1 4','26:22:39:c5:02:e9', 2],['10.0.5.1','82:2e:de:d7:db:a7', 3]]
self.arpTable[1][IPAddr("10.0.1.1")]=EthAddr("92:66:3c:5f:00:4f")
self.arpTable[1][IPAddr("10.0.2.1")]=EthAddr("26:22:39:c5:02:e9")
self.arpTable[1][IPAddr("10.0.5.1")]=EthAddr("82:2e:de:d7:db:a7")
self.arpTable[2][IPAddr("10.0.3.1")]=EthAddr("a6:5e:13:57:db:b4")
self.arpTable[2][IPAddr("10.0.4.1")]=EthAddr("6e:d3:b4:f2:34:5e")
self.arpTable[2][IPAddr("10.0.6.1")]=EthAddr("c6:c1:b9:f8:a9:77")
"""
self.arpTable=[['10.0.1.1','92:66:3c:5f:00:4f','1'],['10.0.3.1','26:22:39:c5:02:e9','2'],['10.0.3.2','82:2e:de:d7:db:a7','1'],['10.0.2.1','a6:5e:13:57:db:b4','2']]
core.register("learning_switch", self)
self.listenTo(core)
def sendWaitingPackets (self, dpid, ipaddr, macaddr, port):
if (dpid,ipaddr) in self.message_queue:
bucket = self.message_queue[(dpid,ipaddr)]
del self.message_queue[(dpid,ipaddr)]
log.debug("Sending %i buffered packets to %s from %s"
% (len(bucket),ipaddr,dpidToStr(dpid)))
for _,buffer_id,in_port in bucket:
po = of.ofp_packet_out(buffer_id=buffer_id,in_port=in_port)
po.actions.append(of.ofp_action_dl_addr.set_dst(macaddr))
po.actions.append(of.ofp_action_output(port = port))
core.openflow.sendToDPID(dpid, po)
def _handle_GoingUpEvent (self, event):
self.listenTo(core.openflow)
log.debug("Up...")
def _handle_PacketIn (self, event):
dpid = event.connection.dpid
inport = event.port
packet = event.parsed
if not packet.parsed:
log.warning("%i %i ignoring unparsed packet", dpid, inport)
return
if dpid not in self.arpTable:
self.arpTable[dpid] = {}
if packet.type == ethernet.LLDP_TYPE:
return
if isinstance(packet.next, ipv4):
log.debug("%i %i IP %s => %s", dpid,inport,packet.next.srcip,packet.next.dstip)
self.sendWaitingPackets (dpid, packet.next.srcip, packet.src, inport)
if packet.next.srcip in self.arpTable[dpid]:
if self.arpTable[dpid][packet.next.srcip] != (inport, packet.src):
log.info("%i %i RE-learned %s", dpid,inport,packet.next.srcip)
else:
log.debug("%i %i learned %s", dpid,inport,str(packet.next.srcip))
self.arpTable[dpid][packet.next.srcip] = Entry(inport, packet.src)
prt = 0
dstaddr = packet.next.dstip
prt = self.ip_to_port.get(dstaddr)
if prt != None:
log.debug("Got port %i from dictionary",prt)
mac = None
if dstaddr in self.arpTable[dpid]:
mac = self.arpTable[dpid][dstaddr].mac
if mac!=None:
actions = []
actions.append(of.ofp_action_dl_addr.set_dst(mac))
actions.append(of.ofp_action_dl_addr.set_src(dpid_to_mac(dpid)))
actions.append(of.ofp_action_output(port = prt))
match = of.ofp_match.from_packet(packet, inport)
match.dl_src = None
msg = of.ofp_flow_mod(command=of.OFPFC_ADD,buffer_id=event.ofp.buffer_id,actions=actions,match=of.ofp_match.from_packet(packet,inport))
event.connection.send(msg.pack())
return
else:
# Add to tracked buffers
if (dpid,dstaddr) not in self.message_queue:
self.message_queue[(dpid,dstaddr)] = []
bucket = self.message_queue[(dpid,dstaddr)]
entry = (None,event.ofp.buffer_id,inport)
bucket.append(entry)
r = arp()
r.hwtype = r.HW_TYPE_ETHERNET
r.prototype = r.PROTO_TYPE_IP
r.hwlen = 6
r.protolen = r.protolen
r.opcode = r.REQUEST
r.hwdst = ETHER_BROADCAST
r.protodst = dstaddr
r.hwsrc = dpid_to_mac(dpid)
r.protosrc = packet.next.srcip
e = ethernet(type=ethernet.ARP_TYPE, src=packet.src,dst=ETHER_BROADCAST)
e.set_payload(r)
log.debug("%i %i ARPing for %s on behalf of %s" % (dpid, inport,str(r.protodst), str(r.protosrc)))
msg = of.ofp_packet_out()
msg.data = e.pack()
msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD))
msg.in_port = inport
event.connection.send(msg)
else:
icmpflag = 0
for list in self.routingTable[dpid]:
if list[0] == str(dstaddr):
log.debug("Searching Routing Table for IP address: %s ",str(dstaddr))
prt = list[4]
if list[3] == str(dstaddr):
prt = list[4]
icmpflag = 1
self.arpTable[dpid][dstaddr] = Entry(prt, dpid_to_mac(dpid))
if icmpflag == 1:
icmp = pkt.icmp()
icmp.type = pkt.TYPE_ECHO_REPLY
icmp.payload = packet.find("icmp").payload
# Make the IP packet around it
ipp = pkt.ipv4()
ipp.protocol = ipp.ICMP_PROTOCOL
ipp.srcip = packet.find("ipv4").dstip
ipp.dstip = packet.find("ipv4").srcip
# Ethernet around that...
e = pkt.ethernet()
e.src = packet.dst
e.dst = packet.src
e.type = e.IP_TYPE
# Hook them up...
ipp.payload = icmp
e.payload = ipp
# Send it back to the input port
msg = of.ofp_packet_out()
msg.actions.append(of.ofp_action_output(port = of.OFPP_IN_PORT))
msg.data = e.pack()
msg.in_port = event.port
event.connection.send(msg)
return
if prt == None:
log.debug("Unreachable IP Address : %s",str(dstaddr))
icmp = pkt.icmp()
icmp.type = 3
icmp.code = 0
if packet.next.dstip.inNetwork("10.0.1.0/24") or packet.next.dstip.inNetwork("10.0.2.0/24"):
icmp.code =1
#if packet.next.dstip.inNetwork("10.0.1.0/24") or packet.next.dstip.inNetwork("10.0.2.0/24"):
#icmp.code = 1
orig_ip = event.parsed.find('ipv4')
d = orig_ip.pack()
d = d[:orig_ip.hl * 4 + 8]
import struct
d = struct.pack("!HH", 0,0) + d #FIXME: MTU
icmp.payload = d
ipp = pkt.ipv4()
ipp.protocol = ipp.ICMP_PROTOCOL
ipp.srcip = IPAddr("10.0.1.1")
ipp.dstip = packet.find("ipv4").srcip
e = pkt.ethernet()
e.src = dpid_to_mac(dpid)
e.dst = packet.src
e.type = e.IP_TYPE
ipp.payload = icmp
e.payload = ipp
msg = of.ofp_packet_out()
msg.actions.append(of.ofp_action_output(port = event.port))
msg.data = e.pack()
#msg.inport = event.port
event.connection.send(msg)
log.debug("%s pinged unreachable %s", ipp.dstip, ipp.srcip)
return
log.debug("Got Port Number : %s from routing table",prt)
self.ip_to_port[dstaddr] = prt
mac = None
if dstaddr in self.arpTable[dpid]:
mac = self.arpTable[dpid][dstaddr].mac
if mac!=None:
actions = []
actions.append(of.ofp_action_dl_addr.set_dst(mac))
actions.append(of.ofp_action_dl_addr.set_src(dpid_to_mac(dpid)))
actions.append(of.ofp_action_output(port = prt))
match = of.ofp_match.from_packet(packet, inport)
match.dl_src = None # Wildcard source MAC
msg = of.ofp_flow_mod(command=of.OFPFC_ADD,buffer_id=event.ofp.buffer_id,actions=actions,match=of.ofp_match.from_packet(packet,inport))
event.connection.send(msg.pack())
return
else:
if (dpid,dstaddr) not in self.message_queue:
self.message_queue[(dpid,dstaddr)] = []
bucket = self.message_queue[(dpid,dstaddr)]
entry = (None,event.ofp.buffer_id,inport)
bucket.append(entry)
r = arp()
r.hwtype = r.HW_TYPE_ETHERNET
r.prototype = r.PROTO_TYPE_IP
r.hwlen = 6
r.protolen = r.protolen
r.opcode = r.REQUEST
r.hwdst = ETHER_BROADCAST
r.protodst = dstaddr
r.hwsrc = dpid_to_mac(dpid)
r.protosrc = packet.next.srcip
e = ethernet(type=ethernet.ARP_TYPE, src=packet.src,dst=ETHER_BROADCAST)
e.set_payload(r)
log.debug("%i %i ARPing for %s on behalf of %s" % (dpid, inport,str(r.protodst), str(r.protosrc)))
msg = of.ofp_packet_out()
msg.data = e.pack()
msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD))
msg.in_port = inport
event.connection.send(msg)
elif isinstance(packet.next, arp):
a = packet.next
log.debug("%i %i ARP %s %s => %s", dpid, inport,
{arp.REQUEST:"request",arp.REPLY:"reply"}.get(a.opcode,
'op:%i' % (a.opcode,)), str(a.protosrc), str(a.protodst))
dstaddr = a.protodst
prt = None
for list in self.routingTable[dpid]:
if list[0] == str(dstaddr):
log.debug("Searching Routing Table for IP address: %s ",str(dstaddr))
prt = list[4]
log.debug("The mac address is %s",dpid_to_mac(dpid))
if list[3] == str(dstaddr):
prt = list[4]
#icmpflag = 1
self.arpTable[dpid][dstaddr] = Entry(prt, dpid_to_mac(dpid))
log.debug("The mac address is %s",dpid_to_mac(dpid))
if prt == None:
log.debug("Unreachable IP Address : %s",str(dstaddr))
return
if a.prototype == arp.PROTO_TYPE_IP:
if a.hwtype == arp.HW_TYPE_ETHERNET:
if a.protosrc != 0:
# Learn or update port/MAC info
if a.protosrc in self.arpTable[dpid]:
if self.arpTable[dpid][a.protosrc] != (inport, packet.src):
log.info("%i %i RE-learned %s", dpid,inport,str(a.protosrc))
else:
log.debug("%i %i learned %s", dpid,inport,str(a.protosrc))
self.arpTable[dpid][a.protosrc] = Entry(inport, packet.src)
log.debug("The inport %i The input mac address is %s",inport,str(packet.src))
# Send any waiting packets...
self.sendWaitingPackets(dpid, a.protosrc, packet.src, inport)
if a.opcode == arp.REQUEST:
# Maybe we can answer
if a.protodst in self.arpTable[dpid]:
# We have an answer...
log.debug("This is a known path")
r = arp()
r.hwtype = a.hwtype
r.prototype = a.prototype
r.hwlen = a.hwlen
r.protolen = a.protolen
r.opcode = arp.REPLY
r.hwdst = a.hwsrc
r.protodst = a.protosrc
r.protosrc = a.protodst
r.hwsrc = self.arpTable[dpid][a.protodst].mac
e = ethernet(type=packet.type, src=dpid_to_mac(dpid), dst=a.hwsrc)
e.set_payload(r)
log.debug("%i %i answering ARP from %s to %s" % (dpid, inport,str(r.protosrc),str(r.protodst)))
msg = of.ofp_packet_out()
msg.data = e.pack()
msg.actions.append(of.ofp_action_output(port =
of.OFPP_IN_PORT))
msg.in_port = inport
event.connection.send(msg)
return
if a.opcode == arp.REPLY:
self.arpTable[dpid][dstaddr] = Entry(prt, a.hwsrc)
for list in self.routingTable[dpid]:
if list[0] == str(a.protodst):
log.debug("Searching Routing Table for IP address: %s ",str(a.protodst))
prt = list[4]
msg = of.ofp_packet_out(in_port = inport, action = of.ofp_action_output(port = prt))
log.debug("%i %i flooding ARP %s %s => %s" % (dpid, inport,
{arp.REQUEST:"request",arp.REPLY:"reply"}.get(a.opcode,
'op:%i' % (a.opcode,)), str(a.protosrc), str(a.protodst)))
msg = of.ofp_packet_out(in_port = inport, action = of.ofp_action_output(port = of.OFPP_FLOOD))
if event.ofp.buffer_id is of.NO_BUFFER:
# Try sending the (probably incomplete) raw data
msg.data = event.data
else:
msg.buffer_id = event.ofp.buffer_id
event.connection.send(msg.pack())
def launch ( a=None):
a = str_to_bool(a)
core.registerNew(router, a)