Repository navigation
Expand file tree
/
Copy pathl2_labv2.py
More file actions
151 lines (118 loc) · 5.58 KB
/
Copy pathl2_labv2.py
File metadata and controls
151 lines (118 loc) · 5.58 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
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
# [4552] Code below is adapted from [<python3 packages]/ryu/app/simple_switch_12.py
#
# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
#
# 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.
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_2
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types
# [4552] Add additional packet types
from ryu.lib.packet import ipv4
from ryu.lib.packet import icmp
class SimpleSwitch12(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_2.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SimpleSwitch12, self).__init__(*args, **kwargs)
self.mac_to_port = {}
# [4552] start of sample custom code (block 1)
self.icmp_count = 0
self.icmpDict = {}
# [4552] end of sample custom code (block 1)
def add_flow(self, datapath, port, dst, src, actions):
ofproto = datapath.ofproto
match = datapath.ofproto_parser.OFPMatch(in_port=port,
eth_dst=dst,
eth_src=src)
inst = [datapath.ofproto_parser.OFPInstructionActions(
ofproto.OFPIT_APPLY_ACTIONS, actions)]
mod = datapath.ofproto_parser.OFPFlowMod(
datapath=datapath, cookie=0, cookie_mask=0, table_id=0,
command=ofproto.OFPFC_ADD, idle_timeout=0, hard_timeout=4,
priority=0, buffer_id=ofproto.OFP_NO_BUFFER,
out_port=ofproto.OFPP_ANY,
out_group=ofproto.OFPG_ANY,
flags=0, match=match, instructions=inst)
datapath.send_msg(mod)
def blockFlow(self, datapath, port, dst, src):
instruction = [datapath.ofproto_parser.OFPInstructionActions(datapath.ofproto.OFPIT_CLEAR_ACTIONS, [])]
match = datapath.ofproto_parser.OFPMatch(in_port=port,eth_dst=dst,eth_src=src)
msg = datapath.ofproto_parser.OFPFlowMod(datapath=datapath, table_id = 0, priority = 1,
command = datapath.ofproto.OFPFC_ADD,
match = match,
instructions = instruction
)
datapath.send_msg(msg)
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
in_port = msg.match['in_port']
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
if eth.ethertype == ether_types.ETH_TYPE_LLDP:
# ignore lldp packet
return
# [4552] start of sample custom code (block 2)
self.logger.info("ether_type: %s", eth.ethertype)
if eth.ethertype == ether_types.ETH_TYPE_IP:
self.logger.info("received an IP packet")
_ip = pkt.get_protocol(ipv4.ipv4)
_icmp = pkt.get_protocol(icmp.icmp)
if _ip:
self.logger.info("srcIP: %s, dstIP: %s", _ip.src, _ip.dst)
if _icmp:
try:
self.icmp_count = self.icmp_count+1
self.logger.info("received an ICMP packet, total: %s", self.icmp_count)
if (eth.src,eth.dst) in self.icmpDict:
self.icmpDict[(eth.src,eth.dst)] += 1
else:
self.icmpDict[(eth.src,eth.dst)] = 1
if self.icmpDict[(eth.src,eth.dst)] > 10:
#drop packets
self.logger.info("exceeded 10 flows, dropping packet.")
self.blockFlow(datapath, in_port, eth.dst, eth.src)
return
except NameError:
self.logger.info("come here!!!")
self.icmp_count = 1
# [4552] end of sample custom code (block 2)
dst = eth.dst
src = eth.src
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port)
# learn a mac address to avoid FLOOD next time.
self.mac_to_port[dpid][src] = in_port
if dst in self.mac_to_port[dpid]:
out_port = self.mac_to_port[dpid][dst]
else:
out_port = ofproto.OFPP_FLOOD
actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
# install a flow to avoid packet_in next time
if out_port != ofproto.OFPP_FLOOD:
self.add_flow(datapath, in_port, dst, src, actions)
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
out = datapath.ofproto_parser.OFPPacketOut(
datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port,
actions=actions, data=data)
datapath.send_msg(out)