-
Notifications
You must be signed in to change notification settings - Fork 1
/
ryu_forward.py
154 lines (122 loc) · 5.8 KB
/
ryu_forward.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
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
from ryu.lib.packet import in_proto
from ryu.lib.packet import ipv4
from ryu.lib.packet import icmp
from ryu.lib.packet import tcp
from ryu.lib.packet import udp
client_ip = '10.0.1.5'
server_1_ip = '10.0.1.2'
server_2_ip = '10.0.1.3'
client_mac = '00:00:00:00:00:03'
server_1_mac = '00:00:00:00:00:01'
server_2_mac = '00:00:00:00:00:02'
class SimpleSwitch13(app_manager.RyuApp):
# use OpenFlow 1.3
OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION]
def __init__(self, *args, **kwargs):
super(SimpleSwitch13, self).__init__(*args, **kwargs)
self.mac_to_port = {}
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, ev):
datapath = ev.msg.datapath # Switch
ofproto = datapath.ofproto
parser = datapath.ofproto_parser
# match all package
match = parser.OFPMatch()
# deal with all table-miss flow entries
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)
def add_flow1(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, idle_timeout=5,
match=match, instructions=inst)
else:
mod = parser.OFPFlowMod(datapath=datapath, priority=priority, idle_timeout=5,
match=match, instructions=inst)
datapath.send_msg(mod)
# deal with PacketIn event
@set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER)
def _packet_in_handler(self, ev):
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]
if eth.ethertype == ether_types.ETH_TYPE_LLDP:
# ignore lldp packet
return
eth_src = eth.src # src MAC address
eth_dst = eth.dst # dst MAC address
dpid = format(datapath.id, "d").zfill(16)
self.mac_to_port.setdefault(dpid, {})
self.logger.info("PackIn Event\n"
" dpid: %s\n"
" src: %s\n"
" dst: %s\n"
" in_port: %s\n",
dpid, eth_src, eth_dst, in_port)
self.mac_to_port[dpid][eth_src] = in_port
self.logger.info("mac_to_port Table %s \n", self.mac_to_port)
if eth_dst in self.mac_to_port[dpid]:
out_port = self.mac_to_port[dpid][eth_dst]
else:
out_port = ofproto.OFPP_FLOOD
actions = [parser.OFPActionOutput(out_port)]
if out_port != ofproto.OFPP_FLOOD:
if eth.ethertype == ether_types.ETH_TYPE_IP:
pkt_ipv4 = pkt.get_protocol(ipv4.ipv4)
ip_src = pkt_ipv4.src
ip_dst = pkt_ipv4.dst
ip_protocol = pkt_ipv4.proto
# if ICMP Protocol
if ip_protocol == in_proto.IPPROTO_ICMP:
match = parser.OFPMatch(eth_type=ether_types.ETH_TYPE_IP, in_port=in_port, ipv4_src=ip_src,
ipv4_dst=ip_dst, ip_proto=ip_protocol)
# if TCP Protocol
elif ip_protocol == in_proto.IPPROTO_TCP:
match = parser.OFPMatch(eth_type=ether_types.ETH_TYPE_IP, in_port=in_port, ipv4_src=ip_src, ipv4_dst=ip_dst,
ip_proto=ip_protocol)
if eth.ethertype == ether_types.ETH_TYPE_ARP:
match = parser.OFPMatch(eth_type=ether_types.ETH_TYPE_ARP, in_port=in_port, eth_dst=eth_dst,
eth_src=eth_src)
if msg.buffer_id != ofproto.OFP_NO_BUFFER:
self.add_flow1(datapath, 1, match, actions, msg.buffer_id)
return
else:
self.add_flow1(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)
self.logger.info("PackOut Event\n"
" dpid=%s\n"
" in_port=%s\n"
" actions=%s\n"
" buffer_id=%s\n",
dpid, in_port, actions, msg.buffer_id)
datapath.send_msg(out)