Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016 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] |
| 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 | |
Zsolt Haraszti | 217a12e | 2016-12-19 16:37:55 -0800 | [diff] [blame] | 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 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 64 | def make_loxi_match(match): |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 65 | assert match.get('type', pb2.OFPMT_STANDARD) == pb2.OFPMT_OXM |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 66 | loxi_match_fields = [] |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 67 | for oxm_field in match.get('oxm_fields', []): |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 68 | assert oxm_field['oxm_class'] == pb2.OFPXMC_OPENFLOW_BASIC |
| 69 | ofb_field = oxm_field['ofb_field'] |
| 70 | field_type = ofb_field.get('type', 0) |
| 71 | if field_type == pb2.OFPXMT_OFB_ETH_TYPE: |
| 72 | loxi_match_fields.append( |
| 73 | of13.oxm.eth_type(value=ofb_field['eth_type'])) |
| 74 | else: |
| 75 | raise NotImplementedError( |
| 76 | 'OXM match field for type %s' % field_type) |
| 77 | return of13.match_v3(oxm_list=loxi_match_fields) |
| 78 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 79 | def ofp_flow_stats_to_loxi_flow_stats(pb): |
| 80 | kw = pb2dict(pb) |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 81 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 82 | def make_loxi_action(a): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 83 | type = a.get('type', 0) |
| 84 | if type == pb2.OFPAT_OUTPUT: |
| 85 | output = a['output'] |
| 86 | return of13.action.output(**output) |
| 87 | else: |
| 88 | raise NotImplementedError( |
| 89 | 'Action decoder for action OFPAT_* %d' % type) |
| 90 | |
| 91 | def make_loxi_instruction(inst): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 92 | type = inst['type'] |
| 93 | if type == pb2.OFPIT_APPLY_ACTIONS: |
| 94 | return of13.instruction.apply_actions( |
| 95 | actions=[make_loxi_action(a) |
| 96 | for a in inst['actions']['actions']]) |
| 97 | else: |
| 98 | raise NotImplementedError('Instruction type %d' % type) |
| 99 | |
| 100 | kw['match'] = make_loxi_match(kw['match']) |
| 101 | kw['instructions'] = [make_loxi_instruction(i) for i in kw['instructions']] |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 102 | del kw['id'] |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 103 | return of13.flow_stats_entry(**kw) |
| 104 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 105 | def ofp_packet_in_to_loxi_packet_in(pb): |
| 106 | kw = pb2dict(pb) |
| 107 | if 'match' in kw: |
| 108 | kw['match'] = make_loxi_match(kw['match']) |
| 109 | return of13.message.packet_in(**kw) |
| 110 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 111 | def ofp_group_entry_to_loxi_group_entry(pb): |
| 112 | return of13.group_stats_entry( |
| 113 | group_id=pb.stats.group_id, |
| 114 | ref_count=pb.stats.ref_count, |
| 115 | packet_count=pb.stats.packet_count, |
| 116 | byte_count=pb.stats.byte_count, |
| 117 | duration_sec=pb.stats.duration_sec, |
| 118 | duration_nsec=pb.stats.duration_nsec, |
| 119 | bucket_stats=[to_loxi(bstat) for bstat in pb.stats.bucket_stats]) |
| 120 | |
| 121 | def ofp_bucket_counter_to_loxy_bucket_counter(pb): |
| 122 | return of13.bucket_counter( |
| 123 | packet_count=pb.packet_count, |
| 124 | byte_count=pb.byte_count) |
| 125 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 126 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 127 | to_loxi_converters = { |
| 128 | pb2.ofp_port: ofp_port_to_loxi_port_desc, |
Zsolt Haraszti | 217a12e | 2016-12-19 16:37:55 -0800 | [diff] [blame] | 129 | pb2.ofp_port_status: ofp_port_status_to_loxi_port_status, |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 130 | pb2.ofp_flow_stats: ofp_flow_stats_to_loxi_flow_stats, |
| 131 | pb2.ofp_packet_in: ofp_packet_in_to_loxi_packet_in, |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 132 | pb2.ofp_group_entry: ofp_group_entry_to_loxi_group_entry, |
| 133 | pb2.ofp_bucket_counter: ofp_bucket_counter_to_loxy_bucket_counter |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 136 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 137 | def loxi_flow_mod_to_ofp_flow_mod(lo): |
| 138 | return pb2.ofp_flow_mod( |
| 139 | cookie=lo.cookie, |
| 140 | cookie_mask=lo.cookie_mask, |
| 141 | table_id=lo.table_id, |
| 142 | command=lo._command, |
| 143 | idle_timeout=lo.idle_timeout, |
| 144 | hard_timeout=lo.hard_timeout, |
| 145 | priority=lo.priority, |
| 146 | buffer_id=lo.buffer_id, |
| 147 | out_port=lo.out_port, |
| 148 | out_group=lo.out_group, |
| 149 | flags=lo.flags, |
| 150 | match=to_grpc(lo.match), |
| 151 | instructions=[to_grpc(i) for i in lo.instructions]) |
| 152 | |
| 153 | |
| 154 | def loxi_group_mod_to_ofp_group_mod(lo): |
| 155 | return pb2.ofp_group_mod( |
| 156 | command=lo.command, |
Zsolt Haraszti | 9125b1a | 2016-10-24 22:54:33 -0700 | [diff] [blame] | 157 | type=lo.group_type, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 158 | group_id=lo.group_id, |
| 159 | buckets=[to_grpc(b) for b in lo.buckets]) |
| 160 | |
| 161 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 162 | def loxi_packet_out_to_ofp_packet_out(lo): |
| 163 | return pb2.ofp_packet_out( |
| 164 | buffer_id=lo.buffer_id, |
| 165 | in_port=lo.in_port, |
| 166 | actions=[to_grpc(a) for a in lo.actions], |
| 167 | data=lo.data) |
| 168 | |
| 169 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 170 | def loxi_match_v3_to_ofp_match(lo): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 171 | return pb2.ofp_match( |
| 172 | type=pb2.OFPMT_OXM, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 173 | oxm_fields=[to_grpc(f) for f in lo.oxm_list]) |
| 174 | |
| 175 | |
| 176 | def loxi_bucket_to_ofp_bucket(lo): |
| 177 | return pb2.ofp_bucket( |
| 178 | weight=lo.weight, |
| 179 | watch_port=lo.watch_port, |
| 180 | watch_group=lo.watch_group, |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 181 | actions=[to_grpc(a) for a in lo.actions]) |
| 182 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 183 | |
| 184 | def loxi_oxm_eth_type_to_ofp_oxm(lo): |
| 185 | return pb2.ofp_oxm_field( |
| 186 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 187 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 188 | type=pb2.OFPXMT_OFB_ETH_TYPE, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 189 | eth_type=lo.value)) |
| 190 | |
| 191 | |
| 192 | def loxi_oxm_in_port_to_ofp_oxm(lo): |
| 193 | return pb2.ofp_oxm_field( |
| 194 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 195 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 196 | type=pb2.OFPXMT_OFB_IN_PORT, |
| 197 | port=lo.value)) |
| 198 | |
| 199 | |
| 200 | def loxi_oxm_ip_proto_to_ofp_oxm(lo): |
| 201 | return pb2.ofp_oxm_field( |
| 202 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 203 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 204 | type=pb2.OFPXMT_OFB_IP_PROTO, |
| 205 | ip_proto=lo.value)) |
| 206 | |
| 207 | |
| 208 | def loxi_oxm_vlan_vid_to_ofp_oxm(lo): |
| 209 | return pb2.ofp_oxm_field( |
| 210 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 211 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 212 | type=pb2.OFPXMT_OFB_VLAN_VID, |
| 213 | vlan_vid=lo.value)) |
| 214 | |
| 215 | |
| 216 | def loxi_oxm_vlan_pcp_to_ofp_oxm(lo): |
| 217 | return pb2.ofp_oxm_field( |
| 218 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 219 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 220 | type=pb2.OFPXMT_OFB_VLAN_PCP, |
| 221 | vlan_pcp=lo.value)) |
| 222 | |
| 223 | |
| 224 | def loxi_oxm_ipv4_dst_to_ofp_oxm(lo): |
| 225 | return pb2.ofp_oxm_field( |
| 226 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 227 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 228 | type=pb2.OFPXMT_OFB_IPV4_DST, |
| 229 | ipv4_dst=lo.value)) |
| 230 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 231 | |
| 232 | def loxi_apply_actions_to_ofp_instruction(lo): |
| 233 | return pb2.ofp_instruction( |
| 234 | type=pb2.OFPIT_APPLY_ACTIONS, |
| 235 | actions=pb2.ofp_instruction_actions( |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 236 | actions=[to_grpc(a) for a in lo.actions])) |
| 237 | |
| 238 | |
| 239 | def loxi_goto_table_to_ofp_instruction(lo): |
| 240 | return pb2.ofp_instruction( |
| 241 | type=pb2.OFPIT_GOTO_TABLE, |
| 242 | goto_table=pb2.ofp_instruction_goto_table(table_id=lo.table_id)) |
| 243 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 244 | |
| 245 | def loxi_output_action_to_ofp_action(lo): |
| 246 | return pb2.ofp_action( |
| 247 | type=pb2.OFPAT_OUTPUT, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 248 | output=pb2.ofp_action_output(port=lo.port, max_len=lo.max_len)) |
| 249 | |
| 250 | |
| 251 | def loxi_group_action_to_ofp_action(lo): |
| 252 | return pb2.ofp_action( |
| 253 | type=pb2.OFPAT_GROUP, |
| 254 | group=pb2.ofp_action_group(group_id=lo.group_id)) |
| 255 | |
| 256 | |
| 257 | def loxi_set_field_action_to_ofp_action(lo): |
| 258 | return pb2.ofp_action( |
| 259 | type=pb2.OFPAT_SET_FIELD, |
| 260 | set_field=pb2.ofp_action_set_field(field=to_grpc(lo.field))) |
| 261 | |
| 262 | |
| 263 | def loxi_pop_vlan_action_to_ofp_action(lo): |
| 264 | return pb2.ofp_action(type=pb2.OFPAT_POP_VLAN) |
| 265 | |
| 266 | |
| 267 | def loxi_push_vlan_action_to_ofp_action(lo): |
| 268 | return pb2.ofp_action( |
| 269 | type=pb2.OFPAT_PUSH_VLAN, |
| 270 | push=pb2.ofp_action_push(ethertype=lo.ethertype)) |
| 271 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 272 | |
| 273 | to_grpc_converters = { |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 274 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 275 | of13.message.flow_add: loxi_flow_mod_to_ofp_flow_mod, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 276 | of13.message.flow_delete: loxi_flow_mod_to_ofp_flow_mod, |
| 277 | of13.message.flow_delete_strict: loxi_flow_mod_to_ofp_flow_mod, |
| 278 | of13.message.flow_modify: loxi_flow_mod_to_ofp_flow_mod, |
| 279 | of13.message.flow_modify_strict: loxi_flow_mod_to_ofp_flow_mod, |
| 280 | |
| 281 | of13.message.group_add: loxi_group_mod_to_ofp_group_mod, |
| 282 | of13.message.group_delete: loxi_group_mod_to_ofp_group_mod, |
| 283 | of13.message.group_modify: loxi_group_mod_to_ofp_group_mod, |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 284 | of13.message.packet_out: loxi_packet_out_to_ofp_packet_out, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 285 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 286 | of13.common.match_v3: loxi_match_v3_to_ofp_match, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 287 | of13.common.bucket: loxi_bucket_to_ofp_bucket, |
| 288 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 289 | of13.oxm.eth_type: loxi_oxm_eth_type_to_ofp_oxm, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 290 | of13.oxm.in_port: loxi_oxm_in_port_to_ofp_oxm, |
| 291 | of13.oxm.ip_proto: loxi_oxm_ip_proto_to_ofp_oxm, |
| 292 | of13.oxm.vlan_vid: loxi_oxm_vlan_vid_to_ofp_oxm, |
| 293 | of13.oxm.vlan_pcp: loxi_oxm_vlan_pcp_to_ofp_oxm, |
| 294 | of13.oxm.ipv4_dst: loxi_oxm_ipv4_dst_to_ofp_oxm, |
| 295 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 296 | of13.instruction.apply_actions: loxi_apply_actions_to_ofp_instruction, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 297 | of13.instruction.goto_table: loxi_goto_table_to_ofp_instruction, |
| 298 | |
| 299 | of13.action.output: loxi_output_action_to_ofp_action, |
| 300 | of13.action.group: loxi_group_action_to_ofp_action, |
| 301 | of13.action.set_field: loxi_set_field_action_to_ofp_action, |
| 302 | of13.action.pop_vlan: loxi_pop_vlan_action_to_ofp_action, |
| 303 | of13.action.push_vlan: loxi_push_vlan_action_to_ofp_action, |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 304 | } |