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
|
# 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 CONFIG_DISPATCHER, MAIN_DISPATCHER
from ryu.controller.handler import set_ev_cls
from ryu.ofproto import ofproto_v1_3
from ryu.lib.packet import packet
from ryu.lib.packet import ethernet
from ryu.lib.packet import ether_types
class SimpleSwitch13(app_manager.RyuApp):
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SimpleSwitch13, self).__init__(*args, **kwargs)
# 处理交换机连接
@set_ev_cls(ofp_event.EventOFPStateChange, MAIN_DISPATCHER)
def switch_features_handler(self, ev):
datapath = ev.datapath
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
# 交换机新连接
if ev.state == MAIN_DISPATCHER:
# 断言dpid存在
assert datapath.id is not None
print("Switch Coming", datapath.id)
# 为交换机的流表新增Table-miss Flow Entry项 用于在无法匹配到流表项后与之匹配
# match表示匹配约束 为空则说明可以匹配所有封包
# action表示匹配成功后执行的操作为向Controller端口发送最大数据长度的封包
# OFPCML_NO_BUFFER表示不需要在交换机上缓存封包将封包整体发给controller
# 优先级为0 即最低的优先权
match = parser.OFPMatch()
actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER,
ofproto.OFPCML_NO_BUFFER)]
self.add_flow(datapath, 0, match, actions)
# 预下发静态路由
self.download_routing(datapath)
# 预下发静态路由表
def download_routing(self, datapath):
dpid = datapath.id
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
if dpid == 1:
# [100-1] 从3端口输入的流量 从4端口输出
in_port, out_port = 3, 4
match = parser.OFPMatch(in_port=in_port)
actions = [parser.OFPActionOutput(out_port)]
self.add_flow(datapath, 1, match, actions)
# [100-2] 从4端口输入的流量 从3端口输出
in_port, out_port = 4, 3
match = parser.OFPMatch(in_port=in_port)
actions = [parser.OFPActionOutput(out_port)]
self.add_flow(datapath, 1, match, actions)
if dpid == 2:
# [200-1] 从9端口输入的流量 从12端口输出
in_port, out_port = 9, 12
match = parser.OFPMatch(in_port=in_port)
actions = [parser.OFPActionOutput(out_port)]
self.add_flow(datapath, 1, match, actions)
# [200-2] 从12端口输入的流量 从9端口输出
in_port, out_port = 12, 9
match = parser.OFPMatch(in_port=in_port)
actions = [parser.OFPActionOutput(out_port)]
self.add_flow(datapath, 1, match, actions)
if dpid == 3:
# [300-1] 从13端口输入的流量 从16端口输出
in_port, out_port = 13, 16
match = parser.OFPMatch(in_port=in_port)
actions = [parser.OFPActionOutput(out_port)]
self.add_flow(datapath, 1, match, actions)
# [300-2] 从16端口输入的流量 从13端口输出
in_port, out_port = 16, 13
match = parser.OFPMatch(in_port=in_port)
actions = [parser.OFPActionOutput(out_port)]
self.add_flow(datapath, 1, match, actions)
if dpid == 4:
# [400-1] 从21端口输入的流量 从23端口输出
in_port, out_port = 21, 23
match = parser.OFPMatch(in_port=in_port)
actions = [parser.OFPActionOutput(out_port)]
self.add_flow(datapath, 1, match, actions)
# [400-2] 从23端口输入的流量 从21端口输出
in_port, out_port = 23, 21
match = parser.OFPMatch(in_port=in_port)
actions = [parser.OFPActionOutput(out_port)]
self.add_flow(datapath, 1, 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)
# 控制器接收到Packet-In消息
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def packet_in_handler(self, ev):
msg = ev.msg
datapath = msg.datapath
in_port = msg.match['in_port']
print("Recv PACKET_IN", datapath.id,"from",in_port)
pkt = packet.Packet(msg.data)
eth = pkt.get_protocols(ethernet.ethernet)[0]
print("Eth pkt: {0}".format(eth))
|