Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 1 | # |
Zsolt Haraszti | 3eb27a5 | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 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 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 34 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 35 | def pb2dict(pb): |
| 36 | """ |
| 37 | Convert protobuf to a dict of values good for instantiating |
| 38 | loxi objects (or any other objects). We specialize the protobuf_to_dict |
| 39 | library call with our modified decoders. |
| 40 | :param pb: protobuf as loaded into Python |
| 41 | :return: dict of values |
| 42 | """ |
| 43 | return protobuf_to_dict(pb, type_callable_map) |
| 44 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 45 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 46 | def to_loxi(grpc_object): |
| 47 | cls = grpc_object.__class__ |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 48 | converter = to_loxi_converters[cls.__name__] |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 49 | return converter(grpc_object) |
| 50 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 51 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 52 | def to_grpc(loxi_object): |
| 53 | cls = loxi_object.__class__ |
| 54 | converter = to_grpc_converters[cls] |
| 55 | return converter(loxi_object) |
| 56 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 57 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 58 | def ofp_port_to_loxi_port_desc(pb): |
| 59 | kw = pb2dict(pb) |
| 60 | return of13.common.port_desc(**kw) |
| 61 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 62 | |
Zsolt Haraszti | 217a12e | 2016-12-19 16:37:55 -0800 | [diff] [blame] | 63 | def ofp_port_status_to_loxi_port_status(pb): |
| 64 | return of13.message.port_status( |
| 65 | reason=pb.reason, |
| 66 | desc=ofp_port_to_loxi_port_desc(pb.desc) |
| 67 | ) |
| 68 | |
Gamze Abaka | 9d34329 | 2019-02-20 06:35:18 +0000 | [diff] [blame] | 69 | def ofp_flow_removed_to_loxi_flow_removed(pb): |
| 70 | return of13.message.flow_removed( |
| 71 | cookie=pb.cookie, |
| 72 | priority=pb.priority, |
| 73 | reason=pb.reason, |
| 74 | table_id=pb.table_id, |
| 75 | match=make_loxi_match(pb2dict(pb.match)) |
| 76 | ) |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 77 | |
Nicolas Palpacuer | fd7b8b1 | 2018-06-15 13:58:06 -0400 | [diff] [blame] | 78 | def ofp_port_stats_to_loxi_port_stats(pb): |
| 79 | kw = pb2dict(pb) |
| 80 | return of13.port_stats_entry(**kw) |
| 81 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 82 | |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 83 | def ofp_meter_stats_to_loxi_meter_stats(pb): |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 84 | return of13.meter_stats( |
| 85 | meter_id=pb.meter_id, |
| 86 | flow_count=pb.flow_count, |
| 87 | packet_in_count=pb.packet_in_count, |
| 88 | byte_in_count=pb.byte_in_count, |
| 89 | duration_sec=pb.duration_sec, |
| 90 | duration_nsec=pb.duration_nsec, |
| 91 | band_stats=[to_loxi(band_stat) for band_stat in pb.band_stats]) |
| 92 | |
| 93 | |
| 94 | def ofp_meter_band_stats_to_loxi_meter_stats(pb): |
| 95 | return of13.meter_band_stats( |
| 96 | packet_band_count=pb.packet_band_count, |
| 97 | byte_band_count=pb.byte_band_count |
| 98 | ) |
| 99 | |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 100 | |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 101 | def make_loxi_field(oxm_field): |
| 102 | assert oxm_field['oxm_class'] == pb2.OFPXMC_OPENFLOW_BASIC |
| 103 | ofb_field = oxm_field['ofb_field'] |
| 104 | field_type = ofb_field.get('type', 0) |
| 105 | |
| 106 | if field_type == pb2.OFPXMT_OFB_ETH_TYPE: |
| 107 | return ( |
| 108 | of13.oxm.eth_type(value=ofb_field['eth_type'])) |
| 109 | |
| 110 | elif field_type == pb2.OFPXMT_OFB_IN_PORT: |
| 111 | return ( |
| 112 | of13.oxm.in_port(value=ofb_field['port'])) |
| 113 | |
| 114 | elif field_type == pb2.OFPXMT_OFB_IP_PROTO: |
| 115 | return ( |
| 116 | of13.oxm.ip_proto(value=ofb_field['ip_proto'])) |
| 117 | |
| 118 | elif field_type == pb2.OFPXMT_OFB_VLAN_VID: |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 119 | if ofb_field.get('has_mask', 0): |
| 120 | return of13.oxm.vlan_vid_masked(value=ofb_field['vlan_vid'], |
| 121 | value_mask=ofb_field['vlan_vid_mask']) |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 122 | return ( |
| 123 | of13.oxm.vlan_vid(value=ofb_field['vlan_vid'])) |
| 124 | |
| 125 | elif field_type == pb2.OFPXMT_OFB_VLAN_PCP: |
| 126 | return ( |
| 127 | of13.oxm.vlan_pcp(value=ofb_field['vlan_pcp'])) |
| 128 | |
| 129 | elif field_type == pb2.OFPXMT_OFB_IPV4_SRC: |
| 130 | return ( |
| 131 | of13.oxm.ipv4_src(value=ofb_field['ipv4_src'])) |
| 132 | |
| 133 | elif field_type == pb2.OFPXMT_OFB_IPV4_DST: |
| 134 | return ( |
| 135 | of13.oxm.ipv4_dst(value=ofb_field['ipv4_dst'])) |
| 136 | |
| 137 | elif field_type == pb2.OFPXMT_OFB_UDP_SRC: |
| 138 | return ( |
| 139 | of13.oxm.udp_src(value=ofb_field['udp_src'])) |
| 140 | |
| 141 | elif field_type == pb2.OFPXMT_OFB_UDP_DST: |
| 142 | return ( |
| 143 | of13.oxm.udp_dst(value=ofb_field['udp_dst'])) |
| 144 | |
| 145 | elif field_type == pb2.OFPXMT_OFB_METADATA: |
| 146 | return ( |
| 147 | of13.oxm.metadata(value=ofb_field['table_metadata'])) |
| 148 | |
| 149 | else: |
| 150 | raise NotImplementedError( |
| 151 | 'OXM match field for type %s' % field_type) |
| 152 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 153 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 154 | def make_loxi_match(match): |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 155 | assert match.get('type', pb2.OFPMT_STANDARD) == pb2.OFPMT_OXM |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 156 | loxi_match_fields = [] |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 157 | for oxm_field in match.get('oxm_fields', []): |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 158 | loxi_match_fields.append(make_loxi_field(oxm_field)) |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 159 | return of13.match_v3(oxm_list=loxi_match_fields) |
| 160 | |
alshabib | f4fb268 | 2017-01-12 00:32:56 -0600 | [diff] [blame] | 161 | |
| 162 | def make_loxi_action(a): |
| 163 | if type(a) is not dict: |
| 164 | a = pb2dict(a) |
| 165 | |
| 166 | typ = a.get('type', 0) |
| 167 | |
| 168 | if typ == pb2.OFPAT_OUTPUT: |
| 169 | output_kws = a['output'] |
| 170 | return of13.action.output(**output_kws) |
| 171 | |
| 172 | elif typ == pb2.OFPAT_POP_VLAN: |
| 173 | return of13.action.pop_vlan() |
| 174 | |
| 175 | elif typ == pb2.OFPAT_PUSH_VLAN: |
| 176 | push_vlan_kws = a['push'] |
| 177 | return of13.action.push_vlan(**push_vlan_kws) |
| 178 | |
| 179 | elif typ == pb2.OFPAT_SET_FIELD: |
| 180 | loxi_field = make_loxi_field(a['set_field']['field']) |
| 181 | return of13.action.set_field(loxi_field) |
| 182 | |
| 183 | elif typ == pb2.OFPAT_GROUP: |
| 184 | group_kws = a['group'] |
| 185 | return of13.action.group(**group_kws) |
| 186 | |
| 187 | else: |
| 188 | raise NotImplementedError( |
| 189 | 'Action decoder for action OFPAT_* %d' % typ) |
| 190 | |
| 191 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 192 | def ofp_flow_stats_to_loxi_flow_stats(pb): |
| 193 | kw = pb2dict(pb) |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 194 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 195 | def make_loxi_instruction(inst): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 196 | type = inst['type'] |
| 197 | if type == pb2.OFPIT_APPLY_ACTIONS: |
| 198 | return of13.instruction.apply_actions( |
| 199 | actions=[make_loxi_action(a) |
| 200 | for a in inst['actions']['actions']]) |
Gamze Abaka | 61c2e98 | 2018-02-14 11:03:36 +0000 | [diff] [blame] | 201 | elif type == pb2.OFPIT_CLEAR_ACTIONS: |
| 202 | return of13.instruction.clear_actions() |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 203 | elif type == pb2.OFPIT_GOTO_TABLE: |
| 204 | return of13.instruction.goto_table( |
| 205 | table_id=inst['goto_table']['table_id']) |
Koray Kokten | efcdf52 | 2018-12-06 00:16:56 +0300 | [diff] [blame] | 206 | elif type == pb2.OFPIT_WRITE_ACTIONS: |
| 207 | return of13.instruction.write_actions( |
| 208 | actions=[make_loxi_action(a) |
| 209 | for a in inst['actions']['actions']]) |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 210 | elif type == pb2.OFPIT_WRITE_METADATA: |
Saurav Das | aa9247f | 2019-06-07 11:36:55 -0700 | [diff] [blame] | 211 | if 'metadata' in inst['write_metadata']: |
| 212 | return of13.instruction.write_metadata( |
| 213 | metadata=inst['write_metadata']['metadata']) |
| 214 | else: |
| 215 | return of13.instruction.write_metadata(0) |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 216 | elif type == pb2.OFPIT_METER: |
| 217 | return of13.instruction.meter( |
| 218 | meter_id=inst['meter']['meter_id']) |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 219 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 220 | else: |
| 221 | raise NotImplementedError('Instruction type %d' % type) |
| 222 | |
| 223 | kw['match'] = make_loxi_match(kw['match']) |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 224 | # if the flow action is drop, then the instruction is not found in the dict |
| 225 | if 'instructions' in kw: |
| 226 | kw['instructions'] = [make_loxi_instruction(i) for i in kw['instructions']] |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 227 | del kw['id'] |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 228 | return of13.flow_stats_entry(**kw) |
| 229 | |
Zsolt Haraszti | 8925d1f | 2016-12-21 00:45:19 -0800 | [diff] [blame] | 230 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 231 | def ofp_packet_in_to_loxi_packet_in(pb): |
Zsolt Haraszti | 8925d1f | 2016-12-21 00:45:19 -0800 | [diff] [blame] | 232 | packet_in = of13.message.packet_in( |
| 233 | buffer_id=pb.buffer_id, |
| 234 | reason=pb.reason, |
| 235 | table_id=pb.table_id, |
| 236 | cookie=pb.cookie, |
| 237 | match=make_loxi_match(pb2dict(pb.match)), |
| 238 | data=pb.data |
| 239 | ) |
| 240 | return packet_in |
| 241 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 242 | |
alshabib | f4fb268 | 2017-01-12 00:32:56 -0600 | [diff] [blame] | 243 | def ofp_group_desc_to_loxi_group_desc(pb): |
| 244 | return of13.group_desc_stats_entry( |
| 245 | group_type=pb.type, |
| 246 | group_id=pb.group_id, |
| 247 | buckets=[to_loxi(bucket) for bucket in pb.buckets]) |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 248 | |
alshabib | f4fb268 | 2017-01-12 00:32:56 -0600 | [diff] [blame] | 249 | |
| 250 | def ofp_group_stats_to_loxi_group_stats(pb): |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 251 | return of13.group_stats_entry( |
alshabib | f4fb268 | 2017-01-12 00:32:56 -0600 | [diff] [blame] | 252 | group_id=pb.group_id, |
| 253 | ref_count=pb.ref_count, |
| 254 | packet_count=pb.packet_count, |
| 255 | byte_count=pb.byte_count, |
| 256 | duration_sec=pb.duration_sec, |
| 257 | duration_nsec=pb.duration_nsec, |
| 258 | bucket_stats=[to_loxi(bstat) for bstat in pb.bucket_stats]) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 259 | |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 260 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 261 | def ofp_bucket_counter_to_loxy_bucket_counter(pb): |
| 262 | return of13.bucket_counter( |
| 263 | packet_count=pb.packet_count, |
| 264 | byte_count=pb.byte_count) |
| 265 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 266 | |
alshabib | f4fb268 | 2017-01-12 00:32:56 -0600 | [diff] [blame] | 267 | def ofp_bucket_to_loxi_bucket(pb): |
| 268 | return of13.bucket( |
| 269 | weight=pb.weight, |
| 270 | watch_port=pb.watch_port, |
| 271 | watch_group=pb.watch_group, |
| 272 | actions=[to_loxi(action) for action in pb.actions] |
| 273 | ) |
| 274 | |
| 275 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 276 | to_loxi_converters = { |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 277 | 'ofp_port': ofp_port_to_loxi_port_desc, |
| 278 | 'ofp_port_status': ofp_port_status_to_loxi_port_status, |
Gamze Abaka | 9d34329 | 2019-02-20 06:35:18 +0000 | [diff] [blame] | 279 | 'ofp_flow_removed': ofp_flow_removed_to_loxi_flow_removed, |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 280 | 'ofp_flow_stats': ofp_flow_stats_to_loxi_flow_stats, |
| 281 | 'ofp_packet_in': ofp_packet_in_to_loxi_packet_in, |
alshabib | f4fb268 | 2017-01-12 00:32:56 -0600 | [diff] [blame] | 282 | 'ofp_group_stats': ofp_group_stats_to_loxi_group_stats, |
| 283 | 'ofp_group_desc': ofp_group_desc_to_loxi_group_desc, |
| 284 | 'ofp_bucket_counter': ofp_bucket_counter_to_loxy_bucket_counter, |
| 285 | 'ofp_bucket': ofp_bucket_to_loxi_bucket, |
Nicolas Palpacuer | fd7b8b1 | 2018-06-15 13:58:06 -0400 | [diff] [blame] | 286 | 'ofp_action': make_loxi_action, |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 287 | 'ofp_port_stats': ofp_port_stats_to_loxi_port_stats, |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 288 | 'ofp_meter_stats': ofp_meter_stats_to_loxi_meter_stats, |
| 289 | 'ofp_meter_band_stats': ofp_meter_band_stats_to_loxi_meter_stats |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 292 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 293 | def loxi_flow_mod_to_ofp_flow_mod(lo): |
| 294 | return pb2.ofp_flow_mod( |
| 295 | cookie=lo.cookie, |
| 296 | cookie_mask=lo.cookie_mask, |
| 297 | table_id=lo.table_id, |
| 298 | command=lo._command, |
| 299 | idle_timeout=lo.idle_timeout, |
| 300 | hard_timeout=lo.hard_timeout, |
| 301 | priority=lo.priority, |
| 302 | buffer_id=lo.buffer_id, |
| 303 | out_port=lo.out_port, |
| 304 | out_group=lo.out_group, |
| 305 | flags=lo.flags, |
| 306 | match=to_grpc(lo.match), |
| 307 | instructions=[to_grpc(i) for i in lo.instructions]) |
| 308 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 309 | |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 310 | def loxi_meter_mod_to_ofp_meter_mod(lo): |
| 311 | return pb2.ofp_meter_mod( |
| 312 | command=lo.command, |
| 313 | flags=lo.flags, |
| 314 | meter_id=lo.meter_id, |
| 315 | bands=[to_grpc(i) for i in lo.meters]) |
| 316 | |
| 317 | |
| 318 | def loxi_meter_band_drop_to_ofp_meter_band_drop(lo): |
| 319 | return pb2.ofp_meter_band_header( |
| 320 | type=lo.type, |
| 321 | rate=lo.rate, |
| 322 | burst_size=lo.burst_size) |
| 323 | |
| 324 | |
| 325 | def loxi_meter_band_dscp_remark_to_ofp_meter_band_dscp_remark(lo): |
| 326 | return pb2.ofp_meter_band_header( |
| 327 | type=lo.type, |
| 328 | rate=lo.rate, |
| 329 | burst_size=lo.burst_size, |
| 330 | dscp_remark=pb2.ofp_meter_band_dscp_remark(prec_level=lo.prec_level)) |
| 331 | |
| 332 | |
| 333 | def loxi_meter_band_experimenter_to_ofp_meter_band_experimenter(lo): |
| 334 | return pb2.ofp_meter_band_header( |
| 335 | type=lo.type, |
| 336 | rate=lo.rate, |
| 337 | burst_size=lo.burst_size, |
| 338 | experimenter=pb2.ofp_meter_band_experimenter(experimenter=lo.experimenter)) |
| 339 | |
| 340 | |
| 341 | def loxi_meter_multipart_request_to_ofp_meter_multipart_request(lo): |
| 342 | return pb2.ofp_meter_multipart_request( |
| 343 | meter_id=lo.meter_id) |
| 344 | |
| 345 | |
| 346 | def loxi_meter_stats_to_ofp_meter_stats(lo): |
| 347 | return pb2.ofp_meter_stats( |
| 348 | meter_id=lo.meter_id, |
| 349 | flow_count=lo.flow_count, |
| 350 | packet_in_count =lo.packet_in_count, |
| 351 | byte_in_count=lo.byte_in_count, |
| 352 | duration_sec=lo.duration_sec, |
| 353 | duration_nsec=lo.duration_nsec, |
| 354 | band_stats=lo.band_stats) |
| 355 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 356 | |
| 357 | def loxi_group_mod_to_ofp_group_mod(lo): |
| 358 | return pb2.ofp_group_mod( |
| 359 | command=lo.command, |
Zsolt Haraszti | 9125b1a | 2016-10-24 22:54:33 -0700 | [diff] [blame] | 360 | type=lo.group_type, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 361 | group_id=lo.group_id, |
| 362 | buckets=[to_grpc(b) for b in lo.buckets]) |
| 363 | |
| 364 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 365 | def loxi_packet_out_to_ofp_packet_out(lo): |
| 366 | return pb2.ofp_packet_out( |
| 367 | buffer_id=lo.buffer_id, |
| 368 | in_port=lo.in_port, |
| 369 | actions=[to_grpc(a) for a in lo.actions], |
| 370 | data=lo.data) |
| 371 | |
| 372 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 373 | def loxi_match_v3_to_ofp_match(lo): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 374 | return pb2.ofp_match( |
| 375 | type=pb2.OFPMT_OXM, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 376 | oxm_fields=[to_grpc(f) for f in lo.oxm_list]) |
| 377 | |
| 378 | |
| 379 | def loxi_bucket_to_ofp_bucket(lo): |
| 380 | return pb2.ofp_bucket( |
| 381 | weight=lo.weight, |
| 382 | watch_port=lo.watch_port, |
| 383 | watch_group=lo.watch_group, |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 384 | actions=[to_grpc(a) for a in lo.actions]) |
| 385 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 386 | |
| 387 | def loxi_oxm_eth_type_to_ofp_oxm(lo): |
| 388 | return pb2.ofp_oxm_field( |
| 389 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 390 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 391 | type=pb2.OFPXMT_OFB_ETH_TYPE, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 392 | eth_type=lo.value)) |
| 393 | |
| 394 | |
| 395 | def loxi_oxm_in_port_to_ofp_oxm(lo): |
| 396 | return pb2.ofp_oxm_field( |
| 397 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 398 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 399 | type=pb2.OFPXMT_OFB_IN_PORT, |
| 400 | port=lo.value)) |
| 401 | |
| 402 | |
| 403 | def loxi_oxm_ip_proto_to_ofp_oxm(lo): |
| 404 | return pb2.ofp_oxm_field( |
| 405 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 406 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 407 | type=pb2.OFPXMT_OFB_IP_PROTO, |
| 408 | ip_proto=lo.value)) |
| 409 | |
| 410 | |
| 411 | def loxi_oxm_vlan_vid_to_ofp_oxm(lo): |
| 412 | return pb2.ofp_oxm_field( |
| 413 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 414 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 415 | type=pb2.OFPXMT_OFB_VLAN_VID, |
| 416 | vlan_vid=lo.value)) |
| 417 | |
| 418 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 419 | def loxi_oxm_vlan_vid_masked_to_ofp_oxm(lo): |
| 420 | return pb2.ofp_oxm_field( |
| 421 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 422 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 423 | type=pb2.OFPXMT_OFB_VLAN_VID, |
| 424 | has_mask=True, |
| 425 | vlan_vid=lo.value, |
| 426 | vlan_vid_mask=lo.value_mask)) |
| 427 | |
| 428 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 429 | def loxi_oxm_vlan_pcp_to_ofp_oxm(lo): |
| 430 | return pb2.ofp_oxm_field( |
| 431 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 432 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 433 | type=pb2.OFPXMT_OFB_VLAN_PCP, |
| 434 | vlan_pcp=lo.value)) |
| 435 | |
| 436 | |
| 437 | def loxi_oxm_ipv4_dst_to_ofp_oxm(lo): |
| 438 | return pb2.ofp_oxm_field( |
| 439 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 440 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 441 | type=pb2.OFPXMT_OFB_IPV4_DST, |
| 442 | ipv4_dst=lo.value)) |
| 443 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 444 | |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 445 | def loxi_oxm_udp_dst_to_ofp_oxm(lo): |
| 446 | return pb2.ofp_oxm_field( |
| 447 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 448 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 449 | type=pb2.OFPXMT_OFB_UDP_DST, |
| 450 | udp_dst=lo.value)) |
| 451 | |
| 452 | |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 453 | def loxi_oxm_udp_src_to_ofp_oxm(lo): |
| 454 | return pb2.ofp_oxm_field( |
| 455 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 456 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 457 | type=pb2.OFPXMT_OFB_UDP_SRC, |
| 458 | udp_src=lo.value)) |
| 459 | |
| 460 | |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 461 | def loxi_oxm_metadata_to_ofp_oxm(lo): |
| 462 | return pb2.ofp_oxm_field( |
| 463 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 464 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 465 | type=pb2.OFPXMT_OFB_METADATA, |
| 466 | table_metadata=lo.value)) |
| 467 | |
| 468 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 469 | def loxi_apply_actions_to_ofp_instruction(lo): |
| 470 | return pb2.ofp_instruction( |
| 471 | type=pb2.OFPIT_APPLY_ACTIONS, |
| 472 | actions=pb2.ofp_instruction_actions( |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 473 | actions=[to_grpc(a) for a in lo.actions])) |
| 474 | |
Gamze Abaka | 61c2e98 | 2018-02-14 11:03:36 +0000 | [diff] [blame] | 475 | def loxi_clear_actions_to_ofp_instruction(lo): |
| 476 | return pb2.ofp_instruction( |
| 477 | type=pb2.OFPIT_CLEAR_ACTIONS) |
| 478 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 479 | |
| 480 | def loxi_goto_table_to_ofp_instruction(lo): |
| 481 | return pb2.ofp_instruction( |
| 482 | type=pb2.OFPIT_GOTO_TABLE, |
| 483 | goto_table=pb2.ofp_instruction_goto_table(table_id=lo.table_id)) |
| 484 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 485 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 486 | def loxi_write_metadata_to_ofp_instruction(lo): |
| 487 | return pb2.ofp_instruction( |
| 488 | type=pb2.OFPIT_WRITE_METADATA, |
| 489 | write_metadata=pb2.ofp_instruction_write_metadata( |
| 490 | metadata=lo.metadata, |
| 491 | metadata_mask=lo.metadata_mask)) |
| 492 | |
| 493 | |
| 494 | def loxi_meter_to_ofp_instruction(lo): |
| 495 | return pb2.ofp_instruction( |
| 496 | type=pb2.OFPIT_METER, |
| 497 | meter=pb2.ofp_instruction_meter(meter_id=lo.meter_id)) |
| 498 | |
| 499 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 500 | def loxi_output_action_to_ofp_action(lo): |
| 501 | return pb2.ofp_action( |
| 502 | type=pb2.OFPAT_OUTPUT, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 503 | output=pb2.ofp_action_output(port=lo.port, max_len=lo.max_len)) |
| 504 | |
| 505 | |
Koray Kokten | efcdf52 | 2018-12-06 00:16:56 +0300 | [diff] [blame] | 506 | def loxi_write_actions_to_ofp_instruction(lo): |
| 507 | return pb2.ofp_instruction( |
| 508 | type=pb2.OFPIT_WRITE_ACTIONS, |
| 509 | actions=pb2.ofp_instruction_actions( |
| 510 | actions=[to_grpc(a) for a in lo.actions])) |
| 511 | |
| 512 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 513 | def loxi_group_action_to_ofp_action(lo): |
| 514 | return pb2.ofp_action( |
| 515 | type=pb2.OFPAT_GROUP, |
| 516 | group=pb2.ofp_action_group(group_id=lo.group_id)) |
| 517 | |
| 518 | |
| 519 | def loxi_set_field_action_to_ofp_action(lo): |
| 520 | return pb2.ofp_action( |
| 521 | type=pb2.OFPAT_SET_FIELD, |
| 522 | set_field=pb2.ofp_action_set_field(field=to_grpc(lo.field))) |
| 523 | |
| 524 | |
| 525 | def loxi_pop_vlan_action_to_ofp_action(lo): |
| 526 | return pb2.ofp_action(type=pb2.OFPAT_POP_VLAN) |
| 527 | |
| 528 | |
| 529 | def loxi_push_vlan_action_to_ofp_action(lo): |
| 530 | return pb2.ofp_action( |
| 531 | type=pb2.OFPAT_PUSH_VLAN, |
| 532 | push=pb2.ofp_action_push(ethertype=lo.ethertype)) |
| 533 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 534 | |
| 535 | to_grpc_converters = { |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 536 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 537 | of13.message.flow_add: loxi_flow_mod_to_ofp_flow_mod, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 538 | of13.message.flow_delete: loxi_flow_mod_to_ofp_flow_mod, |
| 539 | of13.message.flow_delete_strict: loxi_flow_mod_to_ofp_flow_mod, |
| 540 | of13.message.flow_modify: loxi_flow_mod_to_ofp_flow_mod, |
| 541 | of13.message.flow_modify_strict: loxi_flow_mod_to_ofp_flow_mod, |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 542 | of13.message.meter_mod: loxi_meter_mod_to_ofp_meter_mod, |
| 543 | of13.message.meter_stats_request: loxi_meter_stats_to_ofp_meter_stats, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 544 | |
| 545 | of13.message.group_add: loxi_group_mod_to_ofp_group_mod, |
| 546 | of13.message.group_delete: loxi_group_mod_to_ofp_group_mod, |
| 547 | of13.message.group_modify: loxi_group_mod_to_ofp_group_mod, |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 548 | of13.message.packet_out: loxi_packet_out_to_ofp_packet_out, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 549 | |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 550 | of13.meter_band.drop: loxi_meter_band_drop_to_ofp_meter_band_drop, |
| 551 | of13.meter_band.dscp_remark: loxi_meter_band_dscp_remark_to_ofp_meter_band_dscp_remark, |
| 552 | of13.meter_band.experimenter: loxi_meter_band_experimenter_to_ofp_meter_band_experimenter, |
| 553 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 554 | of13.common.match_v3: loxi_match_v3_to_ofp_match, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 555 | of13.common.bucket: loxi_bucket_to_ofp_bucket, |
| 556 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 557 | of13.oxm.eth_type: loxi_oxm_eth_type_to_ofp_oxm, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 558 | of13.oxm.in_port: loxi_oxm_in_port_to_ofp_oxm, |
| 559 | of13.oxm.ip_proto: loxi_oxm_ip_proto_to_ofp_oxm, |
| 560 | of13.oxm.vlan_vid: loxi_oxm_vlan_vid_to_ofp_oxm, |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 561 | of13.oxm.vlan_vid_masked: loxi_oxm_vlan_vid_masked_to_ofp_oxm, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 562 | of13.oxm.vlan_pcp: loxi_oxm_vlan_pcp_to_ofp_oxm, |
| 563 | of13.oxm.ipv4_dst: loxi_oxm_ipv4_dst_to_ofp_oxm, |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 564 | of13.oxm.udp_src: loxi_oxm_udp_src_to_ofp_oxm, |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 565 | of13.oxm.udp_dst: loxi_oxm_udp_dst_to_ofp_oxm, |
| 566 | of13.oxm.metadata: loxi_oxm_metadata_to_ofp_oxm, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 567 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 568 | of13.instruction.apply_actions: loxi_apply_actions_to_ofp_instruction, |
Gamze Abaka | 61c2e98 | 2018-02-14 11:03:36 +0000 | [diff] [blame] | 569 | of13.instruction.clear_actions: loxi_clear_actions_to_ofp_instruction, |
Koray Kokten | efcdf52 | 2018-12-06 00:16:56 +0300 | [diff] [blame] | 570 | of13.instruction.write_actions: loxi_write_actions_to_ofp_instruction, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 571 | of13.instruction.goto_table: loxi_goto_table_to_ofp_instruction, |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 572 | of13.instruction.write_metadata: loxi_write_metadata_to_ofp_instruction, |
| 573 | of13.instruction.meter: loxi_meter_to_ofp_instruction, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 574 | |
| 575 | of13.action.output: loxi_output_action_to_ofp_action, |
| 576 | of13.action.group: loxi_group_action_to_ofp_action, |
| 577 | of13.action.set_field: loxi_set_field_action_to_ofp_action, |
| 578 | of13.action.pop_vlan: loxi_pop_vlan_action_to_ofp_action, |
| 579 | of13.action.push_vlan: loxi_push_vlan_action_to_ofp_action, |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 580 | } |