Stephane Barbarie | 6e1bd50 | 2018-11-05 22:44:45 -0500 | [diff] [blame^] | 1 | # |
| 2 | # Copyright 2017 the original author or authors. |
| 3 | # |
| 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 | """ |
| 18 | Convert loxi objects to openflow_13 messages and back. |
| 19 | """ |
| 20 | from copy import copy |
| 21 | |
| 22 | from google.protobuf.descriptor import FieldDescriptor |
| 23 | |
| 24 | import loxi.of13 as of13 |
| 25 | from protobuf_to_dict import protobuf_to_dict, TYPE_CALLABLE_MAP |
| 26 | from protos import openflow_13_pb2 as pb2 |
| 27 | |
| 28 | |
| 29 | type_callable_map = copy(TYPE_CALLABLE_MAP) |
| 30 | type_callable_map.update({ |
| 31 | FieldDescriptor.TYPE_STRING: str |
| 32 | }) |
| 33 | |
| 34 | def 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 | |
| 44 | def to_loxi(grpc_object): |
| 45 | cls = grpc_object.__class__ |
| 46 | converter = to_loxi_converters[cls.__name__] |
| 47 | return converter(grpc_object) |
| 48 | |
| 49 | def to_grpc(loxi_object): |
| 50 | cls = loxi_object.__class__ |
| 51 | converter = to_grpc_converters[cls] |
| 52 | return converter(loxi_object) |
| 53 | |
| 54 | def ofp_port_to_loxi_port_desc(pb): |
| 55 | kw = pb2dict(pb) |
| 56 | return of13.common.port_desc(**kw) |
| 57 | |
| 58 | def 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 | |
| 64 | def ofp_port_stats_to_loxi_port_stats(pb): |
| 65 | kw = pb2dict(pb) |
| 66 | return of13.port_stats_entry(**kw) |
| 67 | |
| 68 | def make_loxi_field(oxm_field): |
| 69 | assert oxm_field['oxm_class'] == pb2.OFPXMC_OPENFLOW_BASIC |
| 70 | ofb_field = oxm_field['ofb_field'] |
| 71 | field_type = ofb_field.get('type', 0) |
| 72 | |
| 73 | if field_type == pb2.OFPXMT_OFB_ETH_TYPE: |
| 74 | return ( |
| 75 | of13.oxm.eth_type(value=ofb_field['eth_type'])) |
| 76 | |
| 77 | elif field_type == pb2.OFPXMT_OFB_IN_PORT: |
| 78 | return ( |
| 79 | of13.oxm.in_port(value=ofb_field['port'])) |
| 80 | |
| 81 | elif field_type == pb2.OFPXMT_OFB_IP_PROTO: |
| 82 | return ( |
| 83 | of13.oxm.ip_proto(value=ofb_field['ip_proto'])) |
| 84 | |
| 85 | elif field_type == pb2.OFPXMT_OFB_VLAN_VID: |
| 86 | return ( |
| 87 | of13.oxm.vlan_vid(value=ofb_field['vlan_vid'])) |
| 88 | |
| 89 | elif field_type == pb2.OFPXMT_OFB_VLAN_PCP: |
| 90 | return ( |
| 91 | of13.oxm.vlan_pcp(value=ofb_field['vlan_pcp'])) |
| 92 | |
| 93 | elif field_type == pb2.OFPXMT_OFB_IPV4_SRC: |
| 94 | return ( |
| 95 | of13.oxm.ipv4_src(value=ofb_field['ipv4_src'])) |
| 96 | |
| 97 | elif field_type == pb2.OFPXMT_OFB_IPV4_DST: |
| 98 | return ( |
| 99 | of13.oxm.ipv4_dst(value=ofb_field['ipv4_dst'])) |
| 100 | |
| 101 | elif field_type == pb2.OFPXMT_OFB_UDP_SRC: |
| 102 | return ( |
| 103 | of13.oxm.udp_src(value=ofb_field['udp_src'])) |
| 104 | |
| 105 | elif field_type == pb2.OFPXMT_OFB_UDP_DST: |
| 106 | return ( |
| 107 | of13.oxm.udp_dst(value=ofb_field['udp_dst'])) |
| 108 | |
| 109 | elif field_type == pb2.OFPXMT_OFB_METADATA: |
| 110 | return ( |
| 111 | of13.oxm.metadata(value=ofb_field['table_metadata'])) |
| 112 | |
| 113 | else: |
| 114 | raise NotImplementedError( |
| 115 | 'OXM match field for type %s' % field_type) |
| 116 | |
| 117 | def make_loxi_match(match): |
| 118 | assert match.get('type', pb2.OFPMT_STANDARD) == pb2.OFPMT_OXM |
| 119 | loxi_match_fields = [] |
| 120 | for oxm_field in match.get('oxm_fields', []): |
| 121 | loxi_match_fields.append(make_loxi_field(oxm_field)) |
| 122 | return of13.match_v3(oxm_list=loxi_match_fields) |
| 123 | |
| 124 | |
| 125 | def make_loxi_action(a): |
| 126 | if type(a) is not dict: |
| 127 | a = pb2dict(a) |
| 128 | |
| 129 | typ = a.get('type', 0) |
| 130 | |
| 131 | if typ == pb2.OFPAT_OUTPUT: |
| 132 | output_kws = a['output'] |
| 133 | return of13.action.output(**output_kws) |
| 134 | |
| 135 | elif typ == pb2.OFPAT_POP_VLAN: |
| 136 | return of13.action.pop_vlan() |
| 137 | |
| 138 | elif typ == pb2.OFPAT_PUSH_VLAN: |
| 139 | push_vlan_kws = a['push'] |
| 140 | return of13.action.push_vlan(**push_vlan_kws) |
| 141 | |
| 142 | elif typ == pb2.OFPAT_SET_FIELD: |
| 143 | loxi_field = make_loxi_field(a['set_field']['field']) |
| 144 | return of13.action.set_field(loxi_field) |
| 145 | |
| 146 | elif typ == pb2.OFPAT_GROUP: |
| 147 | group_kws = a['group'] |
| 148 | return of13.action.group(**group_kws) |
| 149 | |
| 150 | else: |
| 151 | raise NotImplementedError( |
| 152 | 'Action decoder for action OFPAT_* %d' % typ) |
| 153 | |
| 154 | |
| 155 | def ofp_flow_stats_to_loxi_flow_stats(pb): |
| 156 | kw = pb2dict(pb) |
| 157 | |
| 158 | def make_loxi_instruction(inst): |
| 159 | type = inst['type'] |
| 160 | if type == pb2.OFPIT_APPLY_ACTIONS: |
| 161 | return of13.instruction.apply_actions( |
| 162 | actions=[make_loxi_action(a) |
| 163 | for a in inst['actions']['actions']]) |
| 164 | elif type == pb2.OFPIT_CLEAR_ACTIONS: |
| 165 | return of13.instruction.clear_actions() |
| 166 | elif type == pb2.OFPIT_GOTO_TABLE: |
| 167 | return of13.instruction.goto_table( |
| 168 | table_id=inst['goto_table']['table_id']) |
| 169 | |
| 170 | else: |
| 171 | raise NotImplementedError('Instruction type %d' % type) |
| 172 | |
| 173 | kw['match'] = make_loxi_match(kw['match']) |
| 174 | kw['instructions'] = [make_loxi_instruction(i) for i in kw['instructions']] |
| 175 | del kw['id'] |
| 176 | return of13.flow_stats_entry(**kw) |
| 177 | |
| 178 | |
| 179 | def ofp_packet_in_to_loxi_packet_in(pb): |
| 180 | packet_in = of13.message.packet_in( |
| 181 | buffer_id=pb.buffer_id, |
| 182 | reason=pb.reason, |
| 183 | table_id=pb.table_id, |
| 184 | cookie=pb.cookie, |
| 185 | match=make_loxi_match(pb2dict(pb.match)), |
| 186 | data=pb.data |
| 187 | ) |
| 188 | return packet_in |
| 189 | |
| 190 | def ofp_group_desc_to_loxi_group_desc(pb): |
| 191 | return of13.group_desc_stats_entry( |
| 192 | group_type=pb.type, |
| 193 | group_id=pb.group_id, |
| 194 | buckets=[to_loxi(bucket) for bucket in pb.buckets]) |
| 195 | |
| 196 | |
| 197 | def ofp_group_stats_to_loxi_group_stats(pb): |
| 198 | return of13.group_stats_entry( |
| 199 | group_id=pb.group_id, |
| 200 | ref_count=pb.ref_count, |
| 201 | packet_count=pb.packet_count, |
| 202 | byte_count=pb.byte_count, |
| 203 | duration_sec=pb.duration_sec, |
| 204 | duration_nsec=pb.duration_nsec, |
| 205 | bucket_stats=[to_loxi(bstat) for bstat in pb.bucket_stats]) |
| 206 | |
| 207 | |
| 208 | def ofp_bucket_counter_to_loxy_bucket_counter(pb): |
| 209 | return of13.bucket_counter( |
| 210 | packet_count=pb.packet_count, |
| 211 | byte_count=pb.byte_count) |
| 212 | |
| 213 | |
| 214 | def ofp_bucket_to_loxi_bucket(pb): |
| 215 | return of13.bucket( |
| 216 | weight=pb.weight, |
| 217 | watch_port=pb.watch_port, |
| 218 | watch_group=pb.watch_group, |
| 219 | actions=[to_loxi(action) for action in pb.actions] |
| 220 | ) |
| 221 | |
| 222 | |
| 223 | to_loxi_converters = { |
| 224 | 'ofp_port': ofp_port_to_loxi_port_desc, |
| 225 | 'ofp_port_status': ofp_port_status_to_loxi_port_status, |
| 226 | 'ofp_flow_stats': ofp_flow_stats_to_loxi_flow_stats, |
| 227 | 'ofp_packet_in': ofp_packet_in_to_loxi_packet_in, |
| 228 | 'ofp_group_stats': ofp_group_stats_to_loxi_group_stats, |
| 229 | 'ofp_group_desc': ofp_group_desc_to_loxi_group_desc, |
| 230 | 'ofp_bucket_counter': ofp_bucket_counter_to_loxy_bucket_counter, |
| 231 | 'ofp_bucket': ofp_bucket_to_loxi_bucket, |
| 232 | 'ofp_action': make_loxi_action, |
| 233 | 'ofp_port_stats': ofp_port_stats_to_loxi_port_stats |
| 234 | } |
| 235 | |
| 236 | |
| 237 | def loxi_flow_mod_to_ofp_flow_mod(lo): |
| 238 | return pb2.ofp_flow_mod( |
| 239 | cookie=lo.cookie, |
| 240 | cookie_mask=lo.cookie_mask, |
| 241 | table_id=lo.table_id, |
| 242 | command=lo._command, |
| 243 | idle_timeout=lo.idle_timeout, |
| 244 | hard_timeout=lo.hard_timeout, |
| 245 | priority=lo.priority, |
| 246 | buffer_id=lo.buffer_id, |
| 247 | out_port=lo.out_port, |
| 248 | out_group=lo.out_group, |
| 249 | flags=lo.flags, |
| 250 | match=to_grpc(lo.match), |
| 251 | instructions=[to_grpc(i) for i in lo.instructions]) |
| 252 | |
| 253 | |
| 254 | def loxi_group_mod_to_ofp_group_mod(lo): |
| 255 | return pb2.ofp_group_mod( |
| 256 | command=lo.command, |
| 257 | type=lo.group_type, |
| 258 | group_id=lo.group_id, |
| 259 | buckets=[to_grpc(b) for b in lo.buckets]) |
| 260 | |
| 261 | |
| 262 | def loxi_packet_out_to_ofp_packet_out(lo): |
| 263 | return pb2.ofp_packet_out( |
| 264 | buffer_id=lo.buffer_id, |
| 265 | in_port=lo.in_port, |
| 266 | actions=[to_grpc(a) for a in lo.actions], |
| 267 | data=lo.data) |
| 268 | |
| 269 | |
| 270 | def loxi_match_v3_to_ofp_match(lo): |
| 271 | return pb2.ofp_match( |
| 272 | type=pb2.OFPMT_OXM, |
| 273 | oxm_fields=[to_grpc(f) for f in lo.oxm_list]) |
| 274 | |
| 275 | |
| 276 | def loxi_bucket_to_ofp_bucket(lo): |
| 277 | return pb2.ofp_bucket( |
| 278 | weight=lo.weight, |
| 279 | watch_port=lo.watch_port, |
| 280 | watch_group=lo.watch_group, |
| 281 | actions=[to_grpc(a) for a in lo.actions]) |
| 282 | |
| 283 | |
| 284 | def loxi_oxm_eth_type_to_ofp_oxm(lo): |
| 285 | return pb2.ofp_oxm_field( |
| 286 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 287 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 288 | type=pb2.OFPXMT_OFB_ETH_TYPE, |
| 289 | eth_type=lo.value)) |
| 290 | |
| 291 | |
| 292 | def loxi_oxm_in_port_to_ofp_oxm(lo): |
| 293 | return pb2.ofp_oxm_field( |
| 294 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 295 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 296 | type=pb2.OFPXMT_OFB_IN_PORT, |
| 297 | port=lo.value)) |
| 298 | |
| 299 | |
| 300 | def loxi_oxm_ip_proto_to_ofp_oxm(lo): |
| 301 | return pb2.ofp_oxm_field( |
| 302 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 303 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 304 | type=pb2.OFPXMT_OFB_IP_PROTO, |
| 305 | ip_proto=lo.value)) |
| 306 | |
| 307 | |
| 308 | def loxi_oxm_vlan_vid_to_ofp_oxm(lo): |
| 309 | return pb2.ofp_oxm_field( |
| 310 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 311 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 312 | type=pb2.OFPXMT_OFB_VLAN_VID, |
| 313 | vlan_vid=lo.value)) |
| 314 | |
| 315 | |
| 316 | def loxi_oxm_vlan_pcp_to_ofp_oxm(lo): |
| 317 | return pb2.ofp_oxm_field( |
| 318 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 319 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 320 | type=pb2.OFPXMT_OFB_VLAN_PCP, |
| 321 | vlan_pcp=lo.value)) |
| 322 | |
| 323 | |
| 324 | def loxi_oxm_ipv4_dst_to_ofp_oxm(lo): |
| 325 | return pb2.ofp_oxm_field( |
| 326 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 327 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 328 | type=pb2.OFPXMT_OFB_IPV4_DST, |
| 329 | ipv4_dst=lo.value)) |
| 330 | |
| 331 | |
| 332 | def loxi_oxm_udp_dst_to_ofp_oxm(lo): |
| 333 | return pb2.ofp_oxm_field( |
| 334 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 335 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 336 | type=pb2.OFPXMT_OFB_UDP_DST, |
| 337 | udp_dst=lo.value)) |
| 338 | |
| 339 | |
| 340 | def loxi_oxm_udp_src_to_ofp_oxm(lo): |
| 341 | return pb2.ofp_oxm_field( |
| 342 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 343 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 344 | type=pb2.OFPXMT_OFB_UDP_SRC, |
| 345 | udp_src=lo.value)) |
| 346 | |
| 347 | |
| 348 | def loxi_oxm_metadata_to_ofp_oxm(lo): |
| 349 | return pb2.ofp_oxm_field( |
| 350 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 351 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 352 | type=pb2.OFPXMT_OFB_METADATA, |
| 353 | table_metadata=lo.value)) |
| 354 | |
| 355 | |
| 356 | def loxi_apply_actions_to_ofp_instruction(lo): |
| 357 | return pb2.ofp_instruction( |
| 358 | type=pb2.OFPIT_APPLY_ACTIONS, |
| 359 | actions=pb2.ofp_instruction_actions( |
| 360 | actions=[to_grpc(a) for a in lo.actions])) |
| 361 | |
| 362 | def loxi_clear_actions_to_ofp_instruction(lo): |
| 363 | return pb2.ofp_instruction( |
| 364 | type=pb2.OFPIT_CLEAR_ACTIONS) |
| 365 | |
| 366 | |
| 367 | def loxi_goto_table_to_ofp_instruction(lo): |
| 368 | return pb2.ofp_instruction( |
| 369 | type=pb2.OFPIT_GOTO_TABLE, |
| 370 | goto_table=pb2.ofp_instruction_goto_table(table_id=lo.table_id)) |
| 371 | |
| 372 | |
| 373 | def loxi_output_action_to_ofp_action(lo): |
| 374 | return pb2.ofp_action( |
| 375 | type=pb2.OFPAT_OUTPUT, |
| 376 | output=pb2.ofp_action_output(port=lo.port, max_len=lo.max_len)) |
| 377 | |
| 378 | |
| 379 | def loxi_group_action_to_ofp_action(lo): |
| 380 | return pb2.ofp_action( |
| 381 | type=pb2.OFPAT_GROUP, |
| 382 | group=pb2.ofp_action_group(group_id=lo.group_id)) |
| 383 | |
| 384 | |
| 385 | def loxi_set_field_action_to_ofp_action(lo): |
| 386 | return pb2.ofp_action( |
| 387 | type=pb2.OFPAT_SET_FIELD, |
| 388 | set_field=pb2.ofp_action_set_field(field=to_grpc(lo.field))) |
| 389 | |
| 390 | |
| 391 | def loxi_pop_vlan_action_to_ofp_action(lo): |
| 392 | return pb2.ofp_action(type=pb2.OFPAT_POP_VLAN) |
| 393 | |
| 394 | |
| 395 | def loxi_push_vlan_action_to_ofp_action(lo): |
| 396 | return pb2.ofp_action( |
| 397 | type=pb2.OFPAT_PUSH_VLAN, |
| 398 | push=pb2.ofp_action_push(ethertype=lo.ethertype)) |
| 399 | |
| 400 | |
| 401 | to_grpc_converters = { |
| 402 | |
| 403 | of13.message.flow_add: loxi_flow_mod_to_ofp_flow_mod, |
| 404 | of13.message.flow_delete: loxi_flow_mod_to_ofp_flow_mod, |
| 405 | of13.message.flow_delete_strict: loxi_flow_mod_to_ofp_flow_mod, |
| 406 | of13.message.flow_modify: loxi_flow_mod_to_ofp_flow_mod, |
| 407 | of13.message.flow_modify_strict: loxi_flow_mod_to_ofp_flow_mod, |
| 408 | |
| 409 | of13.message.group_add: loxi_group_mod_to_ofp_group_mod, |
| 410 | of13.message.group_delete: loxi_group_mod_to_ofp_group_mod, |
| 411 | of13.message.group_modify: loxi_group_mod_to_ofp_group_mod, |
| 412 | of13.message.packet_out: loxi_packet_out_to_ofp_packet_out, |
| 413 | |
| 414 | of13.common.match_v3: loxi_match_v3_to_ofp_match, |
| 415 | of13.common.bucket: loxi_bucket_to_ofp_bucket, |
| 416 | |
| 417 | of13.oxm.eth_type: loxi_oxm_eth_type_to_ofp_oxm, |
| 418 | of13.oxm.in_port: loxi_oxm_in_port_to_ofp_oxm, |
| 419 | of13.oxm.ip_proto: loxi_oxm_ip_proto_to_ofp_oxm, |
| 420 | of13.oxm.vlan_vid: loxi_oxm_vlan_vid_to_ofp_oxm, |
| 421 | of13.oxm.vlan_pcp: loxi_oxm_vlan_pcp_to_ofp_oxm, |
| 422 | of13.oxm.ipv4_dst: loxi_oxm_ipv4_dst_to_ofp_oxm, |
| 423 | of13.oxm.udp_src: loxi_oxm_udp_src_to_ofp_oxm, |
| 424 | of13.oxm.udp_dst: loxi_oxm_udp_dst_to_ofp_oxm, |
| 425 | of13.oxm.metadata: loxi_oxm_metadata_to_ofp_oxm, |
| 426 | |
| 427 | of13.instruction.apply_actions: loxi_apply_actions_to_ofp_instruction, |
| 428 | of13.instruction.clear_actions: loxi_clear_actions_to_ofp_instruction, |
| 429 | of13.instruction.goto_table: loxi_goto_table_to_ofp_instruction, |
| 430 | |
| 431 | of13.action.output: loxi_output_action_to_ofp_action, |
| 432 | of13.action.group: loxi_group_action_to_ofp_action, |
| 433 | of13.action.set_field: loxi_set_field_action_to_ofp_action, |
| 434 | of13.action.pop_vlan: loxi_pop_vlan_action_to_ofp_action, |
| 435 | of13.action.push_vlan: loxi_push_vlan_action_to_ofp_action, |
| 436 | } |