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: |
| 211 | return of13.instruction.write_metadata( |
| 212 | metadata=inst['write_metadata']['metadata']) |
| 213 | elif type == pb2.OFPIT_METER: |
| 214 | return of13.instruction.meter( |
| 215 | meter_id=inst['meter']['meter_id']) |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 216 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 217 | else: |
| 218 | raise NotImplementedError('Instruction type %d' % type) |
| 219 | |
| 220 | kw['match'] = make_loxi_match(kw['match']) |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 221 | # if the flow action is drop, then the instruction is not found in the dict |
| 222 | if 'instructions' in kw: |
| 223 | kw['instructions'] = [make_loxi_instruction(i) for i in kw['instructions']] |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 224 | del kw['id'] |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 225 | return of13.flow_stats_entry(**kw) |
| 226 | |
Zsolt Haraszti | 8925d1f | 2016-12-21 00:45:19 -0800 | [diff] [blame] | 227 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 228 | def ofp_packet_in_to_loxi_packet_in(pb): |
Zsolt Haraszti | 8925d1f | 2016-12-21 00:45:19 -0800 | [diff] [blame] | 229 | packet_in = of13.message.packet_in( |
| 230 | buffer_id=pb.buffer_id, |
| 231 | reason=pb.reason, |
| 232 | table_id=pb.table_id, |
| 233 | cookie=pb.cookie, |
| 234 | match=make_loxi_match(pb2dict(pb.match)), |
| 235 | data=pb.data |
| 236 | ) |
| 237 | return packet_in |
| 238 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 239 | |
alshabib | f4fb268 | 2017-01-12 00:32:56 -0600 | [diff] [blame] | 240 | def ofp_group_desc_to_loxi_group_desc(pb): |
| 241 | return of13.group_desc_stats_entry( |
| 242 | group_type=pb.type, |
| 243 | group_id=pb.group_id, |
| 244 | buckets=[to_loxi(bucket) for bucket in pb.buckets]) |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 245 | |
alshabib | f4fb268 | 2017-01-12 00:32:56 -0600 | [diff] [blame] | 246 | |
| 247 | def ofp_group_stats_to_loxi_group_stats(pb): |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 248 | return of13.group_stats_entry( |
alshabib | f4fb268 | 2017-01-12 00:32:56 -0600 | [diff] [blame] | 249 | group_id=pb.group_id, |
| 250 | ref_count=pb.ref_count, |
| 251 | packet_count=pb.packet_count, |
| 252 | byte_count=pb.byte_count, |
| 253 | duration_sec=pb.duration_sec, |
| 254 | duration_nsec=pb.duration_nsec, |
| 255 | bucket_stats=[to_loxi(bstat) for bstat in pb.bucket_stats]) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 256 | |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 257 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 258 | def ofp_bucket_counter_to_loxy_bucket_counter(pb): |
| 259 | return of13.bucket_counter( |
| 260 | packet_count=pb.packet_count, |
| 261 | byte_count=pb.byte_count) |
| 262 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 263 | |
alshabib | f4fb268 | 2017-01-12 00:32:56 -0600 | [diff] [blame] | 264 | def ofp_bucket_to_loxi_bucket(pb): |
| 265 | return of13.bucket( |
| 266 | weight=pb.weight, |
| 267 | watch_port=pb.watch_port, |
| 268 | watch_group=pb.watch_group, |
| 269 | actions=[to_loxi(action) for action in pb.actions] |
| 270 | ) |
| 271 | |
| 272 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 273 | to_loxi_converters = { |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 274 | 'ofp_port': ofp_port_to_loxi_port_desc, |
| 275 | 'ofp_port_status': ofp_port_status_to_loxi_port_status, |
Gamze Abaka | 9d34329 | 2019-02-20 06:35:18 +0000 | [diff] [blame] | 276 | 'ofp_flow_removed': ofp_flow_removed_to_loxi_flow_removed, |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 277 | 'ofp_flow_stats': ofp_flow_stats_to_loxi_flow_stats, |
| 278 | 'ofp_packet_in': ofp_packet_in_to_loxi_packet_in, |
alshabib | f4fb268 | 2017-01-12 00:32:56 -0600 | [diff] [blame] | 279 | 'ofp_group_stats': ofp_group_stats_to_loxi_group_stats, |
| 280 | 'ofp_group_desc': ofp_group_desc_to_loxi_group_desc, |
| 281 | 'ofp_bucket_counter': ofp_bucket_counter_to_loxy_bucket_counter, |
| 282 | 'ofp_bucket': ofp_bucket_to_loxi_bucket, |
Nicolas Palpacuer | fd7b8b1 | 2018-06-15 13:58:06 -0400 | [diff] [blame] | 283 | 'ofp_action': make_loxi_action, |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 284 | 'ofp_port_stats': ofp_port_stats_to_loxi_port_stats, |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 285 | 'ofp_meter_stats': ofp_meter_stats_to_loxi_meter_stats, |
| 286 | 'ofp_meter_band_stats': ofp_meter_band_stats_to_loxi_meter_stats |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 289 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 290 | def loxi_flow_mod_to_ofp_flow_mod(lo): |
| 291 | return pb2.ofp_flow_mod( |
| 292 | cookie=lo.cookie, |
| 293 | cookie_mask=lo.cookie_mask, |
| 294 | table_id=lo.table_id, |
| 295 | command=lo._command, |
| 296 | idle_timeout=lo.idle_timeout, |
| 297 | hard_timeout=lo.hard_timeout, |
| 298 | priority=lo.priority, |
| 299 | buffer_id=lo.buffer_id, |
| 300 | out_port=lo.out_port, |
| 301 | out_group=lo.out_group, |
| 302 | flags=lo.flags, |
| 303 | match=to_grpc(lo.match), |
| 304 | instructions=[to_grpc(i) for i in lo.instructions]) |
| 305 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 306 | |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 307 | def loxi_meter_mod_to_ofp_meter_mod(lo): |
| 308 | return pb2.ofp_meter_mod( |
| 309 | command=lo.command, |
| 310 | flags=lo.flags, |
| 311 | meter_id=lo.meter_id, |
| 312 | bands=[to_grpc(i) for i in lo.meters]) |
| 313 | |
| 314 | |
| 315 | def loxi_meter_band_drop_to_ofp_meter_band_drop(lo): |
| 316 | return pb2.ofp_meter_band_header( |
| 317 | type=lo.type, |
| 318 | rate=lo.rate, |
| 319 | burst_size=lo.burst_size) |
| 320 | |
| 321 | |
| 322 | def loxi_meter_band_dscp_remark_to_ofp_meter_band_dscp_remark(lo): |
| 323 | return pb2.ofp_meter_band_header( |
| 324 | type=lo.type, |
| 325 | rate=lo.rate, |
| 326 | burst_size=lo.burst_size, |
| 327 | dscp_remark=pb2.ofp_meter_band_dscp_remark(prec_level=lo.prec_level)) |
| 328 | |
| 329 | |
| 330 | def loxi_meter_band_experimenter_to_ofp_meter_band_experimenter(lo): |
| 331 | return pb2.ofp_meter_band_header( |
| 332 | type=lo.type, |
| 333 | rate=lo.rate, |
| 334 | burst_size=lo.burst_size, |
| 335 | experimenter=pb2.ofp_meter_band_experimenter(experimenter=lo.experimenter)) |
| 336 | |
| 337 | |
| 338 | def loxi_meter_multipart_request_to_ofp_meter_multipart_request(lo): |
| 339 | return pb2.ofp_meter_multipart_request( |
| 340 | meter_id=lo.meter_id) |
| 341 | |
| 342 | |
| 343 | def loxi_meter_stats_to_ofp_meter_stats(lo): |
| 344 | return pb2.ofp_meter_stats( |
| 345 | meter_id=lo.meter_id, |
| 346 | flow_count=lo.flow_count, |
| 347 | packet_in_count =lo.packet_in_count, |
| 348 | byte_in_count=lo.byte_in_count, |
| 349 | duration_sec=lo.duration_sec, |
| 350 | duration_nsec=lo.duration_nsec, |
| 351 | band_stats=lo.band_stats) |
| 352 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 353 | |
| 354 | def loxi_group_mod_to_ofp_group_mod(lo): |
| 355 | return pb2.ofp_group_mod( |
| 356 | command=lo.command, |
Zsolt Haraszti | 9125b1a | 2016-10-24 22:54:33 -0700 | [diff] [blame] | 357 | type=lo.group_type, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 358 | group_id=lo.group_id, |
| 359 | buckets=[to_grpc(b) for b in lo.buckets]) |
| 360 | |
| 361 | |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 362 | def loxi_packet_out_to_ofp_packet_out(lo): |
| 363 | return pb2.ofp_packet_out( |
| 364 | buffer_id=lo.buffer_id, |
| 365 | in_port=lo.in_port, |
| 366 | actions=[to_grpc(a) for a in lo.actions], |
| 367 | data=lo.data) |
| 368 | |
| 369 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 370 | def loxi_match_v3_to_ofp_match(lo): |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 371 | return pb2.ofp_match( |
| 372 | type=pb2.OFPMT_OXM, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 373 | oxm_fields=[to_grpc(f) for f in lo.oxm_list]) |
| 374 | |
| 375 | |
| 376 | def loxi_bucket_to_ofp_bucket(lo): |
| 377 | return pb2.ofp_bucket( |
| 378 | weight=lo.weight, |
| 379 | watch_port=lo.watch_port, |
| 380 | watch_group=lo.watch_group, |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 381 | actions=[to_grpc(a) for a in lo.actions]) |
| 382 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 383 | |
| 384 | def loxi_oxm_eth_type_to_ofp_oxm(lo): |
| 385 | return pb2.ofp_oxm_field( |
| 386 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 387 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 388 | type=pb2.OFPXMT_OFB_ETH_TYPE, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 389 | eth_type=lo.value)) |
| 390 | |
| 391 | |
| 392 | def loxi_oxm_in_port_to_ofp_oxm(lo): |
| 393 | return pb2.ofp_oxm_field( |
| 394 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 395 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 396 | type=pb2.OFPXMT_OFB_IN_PORT, |
| 397 | port=lo.value)) |
| 398 | |
| 399 | |
| 400 | def loxi_oxm_ip_proto_to_ofp_oxm(lo): |
| 401 | return pb2.ofp_oxm_field( |
| 402 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 403 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 404 | type=pb2.OFPXMT_OFB_IP_PROTO, |
| 405 | ip_proto=lo.value)) |
| 406 | |
| 407 | |
| 408 | def loxi_oxm_vlan_vid_to_ofp_oxm(lo): |
| 409 | return pb2.ofp_oxm_field( |
| 410 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 411 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 412 | type=pb2.OFPXMT_OFB_VLAN_VID, |
| 413 | vlan_vid=lo.value)) |
| 414 | |
| 415 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 416 | def loxi_oxm_vlan_vid_masked_to_ofp_oxm(lo): |
| 417 | return pb2.ofp_oxm_field( |
| 418 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 419 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 420 | type=pb2.OFPXMT_OFB_VLAN_VID, |
| 421 | has_mask=True, |
| 422 | vlan_vid=lo.value, |
| 423 | vlan_vid_mask=lo.value_mask)) |
| 424 | |
| 425 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 426 | def loxi_oxm_vlan_pcp_to_ofp_oxm(lo): |
| 427 | return pb2.ofp_oxm_field( |
| 428 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 429 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 430 | type=pb2.OFPXMT_OFB_VLAN_PCP, |
| 431 | vlan_pcp=lo.value)) |
| 432 | |
| 433 | |
| 434 | def loxi_oxm_ipv4_dst_to_ofp_oxm(lo): |
| 435 | return pb2.ofp_oxm_field( |
| 436 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 437 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 438 | type=pb2.OFPXMT_OFB_IPV4_DST, |
| 439 | ipv4_dst=lo.value)) |
| 440 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 441 | |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 442 | def loxi_oxm_udp_dst_to_ofp_oxm(lo): |
| 443 | return pb2.ofp_oxm_field( |
| 444 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 445 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 446 | type=pb2.OFPXMT_OFB_UDP_DST, |
| 447 | udp_dst=lo.value)) |
| 448 | |
| 449 | |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 450 | def loxi_oxm_udp_src_to_ofp_oxm(lo): |
| 451 | return pb2.ofp_oxm_field( |
| 452 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 453 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 454 | type=pb2.OFPXMT_OFB_UDP_SRC, |
| 455 | udp_src=lo.value)) |
| 456 | |
| 457 | |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 458 | def loxi_oxm_metadata_to_ofp_oxm(lo): |
| 459 | return pb2.ofp_oxm_field( |
| 460 | oxm_class=pb2.OFPXMC_OPENFLOW_BASIC, |
| 461 | ofb_field=pb2.ofp_oxm_ofb_field( |
| 462 | type=pb2.OFPXMT_OFB_METADATA, |
| 463 | table_metadata=lo.value)) |
| 464 | |
| 465 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 466 | def loxi_apply_actions_to_ofp_instruction(lo): |
| 467 | return pb2.ofp_instruction( |
| 468 | type=pb2.OFPIT_APPLY_ACTIONS, |
| 469 | actions=pb2.ofp_instruction_actions( |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 470 | actions=[to_grpc(a) for a in lo.actions])) |
| 471 | |
Gamze Abaka | 61c2e98 | 2018-02-14 11:03:36 +0000 | [diff] [blame] | 472 | def loxi_clear_actions_to_ofp_instruction(lo): |
| 473 | return pb2.ofp_instruction( |
| 474 | type=pb2.OFPIT_CLEAR_ACTIONS) |
| 475 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 476 | |
| 477 | def loxi_goto_table_to_ofp_instruction(lo): |
| 478 | return pb2.ofp_instruction( |
| 479 | type=pb2.OFPIT_GOTO_TABLE, |
| 480 | goto_table=pb2.ofp_instruction_goto_table(table_id=lo.table_id)) |
| 481 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 482 | |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 483 | def loxi_write_metadata_to_ofp_instruction(lo): |
| 484 | return pb2.ofp_instruction( |
| 485 | type=pb2.OFPIT_WRITE_METADATA, |
| 486 | write_metadata=pb2.ofp_instruction_write_metadata( |
| 487 | metadata=lo.metadata, |
| 488 | metadata_mask=lo.metadata_mask)) |
| 489 | |
| 490 | |
| 491 | def loxi_meter_to_ofp_instruction(lo): |
| 492 | return pb2.ofp_instruction( |
| 493 | type=pb2.OFPIT_METER, |
| 494 | meter=pb2.ofp_instruction_meter(meter_id=lo.meter_id)) |
| 495 | |
| 496 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 497 | def loxi_output_action_to_ofp_action(lo): |
| 498 | return pb2.ofp_action( |
| 499 | type=pb2.OFPAT_OUTPUT, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 500 | output=pb2.ofp_action_output(port=lo.port, max_len=lo.max_len)) |
| 501 | |
| 502 | |
Koray Kokten | efcdf52 | 2018-12-06 00:16:56 +0300 | [diff] [blame] | 503 | def loxi_write_actions_to_ofp_instruction(lo): |
| 504 | return pb2.ofp_instruction( |
| 505 | type=pb2.OFPIT_WRITE_ACTIONS, |
| 506 | actions=pb2.ofp_instruction_actions( |
| 507 | actions=[to_grpc(a) for a in lo.actions])) |
| 508 | |
| 509 | |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 510 | def loxi_group_action_to_ofp_action(lo): |
| 511 | return pb2.ofp_action( |
| 512 | type=pb2.OFPAT_GROUP, |
| 513 | group=pb2.ofp_action_group(group_id=lo.group_id)) |
| 514 | |
| 515 | |
| 516 | def loxi_set_field_action_to_ofp_action(lo): |
| 517 | return pb2.ofp_action( |
| 518 | type=pb2.OFPAT_SET_FIELD, |
| 519 | set_field=pb2.ofp_action_set_field(field=to_grpc(lo.field))) |
| 520 | |
| 521 | |
| 522 | def loxi_pop_vlan_action_to_ofp_action(lo): |
| 523 | return pb2.ofp_action(type=pb2.OFPAT_POP_VLAN) |
| 524 | |
| 525 | |
| 526 | def loxi_push_vlan_action_to_ofp_action(lo): |
| 527 | return pb2.ofp_action( |
| 528 | type=pb2.OFPAT_PUSH_VLAN, |
| 529 | push=pb2.ofp_action_push(ethertype=lo.ethertype)) |
| 530 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 531 | |
| 532 | to_grpc_converters = { |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 533 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 534 | of13.message.flow_add: loxi_flow_mod_to_ofp_flow_mod, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 535 | of13.message.flow_delete: loxi_flow_mod_to_ofp_flow_mod, |
| 536 | of13.message.flow_delete_strict: loxi_flow_mod_to_ofp_flow_mod, |
| 537 | of13.message.flow_modify: loxi_flow_mod_to_ofp_flow_mod, |
| 538 | of13.message.flow_modify_strict: loxi_flow_mod_to_ofp_flow_mod, |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 539 | of13.message.meter_mod: loxi_meter_mod_to_ofp_meter_mod, |
| 540 | of13.message.meter_stats_request: loxi_meter_stats_to_ofp_meter_stats, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 541 | |
| 542 | of13.message.group_add: loxi_group_mod_to_ofp_group_mod, |
| 543 | of13.message.group_delete: loxi_group_mod_to_ofp_group_mod, |
| 544 | of13.message.group_modify: loxi_group_mod_to_ofp_group_mod, |
Zsolt Haraszti | cd22adc | 2016-10-25 00:13:06 -0700 | [diff] [blame] | 545 | of13.message.packet_out: loxi_packet_out_to_ofp_packet_out, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 546 | |
Koray Kokten | 8592a23 | 2018-08-27 07:41:14 +0000 | [diff] [blame] | 547 | of13.meter_band.drop: loxi_meter_band_drop_to_ofp_meter_band_drop, |
| 548 | of13.meter_band.dscp_remark: loxi_meter_band_dscp_remark_to_ofp_meter_band_dscp_remark, |
| 549 | of13.meter_band.experimenter: loxi_meter_band_experimenter_to_ofp_meter_band_experimenter, |
| 550 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 551 | of13.common.match_v3: loxi_match_v3_to_ofp_match, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 552 | of13.common.bucket: loxi_bucket_to_ofp_bucket, |
| 553 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 554 | of13.oxm.eth_type: loxi_oxm_eth_type_to_ofp_oxm, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 555 | of13.oxm.in_port: loxi_oxm_in_port_to_ofp_oxm, |
| 556 | of13.oxm.ip_proto: loxi_oxm_ip_proto_to_ofp_oxm, |
| 557 | of13.oxm.vlan_vid: loxi_oxm_vlan_vid_to_ofp_oxm, |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 558 | 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] | 559 | of13.oxm.vlan_pcp: loxi_oxm_vlan_pcp_to_ofp_oxm, |
| 560 | of13.oxm.ipv4_dst: loxi_oxm_ipv4_dst_to_ofp_oxm, |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame] | 561 | of13.oxm.udp_src: loxi_oxm_udp_src_to_ofp_oxm, |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 562 | of13.oxm.udp_dst: loxi_oxm_udp_dst_to_ofp_oxm, |
| 563 | of13.oxm.metadata: loxi_oxm_metadata_to_ofp_oxm, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 564 | |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 565 | of13.instruction.apply_actions: loxi_apply_actions_to_ofp_instruction, |
Gamze Abaka | 61c2e98 | 2018-02-14 11:03:36 +0000 | [diff] [blame] | 566 | of13.instruction.clear_actions: loxi_clear_actions_to_ofp_instruction, |
Koray Kokten | efcdf52 | 2018-12-06 00:16:56 +0300 | [diff] [blame] | 567 | of13.instruction.write_actions: loxi_write_actions_to_ofp_instruction, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 568 | of13.instruction.goto_table: loxi_goto_table_to_ofp_instruction, |
Gamze Abaka | 53cc0a2 | 2019-01-31 12:06:11 +0000 | [diff] [blame] | 569 | of13.instruction.write_metadata: loxi_write_metadata_to_ofp_instruction, |
| 570 | of13.instruction.meter: loxi_meter_to_ofp_instruction, |
Zsolt Haraszti | 8a77438 | 2016-10-24 18:25:54 -0700 | [diff] [blame] | 571 | |
| 572 | of13.action.output: loxi_output_action_to_ofp_action, |
| 573 | of13.action.group: loxi_group_action_to_ofp_action, |
| 574 | of13.action.set_field: loxi_set_field_action_to_ofp_action, |
| 575 | of13.action.pop_vlan: loxi_pop_vlan_action_to_ofp_action, |
| 576 | of13.action.push_vlan: loxi_push_vlan_action_to_ofp_action, |
Zsolt Haraszti | 023ea7c | 2016-10-16 19:30:34 -0700 | [diff] [blame] | 577 | } |