blob: 6855c5634b0b9b48dc9fe1f47c008d32088ddcfd [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']])
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800160 elif type == pb2.OFPIT_GOTO_TABLE:
161 return of13.instruction.goto_table(
162 table_id=inst['goto_table']['table_id'])
163
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700164 else:
165 raise NotImplementedError('Instruction type %d' % type)
166
167 kw['match'] = make_loxi_match(kw['match'])
168 kw['instructions'] = [make_loxi_instruction(i) for i in kw['instructions']]
Zsolt Haraszti66862032016-11-28 14:28:39 -0800169 del kw['id']
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700170 return of13.flow_stats_entry(**kw)
171
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800172
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700173def ofp_packet_in_to_loxi_packet_in(pb):
Zsolt Haraszti8925d1f2016-12-21 00:45:19 -0800174 packet_in = of13.message.packet_in(
175 buffer_id=pb.buffer_id,
176 reason=pb.reason,
177 table_id=pb.table_id,
178 cookie=pb.cookie,
179 match=make_loxi_match(pb2dict(pb.match)),
180 data=pb.data
181 )
182 return packet_in
183
alshabibf4fb2682017-01-12 00:32:56 -0600184def ofp_group_desc_to_loxi_group_desc(pb):
185 return of13.group_desc_stats_entry(
186 group_type=pb.type,
187 group_id=pb.group_id,
188 buckets=[to_loxi(bucket) for bucket in pb.buckets])
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700189
alshabibf4fb2682017-01-12 00:32:56 -0600190
191def ofp_group_stats_to_loxi_group_stats(pb):
Zsolt Haraszti66862032016-11-28 14:28:39 -0800192 return of13.group_stats_entry(
alshabibf4fb2682017-01-12 00:32:56 -0600193 group_id=pb.group_id,
194 ref_count=pb.ref_count,
195 packet_count=pb.packet_count,
196 byte_count=pb.byte_count,
197 duration_sec=pb.duration_sec,
198 duration_nsec=pb.duration_nsec,
199 bucket_stats=[to_loxi(bstat) for bstat in pb.bucket_stats])
Zsolt Haraszti66862032016-11-28 14:28:39 -0800200
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800201
Zsolt Haraszti66862032016-11-28 14:28:39 -0800202def ofp_bucket_counter_to_loxy_bucket_counter(pb):
203 return of13.bucket_counter(
204 packet_count=pb.packet_count,
205 byte_count=pb.byte_count)
206
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700207
alshabibf4fb2682017-01-12 00:32:56 -0600208def ofp_bucket_to_loxi_bucket(pb):
209 return of13.bucket(
210 weight=pb.weight,
211 watch_port=pb.watch_port,
212 watch_group=pb.watch_group,
213 actions=[to_loxi(action) for action in pb.actions]
214 )
215
216
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700217to_loxi_converters = {
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800218 'ofp_port': ofp_port_to_loxi_port_desc,
219 'ofp_port_status': ofp_port_status_to_loxi_port_status,
220 'ofp_flow_stats': ofp_flow_stats_to_loxi_flow_stats,
221 'ofp_packet_in': ofp_packet_in_to_loxi_packet_in,
alshabibf4fb2682017-01-12 00:32:56 -0600222 'ofp_group_stats': ofp_group_stats_to_loxi_group_stats,
223 'ofp_group_desc': ofp_group_desc_to_loxi_group_desc,
224 'ofp_bucket_counter': ofp_bucket_counter_to_loxy_bucket_counter,
225 'ofp_bucket': ofp_bucket_to_loxi_bucket,
226 'ofp_action': make_loxi_action
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700227}
228
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700229
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700230def loxi_flow_mod_to_ofp_flow_mod(lo):
231 return pb2.ofp_flow_mod(
232 cookie=lo.cookie,
233 cookie_mask=lo.cookie_mask,
234 table_id=lo.table_id,
235 command=lo._command,
236 idle_timeout=lo.idle_timeout,
237 hard_timeout=lo.hard_timeout,
238 priority=lo.priority,
239 buffer_id=lo.buffer_id,
240 out_port=lo.out_port,
241 out_group=lo.out_group,
242 flags=lo.flags,
243 match=to_grpc(lo.match),
244 instructions=[to_grpc(i) for i in lo.instructions])
245
246
247def loxi_group_mod_to_ofp_group_mod(lo):
248 return pb2.ofp_group_mod(
249 command=lo.command,
Zsolt Haraszti9125b1a2016-10-24 22:54:33 -0700250 type=lo.group_type,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700251 group_id=lo.group_id,
252 buckets=[to_grpc(b) for b in lo.buckets])
253
254
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700255def loxi_packet_out_to_ofp_packet_out(lo):
256 return pb2.ofp_packet_out(
257 buffer_id=lo.buffer_id,
258 in_port=lo.in_port,
259 actions=[to_grpc(a) for a in lo.actions],
260 data=lo.data)
261
262
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700263def loxi_match_v3_to_ofp_match(lo):
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700264 return pb2.ofp_match(
265 type=pb2.OFPMT_OXM,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700266 oxm_fields=[to_grpc(f) for f in lo.oxm_list])
267
268
269def loxi_bucket_to_ofp_bucket(lo):
270 return pb2.ofp_bucket(
271 weight=lo.weight,
272 watch_port=lo.watch_port,
273 watch_group=lo.watch_group,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700274 actions=[to_grpc(a) for a in lo.actions])
275
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700276
277def loxi_oxm_eth_type_to_ofp_oxm(lo):
278 return pb2.ofp_oxm_field(
279 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
280 ofb_field=pb2.ofp_oxm_ofb_field(
281 type=pb2.OFPXMT_OFB_ETH_TYPE,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700282 eth_type=lo.value))
283
284
285def loxi_oxm_in_port_to_ofp_oxm(lo):
286 return pb2.ofp_oxm_field(
287 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
288 ofb_field=pb2.ofp_oxm_ofb_field(
289 type=pb2.OFPXMT_OFB_IN_PORT,
290 port=lo.value))
291
292
293def loxi_oxm_ip_proto_to_ofp_oxm(lo):
294 return pb2.ofp_oxm_field(
295 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
296 ofb_field=pb2.ofp_oxm_ofb_field(
297 type=pb2.OFPXMT_OFB_IP_PROTO,
298 ip_proto=lo.value))
299
300
301def loxi_oxm_vlan_vid_to_ofp_oxm(lo):
302 return pb2.ofp_oxm_field(
303 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
304 ofb_field=pb2.ofp_oxm_ofb_field(
305 type=pb2.OFPXMT_OFB_VLAN_VID,
306 vlan_vid=lo.value))
307
308
309def loxi_oxm_vlan_pcp_to_ofp_oxm(lo):
310 return pb2.ofp_oxm_field(
311 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
312 ofb_field=pb2.ofp_oxm_ofb_field(
313 type=pb2.OFPXMT_OFB_VLAN_PCP,
314 vlan_pcp=lo.value))
315
316
317def loxi_oxm_ipv4_dst_to_ofp_oxm(lo):
318 return pb2.ofp_oxm_field(
319 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
320 ofb_field=pb2.ofp_oxm_ofb_field(
321 type=pb2.OFPXMT_OFB_IPV4_DST,
322 ipv4_dst=lo.value))
323
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700324
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800325def loxi_oxm_udp_dst_to_ofp_oxm(lo):
326 return pb2.ofp_oxm_field(
327 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
328 ofb_field=pb2.ofp_oxm_ofb_field(
329 type=pb2.OFPXMT_OFB_UDP_DST,
330 udp_dst=lo.value))
331
332
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800333def loxi_oxm_udp_src_to_ofp_oxm(lo):
334 return pb2.ofp_oxm_field(
335 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
336 ofb_field=pb2.ofp_oxm_ofb_field(
337 type=pb2.OFPXMT_OFB_UDP_SRC,
338 udp_src=lo.value))
339
340
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800341def loxi_oxm_metadata_to_ofp_oxm(lo):
342 return pb2.ofp_oxm_field(
343 oxm_class=pb2.OFPXMC_OPENFLOW_BASIC,
344 ofb_field=pb2.ofp_oxm_ofb_field(
345 type=pb2.OFPXMT_OFB_METADATA,
346 table_metadata=lo.value))
347
348
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700349def loxi_apply_actions_to_ofp_instruction(lo):
350 return pb2.ofp_instruction(
351 type=pb2.OFPIT_APPLY_ACTIONS,
352 actions=pb2.ofp_instruction_actions(
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700353 actions=[to_grpc(a) for a in lo.actions]))
354
355
356def loxi_goto_table_to_ofp_instruction(lo):
357 return pb2.ofp_instruction(
358 type=pb2.OFPIT_GOTO_TABLE,
359 goto_table=pb2.ofp_instruction_goto_table(table_id=lo.table_id))
360
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700361
362def loxi_output_action_to_ofp_action(lo):
363 return pb2.ofp_action(
364 type=pb2.OFPAT_OUTPUT,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700365 output=pb2.ofp_action_output(port=lo.port, max_len=lo.max_len))
366
367
368def loxi_group_action_to_ofp_action(lo):
369 return pb2.ofp_action(
370 type=pb2.OFPAT_GROUP,
371 group=pb2.ofp_action_group(group_id=lo.group_id))
372
373
374def loxi_set_field_action_to_ofp_action(lo):
375 return pb2.ofp_action(
376 type=pb2.OFPAT_SET_FIELD,
377 set_field=pb2.ofp_action_set_field(field=to_grpc(lo.field)))
378
379
380def loxi_pop_vlan_action_to_ofp_action(lo):
381 return pb2.ofp_action(type=pb2.OFPAT_POP_VLAN)
382
383
384def loxi_push_vlan_action_to_ofp_action(lo):
385 return pb2.ofp_action(
386 type=pb2.OFPAT_PUSH_VLAN,
387 push=pb2.ofp_action_push(ethertype=lo.ethertype))
388
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700389
390to_grpc_converters = {
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700391
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700392 of13.message.flow_add: loxi_flow_mod_to_ofp_flow_mod,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700393 of13.message.flow_delete: loxi_flow_mod_to_ofp_flow_mod,
394 of13.message.flow_delete_strict: loxi_flow_mod_to_ofp_flow_mod,
395 of13.message.flow_modify: loxi_flow_mod_to_ofp_flow_mod,
396 of13.message.flow_modify_strict: loxi_flow_mod_to_ofp_flow_mod,
397
398 of13.message.group_add: loxi_group_mod_to_ofp_group_mod,
399 of13.message.group_delete: loxi_group_mod_to_ofp_group_mod,
400 of13.message.group_modify: loxi_group_mod_to_ofp_group_mod,
Zsolt Haraszticd22adc2016-10-25 00:13:06 -0700401 of13.message.packet_out: loxi_packet_out_to_ofp_packet_out,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700402
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700403 of13.common.match_v3: loxi_match_v3_to_ofp_match,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700404 of13.common.bucket: loxi_bucket_to_ofp_bucket,
405
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700406 of13.oxm.eth_type: loxi_oxm_eth_type_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700407 of13.oxm.in_port: loxi_oxm_in_port_to_ofp_oxm,
408 of13.oxm.ip_proto: loxi_oxm_ip_proto_to_ofp_oxm,
409 of13.oxm.vlan_vid: loxi_oxm_vlan_vid_to_ofp_oxm,
410 of13.oxm.vlan_pcp: loxi_oxm_vlan_pcp_to_ofp_oxm,
411 of13.oxm.ipv4_dst: loxi_oxm_ipv4_dst_to_ofp_oxm,
Zsolt Haraszti3578a1c2017-01-10 15:29:02 -0800412 of13.oxm.udp_src: loxi_oxm_udp_src_to_ofp_oxm,
Zsolt Haraszti6a5107c2017-01-09 23:42:41 -0800413 of13.oxm.udp_dst: loxi_oxm_udp_dst_to_ofp_oxm,
414 of13.oxm.metadata: loxi_oxm_metadata_to_ofp_oxm,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700415
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700416 of13.instruction.apply_actions: loxi_apply_actions_to_ofp_instruction,
Zsolt Haraszti8a774382016-10-24 18:25:54 -0700417 of13.instruction.goto_table: loxi_goto_table_to_ofp_instruction,
418
419 of13.action.output: loxi_output_action_to_ofp_action,
420 of13.action.group: loxi_group_action_to_ofp_action,
421 of13.action.set_field: loxi_set_field_action_to_ofp_action,
422 of13.action.pop_vlan: loxi_pop_vlan_action_to_ofp_action,
423 of13.action.push_vlan: loxi_push_vlan_action_to_ofp_action,
Zsolt Haraszti023ea7c2016-10-16 19:30:34 -0700424}