/
forwarding_func.py
executable file
·507 lines (415 loc) · 19.1 KB
/
forwarding_func.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# coding:utf-8
"""
本模块是Tor转发功能模块,可以通过监控ARP包来学习IP-MAC地址对,同时可以根据配置的静态ARP列表来对相应ARP请求进行应答。
本模块向控制台模块添加了arp对象,通过此对象可以查看和修改ARP Table
在启动本模块时可以通过以下命令添加静态ARP列表
arp_func --<IP>=<MAC> --<IP>=<MAC>
如果MAC地址没有指定,则默认使用Switch的MAC地址
"""
from pox.core import core
from pox.lib.packet.ethernet import ethernet, ETHER_BROADCAST
from pox.lib.addresses import IPAddr, EthAddr,parse_cidr,same_network
from pox.lib.recoco import Timer
from pox.lib.revent import Event,EventMixin
from pox.lib.util import dpid_to_str,str_to_dpid,str_to_bool
import pox.openflow.libopenflow_01 as of
import pox.lib.packet as pkt
import time
#Global Var
log = core.getLogger() #获得全局日志器
ARP_TIMEOUT = 60 * 4 #ARP列表项的超时时间(秒)
FLOW_IDLE_TIMEOUT = 60 * 3 #流表闲置超时时间(秒)
MAX_BUFFERED_PER_IP = 5 #Datapath为每个未知的目的IP地址缓存多少个报文
MAX_BUFFER_TIME = 5 #缓存报文的超时时间(秒)
class Entry (object):
"""
The MAC is used to answer ARP replies.
When timeout is older than ARP_TIMEOUT,
Just flood the ARP request not to answer it
"""
def __init__(self, mac, port=None, static=None, flood=None):
self.timeout = time.time() + ARP_TIMEOUT
self.static = False
self.flood = True
self.port = port
if mac is True:
# Means use switch's MAC, implies static/noflood
self.mac = True
self.static = True
self.flood = False
else:
self.mac = EthAddr(mac)
if static is not None:
self.static = static
if flood is not None:
self.flood = flood
def __eq__(self, other):
if isinstance(other, Entry):
return (self.static, self.mac) == (other.static, other.mac)
else:
return self.mac == other
def __ne__(self, other):
return not self.__eq__(other)
@property
def is_expired(self):
if self.static:
return False
return time.time() > self.timeout
class ARPTable (dict):
def __repr__(self):
o = []
for k, e in self.iteritems():
t = int(e.timeout - time.time())
if t < 0:
t = "X"
else:
t = str(t) + "s left"
if e.static:
t = "-"
mac = e.mac
port = e.port
if mac is True:
mac = "<Switch MAC>"
o.append((k, "%-16s %-22s %-9s %3s" % (k, mac, port, t)))
o.sort()
o = [e[1] for e in o]
o.insert(0, "----- ARP Table -----")
if len(o) == 1:
o.append("<< Empty >>")
return "\n".join(o)
def __setitem__(self, key, val):
key = IPAddr(key)
if not isinstance(val, Entry):
val = Entry(val)
dict.__setitem__(self, key, val)
def __delitem__(self, key):
key = IPAddr(key)
dict.__delitem__(self, key)
def set(self, key, value=True, static=True):
if not isinstance(value, Entry):
value = Entry(value, static=static)
self[key] = value
class ARPTableSet(dict):
def __repr__(self):
repr = list()
for dpid,table in self.items():
repr.append("DPID:"+dpid)
repr.append(table.__repr__()+"\n")
return "\n".join(repr)
class SendARPRequest(Event):
@property
def dpid(self):
return self.connection.dpid
def __str__(self):
return "Send ARPRequest to DPID-%s Port-%s: %s => %s" \
% (self.dpid,self.outport,self.src_mac,self.src_ip)
def __init__(self,con,src_ip=None,src_mac=None,dst_ip=None,dst_mac=None,outport=None,ofp=None,flood=False):
super(SendARPRequest,self).__init__()
self.connection = con
self.dst_ip = dst_ip
self.src_ip = src_ip
self.dst_mac = dst_mac
self.src_mac = src_mac
self.outport = outport
self.ofp = ofp
self.flood = flood
class SendARPReply(Event):
@property
def dpid(self):
return self.connection.dpid
def __str__(self):
return "Send ARPReply to DPID-%s Port-%s: %s => %s" \
% (self.dpid,self.outport,self.src_mac,self.src_ip)
def __init__(self,con,src_ip,src_mac,dst_ip,dst_mac,outport):
super(SendARPReply,self).__init__()
self.connection = con
self.dst_ip = dst_ip
self.src_ip = src_ip
self.dst_mac = dst_mac
self.src_mac = src_mac
self.outport = outport
class HostEvent (Event):
"""
Event when hosts join, leave, or move within the network
"""
def __init__(self, entry, join):
super(HostEvent, self).__init__()
self.entry = entry
self.join = join
self.leave = not join
def _dpid_to_mac(dpid):
# Should maybe look at internal port MAC instead?
return EthAddr("%012x" % (dpid & 0xffFFffFFffFF,))
class ForwardingFunction (EventMixin):
_eventMixin_events = set([SendARPRequest, SendARPReply,HostEvent])
def __init__(self,timeout,no_flow,eat_packets,no_learn,*args,**kwargs):
self.ARP_TIMEOUT = timeout
self.install_flow = not no_flow
self.eat_packets = eat_packets
self.learn = not no_learn
#buffer for packet stored in datapath
self.ipv4_buffers = dict()
self.arp_buffers = dict()
# This timer handles expiring stuff
self._expire_arp_table_timer = Timer(5, self._handle_arp_table_expiration,recurring=True)
self._expire_ipv4_buffer_timer = Timer(5, self._handle_ipv4_buffer_expiration,recurring=True)
self._expire_arp_buffer_timer = Timer(5, self._handle_arp_buffer_expiration,recurring=True)
# Listen to dependencies
core.addListeners(self)
def _listen_to_dependencies():
core.BaseUtil.addListeners(self,priority=100)
core.LLDPUtil.addListeners(self,priority=100)
core.call_when_ready(_listen_to_dependencies, ('BaseUtil','LLDPUtil'))
#Get SRPConfig
def _get_config():
log.debug('Config Module is loaded!')
self.arp_table = ARPTableSet()
for dpid in core.SRPConfig.tor_list:
self.arp_table[dpid] = ARPTable()
ip = IPAddr(parse_cidr(core.SRPConfig.get_tor_lan_addr(dpid))[0])
self.arp_table[dpid][ip] = Entry(_dpid_to_mac(str_to_dpid(dpid)), static=True)
for k, v in kwargs.iteritems():
self.arp_table[dpid][IPAddr(k)] = Entry(v, static=True)
core.Interactive.variables['arp'] = self.arp_table
core.call_when_ready(_get_config,('SRPConfig',))
def _handle_arp_table_expiration(self):
for dpid in core.SRPConfig.tor_list:
for k, e in self.arp_table[dpid].items():
if e.is_expired:
del self.arp_table[dpid][k]
def _handle_arp_buffer_expiration(self):
empty = []
for k, v in self.arp_buffers.iteritems():
dpid, ip = k
for item in list(v):
expires_at, buffer_id, in_port = item
if expires_at < time.time():
v.remove(item)
#不为packet out消息指定outport action,则默认使DPID丢弃该报文
msg = of.ofp_packet_out(buffer_id=buffer_id, in_port=in_port)
core.openflow.sendToDPID(str_to_dpid(dpid), msg)
if len(v) == 0:
empty.append(k)
for k in empty:
del self.arp_buffers[k]
def _handle_ipv4_buffer_expiration(self):
empty = []
for k, v in self.ipv4_buffers.iteritems():
dpid, ip = k
for item in list(v):
expires_at, buffer_id, in_port = item
if expires_at < time.time():
v.remove(item)
#不为packet out消息指定outport action,则默认使DPID丢弃该报文
msg = of.ofp_packet_out(buffer_id=buffer_id, in_port=in_port)
core.openflow.sendToDPID(str_to_dpid(dpid), msg)
if len(v) == 0:
empty.append(k)
for k in empty:
del self.ipv4_buffers[k]
def _send_ipv4_buffer(self,dpid,dst_ip,mac,outport):
if (dpid,dst_ip) in self.ipv4_buffers:
log.debug("[%s] Send buffered ipv4 packets to %s",dpid,dst_ip)
buffers = self.ipv4_buffers[(dpid,dst_ip)]
del self.ipv4_buffers[(dpid,dst_ip)]
for entry in buffers:
msg = of.ofp_packet_out(buffer_id=entry[1],in_port=entry[2])
msg.actions.append(of.ofp_action_dl_addr.set_dst(mac))
msg.actions.append(of.ofp_action_output(port=outport))
core.openflow.sendToDPID(str_to_dpid(dpid),msg)
def _send_arp_buffer(self,dpid,dst_ip,mac,outport):
if (dpid,dst_ip) in self.arp_buffers:
log.debug("[%s] Send buffered arp packets to %s",dpid,dst_ip)
buffers = self.arp_buffers[(dpid,dst_ip)]
del self.arp_buffers[(dpid,dst_ip)]
for entry in buffers:
msg = of.ofp_packet_out(buffer_id=entry[1],in_port=entry[2])
msg.actions.append(of.ofp_action_dl_addr.set_dst(mac))
msg.actions.append(of.ofp_action_output(port=outport))
core.openflow.sendToDPID(str_to_dpid(dpid),msg)
def _handle_HostDownEvent(self,event):
dpid = event.dpid
port = event.port
if dpid in self.arp_table:
del_ip = None
for ip in self.arp_table[dpid]:
entry = self.arp_table[dpid][ip]
if entry.port == port:
del_ip = ip
entry = {"dpid":dpid,"port":port,"macaddr":entry.mac,"ipaddr":ip}
ev = HostEvent(entry,False)
self.raiseEvent(ev)
break
if del_ip is not None:
del self.arp_table[dpid][del_ip]
def _handle_GoingUpEvent(self, event):
core.openflow.addListeners(self)
log.debug("Up...")
def _handle_ConnectionUp(self, event):
if self.install_flow:
fm = of.ofp_flow_mod()
fm.priority = 0x7000 # Pretty high
fm.match.dl_type = ethernet.ARP_TYPE
fm.actions.append(of.ofp_action_output(port=of.OFPP_CONTROLLER))
event.connection.send(fm)
def _handle_ConnectionDown(self,event):
dpid = dpid_to_str(event.connection.dpid)
if dpid in core.SRPConfig.core_list:
return
log.debug("[%s] Clear non-static entry in ARPTable",dpid)
for ip,entry in self.arp_table[dpid].items():
if entry.static is not True:
del self.arp_table[dpid][ip]
log.debug("[%s] Clear ipv4 buffer",dpid)
delete_buffer = list()
for k,entry in self.ipv4_buffers.iteritems():
dpid_t,dst_ip = k
if dpid == dpid_t:
delete_buffer.append((dpid,dst_ip))
for entry in delete_buffer:
del self.ipv4_buffers[entry]
log.debug("[%s] Clear arp buffer",dpid)
for k,entry in self.arp_buffers.iteritems():
dpid_t,dst_ip = k
if dpid == dpid_t:
del self.arp_buffers[(dpid,dst_ip)]
def _handle_ARPRequest(self,event):
dpid = dpid_to_str(event.dpid)
src_ip = event.arpp.protosrc
src_mac = event.arpp.hwsrc
dst_ip = event.arpp.protodst
if dpid in core.SRPConfig.core_list:
log.debug("[%s] Ignore ARPRequest Packet from Core Switch!",dpid)
return
if event.arpp.protodst in self.arp_table[dpid]:
#如果本地存有此被请求表项,则交换机代替应答此ARPRequest
dst_mac = self.arp_table[dpid][event.arpp.protodst].mac
log.debug("[%s] Raise SendARPReply Event %s => %s",
dpid,dst_ip,src_ip)
ev = SendARPReply(event.connection,dst_ip,dst_mac,src_ip,src_mac,event.inport)
self.raiseEvent(ev)
else:
#如果本地没有存此请求表项,则交换机将此ARPRequest泛洪发送出去
dst_mac = ETHER_BROADCAST
log.debug("[%s] Raise SendARPRequest Event Flood out %s => %s",
dpid,src_ip,dst_ip)
ev = SendARPRequest(event.connection,ofp=event.ofp,flood=True)
self.raiseEvent(ev)
def _handle_ARPReply(self,event):
dpid = dpid_to_str(event.connection.dpid)
src_ip = event.arpp.protosrc
src_mac = event.arpp.hwsrc
dst_ip = event.arpp.protodst
dst_mac = event.arpp.hwdst
inport = event.inport
if dpid in core.SRPConfig.core_list:
log.debug("[%s] Ignore ARPReply Packet from Core Switch!",dpid)
return
if same_network(dst_ip,core.SRPConfig.get_tor_lan_addr(dpid),32):
self._send_arp_buffer(dpid,src_ip,src_mac,inport)
else:
if dst_ip in self.arp_table[dpid]:
outport = self.arp_table[dpid][dst_ip].port
log.debug("[%s] Raise SendARPReply Event %s => %s",
dpid,src_ip,dst_ip)
ev = SendARPReply(event.connection,src_ip,src_mac,dst_ip,dst_mac,outport)
self.raiseEvent(ev)
else:
#缓存ARP Reply报文,并广播ARPRequest报文请求目的IP地址的MAC地址
log.debug("[%s] Buffer unsent arp packet point to %s",dpid,dst_ip)
if (dpid,dst_ip) not in self.arp_buffers:
self.arp_buffers[(dpid,dst_ip)] = []
buffers = self.arp_buffers[(dpid,dst_ip)]
entry = (time.time()+MAX_BUFFER_TIME,event.ofp.buffer_id,inport)
buffers.append(entry)
while len(buffers)>MAX_BUFFERED_PER_IP:
msg = of.ofp_packet_out(buffer_id=buffers[0][1],in_port=buffers[0][2])
event.connection.send(msg)
del buffers[0]
#触发SendARPRequest事件,请求该ARP报文的目的IP地址对应的MAC地址
src_ip = parse_cidr(core.SRPConfig.get_tor_lan_addr(dpid))[0]
src_mac = _dpid_to_mac(event.connection.dpid)
log.debug("[%s] Raise SendARPRequest Event %s => %s ",
dpid,src_ip,dst_ip)
ev = SendARPRequest(event.connection,src_ip,src_mac,dst_ip,ETHER_BROADCAST,flood=True)
self.raiseEvent(ev)
def _handle_NewIpMacPortPair(self,event):
ip = event.src_ip
mac = event.src_mac
port = event.inport
dpid = dpid_to_str(event.connection.dpid)
if dpid in core.SRPConfig.core_list:
log.debug("[%s] Ignore New Ip Mac Port Pair from Core Switch!",dpid)
return
if not same_network(ip,core.SRPConfig.get_tor_lan_addr(dpid)):
log.debug("[%s] Ignore New IP Mac Port Pair with ip not belong to the DPID!",dpid)
return
if ip in self.arp_table[dpid]:
del self.arp_table[dpid][ip]
self.arp_table[dpid][ip] = Entry(mac,port)
entry = {"dpid":dpid,"port":port,"macaddr":mac,"ipaddr":ip}
ev = HostEvent(entry,True)
self.raiseEvent(ev)
#发送之前缓存的buffer中的报文
self._send_ipv4_buffer(dpid,ip,mac,port)
def _handle_IPv4In(self,event):
dst_ip = event.ipv4p.dstip
dpid = dpid_to_str(event.connection.dpid)
inport = event.inport
#判断是否是Core收到了IPv4In事件
if dpid in core.SRPConfig.core_list:
return
#判断目的网段是否在对应DPID连接的网段中
if not same_network(dst_ip,core.SRPConfig.get_tor_lan_addr(dpid)):
return
if dst_ip in self.arp_table[dpid]:
#判断之前是否已经获得目的IP地址的MAC地址
table = self.arp_table[dpid][dst_ip]
log.debug("[%s] Send out the ipv4 packet: %s =>%s",dpid,event.ipv4p.srcip,dst_ip)
msg = of.ofp_packet_out(buffer_id = event.ofp.buffer_id, in_port = inport)
msg.actions.append(of.ofp_action_dl_addr.set_dst(table.mac))
msg.actions.append(of.ofp_action_output(port = table.port))
event.connection.send(msg)
log.debug("[%s] Set up the Flow for destination: %s",dpid,dst_ip)
actions = list()
actions.append(of.ofp_action_dl_addr.set_dst(table.mac))
actions.append(of.ofp_action_output(port = table.port))
match = of.ofp_match()
match.dl_type = pkt.ethernet.IP_TYPE
match.nw_dst = dst_ip
msg = of.ofp_flow_mod(command = of.OFPFC_MODIFY,
actions = actions,
match = match,
idle_timeout = FLOW_IDLE_TIMEOUT,
hard_timeout = of.OFP_FLOW_PERMANENT)
event.connection.send(msg)
else:
#缓存IPv4报文,并广播ARPRequest报文请求目的IP地址的MAC地址
log.debug("[%s] Buffer unsent IPv4 packet point to %s",dpid,dst_ip)
if (dpid,dst_ip) not in self.ipv4_buffers:
self.ipv4_buffers[(dpid,dst_ip)] = []
buffers = self.ipv4_buffers[(dpid,dst_ip)]
entry = (time.time()+MAX_BUFFER_TIME,event.ofp.buffer_id,inport)
buffers.append(entry)
while len(buffers)>MAX_BUFFERED_PER_IP:
msg = of.ofp_packet_out(buffer_id=buffers[0][1],in_port=buffers[0][2])
event.connection.send(msg)
del buffers[0]
#触发SendARPRequest事件,请求该IPv4报文的目的IP地址对应的MAC地址
src_ip = parse_cidr(core.SRPConfig.get_tor_lan_addr(dpid))[0]
src_mac = _dpid_to_mac(event.connection.dpid)
log.debug("[%s] Raise SendARPRequest Event %s | %s => %s | %s",
dpid,src_ip,src_mac,dst_ip,ETHER_BROADCAST)
ev = SendARPRequest(event.connection,src_ip,src_mac,dst_ip,ETHER_BROADCAST,flood=True)
self.raiseEvent(ev)
def launch(timeout=ARP_TIMEOUT,
no_flow=True,
eat_packets=True,
no_learn=False,
*args,**kwargs):
core.registerNew(ForwardingFunction,
int(timeout),
str_to_bool(no_flow),
str_to_bool(eat_packets),
str_to_bool(no_learn),
*args,**kwargs)