Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2016 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | """ |
| 18 | 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 | from voltha.protos import openflow_13_pb2 as ofp |
| 25 | |
| 26 | # aliases |
| 27 | ofb_field = ofp.ofp_oxm_ofb_field |
| 28 | action = ofp.ofp_action |
| 29 | |
| 30 | # OFPAT_* shortcuts |
| 31 | OUTPUT = ofp.OFPAT_OUTPUT |
| 32 | COPY_TTL_OUT = ofp.OFPAT_COPY_TTL_OUT |
| 33 | COPY_TTL_IN = ofp.OFPAT_COPY_TTL_IN |
| 34 | SET_MPLS_TTL = ofp.OFPAT_SET_MPLS_TTL |
| 35 | DEC_MPLS_TTL = ofp.OFPAT_DEC_MPLS_TTL |
| 36 | PUSH_VLAN = ofp.OFPAT_PUSH_VLAN |
| 37 | POP_VLAN = ofp.OFPAT_POP_VLAN |
| 38 | PUSH_MPLS = ofp.OFPAT_PUSH_MPLS |
| 39 | POP_MPLS = ofp.OFPAT_POP_MPLS |
| 40 | SET_QUEUE = ofp.OFPAT_SET_QUEUE |
| 41 | GROUP = ofp.OFPAT_GROUP |
| 42 | SET_NW_TTL = ofp.OFPAT_SET_NW_TTL |
| 43 | NW_TTL = ofp.OFPAT_DEC_NW_TTL |
| 44 | SET_FIELD = ofp.OFPAT_SET_FIELD |
| 45 | PUSH_PBB = ofp.OFPAT_PUSH_PBB |
| 46 | POP_PBB = ofp.OFPAT_POP_PBB |
| 47 | EXPERIMENTER = ofp.OFPAT_EXPERIMENTER |
| 48 | |
| 49 | # OFPXMT_OFB_* shortcuts (incomplete) |
| 50 | IN_PORT = ofp.OFPXMT_OFB_IN_PORT |
| 51 | IN_PHY_PORT = ofp.OFPXMT_OFB_IN_PHY_PORT |
| 52 | METADATA = ofp.OFPXMT_OFB_METADATA |
| 53 | ETH_DST = ofp.OFPXMT_OFB_ETH_DST |
| 54 | ETH_SRC = ofp.OFPXMT_OFB_ETH_SRC |
| 55 | ETH_TYPE = ofp.OFPXMT_OFB_ETH_TYPE |
| 56 | VLAN_VID = ofp.OFPXMT_OFB_VLAN_VID |
| 57 | VLAN_PCP = ofp.OFPXMT_OFB_VLAN_PCP |
| 58 | IP_DSCP = ofp.OFPXMT_OFB_IP_DSCP |
| 59 | IP_ECN = ofp.OFPXMT_OFB_IP_ECN |
| 60 | IP_PROTO = ofp.OFPXMT_OFB_IP_PROTO |
| 61 | IPV4_SRC = ofp.OFPXMT_OFB_IPV4_SRC |
| 62 | IPV4_DST = ofp.OFPXMT_OFB_IPV4_DST |
| 63 | TCP_SRC = ofp.OFPXMT_OFB_TCP_SRC |
| 64 | TCP_DST = ofp.OFPXMT_OFB_TCP_DST |
| 65 | UDP_SRC = ofp.OFPXMT_OFB_UDP_SRC |
| 66 | UDP_DST = ofp.OFPXMT_OFB_UDP_DST |
| 67 | SCTP_SRC = ofp.OFPXMT_OFB_SCTP_SRC |
| 68 | SCTP_DST = ofp.OFPXMT_OFB_SCTP_DST |
| 69 | ICMPV4_TYPE = ofp.OFPXMT_OFB_ICMPV4_TYPE |
| 70 | ICMPV4_CODE = ofp.OFPXMT_OFB_ICMPV4_CODE |
| 71 | ARP_OP = ofp.OFPXMT_OFB_ARP_OP |
| 72 | ARP_SPA = ofp.OFPXMT_OFB_ARP_SPA |
| 73 | ARP_TPA = ofp.OFPXMT_OFB_ARP_TPA |
| 74 | ARP_SHA = ofp.OFPXMT_OFB_ARP_SHA |
| 75 | ARP_THA = ofp.OFPXMT_OFB_ARP_THA |
| 76 | IPV6_SRC = ofp.OFPXMT_OFB_IPV6_SRC |
| 77 | IPV6_DST = ofp.OFPXMT_OFB_IPV6_DST |
| 78 | IPV6_FLABEL = ofp.OFPXMT_OFB_IPV6_FLABEL |
| 79 | ICMPV6_TYPE = ofp.OFPXMT_OFB_ICMPV6_TYPE |
| 80 | ICMPV6_CODE = ofp.OFPXMT_OFB_ICMPV6_CODE |
| 81 | IPV6_ND_TARGET = ofp.OFPXMT_OFB_IPV6_ND_TARGET |
| 82 | OFB_IPV6_ND_SLL = ofp.OFPXMT_OFB_IPV6_ND_SLL |
| 83 | IPV6_ND_TLL = ofp.OFPXMT_OFB_IPV6_ND_TLL |
| 84 | MPLS_LABEL = ofp.OFPXMT_OFB_MPLS_LABEL |
| 85 | MPLS_TC = ofp.OFPXMT_OFB_MPLS_TC |
| 86 | MPLS_BOS = ofp.OFPXMT_OFB_MPLS_BOS |
| 87 | PBB_ISID = ofp.OFPXMT_OFB_PBB_ISID |
| 88 | TUNNEL_ID = ofp.OFPXMT_OFB_TUNNEL_ID |
| 89 | IPV6_EXTHDR = ofp.OFPXMT_OFB_IPV6_EXTHDR |
| 90 | |
| 91 | # ofp_action_* shortcuts |
| 92 | |
| 93 | def output(port, max_len=ofp.OFPCML_MAX): |
| 94 | return action( |
| 95 | type=OUTPUT, |
| 96 | output=ofp.ofp_action_output(port=port, max_len=max_len) |
| 97 | ) |
| 98 | |
| 99 | def mpls_ttl(ttl): |
| 100 | return action( |
| 101 | type=SET_MPLS_TTL, |
| 102 | mpls_ttl=ofp.ofp_action_mpls_ttl(mpls_ttl=ttl) |
| 103 | ) |
| 104 | |
| 105 | def push_vlan(eth_type): |
| 106 | return action( |
| 107 | type=PUSH_VLAN, |
| 108 | push=ofp.ofp_action_push(ethertype=eth_type) |
| 109 | ) |
| 110 | |
| 111 | def pop_vlan(): |
| 112 | return action( |
| 113 | type=POP_VLAN |
| 114 | ) |
| 115 | |
| 116 | def pop_mpls(eth_type): |
| 117 | return action( |
| 118 | type=POP_MPLS, |
| 119 | pop_mpls=ofp.ofp_action_pop_mpls(ethertype=eth_type) |
| 120 | ) |
| 121 | |
| 122 | def group(group_id): |
| 123 | return action( |
| 124 | type=GROUP, |
| 125 | group=ofp.ofp_action_group(group_id=group_id) |
| 126 | ) |
| 127 | |
| 128 | def nw_ttl(nw_ttl): |
| 129 | return action( |
| 130 | type=NW_TTL, |
| 131 | nw_ttl=ofp.ofp_action_nw_ttl(nw_ttl=nw_ttl) |
| 132 | ) |
| 133 | |
| 134 | def set_field(field): |
| 135 | return action( |
| 136 | type=SET_FIELD, |
| 137 | set_field=ofp.ofp_action_set_field( |
| 138 | field=ofp.ofp_oxm_field( |
| 139 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 140 | ofb_field=field)) |
| 141 | ) |
| 142 | |
| 143 | def experimenter(experimenter, data): |
| 144 | return action( |
| 145 | type=EXPERIMENTER, |
| 146 | experimenter=ofp.ofp_action_experimenter( |
| 147 | experimenter=experimenter, data=data) |
| 148 | ) |
| 149 | |
| 150 | |
| 151 | # ofb_field generators (incomplete set) |
| 152 | |
| 153 | def in_port(_in_port): |
| 154 | return ofb_field(type=IN_PORT, port=_in_port) |
| 155 | |
| 156 | def eth_type(_eth_type): |
| 157 | return ofb_field(type=ETH_TYPE, eth_type=_eth_type) |
| 158 | |
| 159 | def vlan_vid(_vlan_vid): |
| 160 | return ofb_field(type=VLAN_VID, vlan_vid=_vlan_vid) |
| 161 | |
| 162 | def vlan_pcp(_vlan_pcp): |
| 163 | return ofb_field(type=VLAN_PCP, vlan_pcp=_vlan_pcp) |
| 164 | |
| 165 | def ip_dscp(_ip_dscp): |
| 166 | return ofb_field(type=IP_DSCP, ip_dscp=_ip_dscp) |
| 167 | |
| 168 | def ip_ecn(_ip_ecn): |
| 169 | return ofb_field(type=IP_ECN, ip_ecn=_ip_ecn) |
| 170 | |
| 171 | def ip_proto(_ip_proto): |
| 172 | return ofb_field(type=IP_PROTO, ip_proto=_ip_proto) |
| 173 | |
| 174 | def ipv4_src(_ipv4_src): |
| 175 | return ofb_field(type=IPV4_SRC, ipv4_src=_ipv4_src) |
| 176 | |
| 177 | def ipv4_dst(_ipv4_dst): |
| 178 | return ofb_field(type=IPV4_DST, ipv4_dst=_ipv4_dst) |
| 179 | |
| 180 | def tcp_src(_tcp_src): |
| 181 | return ofb_field(type=TCP_SRC, tcp_src=_tcp_src) |
| 182 | |
| 183 | def tcp_dst(_tcp_dst): |
| 184 | return ofb_field(type=TCP_DST, tcp_dst=_tcp_dst) |
| 185 | |
| 186 | def udp_src(_udp_src): |
| 187 | return ofb_field(type=UDP_SRC, udp_src=_udp_src) |
| 188 | |
| 189 | def udp_dst(_udp_dst): |
| 190 | return ofb_field(type=UDP_DST, udp_dst=_udp_dst) |
| 191 | |
| 192 | def sctp_src(_sctp_src): |
| 193 | return ofb_field(type=SCTP_SRC, sctp_src=_sctp_src) |
| 194 | |
| 195 | def sctp_dst(_sctp_dst): |
| 196 | return ofb_field(type=SCTP_DST, sctp_dst=_sctp_dst) |
| 197 | |
| 198 | def icmpv4_type(_icmpv4_type): |
| 199 | return ofb_field(type=ICMPV4_TYPE, icmpv4_type=_icmpv4_type) |
| 200 | |
| 201 | def icmpv4_code(_icmpv4_code): |
| 202 | return ofb_field(type=ICMPV4_CODE, icmpv4_code=_icmpv4_code) |
| 203 | |
| 204 | def arp_op(_arp_op): |
| 205 | return ofb_field(type=ARP_OP, arp_op=_arp_op) |
| 206 | |
| 207 | def arp_spa(_arp_spa): |
| 208 | return ofb_field(type=ARP_SPA, arp_spa=_arp_spa) |
| 209 | |
| 210 | def arp_tpa(_arp_tpa): |
| 211 | return ofb_field(type=ARP_TPA, arp_tpa=_arp_tpa) |
| 212 | |
| 213 | def arp_sha(_arp_sha): |
| 214 | return ofb_field(type=ARP_SHA, arp_sha=_arp_sha) |
| 215 | |
| 216 | def arp_tha(_arp_tha): |
| 217 | return ofb_field(type=ARP_THA, arp_tha=_arp_tha) |
| 218 | |
| 219 | # TODO finish for rest of match fields |
| 220 | |
| 221 | |
| 222 | # frequently used extractors: |
| 223 | |
| 224 | def get_actions(flow): |
| 225 | """Extract list of ofp_action objects from flow spec object""" |
| 226 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 227 | # we have the following hard assumptions for now |
| 228 | for instruction in flow.instructions: |
| 229 | if instruction.type == ofp.OFPIT_APPLY_ACTIONS: |
| 230 | return instruction.actions.actions |
| 231 | |
| 232 | def get_ofb_fields(flow): |
| 233 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 234 | assert flow.match.type == ofp.OFPMT_OXM |
| 235 | ofb_fields = [] |
| 236 | for field in flow.match.oxm_fields: |
| 237 | assert field.oxm_class == ofp.OFPXMC_OPENFLOW_BASIC |
| 238 | ofb_fields.append(field.ofb_field) |
| 239 | return ofb_fields |
| 240 | |
| 241 | def get_out_port(flow): |
| 242 | for action in get_actions(flow): |
| 243 | if action.type == OUTPUT: |
| 244 | return action.output.port |
| 245 | return None |
| 246 | |
| 247 | def get_in_port(flow): |
| 248 | for field in get_ofb_fields(flow): |
| 249 | if field.type == IN_PORT: |
| 250 | return field.port |
| 251 | return None |
| 252 | |
| 253 | def get_goto_table_id(flow): |
| 254 | for instruction in flow.instructions: |
| 255 | if instruction.type == ofp.OFPIT_GOTO_TABLE: |
| 256 | return instruction.goto_table.table_id |
| 257 | return None |
| 258 | |
| 259 | |
| 260 | # test and extract next table and group information |
| 261 | |
| 262 | def has_next_table(flow): |
| 263 | return get_goto_table_id(flow) is not None |
| 264 | |
| 265 | def get_group(flow): |
| 266 | for action in get_actions(flow): |
| 267 | if action.type == GROUP: |
| 268 | return action.group.group_id |
| 269 | return None |
| 270 | |
| 271 | def has_group(flow): |
| 272 | return get_group(flow) is not None |
| 273 | |
| 274 | |
| 275 | def mk_simple_flow_mod(match_fields, actions, command=ofp.OFPFC_ADD, |
| 276 | next_table_id=None, **kw): |
| 277 | """ |
| 278 | Convenience function to generare ofp_flow_mod message with OXM BASIC match |
| 279 | composed from the match_fields, and single APPLY_ACTIONS instruction with |
| 280 | a list if ofp_action objects. |
| 281 | :param match_fields: list(ofp_oxm_ofb_field) |
| 282 | :param actions: list(ofp_action) |
| 283 | :param command: one of OFPFC_* |
| 284 | :param kw: additional keyword-based params to ofp_flow_mod |
| 285 | :return: initialized ofp_flow_mod object |
| 286 | """ |
| 287 | instructions = [ |
| 288 | ofp.ofp_instruction( |
| 289 | type=ofp.OFPIT_APPLY_ACTIONS, |
| 290 | actions=ofp.ofp_instruction_actions(actions=actions) |
| 291 | ) |
| 292 | ] |
| 293 | if next_table_id is not None: |
| 294 | instructions.append(ofp.ofp_instruction( |
| 295 | type=ofp.OFPIT_GOTO_TABLE, |
| 296 | goto_table=ofp.ofp_instruction_goto_table(table_id=next_table_id) |
| 297 | )) |
| 298 | |
| 299 | return ofp.ofp_flow_mod( |
| 300 | command=command, |
| 301 | match=ofp.ofp_match( |
| 302 | type=ofp.OFPMT_OXM, |
| 303 | oxm_fields=[ |
| 304 | ofp.ofp_oxm_field( |
| 305 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 306 | ofb_field=field |
| 307 | ) for field in match_fields |
| 308 | ] |
| 309 | ), |
| 310 | instructions=instructions, |
| 311 | **kw |
| 312 | ) |
| 313 | |
| 314 | |
| 315 | def mk_multicast_group_mod(group_id, buckets, command=ofp.OFPGC_ADD): |
| 316 | group = ofp.ofp_group_mod( |
| 317 | command=command, |
| 318 | type=ofp.OFPGT_ALL, |
| 319 | group_id=group_id, |
| 320 | buckets=buckets |
| 321 | ) |
| 322 | return group |
| 323 | |
| 324 | |
| 325 | def hash_flow_stats(flow): |
| 326 | """ |
| 327 | Return unique 64-bit integer hash for flow covering the following |
| 328 | attributes: 'table_id', 'priority', 'flags', 'cookie', 'match' |
| 329 | """ |
| 330 | hex = md5('{},{},{},{},{}'.format( |
| 331 | flow.table_id, |
| 332 | flow.priority, |
| 333 | flow.flags, |
| 334 | flow.cookie, |
| 335 | flow.match.SerializeToString() |
| 336 | )).hexdigest() |
| 337 | return int(hex[:16], 16) |
| 338 | |
| 339 | |
| 340 | def flow_stats_entry_from_flow_mod_message(mod): |
| 341 | flow = ofp.ofp_flow_stats( |
| 342 | table_id=mod.table_id, |
| 343 | priority=mod.priority, |
| 344 | idle_timeout=mod.idle_timeout, |
| 345 | hard_timeout=mod.hard_timeout, |
| 346 | flags=mod.flags, |
| 347 | cookie=mod.cookie, |
| 348 | match=mod.match, |
| 349 | instructions=mod.instructions |
| 350 | ) |
| 351 | flow.id = hash_flow_stats(flow) |
| 352 | return flow |
| 353 | |
| 354 | |
| 355 | def group_entry_from_group_mod(mod): |
| 356 | group = ofp.ofp_group_entry( |
| 357 | desc=ofp.ofp_group_desc( |
| 358 | type=mod.type, |
| 359 | group_id=mod.group_id, |
| 360 | buckets=mod.buckets |
| 361 | ), |
| 362 | stats=ofp.ofp_group_stats( |
| 363 | group_id=mod.group_id |
| 364 | # TODO do we need to instantiate bucket bins? |
| 365 | ) |
| 366 | ) |
| 367 | return group |
| 368 | |
| 369 | |
| 370 | def mk_flow_stat(**kw): |
| 371 | return flow_stats_entry_from_flow_mod_message(mk_simple_flow_mod(**kw)) |
| 372 | |
| 373 | |
| 374 | def mk_group_stat(**kw): |
| 375 | return group_entry_from_group_mod(mk_multicast_group_mod(**kw)) |
| 376 | |
| 377 | class RouteHop(object): |
| 378 | __slots__ = ('_device', '_ingress_port', '_egress_port') |
| 379 | def __init__(self, device, ingress_port, egress_port): |
| 380 | self._device = device |
| 381 | self._ingress_port = ingress_port |
| 382 | self._egress_port = egress_port |
| 383 | @property |
| 384 | def device(self): return self._device |
| 385 | @property |
| 386 | def ingress_port(self): return self._ingress_port |
| 387 | @property |
| 388 | def egress_port(self): return self._egress_port |
| 389 | def __eq__(self, other): |
| 390 | return ( |
| 391 | self._device == other._device and |
| 392 | self._ingress_port == other._ingress_port and |
| 393 | self._egress_port == other._egress_port) |
| 394 | |
| 395 | |
| 396 | class FlowDecomposer(object): |
| 397 | |
| 398 | def __init__(self, *args, **kw): |
| 399 | self.logical_device_id = 'this shall be overwritten in derived class' |
| 400 | super(FlowDecomposer, self).__init__(*args, **kw) |
| 401 | |
| 402 | # ~~~~~~~~~~~~~~~~~~~~ methods exposed *to* derived class ~~~~~~~~~~~~~~~~~ |
| 403 | |
| 404 | def decompose_rules(self, flows, groups): |
| 405 | """ |
| 406 | Generate per-device flows and flow-groups from the flows and groups |
| 407 | defined on a logical device |
| 408 | :param flows: logical device flows |
| 409 | :param groups: logical device flow groups |
| 410 | :return: dict(device_id -> |
| 411 | (OrderedDict-of-device-flows, OrderedDict-of-device-flow-groups)) |
| 412 | """ |
| 413 | |
| 414 | device_rules = deepcopy(self.get_all_default_rules()) |
| 415 | group_map = dict((g.desc.group_id, g) for g in groups) |
| 416 | |
| 417 | for flow in flows: |
| 418 | for device_id, (_flows, _groups) \ |
| 419 | in self.decompose_flow(flow, group_map).iteritems(): |
| 420 | fl_lst, gr_lst = device_rules.setdefault( |
| 421 | device_id, (OrderedDict(), OrderedDict())) |
| 422 | for _flow in _flows: |
| 423 | if _flow.id not in fl_lst: |
| 424 | fl_lst[_flow.id] = _flow |
| 425 | for _group in _groups: |
| 426 | if _group.group_id not in gr_lst: |
| 427 | gr_lst[_group.group_id] = _group |
| 428 | return device_rules |
| 429 | |
| 430 | def decompose_flow(self, flow, group_map): |
| 431 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 432 | |
| 433 | #################################################################### |
| 434 | # |
| 435 | # TODO this is a very limited, heuristics based implementation |
| 436 | # |
| 437 | #################################################################### |
| 438 | |
| 439 | in_port_no = get_in_port(flow) |
| 440 | out_port_no = get_out_port(flow) # may be None |
| 441 | |
| 442 | route = self.get_route(in_port_no, out_port_no) |
| 443 | |
| 444 | assert len(route) == 2 |
| 445 | ingress_hop, egress_hop = route |
| 446 | |
| 447 | def is_downstream(): |
| 448 | return ingress_hop.device.root |
| 449 | |
| 450 | def is_upstream(): |
| 451 | return not is_downstream() |
| 452 | |
| 453 | device_rules = {} # accumulator |
| 454 | |
Zsolt Haraszti | 91730da | 2016-12-12 12:54:38 -0800 | [diff] [blame^] | 455 | if out_port_no is not None and \ |
| 456 | (out_port_no & 0x7fffffff) == ofp.OFPP_CONTROLLER: |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 457 | |
| 458 | # UPSTREAM CONTROLLER-BOUND FLOW |
| 459 | |
| 460 | # we assume that the ingress device is already pushing a |
| 461 | # customer-specific vlan (c-vid), based on its default flow |
| 462 | # rules so there is nothing else to do on the ONU |
| 463 | |
| 464 | # on the olt, we need to push a new tag and set it to 4000 |
| 465 | # which for now represents in-bound channel to the controller |
| 466 | # (via Voltha) |
| 467 | # TODO make the 4000 configurable |
| 468 | fl_lst, _ = device_rules.setdefault( |
| 469 | egress_hop.device.id, ([], [])) |
| 470 | fl_lst.append(mk_flow_stat( |
| 471 | priority=flow.priority, |
| 472 | cookie=flow.cookie, |
| 473 | match_fields=[ |
| 474 | in_port(egress_hop.ingress_port.port_no) |
| 475 | ] + [ |
| 476 | field for field in get_ofb_fields(flow) |
| 477 | if field.type not in (IN_PORT, VLAN_VID) |
| 478 | ], |
| 479 | actions=[ |
| 480 | push_vlan(0x8100), |
| 481 | set_field(vlan_vid(ofp.OFPVID_PRESENT | 4000)), |
| 482 | output(egress_hop.egress_port.port_no)] |
| 483 | )) |
| 484 | |
| 485 | else: |
| 486 | # NOT A CONTROLLER-BOUND FLOW |
| 487 | if is_upstream(): |
| 488 | |
| 489 | # We assume that anything that is upstream needs to get Q-in-Q |
| 490 | # treatment and that this is expressed via two flow rules, |
| 491 | # the first using the goto-statement. We also assume that the |
| 492 | # inner tag is applied at the ONU, while the outer tag is |
| 493 | # applied at the OLT |
| 494 | if has_next_table(flow): |
| 495 | assert out_port_no is None |
| 496 | fl_lst, _ = device_rules.setdefault( |
| 497 | ingress_hop.device.id, ([], [])) |
| 498 | fl_lst.append(mk_flow_stat( |
| 499 | priority=flow.priority, |
| 500 | cookie=flow.cookie, |
| 501 | match_fields=[ |
| 502 | in_port(ingress_hop.ingress_port.port_no) |
| 503 | ] + [ |
| 504 | field for field in get_ofb_fields(flow) |
| 505 | if field.type not in (IN_PORT,) |
| 506 | ], |
| 507 | actions=[ |
| 508 | action for action in get_actions(flow) |
| 509 | ] + [ |
| 510 | output(ingress_hop.egress_port.port_no) |
| 511 | ] |
| 512 | )) |
| 513 | |
| 514 | else: |
| 515 | assert out_port_no is not None |
| 516 | fl_lst, _ = device_rules.setdefault( |
| 517 | egress_hop.device.id, ([], [])) |
| 518 | fl_lst.append(mk_flow_stat( |
| 519 | priority=flow.priority, |
| 520 | cookie=flow.cookie, |
| 521 | match_fields=[ |
| 522 | in_port(egress_hop.ingress_port.port_no), |
| 523 | ] + [ |
| 524 | field for field in get_ofb_fields(flow) |
| 525 | if field.type not in (IN_PORT, ) |
| 526 | ], |
| 527 | actions=[ |
| 528 | action for action in get_actions(flow) |
| 529 | if action.type != OUTPUT |
| 530 | ] + [ |
| 531 | output(egress_hop.egress_port.port_no) |
| 532 | ] |
| 533 | )) |
| 534 | |
| 535 | else: # downstream |
| 536 | if has_next_table(flow): |
| 537 | assert out_port_no is None |
| 538 | fl_lst, _ = device_rules.setdefault( |
| 539 | ingress_hop.device.id, ([], [])) |
| 540 | fl_lst.append(mk_flow_stat( |
| 541 | priority=flow.priority, |
| 542 | cookie=flow.cookie, |
| 543 | match_fields=[ |
| 544 | in_port(ingress_hop.ingress_port.port_no) |
| 545 | ] + [ |
| 546 | field for field in get_ofb_fields(flow) |
| 547 | if field.type not in (IN_PORT,) |
| 548 | ], |
| 549 | actions=[ |
| 550 | action for action in get_actions(flow) |
| 551 | ] + [ |
| 552 | output(ingress_hop.egress_port.port_no) |
| 553 | ] |
| 554 | )) |
| 555 | elif out_port_no is not None: # unicast case |
| 556 | fl_lst, _ = device_rules.setdefault( |
| 557 | egress_hop.device.id, ([], [])) |
| 558 | fl_lst.append(mk_flow_stat( |
| 559 | priority=flow.priority, |
| 560 | cookie=flow.cookie, |
| 561 | match_fields=[ |
| 562 | in_port(egress_hop.ingress_port.port_no) |
| 563 | ] + [ |
| 564 | field for field in get_ofb_fields(flow) |
| 565 | if field.type not in (IN_PORT,) |
| 566 | ], |
| 567 | actions=[ |
| 568 | action for action in get_actions(flow) |
| 569 | if action.type not in (OUTPUT,) |
| 570 | ] + [ |
| 571 | output(egress_hop.egress_port.port_no) |
| 572 | ] |
| 573 | |
| 574 | )) |
| 575 | |
| 576 | else: # multicast case |
| 577 | grp_id = get_group(flow) |
| 578 | assert grp_id is not None |
| 579 | |
| 580 | fl_lst, _ = device_rules.setdefault( |
| 581 | ingress_hop.device.id, ([], [])) |
| 582 | fl_lst.append(mk_flow_stat( |
| 583 | priority=flow.priority, |
| 584 | cookie=flow.cookie, |
| 585 | match_fields=[ |
| 586 | in_port(ingress_hop.ingress_port.port_no) |
| 587 | ] + [ |
| 588 | field for field in get_ofb_fields(flow) |
| 589 | if field.type not in (IN_PORT, ETH_TYPE, IPV4_DST) |
| 590 | ], |
| 591 | actions=[ |
| 592 | action for action in get_actions(flow) |
| 593 | if action.type not in (GROUP,) |
| 594 | ] + [ |
| 595 | pop_vlan(), |
| 596 | output(ingress_hop.egress_port.port_no) |
| 597 | ] |
| 598 | )) |
| 599 | |
| 600 | group = group_map[grp_id] |
| 601 | for bucket in group.desc.buckets: |
| 602 | found_pop_vlan = False |
| 603 | other_actions = [] |
| 604 | for action in bucket.actions: |
| 605 | if action.type == POP_VLAN: |
| 606 | found_pop_vlan = True |
| 607 | elif action.type == OUTPUT: |
| 608 | out_port_no = action.output.port |
| 609 | else: |
| 610 | other_actions.append(action) |
| 611 | # re-run route request to determine egress device and |
| 612 | # ports |
| 613 | route2 = self.get_route(in_port_no, out_port_no) |
| 614 | |
| 615 | assert len(route2) == 2 |
| 616 | ingress_hop2, egress_hop = route2 |
| 617 | assert ingress_hop == ingress_hop2 |
| 618 | |
| 619 | fl_lst, _ = device_rules.setdefault( |
| 620 | egress_hop.device.id, ([], [])) |
| 621 | fl_lst.append(mk_flow_stat( |
| 622 | priority=flow.priority, |
| 623 | cookie=flow.cookie, |
| 624 | match_fields=[ |
| 625 | in_port(egress_hop.ingress_port.port_no) |
| 626 | ] + [ |
| 627 | field for field in get_ofb_fields(flow) |
| 628 | if field.type not in (IN_PORT, VLAN_VID, VLAN_PCP) |
| 629 | ], |
| 630 | actions=other_actions + [ |
| 631 | output(egress_hop.egress_port.port_no) |
| 632 | ] |
| 633 | )) |
| 634 | |
| 635 | return device_rules |
| 636 | |
| 637 | # ~~~~~~~~~~~~ methods expected to be provided by derived class ~~~~~~~~~~~ |
| 638 | |
| 639 | def get_all_default_rules(self): |
| 640 | raise NotImplementedError('derived class must provide') |
| 641 | |
| 642 | def get_default_rules(self, device_id): |
| 643 | raise NotImplementedError('derived class must provide') |
| 644 | |
| 645 | def get_route(self, ingress_port_no, egress_port_no): |
| 646 | raise NotImplementedError('derived class must provide') |
| 647 | |
| 648 | |