-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py.bak
122 lines (93 loc) · 4.08 KB
/
controller.py.bak
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
'''
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
from pox.lib.revent import *
from pox.lib.util import dpid_to_str
from pox.lib.addresses import IPAddr, EthAddr
log = core.getLogger()
# We don't want to flood immediately when a switch connects.
# Can be overriden on commandline.
_flood_delay = 0
class Controller(EventMixin):
def __init__(self):
self.listenTo(core.openflow)
core.openflow_discovery.addListeners(self)
# Initialize forwarding table
self.macToPort = {}
# Initialize firewall table
self.firewallTable = {}
self.hold_down_expired = _flood_delay == 0
# You can write other functions as you need.
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):
packet = event.parsed
if packet.dst.is_multicast:
flood()
else:
if packet.dst not in self.macToPort:
flood("Port for %s unknown -- flooding" % (packet.dst,))
else:
port = self.macToPort[packet.dst]
if port == event.port:
log.warning("Same port for packet from %s -> %s on %s.%s. Drop."
% (packet.src, packet.dst, dpid_to_str(event.dpid), port))
# drop(10)
return
log.debug("installing flow for %s.%i -> %s.%i" %
(packet.src, event.port, packet.dst, port))
msg = of.ofp_flow_mod()
msg.match = of.ofp_match.from_packet(packet, event.port)
msg.idle_timeout = 10
msg.hard_timeout = 30
msg.actions.append(of.ofp_action_output(port = port))
msg.data = event.ofp # 6a
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)
forward()
def _handle_ConnectionUp(self, event):
dpid = dpid_to_str(event.dpid)
log.debug("Switch %s has come up.", dpid)
# 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):
# Run discovery and spanning tree modules
pox.openflow.discovery.launch()
pox.openflow.spanning_tree.launch()
# Starting the controller module
core.registerNew(Controller)