William Kurkian | fefd464 | 2019-02-07 15:30:03 -0500 | [diff] [blame^] | 1 | # |
| 2 | # Copyright 2017 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | """ |
| 18 | A mix-in class implementing flow decomposition |
| 19 | """ |
| 20 | from collections import OrderedDict |
| 21 | from copy import copy, deepcopy |
| 22 | from hashlib import md5 |
| 23 | |
| 24 | import structlog |
| 25 | |
| 26 | from voltha.protos import third_party |
| 27 | from voltha.protos import openflow_13_pb2 as ofp |
| 28 | from common.tech_profile import tech_profile |
| 29 | _ = third_party |
| 30 | log = structlog.get_logger() |
| 31 | |
| 32 | |
| 33 | # aliases |
| 34 | ofb_field = ofp.ofp_oxm_ofb_field |
| 35 | action = ofp.ofp_action |
| 36 | |
| 37 | # OFPAT_* shortcuts |
| 38 | OUTPUT = ofp.OFPAT_OUTPUT |
| 39 | COPY_TTL_OUT = ofp.OFPAT_COPY_TTL_OUT |
| 40 | COPY_TTL_IN = ofp.OFPAT_COPY_TTL_IN |
| 41 | SET_MPLS_TTL = ofp.OFPAT_SET_MPLS_TTL |
| 42 | DEC_MPLS_TTL = ofp.OFPAT_DEC_MPLS_TTL |
| 43 | PUSH_VLAN = ofp.OFPAT_PUSH_VLAN |
| 44 | POP_VLAN = ofp.OFPAT_POP_VLAN |
| 45 | PUSH_MPLS = ofp.OFPAT_PUSH_MPLS |
| 46 | POP_MPLS = ofp.OFPAT_POP_MPLS |
| 47 | SET_QUEUE = ofp.OFPAT_SET_QUEUE |
| 48 | GROUP = ofp.OFPAT_GROUP |
| 49 | SET_NW_TTL = ofp.OFPAT_SET_NW_TTL |
| 50 | NW_TTL = ofp.OFPAT_DEC_NW_TTL |
| 51 | SET_FIELD = ofp.OFPAT_SET_FIELD |
| 52 | PUSH_PBB = ofp.OFPAT_PUSH_PBB |
| 53 | POP_PBB = ofp.OFPAT_POP_PBB |
| 54 | EXPERIMENTER = ofp.OFPAT_EXPERIMENTER |
| 55 | |
| 56 | # OFPXMT_OFB_* shortcuts (incomplete) |
| 57 | IN_PORT = ofp.OFPXMT_OFB_IN_PORT |
| 58 | IN_PHY_PORT = ofp.OFPXMT_OFB_IN_PHY_PORT |
| 59 | METADATA = ofp.OFPXMT_OFB_METADATA |
| 60 | ETH_DST = ofp.OFPXMT_OFB_ETH_DST |
| 61 | ETH_SRC = ofp.OFPXMT_OFB_ETH_SRC |
| 62 | ETH_TYPE = ofp.OFPXMT_OFB_ETH_TYPE |
| 63 | VLAN_VID = ofp.OFPXMT_OFB_VLAN_VID |
| 64 | VLAN_PCP = ofp.OFPXMT_OFB_VLAN_PCP |
| 65 | IP_DSCP = ofp.OFPXMT_OFB_IP_DSCP |
| 66 | IP_ECN = ofp.OFPXMT_OFB_IP_ECN |
| 67 | IP_PROTO = ofp.OFPXMT_OFB_IP_PROTO |
| 68 | IPV4_SRC = ofp.OFPXMT_OFB_IPV4_SRC |
| 69 | IPV4_DST = ofp.OFPXMT_OFB_IPV4_DST |
| 70 | TCP_SRC = ofp.OFPXMT_OFB_TCP_SRC |
| 71 | TCP_DST = ofp.OFPXMT_OFB_TCP_DST |
| 72 | UDP_SRC = ofp.OFPXMT_OFB_UDP_SRC |
| 73 | UDP_DST = ofp.OFPXMT_OFB_UDP_DST |
| 74 | SCTP_SRC = ofp.OFPXMT_OFB_SCTP_SRC |
| 75 | SCTP_DST = ofp.OFPXMT_OFB_SCTP_DST |
| 76 | ICMPV4_TYPE = ofp.OFPXMT_OFB_ICMPV4_TYPE |
| 77 | ICMPV4_CODE = ofp.OFPXMT_OFB_ICMPV4_CODE |
| 78 | ARP_OP = ofp.OFPXMT_OFB_ARP_OP |
| 79 | ARP_SPA = ofp.OFPXMT_OFB_ARP_SPA |
| 80 | ARP_TPA = ofp.OFPXMT_OFB_ARP_TPA |
| 81 | ARP_SHA = ofp.OFPXMT_OFB_ARP_SHA |
| 82 | ARP_THA = ofp.OFPXMT_OFB_ARP_THA |
| 83 | IPV6_SRC = ofp.OFPXMT_OFB_IPV6_SRC |
| 84 | IPV6_DST = ofp.OFPXMT_OFB_IPV6_DST |
| 85 | IPV6_FLABEL = ofp.OFPXMT_OFB_IPV6_FLABEL |
| 86 | ICMPV6_TYPE = ofp.OFPXMT_OFB_ICMPV6_TYPE |
| 87 | ICMPV6_CODE = ofp.OFPXMT_OFB_ICMPV6_CODE |
| 88 | IPV6_ND_TARGET = ofp.OFPXMT_OFB_IPV6_ND_TARGET |
| 89 | OFB_IPV6_ND_SLL = ofp.OFPXMT_OFB_IPV6_ND_SLL |
| 90 | IPV6_ND_TLL = ofp.OFPXMT_OFB_IPV6_ND_TLL |
| 91 | MPLS_LABEL = ofp.OFPXMT_OFB_MPLS_LABEL |
| 92 | MPLS_TC = ofp.OFPXMT_OFB_MPLS_TC |
| 93 | MPLS_BOS = ofp.OFPXMT_OFB_MPLS_BOS |
| 94 | PBB_ISID = ofp.OFPXMT_OFB_PBB_ISID |
| 95 | TUNNEL_ID = ofp.OFPXMT_OFB_TUNNEL_ID |
| 96 | IPV6_EXTHDR = ofp.OFPXMT_OFB_IPV6_EXTHDR |
| 97 | |
| 98 | # ofp_action_* shortcuts |
| 99 | |
| 100 | def output(port, max_len=ofp.OFPCML_MAX): |
| 101 | return action( |
| 102 | type=OUTPUT, |
| 103 | output=ofp.ofp_action_output(port=port, max_len=max_len) |
| 104 | ) |
| 105 | |
| 106 | def mpls_ttl(ttl): |
| 107 | return action( |
| 108 | type=SET_MPLS_TTL, |
| 109 | mpls_ttl=ofp.ofp_action_mpls_ttl(mpls_ttl=ttl) |
| 110 | ) |
| 111 | |
| 112 | def push_vlan(eth_type): |
| 113 | return action( |
| 114 | type=PUSH_VLAN, |
| 115 | push=ofp.ofp_action_push(ethertype=eth_type) |
| 116 | ) |
| 117 | |
| 118 | def pop_vlan(): |
| 119 | return action( |
| 120 | type=POP_VLAN |
| 121 | ) |
| 122 | |
| 123 | def pop_mpls(eth_type): |
| 124 | return action( |
| 125 | type=POP_MPLS, |
| 126 | pop_mpls=ofp.ofp_action_pop_mpls(ethertype=eth_type) |
| 127 | ) |
| 128 | |
| 129 | def group(group_id): |
| 130 | return action( |
| 131 | type=GROUP, |
| 132 | group=ofp.ofp_action_group(group_id=group_id) |
| 133 | ) |
| 134 | |
| 135 | def nw_ttl(nw_ttl): |
| 136 | return action( |
| 137 | type=NW_TTL, |
| 138 | nw_ttl=ofp.ofp_action_nw_ttl(nw_ttl=nw_ttl) |
| 139 | ) |
| 140 | |
| 141 | def set_field(field): |
| 142 | return action( |
| 143 | type=SET_FIELD, |
| 144 | set_field=ofp.ofp_action_set_field( |
| 145 | field=ofp.ofp_oxm_field( |
| 146 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 147 | ofb_field=field)) |
| 148 | ) |
| 149 | |
| 150 | def experimenter(experimenter, data): |
| 151 | return action( |
| 152 | type=EXPERIMENTER, |
| 153 | experimenter=ofp.ofp_action_experimenter( |
| 154 | experimenter=experimenter, data=data) |
| 155 | ) |
| 156 | |
| 157 | |
| 158 | # ofb_field generators (incomplete set) |
| 159 | |
| 160 | def in_port(_in_port): |
| 161 | return ofb_field(type=IN_PORT, port=_in_port) |
| 162 | |
| 163 | def in_phy_port(_in_phy_port): |
| 164 | return ofb_field(type=IN_PHY_PORT, port=_in_phy_port) |
| 165 | |
| 166 | def metadata(_table_metadata): |
| 167 | return ofb_field(type=METADATA, table_metadata=_table_metadata) |
| 168 | |
| 169 | def eth_dst(_eth_dst): |
| 170 | return ofb_field(type=ETH_DST, table_metadata=_eth_dst) |
| 171 | |
| 172 | def eth_src(_eth_src): |
| 173 | return ofb_field(type=ETH_SRC, table_metadata=_eth_src) |
| 174 | |
| 175 | def eth_type(_eth_type): |
| 176 | return ofb_field(type=ETH_TYPE, eth_type=_eth_type) |
| 177 | |
| 178 | def vlan_vid(_vlan_vid): |
| 179 | return ofb_field(type=VLAN_VID, vlan_vid=_vlan_vid) |
| 180 | |
| 181 | def vlan_pcp(_vlan_pcp): |
| 182 | return ofb_field(type=VLAN_PCP, vlan_pcp=_vlan_pcp) |
| 183 | |
| 184 | def ip_dscp(_ip_dscp): |
| 185 | return ofb_field(type=IP_DSCP, ip_dscp=_ip_dscp) |
| 186 | |
| 187 | def ip_ecn(_ip_ecn): |
| 188 | return ofb_field(type=IP_ECN, ip_ecn=_ip_ecn) |
| 189 | |
| 190 | def ip_proto(_ip_proto): |
| 191 | return ofb_field(type=IP_PROTO, ip_proto=_ip_proto) |
| 192 | |
| 193 | def ipv4_src(_ipv4_src): |
| 194 | return ofb_field(type=IPV4_SRC, ipv4_src=_ipv4_src) |
| 195 | |
| 196 | def ipv4_dst(_ipv4_dst): |
| 197 | return ofb_field(type=IPV4_DST, ipv4_dst=_ipv4_dst) |
| 198 | |
| 199 | def tcp_src(_tcp_src): |
| 200 | return ofb_field(type=TCP_SRC, tcp_src=_tcp_src) |
| 201 | |
| 202 | def tcp_dst(_tcp_dst): |
| 203 | return ofb_field(type=TCP_DST, tcp_dst=_tcp_dst) |
| 204 | |
| 205 | def udp_src(_udp_src): |
| 206 | return ofb_field(type=UDP_SRC, udp_src=_udp_src) |
| 207 | |
| 208 | def udp_dst(_udp_dst): |
| 209 | return ofb_field(type=UDP_DST, udp_dst=_udp_dst) |
| 210 | |
| 211 | def sctp_src(_sctp_src): |
| 212 | return ofb_field(type=SCTP_SRC, sctp_src=_sctp_src) |
| 213 | |
| 214 | def sctp_dst(_sctp_dst): |
| 215 | return ofb_field(type=SCTP_DST, sctp_dst=_sctp_dst) |
| 216 | |
| 217 | def icmpv4_type(_icmpv4_type): |
| 218 | return ofb_field(type=ICMPV4_TYPE, icmpv4_type=_icmpv4_type) |
| 219 | |
| 220 | def icmpv4_code(_icmpv4_code): |
| 221 | return ofb_field(type=ICMPV4_CODE, icmpv4_code=_icmpv4_code) |
| 222 | |
| 223 | def arp_op(_arp_op): |
| 224 | return ofb_field(type=ARP_OP, arp_op=_arp_op) |
| 225 | |
| 226 | def arp_spa(_arp_spa): |
| 227 | return ofb_field(type=ARP_SPA, arp_spa=_arp_spa) |
| 228 | |
| 229 | def arp_tpa(_arp_tpa): |
| 230 | return ofb_field(type=ARP_TPA, arp_tpa=_arp_tpa) |
| 231 | |
| 232 | def arp_sha(_arp_sha): |
| 233 | return ofb_field(type=ARP_SHA, arp_sha=_arp_sha) |
| 234 | |
| 235 | def arp_tha(_arp_tha): |
| 236 | return ofb_field(type=ARP_THA, arp_tha=_arp_tha) |
| 237 | |
| 238 | def ipv6_src(_ipv6_src): |
| 239 | return ofb_field(type=IPV6_SRC, arp_tha=_ipv6_src) |
| 240 | |
| 241 | def ipv6_dst(_ipv6_dst): |
| 242 | return ofb_field(type=IPV6_DST, arp_tha=_ipv6_dst) |
| 243 | |
| 244 | def ipv6_flabel(_ipv6_flabel): |
| 245 | return ofb_field(type=IPV6_FLABEL, arp_tha=_ipv6_flabel) |
| 246 | |
| 247 | def ipmpv6_type(_icmpv6_type): |
| 248 | return ofb_field(type=ICMPV6_TYPE, arp_tha=_icmpv6_type) |
| 249 | |
| 250 | def icmpv6_code(_icmpv6_code): |
| 251 | return ofb_field(type=ICMPV6_CODE, arp_tha=_icmpv6_code) |
| 252 | |
| 253 | def ipv6_nd_target(_ipv6_nd_target): |
| 254 | return ofb_field(type=IPV6_ND_TARGET, arp_tha=_ipv6_nd_target) |
| 255 | |
| 256 | def ofb_ipv6_nd_sll(_ofb_ipv6_nd_sll): |
| 257 | return ofb_field(type=OFB_IPV6_ND_SLL, arp_tha=_ofb_ipv6_nd_sll) |
| 258 | |
| 259 | def ipv6_nd_tll(_ipv6_nd_tll): |
| 260 | return ofb_field(type=IPV6_ND_TLL, arp_tha=_ipv6_nd_tll) |
| 261 | |
| 262 | def mpls_label(_mpls_label): |
| 263 | return ofb_field(type=MPLS_LABEL, arp_tha=_mpls_label) |
| 264 | |
| 265 | def mpls_tc(_mpls_tc): |
| 266 | return ofb_field(type=MPLS_TC, arp_tha=_mpls_tc) |
| 267 | |
| 268 | def mpls_bos(_mpls_bos): |
| 269 | return ofb_field(type=MPLS_BOS, arp_tha=_mpls_bos) |
| 270 | |
| 271 | def pbb_isid(_pbb_isid): |
| 272 | return ofb_field(type=PBB_ISID, arp_tha=_pbb_isid) |
| 273 | |
| 274 | def tunnel_id(_tunnel_id): |
| 275 | return ofb_field(type=TUNNEL_ID, arp_tha=_tunnel_id) |
| 276 | |
| 277 | def ipv6_exthdr(_ipv6_exthdr): |
| 278 | return ofb_field(type=IPV6_EXTHDR, arp_tha=_ipv6_exthdr) |
| 279 | |
| 280 | |
| 281 | # frequently used extractors: |
| 282 | |
| 283 | def get_actions(flow): |
| 284 | """Extract list of ofp_action objects from flow spec object""" |
| 285 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 286 | # we have the following hard assumptions for now |
| 287 | actions = [] |
| 288 | for instruction in flow.instructions: |
| 289 | if instruction.type == ofp.OFPIT_APPLY_ACTIONS or instruction.type == ofp.OFPIT_WRITE_ACTIONS: |
| 290 | actions.extend(instruction.actions.actions) |
| 291 | return actions |
| 292 | |
| 293 | |
| 294 | def get_ofb_fields(flow): |
| 295 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 296 | assert flow.match.type == ofp.OFPMT_OXM |
| 297 | ofb_fields = [] |
| 298 | for field in flow.match.oxm_fields: |
| 299 | assert field.oxm_class == ofp.OFPXMC_OPENFLOW_BASIC |
| 300 | ofb_fields.append(field.ofb_field) |
| 301 | return ofb_fields |
| 302 | |
| 303 | def get_out_port(flow): |
| 304 | for action in get_actions(flow): |
| 305 | if action.type == OUTPUT: |
| 306 | return action.output.port |
| 307 | return None |
| 308 | |
| 309 | def get_in_port(flow): |
| 310 | for field in get_ofb_fields(flow): |
| 311 | if field.type == IN_PORT: |
| 312 | return field.port |
| 313 | return None |
| 314 | |
| 315 | def get_goto_table_id(flow): |
| 316 | for instruction in flow.instructions: |
| 317 | if instruction.type == ofp.OFPIT_GOTO_TABLE: |
| 318 | return instruction.goto_table.table_id |
| 319 | return None |
| 320 | |
| 321 | def get_metadata(flow): |
| 322 | ''' legacy get method (only want lower 32 bits ''' |
| 323 | for field in get_ofb_fields(flow): |
| 324 | if field.type == METADATA: |
| 325 | return field.table_metadata & 0xffffffff |
| 326 | return None |
| 327 | |
| 328 | def get_metadata_64_bit(flow): |
| 329 | for field in get_ofb_fields(flow): |
| 330 | if field.type == METADATA: |
| 331 | return field.table_metadata |
| 332 | return None |
| 333 | |
| 334 | |
| 335 | def get_port_number_from_metadata(flow): |
| 336 | """ |
| 337 | The port number (UNI on ONU) is in the lower 32-bits of metadata and |
| 338 | the inner_tag is in the upper 32-bits |
| 339 | |
| 340 | This is set in the ONOS OltPipeline as a metadata field |
| 341 | """ |
| 342 | md = get_metadata_64_bit(flow) |
| 343 | |
| 344 | if md is None: |
| 345 | return None |
| 346 | |
| 347 | if md <= 0xffffffff: |
| 348 | log.warn('onos-upgrade-suggested', |
| 349 | netadata=md, |
| 350 | message='Legacy MetaData detected form OltPipeline') |
| 351 | return md |
| 352 | |
| 353 | return md & 0xffffffff |
| 354 | |
| 355 | |
| 356 | def get_inner_tag_from_metadata(flow): |
| 357 | """ |
| 358 | The port number (UNI on ONU) is in the lower 32-bits of metadata and |
| 359 | the inner_tag is in the upper 32-bits |
| 360 | |
| 361 | This is set in the ONOS OltPipeline as a metadata field |
| 362 | """ |
| 363 | md = get_metadata_64_bit(flow) |
| 364 | |
| 365 | if md is None: |
| 366 | return None |
| 367 | |
| 368 | if md <= 0xffffffff: |
| 369 | log.warn('onos-upgrade-suggested', |
| 370 | netadata=md, |
| 371 | message='Legacy MetaData detected form OltPipeline') |
| 372 | return md |
| 373 | |
| 374 | return (md >> 32) & 0xffffffff |
| 375 | |
| 376 | |
| 377 | # test and extract next table and group information |
| 378 | def has_next_table(flow): |
| 379 | return get_goto_table_id(flow) is not None |
| 380 | |
| 381 | def get_group(flow): |
| 382 | for action in get_actions(flow): |
| 383 | if action.type == GROUP: |
| 384 | return action.group.group_id |
| 385 | return None |
| 386 | |
| 387 | def get_meter_ids_from_flow(flow): |
| 388 | meter_ids = list() |
| 389 | for instruction in flow.instructions: |
| 390 | if instruction.type == ofp.OFPIT_METER: |
| 391 | meter_ids.append(instruction.meter.meter_id) |
| 392 | return meter_ids |
| 393 | |
| 394 | def has_group(flow): |
| 395 | return get_group(flow) is not None |
| 396 | |
| 397 | def mk_oxm_fields(match_fields): |
| 398 | oxm_fields = [ |
| 399 | ofp.ofp_oxm_field( |
| 400 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 401 | ofb_field=field |
| 402 | ) for field in match_fields |
| 403 | ] |
| 404 | |
| 405 | return oxm_fields |
| 406 | |
| 407 | def mk_instructions_from_actions(actions): |
| 408 | instructions_action = ofp.ofp_instruction_actions() |
| 409 | instructions_action.actions.extend(actions) |
| 410 | instruction = ofp.ofp_instruction(type=ofp.OFPIT_APPLY_ACTIONS, |
| 411 | actions=instructions_action) |
| 412 | return [instruction] |
| 413 | |
| 414 | def mk_simple_flow_mod(match_fields, actions, command=ofp.OFPFC_ADD, |
| 415 | next_table_id=None, meters=None, **kw): |
| 416 | """ |
| 417 | Convenience function to generare ofp_flow_mod message with OXM BASIC match |
| 418 | composed from the match_fields, and single APPLY_ACTIONS instruction with |
| 419 | a list if ofp_action objects. |
| 420 | :param match_fields: list(ofp_oxm_ofb_field) |
| 421 | :param actions: list(ofp_action) |
| 422 | :param command: one of OFPFC_* |
| 423 | :param kw: additional keyword-based params to ofp_flow_mod |
| 424 | :return: initialized ofp_flow_mod object |
| 425 | """ |
| 426 | instructions = [ |
| 427 | ofp.ofp_instruction( |
| 428 | type=ofp.OFPIT_APPLY_ACTIONS, |
| 429 | actions=ofp.ofp_instruction_actions(actions=actions) |
| 430 | ) |
| 431 | ] |
| 432 | |
| 433 | if meters is not None: |
| 434 | for meter_id in meters: |
| 435 | instructions.append(ofp.ofp_instruction( |
| 436 | type=ofp.OFPIT_METER, |
| 437 | meter=ofp.ofp_instruction_meter(meter_id=meter_id) |
| 438 | )) |
| 439 | |
| 440 | if next_table_id is not None: |
| 441 | instructions.append(ofp.ofp_instruction( |
| 442 | type=ofp.OFPIT_GOTO_TABLE, |
| 443 | goto_table=ofp.ofp_instruction_goto_table(table_id=next_table_id) |
| 444 | )) |
| 445 | |
| 446 | return ofp.ofp_flow_mod( |
| 447 | command=command, |
| 448 | match=ofp.ofp_match( |
| 449 | type=ofp.OFPMT_OXM, |
| 450 | oxm_fields=[ |
| 451 | ofp.ofp_oxm_field( |
| 452 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 453 | ofb_field=field |
| 454 | ) for field in match_fields |
| 455 | ] |
| 456 | ), |
| 457 | instructions=instructions, |
| 458 | **kw |
| 459 | ) |
| 460 | |
| 461 | |
| 462 | def mk_multicast_group_mod(group_id, buckets, command=ofp.OFPGC_ADD): |
| 463 | group = ofp.ofp_group_mod( |
| 464 | command=command, |
| 465 | type=ofp.OFPGT_ALL, |
| 466 | group_id=group_id, |
| 467 | buckets=buckets |
| 468 | ) |
| 469 | return group |
| 470 | |
| 471 | |
| 472 | def hash_flow_stats(flow): |
| 473 | """ |
| 474 | Return unique 64-bit integer hash for flow covering the following |
| 475 | attributes: 'table_id', 'priority', 'flags', 'cookie', 'match', '_instruction_string' |
| 476 | """ |
| 477 | _instruction_string = "" |
| 478 | for _instruction in flow.instructions: |
| 479 | _instruction_string += _instruction.SerializeToString() |
| 480 | |
| 481 | hex = md5('{},{},{},{},{},{}'.format( |
| 482 | flow.table_id, |
| 483 | flow.priority, |
| 484 | flow.flags, |
| 485 | flow.cookie, |
| 486 | flow.match.SerializeToString(), |
| 487 | _instruction_string |
| 488 | )).hexdigest() |
| 489 | return int(hex[:16], 16) |
| 490 | |
| 491 | |
| 492 | def flow_stats_entry_from_flow_mod_message(mod): |
| 493 | flow = ofp.ofp_flow_stats( |
| 494 | table_id=mod.table_id, |
| 495 | priority=mod.priority, |
| 496 | idle_timeout=mod.idle_timeout, |
| 497 | hard_timeout=mod.hard_timeout, |
| 498 | flags=mod.flags, |
| 499 | cookie=mod.cookie, |
| 500 | match=mod.match, |
| 501 | instructions=mod.instructions |
| 502 | ) |
| 503 | flow.id = hash_flow_stats(flow) |
| 504 | return flow |
| 505 | |
| 506 | |
| 507 | def group_entry_from_group_mod(mod): |
| 508 | group = ofp.ofp_group_entry( |
| 509 | desc=ofp.ofp_group_desc( |
| 510 | type=mod.type, |
| 511 | group_id=mod.group_id, |
| 512 | buckets=mod.buckets |
| 513 | ), |
| 514 | stats=ofp.ofp_group_stats( |
| 515 | group_id=mod.group_id |
| 516 | # TODO do we need to instantiate bucket bins? |
| 517 | ) |
| 518 | ) |
| 519 | return group |
| 520 | |
| 521 | |
| 522 | def mk_flow_stat(**kw): |
| 523 | return flow_stats_entry_from_flow_mod_message(mk_simple_flow_mod(**kw)) |
| 524 | |
| 525 | |
| 526 | def mk_group_stat(**kw): |
| 527 | return group_entry_from_group_mod(mk_multicast_group_mod(**kw)) |
| 528 | |
| 529 | class RouteHop(object): |
| 530 | __slots__ = ('_device', '_ingress_port', '_egress_port') |
| 531 | def __init__(self, device, ingress_port, egress_port): |
| 532 | self._device = device |
| 533 | self._ingress_port = ingress_port |
| 534 | self._egress_port = egress_port |
| 535 | @property |
| 536 | def device(self): return self._device |
| 537 | @property |
| 538 | def ingress_port(self): return self._ingress_port |
| 539 | @property |
| 540 | def egress_port(self): return self._egress_port |
| 541 | def __eq__(self, other): |
| 542 | return ( |
| 543 | self._device == other._device and |
| 544 | self._ingress_port == other._ingress_port and |
| 545 | self._egress_port == other._egress_port) |
| 546 | def __ne__(self, other): |
| 547 | return not self.__eq__(other) |
| 548 | def __str__(self): |
| 549 | return 'RouteHop device_id {}, ingress_port {}, egress_port {}'.format( |
| 550 | self._device.id, self._ingress_port, self._egress_port) |
| 551 | |
| 552 | class FlowDecomposer(object): |
| 553 | |
| 554 | def __init__(self, *args, **kw): |
| 555 | self.logical_device_id = 'this shall be overwritten in derived class' |
| 556 | super(FlowDecomposer, self).__init__(*args, **kw) |
| 557 | |
| 558 | # ~~~~~~~~~~~~~~~~~~~~ methods exposed *to* derived class ~~~~~~~~~~~~~~~~~ |
| 559 | |
| 560 | def decompose_rules(self, flows, groups): |
| 561 | """ |
| 562 | Generate per-device flows and flow-groups from the flows and groups |
| 563 | defined on a logical device |
| 564 | :param flows: logical device flows |
| 565 | :param groups: logical device flow groups |
| 566 | :return: dict(device_id -> |
| 567 | (OrderedDict-of-device-flows, OrderedDict-of-device-flow-groups)) |
| 568 | """ |
| 569 | |
| 570 | device_rules = deepcopy(self.get_all_default_rules()) |
| 571 | group_map = dict((g.desc.group_id, g) for g in groups) |
| 572 | |
| 573 | for flow in flows: |
| 574 | for device_id, (_flows, _groups) \ |
| 575 | in self.decompose_flow(flow, group_map).iteritems(): |
| 576 | fl_lst, gr_lst = device_rules.setdefault( |
| 577 | device_id, (OrderedDict(), OrderedDict())) |
| 578 | for _flow in _flows: |
| 579 | if _flow.id not in fl_lst: |
| 580 | fl_lst[_flow.id] = _flow |
| 581 | for _group in _groups: |
| 582 | if _group.group_id not in gr_lst: |
| 583 | gr_lst[_group.group_id] = _group |
| 584 | return device_rules |
| 585 | |
| 586 | def decompose_flow(self, flow, group_map): |
| 587 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 588 | |
| 589 | #################################################################### |
| 590 | # |
| 591 | # limited, heuristics based implementation |
| 592 | # needs to be replaced, see https://jira.opencord.org/browse/CORD-841 |
| 593 | # |
| 594 | #################################################################### |
| 595 | |
| 596 | in_port_no = get_in_port(flow) |
| 597 | out_port_no = get_out_port(flow) # may be None |
| 598 | |
| 599 | device_rules = {} # accumulator |
| 600 | |
| 601 | route = self.get_route(in_port_no, out_port_no) |
| 602 | if route is None: |
| 603 | log.error('no-route', in_port_no=in_port_no, |
| 604 | out_port_no=out_port_no, comment='deleting flow') |
| 605 | self.flow_delete(flow) |
| 606 | return device_rules |
| 607 | |
| 608 | assert len(route) == 2 |
| 609 | ingress_hop, egress_hop = route |
| 610 | |
| 611 | def is_downstream(): |
| 612 | return ingress_hop.device.root |
| 613 | |
| 614 | def is_upstream(): |
| 615 | return not is_downstream() |
| 616 | |
| 617 | def update_devices_rules(flow, curr_device_rules, meter_ids=None, table_id=None): |
| 618 | actions = [action.type for action in get_actions(flow)] |
| 619 | if len(actions) == 1 and OUTPUT in actions: |
| 620 | # Transparent ONU and OLT case (No-L2-Modification flow) |
| 621 | child_device_flow_lst, _ = curr_device_rules.setdefault( |
| 622 | ingress_hop.device.id, ([], [])) |
| 623 | parent_device_flow_lst, _ = curr_device_rules.setdefault( |
| 624 | egress_hop.device.id, ([], [])) |
| 625 | |
| 626 | child_device_flow_lst.append(mk_flow_stat( |
| 627 | priority=flow.priority, |
| 628 | cookie=flow.cookie, |
| 629 | match_fields=[ |
| 630 | in_port(ingress_hop.ingress_port.port_no) |
| 631 | ] + [ |
| 632 | field for field in get_ofb_fields(flow) |
| 633 | if field.type not in (IN_PORT,) |
| 634 | ], |
| 635 | actions=[ |
| 636 | output(ingress_hop.egress_port.port_no) |
| 637 | ] |
| 638 | )) |
| 639 | |
| 640 | parent_device_flow_lst.append(mk_flow_stat( |
| 641 | priority=flow.priority, |
| 642 | cookie=flow.cookie, |
| 643 | match_fields=[ |
| 644 | in_port(egress_hop.ingress_port.port_no), |
| 645 | ] + [ |
| 646 | field for field in get_ofb_fields(flow) |
| 647 | if field.type not in (IN_PORT,) |
| 648 | ], |
| 649 | actions=[ |
| 650 | output(egress_hop.egress_port.port_no) |
| 651 | ], |
| 652 | table_id=table_id, |
| 653 | meters=meter_ids |
| 654 | )) |
| 655 | |
| 656 | else: |
| 657 | fl_lst, _ = curr_device_rules.setdefault( |
| 658 | egress_hop.device.id, ([], [])) |
| 659 | fl_lst.append(mk_flow_stat( |
| 660 | priority=flow.priority, |
| 661 | cookie=flow.cookie, |
| 662 | match_fields=[ |
| 663 | in_port(egress_hop.ingress_port.port_no) |
| 664 | ] + [ |
| 665 | field for field in get_ofb_fields(flow) |
| 666 | if field.type not in (IN_PORT,) |
| 667 | ], |
| 668 | actions=[ |
| 669 | action for action in get_actions(flow) |
| 670 | if action.type != OUTPUT |
| 671 | ] + [ |
| 672 | output(egress_hop.egress_port.port_no) |
| 673 | ], |
| 674 | table_id=table_id, |
| 675 | meters=meter_ids |
| 676 | )) |
| 677 | |
| 678 | if out_port_no is not None and \ |
| 679 | (out_port_no & 0x7fffffff) == ofp.OFPP_CONTROLLER: |
| 680 | |
| 681 | # UPSTREAM CONTROLLER-BOUND FLOW |
| 682 | |
| 683 | # we assume that the ingress device is already pushing a |
| 684 | # customer-specific vlan (c-vid), based on its default flow |
| 685 | # rules so there is nothing else to do on the ONU |
| 686 | |
| 687 | # on the olt, we need to push a new tag and set it to 4000 |
| 688 | # which for now represents in-bound channel to the controller |
| 689 | # (via Voltha) |
| 690 | # TODO make the 4000 configurable |
| 691 | fl_lst, _ = device_rules.setdefault( |
| 692 | egress_hop.device.id, ([], [])) |
| 693 | |
| 694 | log.info('trap-flow', in_port_no=in_port_no, |
| 695 | nni=self._nni_logical_port_no) |
| 696 | |
| 697 | if in_port_no == self._nni_logical_port_no: |
| 698 | log.debug('trap-nni') |
| 699 | # Trap flow for NNI port |
| 700 | fl_lst.append(mk_flow_stat( |
| 701 | priority=flow.priority, |
| 702 | cookie=flow.cookie, |
| 703 | match_fields=[ |
| 704 | in_port(egress_hop.egress_port.port_no) |
| 705 | ] + [ |
| 706 | field for field in get_ofb_fields(flow) |
| 707 | if field.type not in (IN_PORT,) |
| 708 | ], |
| 709 | actions=[ |
| 710 | action for action in get_actions(flow) |
| 711 | ] |
| 712 | )) |
| 713 | |
| 714 | else: |
| 715 | log.debug('trap-uni') |
| 716 | # Trap flow for UNI port |
| 717 | |
| 718 | # in_port_no is None for wildcard input case, do not include |
| 719 | # upstream port for 4000 flow in input |
| 720 | if in_port_no is None: |
| 721 | in_ports = self.get_wildcard_input_ports(exclude_port= |
| 722 | egress_hop.egress_port.port_no) |
| 723 | else: |
| 724 | in_ports = [in_port_no] |
| 725 | |
| 726 | for input_port in in_ports: |
| 727 | fl_lst.append(mk_flow_stat( # Upstream flow |
| 728 | priority=flow.priority, |
| 729 | cookie=flow.cookie, |
| 730 | match_fields=[ |
| 731 | in_port(egress_hop.ingress_port.port_no), |
| 732 | vlan_vid(ofp.OFPVID_PRESENT | input_port) |
| 733 | ] + [ |
| 734 | field for field in get_ofb_fields(flow) |
| 735 | if field.type not in (IN_PORT, VLAN_VID) |
| 736 | ], |
| 737 | actions=[ |
| 738 | push_vlan(0x8100), |
| 739 | set_field(vlan_vid(ofp.OFPVID_PRESENT | 4000)), |
| 740 | output(egress_hop.egress_port.port_no)] |
| 741 | )) |
| 742 | fl_lst.append(mk_flow_stat( # Downstream flow |
| 743 | priority=flow.priority, |
| 744 | match_fields=[ |
| 745 | in_port(egress_hop.egress_port.port_no), |
| 746 | vlan_vid(ofp.OFPVID_PRESENT | 4000), |
| 747 | vlan_pcp(0), |
| 748 | metadata(input_port) |
| 749 | ], |
| 750 | actions=[ |
| 751 | pop_vlan(), |
| 752 | output(egress_hop.ingress_port.port_no)] |
| 753 | )) |
| 754 | else: |
| 755 | # NOT A CONTROLLER-BOUND FLOW |
| 756 | if is_upstream(): |
| 757 | |
| 758 | # We assume that anything that is upstream needs to get Q-in-Q |
| 759 | # treatment and that this is expressed via two flow rules, |
| 760 | # the first using the goto-statement. We also assume that the |
| 761 | # inner tag is applied at the ONU, while the outer tag is |
| 762 | # applied at the OLT |
| 763 | next_table_id = get_goto_table_id(flow) |
| 764 | if next_table_id is not None and next_table_id < tech_profile.DEFAULT_TECH_PROFILE_TABLE_ID: |
| 765 | assert out_port_no is None |
| 766 | fl_lst, _ = device_rules.setdefault( |
| 767 | ingress_hop.device.id, ([], [])) |
| 768 | fl_lst.append(mk_flow_stat( |
| 769 | priority=flow.priority, |
| 770 | cookie=flow.cookie, |
| 771 | match_fields=[ |
| 772 | in_port(ingress_hop.ingress_port.port_no) |
| 773 | ] + [ |
| 774 | field for field in get_ofb_fields(flow) |
| 775 | if field.type not in (IN_PORT,) |
| 776 | ], |
| 777 | actions=[ |
| 778 | action for action in get_actions(flow) |
| 779 | ] + [ |
| 780 | output(ingress_hop.egress_port.port_no) |
| 781 | ] |
| 782 | )) |
| 783 | |
| 784 | elif next_table_id is not None and next_table_id >= tech_profile.DEFAULT_TECH_PROFILE_TABLE_ID: |
| 785 | assert out_port_no is not None |
| 786 | meter_ids = get_meter_ids_from_flow(flow) |
| 787 | update_devices_rules(flow, device_rules, meter_ids, next_table_id) |
| 788 | else: |
| 789 | update_devices_rules(flow, device_rules) |
| 790 | |
| 791 | else: # downstream |
| 792 | next_table_id = get_goto_table_id(flow) |
| 793 | if next_table_id is not None and next_table_id < tech_profile.DEFAULT_TECH_PROFILE_TABLE_ID: |
| 794 | assert out_port_no is None |
| 795 | |
| 796 | if get_metadata(flow) is not None: |
| 797 | log.debug('creating-metadata-flow', flow=flow) |
| 798 | # For downstream flows with dual-tags, recalculate route. |
| 799 | port_number = get_port_number_from_metadata(flow) |
| 800 | |
| 801 | if port_number is not None: |
| 802 | route = self.get_route(in_port_no, port_number) |
| 803 | if route is None: |
| 804 | log.error('no-route-double-tag', in_port_no=in_port_no, |
| 805 | out_port_no=port_number, comment='deleting flow', |
| 806 | metadata=get_metadata_64_bit(flow)) |
| 807 | self.flow_delete(flow) |
| 808 | return device_rules |
| 809 | assert len(route) == 2 |
| 810 | ingress_hop, egress_hop = route |
| 811 | |
| 812 | inner_tag = get_inner_tag_from_metadata(flow) |
| 813 | |
| 814 | if inner_tag is None: |
| 815 | log.error('no-inner-tag-double-tag', in_port_no=in_port_no, |
| 816 | out_port_no=port_number, comment='deleting flow', |
| 817 | metadata=get_metadata_64_bit(flow)) |
| 818 | self.flow_delete(flow) |
| 819 | return device_rules |
| 820 | |
| 821 | fl_lst, _ = device_rules.setdefault( |
| 822 | ingress_hop.device.id, ([], [])) |
| 823 | fl_lst.append(mk_flow_stat( |
| 824 | priority=flow.priority, |
| 825 | cookie=flow.cookie, |
| 826 | match_fields=[ |
| 827 | in_port(ingress_hop.ingress_port.port_no), |
| 828 | metadata(inner_tag) |
| 829 | ] + [ |
| 830 | field for field in get_ofb_fields(flow) |
| 831 | if field.type not in (IN_PORT, METADATA) |
| 832 | ], |
| 833 | actions=[ |
| 834 | action for action in get_actions(flow) |
| 835 | ] + [ |
| 836 | output(ingress_hop.egress_port.port_no) |
| 837 | ] |
| 838 | )) |
| 839 | else: |
| 840 | log.debug('creating-standard-flow', flow=flow) |
| 841 | fl_lst, _ = device_rules.setdefault( |
| 842 | ingress_hop.device.id, ([], [])) |
| 843 | fl_lst.append(mk_flow_stat( |
| 844 | priority=flow.priority, |
| 845 | cookie=flow.cookie, |
| 846 | match_fields=[ |
| 847 | in_port(ingress_hop.ingress_port.port_no) |
| 848 | ] + [ |
| 849 | field for field in get_ofb_fields(flow) |
| 850 | if field.type not in (IN_PORT,) |
| 851 | ], |
| 852 | actions=[ |
| 853 | action for action in get_actions(flow) |
| 854 | ] + [ |
| 855 | output(ingress_hop.egress_port.port_no) |
| 856 | ] |
| 857 | )) |
| 858 | |
| 859 | elif out_port_no is not None: # unicast case |
| 860 | |
| 861 | actions = [action.type for action in get_actions(flow)] |
| 862 | # Transparent ONU and OLT case (No-L2-Modification flow) |
| 863 | if len(actions) == 1 and OUTPUT in actions: |
| 864 | parent_device_flow_lst, _ = device_rules.setdefault( |
| 865 | ingress_hop.device.id, ([], [])) |
| 866 | child_device_flow_lst, _ = device_rules.setdefault( |
| 867 | egress_hop.device.id, ([], [])) |
| 868 | |
| 869 | parent_device_flow_lst.append(mk_flow_stat( |
| 870 | priority=flow.priority, |
| 871 | cookie=flow.cookie, |
| 872 | match_fields=[ |
| 873 | in_port(ingress_hop.ingress_port.port_no) |
| 874 | ] + [ |
| 875 | field for field in get_ofb_fields(flow) |
| 876 | if field.type not in (IN_PORT,) |
| 877 | ], |
| 878 | actions=[ |
| 879 | output(ingress_hop.egress_port.port_no) |
| 880 | ] |
| 881 | )) |
| 882 | |
| 883 | child_device_flow_lst.append(mk_flow_stat( |
| 884 | priority = flow.priority, |
| 885 | cookie=flow.cookie, |
| 886 | match_fields = [ |
| 887 | in_port(egress_hop.ingress_port.port_no), |
| 888 | ] + [ |
| 889 | field for field in get_ofb_fields(flow) |
| 890 | if field.type not in (IN_PORT, ) |
| 891 | ], |
| 892 | actions=[ |
| 893 | output(egress_hop.egress_port.port_no) |
| 894 | ] |
| 895 | )) |
| 896 | else: |
| 897 | fl_lst, _ = device_rules.setdefault( |
| 898 | egress_hop.device.id, ([], [])) |
| 899 | fl_lst.append(mk_flow_stat( |
| 900 | priority=flow.priority, |
| 901 | cookie=flow.cookie, |
| 902 | match_fields=[ |
| 903 | in_port(egress_hop.ingress_port.port_no) |
| 904 | ] + [ |
| 905 | field for field in get_ofb_fields(flow) |
| 906 | if field.type not in (IN_PORT,) |
| 907 | ], |
| 908 | actions=[ |
| 909 | action for action in get_actions(flow) |
| 910 | if action.type not in (OUTPUT,) |
| 911 | ] + [ |
| 912 | output(egress_hop.egress_port.port_no) |
| 913 | ], |
| 914 | #table_id=flow.table_id, |
| 915 | #meters=None if len(get_meter_ids_from_flow(flow)) == 0 else get_meter_ids_from_flow(flow) |
| 916 | )) |
| 917 | else: |
| 918 | grp_id = get_group(flow) |
| 919 | |
| 920 | if grp_id is not None: # multicast case |
| 921 | fl_lst_olt, _ = device_rules.setdefault( |
| 922 | ingress_hop.device.id, ([], [])) |
| 923 | # having no group yet is the same as having a group with |
| 924 | # no buckets |
| 925 | group = group_map.get(grp_id, ofp.ofp_group_entry()) |
| 926 | |
| 927 | for bucket in group.desc.buckets: |
| 928 | found_pop_vlan = False |
| 929 | other_actions = [] |
| 930 | for action in bucket.actions: |
| 931 | if action.type == POP_VLAN: |
| 932 | found_pop_vlan = True |
| 933 | elif action.type == OUTPUT: |
| 934 | out_port_no = action.output.port |
| 935 | else: |
| 936 | other_actions.append(action) |
| 937 | # re-run route request to determine egress device and |
| 938 | # ports |
| 939 | route2 = self.get_route(in_port_no, out_port_no) |
| 940 | if not route2 or len(route2) != 2: |
| 941 | log.error('mc-no-route', in_port_no=in_port_no, |
| 942 | out_port_no=out_port_no, route2=route2, |
| 943 | comment='deleting flow') |
| 944 | self.flow_delete(flow) |
| 945 | continue |
| 946 | |
| 947 | ingress_hop2, egress_hop = route2 |
| 948 | |
| 949 | if ingress_hop.ingress_port != ingress_hop2.ingress_port: |
| 950 | log.error('mc-ingress-hop-hop2-mismatch', |
| 951 | ingress_hop=ingress_hop, |
| 952 | ingress_hop2=ingress_hop2, |
| 953 | in_port_no=in_port_no, |
| 954 | out_port_no=out_port_no, |
| 955 | comment='ignoring flow') |
| 956 | continue |
| 957 | |
| 958 | fl_lst_olt.append(mk_flow_stat( |
| 959 | priority=flow.priority, |
| 960 | cookie=flow.cookie, |
| 961 | match_fields=[ |
| 962 | in_port(ingress_hop.ingress_port.port_no) |
| 963 | ] + [ |
| 964 | field for field in get_ofb_fields(flow) |
| 965 | if field.type not in (IN_PORT,) |
| 966 | ], |
| 967 | actions=[ |
| 968 | action for action in get_actions(flow) |
| 969 | if action.type not in (GROUP,) |
| 970 | ] + [ |
| 971 | pop_vlan(), |
| 972 | output(egress_hop.ingress_port.port_no) |
| 973 | ] |
| 974 | )) |
| 975 | |
| 976 | fl_lst_onu, _ = device_rules.setdefault( |
| 977 | egress_hop.device.id, ([], [])) |
| 978 | fl_lst_onu.append(mk_flow_stat( |
| 979 | priority=flow.priority, |
| 980 | cookie=flow.cookie, |
| 981 | match_fields=[ |
| 982 | in_port(egress_hop.ingress_port.port_no) |
| 983 | ] + [ |
| 984 | field for field in get_ofb_fields(flow) |
| 985 | if field.type not in (IN_PORT, VLAN_VID, VLAN_PCP) |
| 986 | ], |
| 987 | actions=other_actions + [ |
| 988 | output(egress_hop.egress_port.port_no) |
| 989 | ] |
| 990 | )) |
| 991 | else: |
| 992 | raise NotImplementedError('undefined downstream case for flows') |
| 993 | return device_rules |
| 994 | |
| 995 | # ~~~~~~~~~~~~ methods expected to be provided by derived class ~~~~~~~~~~~ |
| 996 | |
| 997 | def get_all_default_rules(self): |
| 998 | raise NotImplementedError('derived class must provide') |
| 999 | |
| 1000 | def get_default_rules(self, device_id): |
| 1001 | raise NotImplementedError('derived class must provide') |
| 1002 | |
| 1003 | def get_route(self, ingress_port_no, egress_port_no): |
| 1004 | raise NotImplementedError('derived class must provide') |
| 1005 | |
| 1006 | def get_wildcard_input_ports(self, exclude_port=None): |
| 1007 | raise NotImplementedError('derived class must provide') |
| 1008 | |
| 1009 | def flow_delete(self, mod): |
| 1010 | raise NotImplementedError('derived class must provide') |