-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofinterface.py
220 lines (188 loc) · 8.33 KB
/
ofinterface.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
from rflogging import log
from rflib.defs import *
from binascii import *
from rflib.types.Match import *
from rflib.types.Action import *
from rflib.types.Option import *
OFP_BUFFER_NONE = 0xffffffff
def actions_from_routemod(ofproto, parser, action_tlvs):
instructions = []
actions = []
for a in action_tlvs:
action = Action.from_dict(a)
if action._type == RFAT_SET_ETH_SRC:
srcMac = action._value
src = parser.OFPMatchField.make(ofproto.OXM_OF_ETH_SRC, srcMac)
actions.append(parser.OFPActionSetField(src))
elif action._type == RFAT_SET_ETH_DST:
dstMac = action._value
dst = parser.OFPMatchField.make(ofproto.OXM_OF_ETH_DST, dstMac)
actions.append(parser.OFPActionSetField(dst))
elif action._type == RFAT_SET_VLAN_ID:
actions.append(parser.OFPActionPushVlan(0x8100));
vlan_id = bin_to_int(action._value)
vlan = parser.OFPMatchField.make(ofproto.OXM_OF_VLAN_VID, vlan_id)
actions.append(parser.OFPActionSetField(vlan))
pcp = parser.OFPMatchField.make(ofproto.OXM_OF_VLAN_PCP, 0)
actions.append(parser.OFPActionSetField(pcp))
elif action._type == RFAT_OUTPUT:
port = bin_to_int(action._value)
actions.append(parser.OFPActionOutput(port, ofproto.OFPCML_MAX))
elif action._type == RFAT_GROUP:
group = bin_to_int(action._value)
actions.append(parser.OFPActionGroup(group))
elif action._type == RFAT_GOTO:
table_id = bin_to_int(action._value)
instructions.append(parser.OFPInstructionGotoTable(table_id))
elif action._type == RFAT_STRIP_VLAN_DEFERRED:
actions.append(parser.OFPActionPopVlan())
elif action.optional():
log.info("Dropping unsupported Action (type: %s)" % action._type)
else:
log.warning("Failed to serialise Action (type: %s)" % action._type)
return (actions, instructions)
def create_group_mod(dp, command, group, action_tlvs, weight=0,type_=None):
ofproto = dp.ofproto
parser = dp.ofproto_parser
if command == RMT_ADD_GROUP:
command = ofproto.OFPGC_ADD
elif command == RMT_DELETE_GROUP:
command = ofproto.OFPGC_DELETE
if type_ is None:
type_ = ofproto.OFPGT_INDIRECT
if group == 0:
group = ofproto.OFPG_ALL
buckets = []
actions, _ = actions_from_routemod(ofproto, parser, action_tlvs)
buckets = [parser.OFPBucket(weight, actions=actions)]
return parser.OFPGroupMod(dp, command, type_, group, buckets)
def create_default_flow_mod(dp, cookie=0, cookie_mask=0, table_id=0,
command=None, idle_timeout=0, hard_timeout=0,
priority=None,
buffer_id=OFP_BUFFER_NONE, match=None, actions=None,
inst_type=None, out_port=None, out_group=None,
flags=0, inst=[]):
ofproto = dp.ofproto
parser = dp.ofproto_parser
if command is None:
command = ofproto.OFPFC_ADD
if inst is []:
if inst_type is None:
inst_type = ofproto.OFPIT_APPLY_ACTIONS
if actions is not None:
inst = [parser.OFPInstructionActions(inst_type, actions)]
if match is None:
match = parser.OFPMatch()
if out_port is None:
out_port = ofproto.OFPP_ANY
if out_group is None:
out_group = ofproto.OFPG_ANY
return parser.OFPFlowMod(dp, cookie, cookie_mask,
table_id, command,
idle_timeout, hard_timeout,
priority, buffer_id,
out_port, out_group,
flags, match, inst)
def create_flow_mod(dp, table_id, mod, matches, actions, options):
ofproto = dp.ofproto
parser = dp.ofproto_parser
flow_mod = None
if mod == RMT_DELETE and table_id == 0 and len(matches) == 0 and len(actions) == 0:
return parser.OFPFlowMod(dp, 0, 0,
ofproto.OFPTT_ALL, ofproto.OFPFC_DELETE,
0, 0, 0, OFP_BUFFER_NONE,
ofproto.OFPP_ANY,
ofproto.OFPG_ANY,
0, parser.OFPMatch(), [])
else:
command = add_command(ofproto, mod)
flow_mod = create_default_flow_mod(dp, table_id=table_id, command=command)
add_matches(flow_mod, matches)
add_actions(flow_mod, actions)
add_options(flow_mod, options)
return flow_mod
def add_command(ofproto, mod):
if mod == RMT_ADD:
return ofproto.OFPFC_ADD
elif mod == RMT_DELETE:
return ofproto.OFPFC_DELETE_STRICT
return None
def add_matches(flow_mod, matches):
for m in matches:
match = Match.from_dict(m)
if match._type == RFMT_IPV4:
value = bin_to_int(match._value)
addr = value >> 32
mask = value & ((1 << 32) - 1)
flow_mod.match.set_dl_type(ETHERTYPE_IP)
flow_mod.match.set_ipv4_dst_masked(addr, mask)
elif match._type == RFMT_IPV4_SRC:
value = bin_to_int(match._value)
addr = value >> 32
mask = value & ((1 << 32) - 1)
flow_mod.match.set_dl_type(ETHERTYPE_IP)
flow_mod.match.set_ipv4_src_masked(addr, mask)
elif match._type == RFMT_IPV6:
v = match._value
addr = tuple((ord(v[i]) << 8) | ord(v[i + 1])
for i in range(0, 16, 2))
mask = tuple((ord(v[i]) << 8) | ord(v[i + 1])
for i in range(16, 32, 2))
flow_mod.match.set_dl_type(ETHERTYPE_IPV6)
flow_mod.match.set_ipv6_dst_masked(addr, mask)
elif match._type == RFMT_ETHERNET:
flow_mod.match.set_dl_dst(match._value)
elif match._type == RFMT_ETHERTYPE:
flow_mod.match.set_dl_type(bin_to_int(match._value))
elif match._type == RFMT_NW_PROTO:
flow_mod.match.set_ip_proto(bin_to_int(match._value))
elif match._type == RFMT_TP_SRC:
flow_mod.match.set_ip_proto(IPPROTO_TCP)
flow_mod.match.set_tcp_src(bin_to_int(match._value))
elif match._type == RFMT_TP_DST:
flow_mod.match.set_ip_proto(IPPROTO_TCP)
flow_mod.match.set_tcp_dst(bin_to_int(match._value))
elif match._type == RFMT_IN_PORT:
flow_mod.match.set_in_port(bin_to_int(match._value))
elif match._type == RFMT_VLAN_ID:
flow_mod.match.set_vlan_vid(bin_to_int(match._value))
elif TLV.optional(match):
log.info("Dropping unsupported Match (type: %s)" % match._type)
else:
log.warning("Failed to serialise Match (type: %s)" % match._type)
return
def add_actions(flow_mod, action_tlvs):
parser = flow_mod.datapath.ofproto_parser
ofproto = flow_mod.datapath.ofproto
actions, instructions = actions_from_routemod(ofproto, parser, action_tlvs)
if len(instructions) == 0:
instructions = [ parser.OFPInstructionActions(ofproto.OFPIT_APPLY_ACTIONS, actions) ]
flow_mod.instructions = instructions
def add_options(flow_mod, options):
for o in options:
option = Option.from_dict(o)
if option._type == RFOT_PRIORITY:
flow_mod.priority = bin_to_int(option._value)
elif option._type == RFOT_IDLE_TIMEOUT:
flow_mod.idle_timeout = bin_to_int(option._value)
elif option._type == RFOT_HARD_TIMEOUT:
flow_mod.hard_timeout = bin_to_int(option._value)
elif option._type == RFOT_CT_ID:
pass
elif option.optional():
log.info("Dropping unsupported Option (type: %s)" % option._type)
else:
log.warning("Failed to serialise Option (type: %s)" % option._type)
return
def send_pkt_out(dp, port, msg_data):
ofproto = dp.ofproto
parser = dp.ofproto_parser
actions = [parser.OFPActionOutput(port, len(msg_data))]
buffer_id = OFP_BUFFER_NONE
in_port = ofproto.OFPP_CONTROLLER
packet_out = parser.OFPPacketOut(dp,
buffer_id,
in_port,
actions,
msg_data)
dp.send_msg(packet_out)