-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontroller.py
More file actions
84 lines (69 loc) · 2.64 KB
/
controller.py
File metadata and controls
84 lines (69 loc) · 2.64 KB
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
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.topology import event, switches
from ryu.ofproto import ofproto_v1_0
from ryu.lib.packet import packet, ethernet, ether_types, arp
from ryu.lib.packet import dhcp
from ryu.lib.packet import ethernet
from ryu.lib.packet import ipv4
from ryu.lib.packet import packet
from ryu.lib.packet import udp
from dhcp import DHCPServer
class ControllerApp(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(ControllerApp, self).__init__(*args, **kwargs)
@set_ev_cls(event.EventSwitchEnter)
def handle_switch_add(self, ev):
"""
Event handler indicating a switch has come online.
"""
@set_ev_cls(event.EventSwitchLeave)
def handle_switch_delete(self, ev):
"""
Event handler indicating a switch has been removed
"""
@set_ev_cls(event.EventHostAdd)
def handle_host_add(self, ev):
"""
Event handler indiciating a host has joined the network
This handler is automatically triggered when a host sends an ARP response.
"""
# TODO: Update network topology and flow rules
@set_ev_cls(event.EventLinkAdd)
def handle_link_add(self, ev):
"""
Event handler indicating a link between two switches has been added
"""
# TODO: Update network topology and flow rules
@set_ev_cls(event.EventLinkDelete)
def handle_link_delete(self, ev):
"""
Event handler indicating when a link between two switches has been deleted
"""
# TODO: Update network topology and flow rules
@set_ev_cls(event.EventPortModify)
def handle_port_modify(self, ev):
"""
Event handler for when any switch port changes state.
This includes links for hosts as well as links between switches.
"""
# TODO: Update network topology and flow rules
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
try:
msg = ev.msg
datapath = msg.datapath
pkt = packet.Packet(data=msg.data)
pkt_dhcp = pkt.get_protocols(dhcp.dhcp)
inPort = msg.in_port
if not pkt_dhcp:
# TODO: handle other protocols like ARP
pass
else:
DHCPServer.handle_dhcp(datapath, inPort, pkt)
return
except Exception as e:
self.logger.error(e)