forked from faucetsdn/ryu
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmaclearning.py
More file actions
90 lines (74 loc) · 2.88 KB
/
maclearning.py
File metadata and controls
90 lines (74 loc) · 2.88 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
import logging
from ryu.base import app_manager
from ryu.controller import ofp_event
from ryu.controller.handler import CONFIG_DISPATCHER
from ryu.controller.handler import set_ev_cls
import ryu.ofproto.ofproto_v1_3 as ofproto
import ryu.ofproto.ofproto_v1_3_parser as ofparser
import ryu.ofproto.beba_v1_0 as bebaproto
import ryu.ofproto.beba_v1_0_parser as bebaparser
LOG = logging.getLogger('app.beba.maclearning')
# Number of switch ports
N = 4
LOG.info("Support max %d ports per switch" % N)
class BebaMacLearning(app_manager.RyuApp):
def __init__(self, *args, **kwargs):
super(BebaMacLearning, self).__init__(*args, **kwargs)
def add_flow(self, datapath, table_id, priority, match, actions):
if len(actions) > 0:
inst = [ofparser.OFPInstructionActions(
ofproto.OFPIT_APPLY_ACTIONS, actions)]
else:
inst = []
mod = ofparser.OFPFlowMod(datapath=datapath, table_id=table_id,
priority=priority, match=match, instructions=inst)
datapath.send_msg(mod)
@set_ev_cls(ofp_event.EventOFPSwitchFeatures, CONFIG_DISPATCHER)
def switch_features_handler(self, event):
""" Switche sent his features, check if Beba supported """
msg = event.msg
datapath = msg.datapath
LOG.info("Configuring switch %d..." % datapath.id)
""" Set table 0 as stateful """
req = bebaparser.OFPExpMsgConfigureStatefulTable(
datapath=datapath,
table_id=0,
stateful=1)
datapath.send_msg(req)
""" Set lookup extractor = {eth_dst} """
req = bebaparser.OFPExpMsgKeyExtract(datapath=datapath,
command=bebaproto.OFPSC_EXP_SET_L_EXTRACTOR,
fields=[ofproto.OXM_OF_ETH_DST],
table_id=0)
datapath.send_msg(req)
""" Set update extractor = {eth_src} """
req = bebaparser.OFPExpMsgKeyExtract(datapath=datapath,
command=bebaproto.OFPSC_EXP_SET_U_EXTRACTOR,
fields=[ofproto.OXM_OF_ETH_SRC],
table_id=0)
datapath.send_msg(req)
# for each input port, for each state
for i in range(1, N+1):
for s in range(N+1):
match = ofparser.OFPMatch(in_port=i, state=s)
if s == 0:
out_port = ofproto.OFPP_FLOOD
else:
out_port = s
actions = [bebaparser.OFPExpActionSetState(state=i, table_id=0, hard_timeout=10),
ofparser.OFPActionOutput(out_port)]
self.add_flow(datapath=datapath, table_id=0, priority=0,
match=match, actions=actions)
""" Need to drop some packets for DEMO puporses only (avoid learning before manual send_eth)"""
#ARP packets
# LOG.info("WARN: ARP packets will be dropped on switch %d" % datapath.id)
# match = ofparser.OFPMatch(eth_type=0x0806)
# actions = []
# self.add_flow(datapath=datapath, table_id=0, priority=100,
# match=match, actions=actions)
#IPv6 packets
# #LOG.info("WARN: IPv6 packets will be dropped on switch %d" % datapath.id)
# match = ofparser.OFPMatch(eth_type=0x86dd)
# actions = []
# self.add_flow(datapath=datapath, table_id=0, priority=100,
# match=match, actions=actions)