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