blob: 06b31badaf66243ff59a61658ed8a64c40ddacd1 [file] [log] [blame]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07001#
Zsolt Haraszti3eb27a52017-01-03 21:56:48 -08002# Copyright 2017 the original author or authors.
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -07003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""
18Convert loxi objects to openflow_13 messages and back.
19"""
20from copy import copy
21
22from google.protobuf.descriptor import FieldDescriptor
23
24import loxi.of13 as of13
25from protobuf_to_dict import protobuf_to_dict, TYPE_CALLABLE_MAP
26from protos import openflow_13_pb2 as pb2
27
28
29type_callable_map = copy(TYPE_CALLABLE_MAP)
30type_callable_map.update({
31 FieldDescriptor.TYPE_STRING: str
32})
33
34def pb2dict(pb):
35 """
36 Convert protobuf to a dict of values good for instantiating
37 loxi objects (or any other objects). We specialize the protobuf_to_dict
38 library call with our modified decoders.
39 :param pb: protobuf as loaded into Python
40 :return: dict of values
41 """
42 return protobuf_to_dict(pb, type_callable_map)
43
44def to_loxi(grpc_object):
45 cls = grpc_object.__class__
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -080046 converter = to_loxi_converters[cls.__name__]
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -070047 return converter(grpc_object)
48
49def to_grpc(loxi_object):
50 cls = loxi_object.__class__
51 converter = to_grpc_converters[cls]
52 return converter(loxi_object)
53
54def ofp_port_to_loxi_port_desc(pb):
55 kw = pb2dict(pb)
56 return of13.common.port_desc(**kw)
57
Zsolt Haraszti217a12e2016-12-19 16:37:55 -080058def ofp_port_status_to_loxi_port_status(pb):
59 return of13.message.port_status(
60 reason=pb.reason,
61 desc=ofp_port_to_loxi_port_desc(pb.desc)
62 )
63
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -080064def make_loxi_field(oxm_field):
65 assert oxm_field['oxm_class'] == pb2.OFPXMC_OPENFLOW_BASIC
66 ofb_field = oxm_field['ofb_field']
67 field_type = ofb_field.get('type', 0)
68
69 if field_type == pb2.OFPXMT_OFB_ETH_TYPE:
70 return (
71 of13.oxm.eth_type(value=ofb_field['eth_type']))
72
73 elif field_type == pb2.OFPXMT_OFB_IN_PORT:
74 return (
75 of13.oxm.in_port(value=ofb_field['port']))
76
77 elif field_type == pb2.OFPXMT_OFB_IP_PROTO:
78 return (
79 of13.oxm.ip_proto(value=ofb_field['ip_proto']))
80
81 elif field_type == pb2.OFPXMT_OFB_VLAN_VID:
82 return (
83 of13.oxm.vlan_vid(value=ofb_field['vlan_vid']))
84
85 elif field_type == pb2.OFPXMT_OFB_VLAN_PCP:
86 return (
87 of13.oxm.vlan_pcp(value=ofb_field['vlan_pcp']))
88
89 elif field_type == pb2.OFPXMT_OFB_IPV4_SRC:
90 return (
91 of13.oxm.ipv4_src(value=ofb_field['ipv4_src']))
92
93 elif field_type == pb2.OFPXMT_OFB_IPV4_DST:
94 return (
95 of13.oxm.ipv4_dst(value=ofb_field['ipv4_dst']))
96
97 elif field_type == pb2.OFPXMT_OFB_UDP_SRC:
98 return (
99 of13.oxm.udp_src(value=ofb_field['udp_src']))
100
101 elif field_type == pb2.OFPXMT_OFB_UDP_DST:
102 return (
103 of13.oxm.udp_dst(value=ofb_field['udp_dst']))
104
105 elif field_type == pb2.OFPXMT_OFB_METADATA:
106 return (
107 of13.oxm.metadata(value=ofb_field['table_metadata']))
108
109 else:
110 raise NotImplementedError(
111 'OXM match field for type %s' % field_type)
112
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700113def make_loxi_match(match):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800114 assert match.get('type', pb2.OFPMT_STANDARD) == pb2.OFPMT_OXM
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700115 loxi_match_fields = []
Zsolt Haraszti66862032016-11-28 14:28:39 -0800116 for oxm_field in match.get('oxm_fields', []):
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800117 loxi_match_fields.append(make_loxi_field(oxm_field))
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700118 return of13.match_v3(oxm_list=loxi_match_fields)
119
alshabibf4fb2682017-01-12 00:32:56 -0600120
121def make_loxi_action(a):
122 if type(a) is not dict:
123 a = pb2dict(a)
124
125 typ = a.get('type', 0)
126
127 if typ == pb2.OFPAT_OUTPUT:
128 output_kws = a['output']
129 return of13.action.output(**output_kws)
130
131 elif typ == pb2.OFPAT_POP_VLAN:
132 return of13.action.pop_vlan()
133
134 elif typ == pb2.OFPAT_PUSH_VLAN:
135 push_vlan_kws = a['push']
136 return of13.action.push_vlan(**push_vlan_kws)
137
138 elif typ == pb2.OFPAT_SET_FIELD:
139 loxi_field = make_loxi_field(a['set_field']['field'])
140 return of13.action.set_field(loxi_field)
141
142 elif typ == pb2.OFPAT_GROUP:
143 group_kws = a['group']
144 return of13.action.group(**group_kws)
145
146 else:
147 raise NotImplementedError(
148 'Action decoder for action OFPAT_* %d' % typ)
149
150
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700151def ofp_flow_stats_to_loxi_flow_stats(pb):
152 kw = pb2dict(pb)
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700153
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700154 def make_loxi_instruction(inst):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700155 type = inst['type']
156 if type == pb2.OFPIT_APPLY_ACTIONS:
157 return of13.instruction.apply_actions(
158 actions=[make_loxi_action(a)
159 for a in inst['actions']['actions']])
Gamze Abaka61c2e982018-02-14 11:03:36 +0000160 elif type == pb2.OFPIT_CLEAR_ACTIONS:
161 return of13.instruction.clear_actions()
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800162 elif type == pb2.OFPIT_GOTO_TABLE:
163 return of13.instruction.goto_table(
164 table_id=inst['goto_table']['table_id'])
165
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700166 else:
167 raise NotImplementedError('Instruction type %d' % type)
168
169 kw['match'] = make_loxi_match(kw['match'])
170 kw['instructions'] = [make_loxi_instruction(i) for i in kw['instructions']]
Zsolt Haraszti66862032016-11-28 14:28:39 -0800171 del kw['id']
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700172 return of13.flow_stats_entry(**kw)
173
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800174
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700175def ofp_packet_in_to_loxi_packet_in(pb):
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800176 packet_in = of13.message.packet_in(
177 buffer_id=pb.buffer_id,
178 reason=pb.reason,
179 table_id=pb.table_id,
180 cookie=pb.cookie,
181 match=make_loxi_match(pb2dict(pb.match)),
182 data=pb.data
183 )
184 return packet_in
185
alshabibf4fb2682017-01-12 00:32:56 -0600186def ofp_group_desc_to_loxi_group_desc(pb):
187 return of13.group_desc_stats_entry(
188 group_type=pb.type,
189 group_id=pb.group_id,
190 buckets=[to_loxi(bucket) for bucket in pb.buckets])
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700191
alshabibf4fb2682017-01-12 00:32:56 -0600192
193def ofp_group_stats_to_loxi_group_stats(pb):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800194 return of13.group_stats_entry(
alshabibf4fb2682017-01-12 00:32:56 -0600195 group_id=pb.group_id,
196 ref_count=pb.ref_count,
197 packet_count=pb.packet_count,
198 byte_count=pb.byte_count,
199 duration_sec=pb.duration_sec,
200 duration_nsec=pb.duration_nsec,
201 bucket_stats=[to_loxi(bstat) for bstat in pb.bucket_stats])
Zsolt Haraszti66862032016-11-28 14:28:39 -0800202
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800203
Zsolt Haraszti66862032016-11-28 14:28:39 -0800204def ofp_bucket_counter_to_loxy_bucket_counter(pb):
205 return of13.bucket_counter(
206 packet_count=pb.packet_count,
207 byte_count=pb.byte_count)
208
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700209
alshabibf4fb2682017-01-12 00:32:56 -0600210def ofp_bucket_to_loxi_bucket(pb):
211 return of13.bucket(
212 weight=pb.weight,
213 watch_port=pb.watch_port,
214 watch_group=pb.watch_group,
215 actions=[to_loxi(action) for action in pb.actions]
216 )
217
218
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700219to_loxi_converters = {
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800220 'ofp_port': ofp_port_to_loxi_port_desc,
221 'ofp_port_status': ofp_port_status_to_loxi_port_status,
222 'ofp_flow_stats': ofp_flow_stats_to_loxi_flow_stats,
223 'ofp_packet_in': ofp_packet_in_to_loxi_packet_in,
alshabibf4fb2682017-01-12 00:32:56 -0600224 'ofp_group_stats': ofp_group_stats_to_loxi_group_stats,
225 'ofp_group_desc': ofp_group_desc_to_loxi_group_desc,
226 'ofp_bucket_counter': ofp_bucket_counter_to_loxy_bucket_counter,
227 'ofp_bucket': ofp_bucket_to_loxi_bucket,
228 'ofp_action': make_loxi_action
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700229}
230
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700231
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700232def loxi_flow_mod_to_ofp_flow_mod(lo):
233 return pb2.ofp_flow_mod(
234 cookie=lo.cookie,
235 cookie_mask=lo.cookie_mask,
236 table_id=lo.table_id,
237 command=lo._command,
238 idle_timeout=lo.idle_timeout,
239 hard_timeout=lo.hard_timeout,
240 priority=lo.priority,
241 buffer_id=lo.buffer_id,
242 out_port=lo.out_port,
243 out_group=lo.out_group,
244 flags=lo.flags,
245 match=to_grpc(lo.match),
246 instructions=[to_grpc(i) for i in lo.instructions])
247
248
249def loxi_group_mod_to_ofp_group_mod(lo):
250 return pb2.ofp_group_mod(
251 command=lo.command,
Zsolt Haraszti9125b1a2016-10-24 22:54:33 -0700252 type=lo.group_type,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700253 group_id=lo.group_id,
254 buckets=[to_grpc(b) for b in lo.buckets])
255
256
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700257def loxi_packet_out_to_ofp_packet_out(lo):
258 return pb2.ofp_packet_out(
259 buffer_id=lo.buffer_id,
260 in_port=lo.in_port,
261 actions=[to_grpc(a) for a in lo.actions],
262 data=lo.data)
263
264
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700265def loxi_match_v3_to_ofp_match(lo):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700266 return pb2.ofp_match(
267 type=pb2.OFPMT_OXM,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700268 oxm_fields=[to_grpc(f) for f in lo.oxm_list])
269
270
271def loxi_bucket_to_ofp_bucket(lo):
272 return pb2.ofp_bucket(
273 weight=lo.weight,
274 watch_port=lo.watch_port,
275 watch_group=lo.watch_group,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700276 actions=[to_grpc(a) for a in lo.actions])
277
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700278
279def loxi_oxm_eth_type_to_ofp_oxm(lo):
280 return pb2.ofp_oxm_field(
281 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
282 ofb_field=pb2.ofp_oxm_ofb_field(
283 type=pb2.OFPXMT_OFB_ETH_TYPE,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700284 eth_type=lo.value))
285
286
287def loxi_oxm_in_port_to_ofp_oxm(lo):
288 return pb2.ofp_oxm_field(
289 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
290 ofb_field=pb2.ofp_oxm_ofb_field(
291 type=pb2.OFPXMT_OFB_IN_PORT,
292 port=lo.value))
293
294
295def loxi_oxm_ip_proto_to_ofp_oxm(lo):
296 return pb2.ofp_oxm_field(
297 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
298 ofb_field=pb2.ofp_oxm_ofb_field(
299 type=pb2.OFPXMT_OFB_IP_PROTO,
300 ip_proto=lo.value))
301
302
303def loxi_oxm_vlan_vid_to_ofp_oxm(lo):
304 return pb2.ofp_oxm_field(
305 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
306 ofb_field=pb2.ofp_oxm_ofb_field(
307 type=pb2.OFPXMT_OFB_VLAN_VID,
308 vlan_vid=lo.value))
309
310
311def loxi_oxm_vlan_pcp_to_ofp_oxm(lo):
312 return pb2.ofp_oxm_field(
313 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
314 ofb_field=pb2.ofp_oxm_ofb_field(
315 type=pb2.OFPXMT_OFB_VLAN_PCP,
316 vlan_pcp=lo.value))
317
318
319def loxi_oxm_ipv4_dst_to_ofp_oxm(lo):
320 return pb2.ofp_oxm_field(
321 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
322 ofb_field=pb2.ofp_oxm_ofb_field(
323 type=pb2.OFPXMT_OFB_IPV4_DST,
324 ipv4_dst=lo.value))
325
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700326
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800327def loxi_oxm_udp_dst_to_ofp_oxm(lo):
328 return pb2.ofp_oxm_field(
329 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
330 ofb_field=pb2.ofp_oxm_ofb_field(
331 type=pb2.OFPXMT_OFB_UDP_DST,
332 udp_dst=lo.value))
333
334
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800335def loxi_oxm_udp_src_to_ofp_oxm(lo):
336 return pb2.ofp_oxm_field(
337 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
338 ofb_field=pb2.ofp_oxm_ofb_field(
339 type=pb2.OFPXMT_OFB_UDP_SRC,
340 udp_src=lo.value))
341
342
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800343def loxi_oxm_metadata_to_ofp_oxm(lo):
344 return pb2.ofp_oxm_field(
345 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
346 ofb_field=pb2.ofp_oxm_ofb_field(
347 type=pb2.OFPXMT_OFB_METADATA,
348 table_metadata=lo.value))
349
350
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700351def loxi_apply_actions_to_ofp_instruction(lo):
352 return pb2.ofp_instruction(
353 type=pb2.OFPIT_APPLY_ACTIONS,
354 actions=pb2.ofp_instruction_actions(
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700355 actions=[to_grpc(a) for a in lo.actions]))
356
Gamze Abaka61c2e982018-02-14 11:03:36 +0000357def loxi_clear_actions_to_ofp_instruction(lo):
358 return pb2.ofp_instruction(
359 type=pb2.OFPIT_CLEAR_ACTIONS)
360
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700361
362def loxi_goto_table_to_ofp_instruction(lo):
363 return pb2.ofp_instruction(
364 type=pb2.OFPIT_GOTO_TABLE,
365 goto_table=pb2.ofp_instruction_goto_table(table_id=lo.table_id))
366
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700367
368def loxi_output_action_to_ofp_action(lo):
369 return pb2.ofp_action(
370 type=pb2.OFPAT_OUTPUT,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700371 output=pb2.ofp_action_output(port=lo.port, max_len=lo.max_len))
372
373
374def loxi_group_action_to_ofp_action(lo):
375 return pb2.ofp_action(
376 type=pb2.OFPAT_GROUP,
377 group=pb2.ofp_action_group(group_id=lo.group_id))
378
379
380def loxi_set_field_action_to_ofp_action(lo):
381 return pb2.ofp_action(
382 type=pb2.OFPAT_SET_FIELD,
383 set_field=pb2.ofp_action_set_field(field=to_grpc(lo.field)))
384
385
386def loxi_pop_vlan_action_to_ofp_action(lo):
387 return pb2.ofp_action(type=pb2.OFPAT_POP_VLAN)
388
389
390def loxi_push_vlan_action_to_ofp_action(lo):
391 return pb2.ofp_action(
392 type=pb2.OFPAT_PUSH_VLAN,
393 push=pb2.ofp_action_push(ethertype=lo.ethertype))
394
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700395
396to_grpc_converters = {
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700397
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700398 of13.message.flow_add: loxi_flow_mod_to_ofp_flow_mod,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700399 of13.message.flow_delete: loxi_flow_mod_to_ofp_flow_mod,
400 of13.message.flow_delete_strict: loxi_flow_mod_to_ofp_flow_mod,
401 of13.message.flow_modify: loxi_flow_mod_to_ofp_flow_mod,
402 of13.message.flow_modify_strict: loxi_flow_mod_to_ofp_flow_mod,
403
404 of13.message.group_add: loxi_group_mod_to_ofp_group_mod,
405 of13.message.group_delete: loxi_group_mod_to_ofp_group_mod,
406 of13.message.group_modify: loxi_group_mod_to_ofp_group_mod,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700407 of13.message.packet_out: loxi_packet_out_to_ofp_packet_out,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700408
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700409 of13.common.match_v3: loxi_match_v3_to_ofp_match,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700410 of13.common.bucket: loxi_bucket_to_ofp_bucket,
411
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700412 of13.oxm.eth_type: loxi_oxm_eth_type_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700413 of13.oxm.in_port: loxi_oxm_in_port_to_ofp_oxm,
414 of13.oxm.ip_proto: loxi_oxm_ip_proto_to_ofp_oxm,
415 of13.oxm.vlan_vid: loxi_oxm_vlan_vid_to_ofp_oxm,
416 of13.oxm.vlan_pcp: loxi_oxm_vlan_pcp_to_ofp_oxm,
417 of13.oxm.ipv4_dst: loxi_oxm_ipv4_dst_to_ofp_oxm,
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800418 of13.oxm.udp_src: loxi_oxm_udp_src_to_ofp_oxm,
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800419 of13.oxm.udp_dst: loxi_oxm_udp_dst_to_ofp_oxm,
420 of13.oxm.metadata: loxi_oxm_metadata_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700421
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700422 of13.instruction.apply_actions: loxi_apply_actions_to_ofp_instruction,
Gamze Abaka61c2e982018-02-14 11:03:36 +0000423 of13.instruction.clear_actions: loxi_clear_actions_to_ofp_instruction,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700424 of13.instruction.goto_table: loxi_goto_table_to_ofp_instruction,
425
426 of13.action.output: loxi_output_action_to_ofp_action,
427 of13.action.group: loxi_group_action_to_ofp_action,
428 of13.action.set_field: loxi_set_field_action_to_ofp_action,
429 of13.action.pop_vlan: loxi_pop_vlan_action_to_ofp_action,
430 of13.action.push_vlan: loxi_push_vlan_action_to_ofp_action,
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700431}