-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_3.py
107 lines (90 loc) · 3.89 KB
/
controller_3.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
# Copyright 2012 James McCauley
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
[555 Comments]
This is the controller file corresponding to scenario 3.
"""
from pox.core import core
import pox.openflow.libopenflow_01 as of
from pox.lib.packet.arp import arp
from pox.lib.packet.ethernet import *
from pox.lib.addresses import *
from pox.lib.packet.icmp import *
from pox.lib.packet.ipv4 import *
from switch import *
from router import *
log = core.getLogger()
class Tutorial (object):
"""
A Tutorial object is created for each switch that connects.
A Connection object for that switch is passed to the __init__ function.
"""
def __init__ (self, connection):
# Keep track of the connection to the switch so that we can
# send it messages!
self.connection = connection
# This binds our PacketIn event listener
connection.addListeners(self)
"""
[555 Comments]
In scenario 3, there are many routers and switches. You need to classify a device as a router or a switch based on its DPID
Remember one thing very carefully. The DPID gets assigned based on how you define tour devices in the topology file.
So, be careful and DPID here should be coordinated with your definition in topology file.
For the details of port info table, routing table, of different routers look into the project description document provided.
Initialize any other data structures you wish to for the routers and switches here
A word of caution:
Your router and switch code should be the same for all scenarios. So, be careful to design your data structures for router
and switches in such a way that your single piece of switch code and router code along with your data structure design
should work for all the scenarios
"""
def resend_packet (self, packet_in, out_port):
"""
Instructs the switch to resend a packet that it had sent to us.
"packet_in" is the ofp_packet_in object the switch had sent to the
controller due to a table-miss.
"""
msg = of.ofp_packet_out()
msg.data = packet_in
# Add an action to send to the specified port
action = of.ofp_action_output(port = out_port)
msg.actions.append(action)
# Send message to switch
self.connection.send(msg)
def _handle_PacketIn (self, event):
"""
Handles packet in messages from the switch.
"""
packet = event.parsed # This is the parsed packet data.
if not packet.parsed:
log.warning("Ignoring incomplete packet")
return
packet_in = event.ofp # The actual ofp_packet_in message.
"""
[555 Comments]
You need to classify a device as either switch or router based on its DPID received in the connection object during
initialization in __init__ function in tutorial class. Based on the device type you need to call the respective function
Here is the pseudo code to write
if packet received from device type is switch:
invoke switch_handler and pass the object (i.e., self) and the packet and packet_in
else: (if it is not switch, it means router. We have only two kinds of devices, one is switch and one is router)
invoke router_handler and pass the object (i.e., self) and the packet and packet_in
"""
def launch ():
"""
Starts the component
"""
def start_switch (event):
#log.debug("Controlling %s" % (event.connection,))
Tutorial(event.connection)
core.openflow.addListenerByName("ConnectionUp", start_switch)