-
Notifications
You must be signed in to change notification settings - Fork 45
/
l2.py
278 lines (238 loc) · 11.2 KB
/
l2.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
__author__ = 'Ehsan'
""" ryu.base.app_manager:
The central management of Ryu applications.
- Load Ryu applications
- Provide contexts to Ryu applications
- Route messages among Ryu applications
"""
from ryu.base import app_manager
"""ryu.controller.ofp_event:
OpenFlow event definitions.
"""
from ryu.controller import ofp_event
# Version negotiated and sent features-request message
from ryu.controller.handler import CONFIG_DISPATCHER
from ryu.ofproto import ofproto_v1_3
# Switch-features message received and sent set-config message
from ryu.controller.handler import MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
"""ryu.lib.packet:
Ryu packet library. Decoder/Encoder implementations of popular protocols like TCP/IP.
"""
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types
from ryu.lib import dpid as dpid_lib
"""
Usage Example:
1. Run this application:
$ sudo ryu-manager --verbose --observe-links ~/code/RyuApp/l2.py
2. Join switches (use your favorite method):
$ sudo mn --controller=remote --topo linear,2
"""
"""
In order to implement as a Ryu application, ryu.base.app_manager.RyuApp is inherited. Also, to use
OpenFlow 1.3, the OpenFlow 1.3 version is specified for OFP_VERSIONS.
The http://osrg.github.io/ryu-book/en/html/switching_hub.html explain similar features pretty good.
"""
class L2Switch(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(L2Switch, self).__init__(*args, **kwargs)
self.mac_to_port = {}
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
self.logger.info("[Ehsan] Received EventOFPSwitchFeatures")
msg = ev.msg
self.logger.info('OFPSwitchFeatures received: '
'\n\tdatapath_id=0x%016x n_buffers=%d '
'\n\tn_tables=%d auxiliary_id=%d '
'\n\tcapabilities=0x%08x',
msg.datapath_id, msg.n_buffers, msg.n_tables,
msg.auxiliary_id, msg.capabilities)
datapath = ev.msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
# install table-miss flow entry
#
# We specify NO BUFFER to max_len of the output action due to
# OVS bug. At this moment, if we specify a lesser number, e.g.,
# 128, OVS will send Packet-In with invalid buffer_id and
# truncated packet data. In that case, we cannot output packets
# correctly. The bug has been fixed in OVS v2.1.0.
match = parser.OFPMatch()
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 0, match, actions)
def add_flow(self, datapath, priority, match, actions, buffer_id=None):
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
inst = [parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS,
actions)]
if buffer_id:
mod = parser.OFPFlowMod(datapath=datapath, buffer_id=buffer_id,
priority=priority, match=match,
instructions=inst)
else:
mod = parser.OFPFlowMod(datapath=datapath, priority=priority,
match=match, instructions=inst)
datapath.send_msg(mod)
"""
This is called when Ryu receives an OpenFlow packet_in message. The trick is set_ev_cls decorator. This decorator
tells Ryu when the decorated function should be called.
Arguments of the decorator:
1. The first argument of the decorator indicates an event that makes function called. As you expect easily, every time
Ryu gets a packet_in message, this function is called.
2. The second argument indicates the state of the switch. Probably, you want to ignore packet_in messages before the
negotiation between Ryu and the switch finishes. Using MAIN_DISPATCHER as the second argument means this function
is called only after the negotiation completes.
"""
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
#self.logger.info("[Ehsan] Received EventOFPPacketIn")
# If you hit this you might want to increase
# the "miss_send_length" of your switch
if ev.msg.msg_len < ev.msg.total_len:
self.logger.debug("packet truncated: only %s of %s bytes",
ev.msg.msg_len, ev.msg.total_len)
msg = ev.msg
datapath = msg.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
in_port = msg.match['in_port']
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
dst = eth.dst
src = eth.src
dpid = datapath.id
self.mac_to_port.setdefault(dpid, {})
self.logger.info("\tpacket 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 = [parser.OFPActionOutput(out_port)]
# install a flow to avoid packet_in next time
if out_port != ofproto.OFPP_FLOOD:
match = parser.OFPMatch(in_port=in_port, eth_dst=dst)
# verify if we have a valid buffer_id, if yes avoid to send both
# flow_mod & packet_out
if msg.buffer_id != ofproto.OFP_NO_BUFFER:
self.add_flow(datapath, 1, match, actions, msg.buffer_id)
return
else:
self.add_flow(datapath, 1, match, actions)
data = None
if msg.buffer_id == ofproto.OFP_NO_BUFFER:
data = msg.data
out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id,
in_port=in_port, actions=actions, data=data)
datapath.send_msg(out)
"""
EventOFPPortStatus: An event class for switch port status notification.
The bellow handles the event.
"""
@set_ev_cls(ofp_event.EventOFPPortStatus, MAIN_DISPATCHER)
def _port_status_handler(self, ev):
#self.logger.info("[Ehsan] Received EventOFPPortStatus")
""" Port status message
The switch notifies controller of change of ports.
Attribute | Description
--------------------------------
reason | One of the following values.
| OFPPR_ADD
| OFPPR_DELETE
| OFPPR_MODIFY
--------------------------------
desc | instance of OFPPort
"""
msg = ev.msg
dp = msg.datapath
reason = msg.reason
port_no = msg.desc.port_no
ofproto = msg.datapath.ofproto
if reason == ofproto.OFPPR_ADD:
self.logger.info("\tport added %s", port_no)
elif reason == ofproto.OFPPR_DELETE:
self.logger.info("\tport deleted %s", port_no)
elif reason == ofproto.OFPPR_MODIFY:
self.logger.info("\tport modified %s", port_no)
dp_str = dpid_lib.dpid_to_str(dp.id)
self.logger.info("\t[Ehsan] Sending send_port_desc_stats_request to datapath id : " + dp_str)
self.send_port_desc_stats_request(dp)
else:
self.logger.info("\tIlleagal port state %s %s", port_no, reason)
def send_port_stats_request(self, datapath):
ofp = datapath.ofproto
ofp_parser = datapath.ofproto_parser
"""
class ryu.ofproto.ofproto_v1_3_parser.OFPPortStatsRequest(datapath, flags=0, port_no=4294967295, type_=None)
Port statistics request message
The controller uses this message to query information about ports statistics.
Attribute | Description
--------------------------
flags | Zero or OFPMPF_REQ_MORE
port_no | Port number to read (OFPP_ANY to all ports)
"""
req = ofp_parser.OFPPortStatsRequest(datapath, 0, ofp.OFPP_ANY)
datapath.send_msg(req)
"""
Creates an event handler that receives the PortStatsReply message.
The bellow handles the event.
"""
@set_ev_cls(ofp_event.EventOFPPortStatsReply, MAIN_DISPATCHER)
def port_stats_reply_handler(self, ev):
#self.logger.info("[Ehsan] Received EventOFPPortStatsReply")
#self.logger.info('PortStats: \n')
""" Port statistics reply message
The switch responds with this message to a port statistics request.
Attribute | Description
-----------------------
body | List of OFPPortStats instance
"""
for stat in ev.msg.body:
self.logger.info("\tport_no=%d "
"rx_packets=%d tx_packets=%d "
"\n \trx_bytes=%d tx_bytes=%d "
"rx_dropped=%d tx_dropped=%d "
"rx_errors=%d tx_errors=%d "
"\n \trx_frame_err=%d rx_over_err=%d rx_crc_err=%d "
"\n \tcollisions=%d duration_sec=%d duration_nsec=%d" %
(stat.port_no,
stat.rx_packets, stat.tx_packets,
stat.rx_bytes, stat.tx_bytes,
stat.rx_dropped, stat.tx_dropped,
stat.rx_errors, stat.tx_errors,
stat.rx_frame_err, stat.rx_over_err,
stat.rx_crc_err, stat.collisions,
stat.duration_sec, stat.duration_nsec))
def send_port_desc_stats_request(self, datapath):
ofp_parser = datapath.ofproto_parser
req = ofp_parser.OFPPortDescStatsRequest(datapath, 0)
datapath.send_msg(req)
"""
EventOFPPortDescStatsReply: an event where it is fired when Port description reply message
The bellow handles the event.
"""
@set_ev_cls(ofp_event.EventOFPPortDescStatsReply, MAIN_DISPATCHER)
def port_desc_stats_reply_handler(self, ev):
#self.logger.info('OFPPortDescStatsReply received: \n')
"""
Port description reply message
The switch responds with this message to a port description request.
Attribute | Description
------------|---------------
body | List of OFPPortDescStats instance
"""
for p in ev.msg.body:
self.logger.info("\t port_no=%d hw_addr=%s name=%s config=0x%08x "
"\n \t state=0x%08x curr=0x%08x advertised=0x%08x "
"\n \t supported=0x%08x peer=0x%08x curr_speed=%d "
"max_speed=%d" %
(p.port_no, p.hw_addr,
p.name, p.config,
p.state, p.curr, p.advertised,
p.supported, p.peer, p.curr_speed,
p.max_speed))