-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
369 lines (289 loc) · 14.7 KB
/
controller.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
'''
Please add your name: Cheng Boon Yew Joseph
Please add your matric number: A0125474E
'''
import sys
import os
import time
from sets import Set
from pox.core import core
import pox.openflow.libopenflow_01 as of
import pox.openflow.discovery
import pox.openflow.spanning_tree
import pox.lib.packet as pkt
from pox.lib.revent import *
from pox.lib.util import str_to_bool, dpid_to_str
from pox.lib.addresses import IPAddr, EthAddr
from pox.lib.packet.ethernet import ethernet, ETHER_BROADCAST
from pox.lib.packet.tcp import tcp
from pox.lib.packet.ipv4 import ipv4
from pox.lib.packet.arp import arp
from csv import DictReader
log = core.getLogger()
# Timeout for flows
FLOW_IDLE_TIMEOUT = 10
# We don't want to flood immediately when a switch connects.
# Can be overriden on commandline.
_flood_delay = 0
numRowFirewall = 0
numRowPreimum = 0
priorityLearn = 1
priorityPremium = 10
priorityFirewall = 100
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 Controller(EventMixin):
def __init__(self, policyFile):
self.listenTo(core.openflow)
core.openflow_discovery.addListeners(self)
# Initialize forwarding table
# self.macToPort = {}
self.forwardTable = {}
self.lost_buffers = {}
# Initialize firewall table
self.firewallTable = {}
# Initialize premium table
self.premiumTable = {}
self.policyFile = policyFile;
self.hold_down_expired = _flood_delay == 0
def read_Policy(self, file):
with open(file, 'r') as f:
index = 0
for line in f:
line = line.strip("\n ' '")
if index == 0:
firstLineItems = line.split(' ')
numRowFirewall = int(firstLineItems[0])
numRowPreimum = int(firstLineItems[1])
elif (numRowFirewall >= index):
items = line.split(',')
# (srcIp, destIp, port) = True
self.firewallTable[(items[0], items[1], items[2])] = True
else:
# left the premium lines
self.premiumTable[(line)] = True
index +=1
log.debug("Size of firewall table = %s", str(len(self.firewallTable)))
log.debug("Size of premium table = %s", str(len(self.premiumTable)))
# reader = DictReader(f, delimiter = ",")
## policies = {}
# for row in reader:
# log.info("...ROW = "
# self.firewallTable[(row[0], row[1])] = row[2]
## log.info("*** Added firewall rule in src=%s, dest=%s, port=%s", row[0], row[1], row[2])
## return policies
def check_premium(self, srcIp):
try:
entry = self.premiumTable[(str(srcIp))]
log.debug("CHECK PREMIUM src=%s, result=%s", str(srcIp), str(entry))
return entry
except KeyError:
log.debug("NOT IN PREMIUM src=%s", str(srcIp))
return False
def check_rule(self, srcIp, destIp, port):
try:
log.debug("CHECK RULE src=%s, dst=%s, port=%s", str(srcIp), str(destIp), str(port))
entry = self.firewallTable[(str(srcIp), str(destIp), str(port))]
return entry
except KeyError:
log.debug("NOT IN FIREWALL src=%s, dst=%s port=%s", str(srcIp), str(destIp), str(port))
return False
def _send_lost_buffers (self, dpid, ipaddr, macaddr, port):
if (dpid, ipaddr) in self.lost_buffers:
bucket = self.lost_buffers[(dpid,ipaddr)]
del self.lost_buffers[(dpid,ipaddr)]
log.debug("Sending %i buffered packets to %s from %s" % (len(bucket), ipaddr, dpid_to_str(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_PacketIn (self, event):
# install entries to the route table
#
# def install_enqueue(event, packet, outport, q_id):
# self.macToPort[packet.src] = event.port
# Check the packet and decide how to route the packet
def forward(message = None):
# dpid => switch (i)
dpid = event.connection.dpid
# port of a switch
inport = event.port
packet = event.parsed
log.debug("Add to forward table src=%s, port=%s" % (packet.src, event.port))
# self.macToPort[packet.src] = event.port
if dpid not in self.forwardTable:
self.forwardTable[dpid] = {}
if packet.type == ethernet.LLDP_TYPE:
# Ignore LLDP packets
return
if isinstance(packet.next, ipv4):
log.debug("IPV4 packet s=%i, sp=%i src=%s => dst=%s", dpid, inport,
packet.next.srcip, packet.next.dstip)
if (packet.next.protocol == packet.next.TCP_PROTOCOL):
# Get TCP payload in IPv4
tcpPacket = packet.next.payload
log.debug("TCP payload srcport= %s dstport=%s", str(tcpPacket.srcport), str(tcpPacket.dstport))
# Check firewall
if self.check_rule(packet.next.srcip, packet.next.dstip, tcpPacket.dstport) or self.check_rule(packet.next.dstip, packet.next.srcip, tcpPacket.dstport):
log.warning("Firewall DROP between %s <--> %s on port %s", str(packet.next.srcip), str(packet.next.dstip), str(tcpPacket.dstport))
drop(priorityFirewall)
return
# Send any waiting packets...
self._send_lost_buffers(dpid, packet.next.srcip, packet.src, inport)
# Learn or update forwarding table
log.debug("s=%i, sp=%i LEARN or UPDATE src=%s", dpid, inport, str(packet.next.srcip))
self.forwardTable[dpid][packet.next.srcip] = Entry(inport, packet.src)
# Try to forward to destination by checking port
dstip = packet.next.dstip
if (dstip in self.forwardTable[dpid]):
# Got the port info to send out
dstport = self.forwardTable[dpid][dstip].port
dstmac = self.forwardTable[dpid][dstip].mac
if dstport == inport:
# Same port for destination and switch..
log.warning("s=%i, sp=%i NOT SEND PACKET for dst=%s with same port" % (dpid, inport, str(dstip)))
else:
log.debug("s=%i, sp=%i INSTALL FLOW for src=%s => dst=%s out port=%i"
% (dpid, inport, packet.next.srcip, dstip, dstport))
actions = []
if self.check_premium(packet.next.dstip):
actions.append(of.ofp_action_enqueue(port = dstport, queue_id = 1))
else:
actions.append(of.ofp_action_output(port = dstport))
# actions.append(of.ofp_action_output(port = dstport))
actions.append(of.ofp_action_dl_addr.set_dst(dstmac))
match = of.ofp_match.from_packet(packet, inport)
match.dl_src = None # Wildcard source MAC
msg = of.ofp_flow_mod(command=of.OFPFC_ADD, idle_timeout=FLOW_IDLE_TIMEOUT, hard_timeout=of.OFP_FLOW_PERMANENT, buffer_id=event.ofp.buffer_id, actions=actions, match=of.ofp_match.from_packet(packet, inport))
msg.priority = priorityPremium
# if self.check_premium(packet.next.dstip):
# log.debug("PRIORITY dst ip=%s has priority of 100!!!!!" % packet.next.dstip)
# msg.priority = 100
# else:
# log.debug("PRIORITY dst ip=%s has priority of 1...." % packet.next.dstip)
# msg.priority = 1
# event.connection.send(msg.pack())
event.connection.send(msg)
elif isinstance(packet.next, arp):
pktnext = packet.next
if pktnext.prototype == arp.PROTO_TYPE_IP:
if pktnext.hwtype == arp.HW_TYPE_ETHERNET:
if pktnext.protosrc != 0:
# Learn or update port, mac info
self.forwardTable[dpid][pktnext.protosrc] = Entry(inport, packet.src)
# Send any waiting packets...
self._send_lost_buffers(dpid, pktnext.protosrc, packet.src, inport)
if pktnext.opcode == arp.REQUEST:
if pktnext.protodst in self.forwardTable[dpid]:
# Reply ARP back
r = arp()
r.hwtype = pktnext.hwtype
r.prototype = pktnext.prototype
r.hwlen = pktnext.hwlen
r.protolen = pktnext.protolen
r.opcode = arp.REPLY
r.hwdst = pktnext.hwsrc
r.protodst = pktnext.protosrc
r.protosrc = pktnext.protodst
r.hwsrc = self.forwardTable[dpid][pktnext.protodst].mac
e = ethernet(type=packet.type, src=dpid_to_mac(dpid), dst=pktnext.hwsrc)
e.set_payload(r)
log.debug("s=%i sp=%i answer ARP for %s" % (dpid, inport, str(r.protosrc)))
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
log.debug("s=%i sp=%i FLOOD ARP %s %s => %s" % (dpid, inport, {arp.REQUEST:"request",arp.REPLY:"reply"}.get(pktnext.opcode, 'op:%i' % (pktnext.opcode,)), str(pktnext.protosrc), str(pktnext.protodst)))
msg = of.ofp_packet_out(in_port = inport, data = event.ofp, action = of.ofp_action_output(port = of.OFPP_FLOOD))
# msg.priority = priorityLearn
event.connection.send(msg)
# When it knows nothing about the destination, flood but don't install the rule
def flood (message = None):
""" Floods the packet """
msg = of.ofp_packet_out()
if time.time() - event.connection.connect_time >= _flood_delay:
# Only flood if we've been connected for a little while...
if self.hold_down_expired is False:
# Oh yes it is!
self.hold_down_expired = True
log.info("%s: Flood hold-down expired -- flooding",
dpid_to_str(event.dpid))
if message is not None: log.debug(message)
#log.debug("%i: flood %s -> %s", event.dpid,packet.src,packet.dst)
# OFPP_FLOOD is optional; on some switches you may need to change
# this to OFPP_ALL.
msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD))
else:
pass
#log.info("Holding down flood for %s", dpid_to_str(event.dpid))
msg.data = event.ofp
msg.in_port = event.port
event.connection.send(msg)
def drop (priorityLevel = 1):
# drop (duration = None)
"""
Drops this packet and optionally installs a flow to continue
dropping similar ones for a while
"""
log.debug("======= DROPPING PACKET =======")
msg = of.ofp_packet_out()
msg.buffer_id = event.ofp.buffer_id
msg.in_port = event.port
msg.actions.append(of.ofp_action_output(port=of.OFPP_NONE))
msg.priority = priorityLevel
event.connection.send(msg)
# if duration is not None:
# if not isinstance(duration, tuple):
# duration = (duration,duration)
# msg = of.ofp_flow_mod()
# msg.match = of.ofp_match.from_packet(packet)
# msg.idle_timeout = duration[0]
# msg.hard_timeout = duration[1]
# msg.buffer_id = event.ofp.buffer_id
# event.connection.send(msg)
# elif event.ofp.buffer_id is not None:
# msg = of.ofp_packet_out()
# msg.buffer_id = event.ofp.buffer_id
# msg.in_port = event.port
# msg.actions.append(of.ofp_action_output(port=of.OFPP_NONE))
# msg.priority = 1
# event.connection.send(msg)
forward()
def _handle_ConnectionUp(self, event):
dpid = dpid_to_str(event.dpid)
log.debug("Switch %s has come up.", dpid)
self.read_Policy(self.policyFile)
# Send the firewall policies to the switch
# def sendFirewallPolicy(connection, policy):
# for i in [FIREWALL POLICIES]:
# sendFirewallPolicy(event.connection, i)
def launch(transparent=False, hold_down=_flood_delay, rule="policy.in"):
# Run discovery and spanning tree modules
pox.openflow.discovery.launch()
pox.openflow.spanning_tree.launch()
# Check the firewall file exists locally and not empty
log.info("*** Firewall file : %s", (rule))
if os.path.isfile(rule) == False:
#log.debug("*** Firewall file %s not found!",(rule))
raise RuntimeError(" Firewall rules %s not found!" % (rule))
else:
if os.stat(rule).st_size == 0:
#raise RuntimeError(" Firewall rules %s empty!" % (rule))
log.info("*** Warning: empty firewall rules!")
log.info("*** Firewall file: %s found!" % (rule))
# Starting the controller module
core.registerNew(Controller, rule)