Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [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 | 6686203 | 2016-11-28 14:28:39 -0800 | [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 | 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 | |
Zsolt Haraszti | ee5c4c8 | 2017-01-09 14:37:57 -0800 | [diff] [blame] | 24 | import structlog |
| 25 | |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame^] | 26 | from voltha.protos import third_party |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 27 | from voltha.protos import openflow_13_pb2 as ofp |
| 28 | |
Zsolt Haraszti | 3578a1c | 2017-01-10 15:29:02 -0800 | [diff] [blame^] | 29 | _ = third_party |
Zsolt Haraszti | ee5c4c8 | 2017-01-09 14:37:57 -0800 | [diff] [blame] | 30 | log = structlog.get_logger() |
| 31 | |
| 32 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 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 eth_type(_eth_type): |
| 164 | return ofb_field(type=ETH_TYPE, eth_type=_eth_type) |
| 165 | |
| 166 | def vlan_vid(_vlan_vid): |
| 167 | return ofb_field(type=VLAN_VID, vlan_vid=_vlan_vid) |
| 168 | |
| 169 | def vlan_pcp(_vlan_pcp): |
| 170 | return ofb_field(type=VLAN_PCP, vlan_pcp=_vlan_pcp) |
| 171 | |
| 172 | def ip_dscp(_ip_dscp): |
| 173 | return ofb_field(type=IP_DSCP, ip_dscp=_ip_dscp) |
| 174 | |
| 175 | def ip_ecn(_ip_ecn): |
| 176 | return ofb_field(type=IP_ECN, ip_ecn=_ip_ecn) |
| 177 | |
| 178 | def ip_proto(_ip_proto): |
| 179 | return ofb_field(type=IP_PROTO, ip_proto=_ip_proto) |
| 180 | |
| 181 | def ipv4_src(_ipv4_src): |
| 182 | return ofb_field(type=IPV4_SRC, ipv4_src=_ipv4_src) |
| 183 | |
| 184 | def ipv4_dst(_ipv4_dst): |
| 185 | return ofb_field(type=IPV4_DST, ipv4_dst=_ipv4_dst) |
| 186 | |
| 187 | def tcp_src(_tcp_src): |
| 188 | return ofb_field(type=TCP_SRC, tcp_src=_tcp_src) |
| 189 | |
| 190 | def tcp_dst(_tcp_dst): |
| 191 | return ofb_field(type=TCP_DST, tcp_dst=_tcp_dst) |
| 192 | |
| 193 | def udp_src(_udp_src): |
| 194 | return ofb_field(type=UDP_SRC, udp_src=_udp_src) |
| 195 | |
| 196 | def udp_dst(_udp_dst): |
| 197 | return ofb_field(type=UDP_DST, udp_dst=_udp_dst) |
| 198 | |
| 199 | def sctp_src(_sctp_src): |
| 200 | return ofb_field(type=SCTP_SRC, sctp_src=_sctp_src) |
| 201 | |
| 202 | def sctp_dst(_sctp_dst): |
| 203 | return ofb_field(type=SCTP_DST, sctp_dst=_sctp_dst) |
| 204 | |
| 205 | def icmpv4_type(_icmpv4_type): |
| 206 | return ofb_field(type=ICMPV4_TYPE, icmpv4_type=_icmpv4_type) |
| 207 | |
| 208 | def icmpv4_code(_icmpv4_code): |
| 209 | return ofb_field(type=ICMPV4_CODE, icmpv4_code=_icmpv4_code) |
| 210 | |
| 211 | def arp_op(_arp_op): |
| 212 | return ofb_field(type=ARP_OP, arp_op=_arp_op) |
| 213 | |
| 214 | def arp_spa(_arp_spa): |
| 215 | return ofb_field(type=ARP_SPA, arp_spa=_arp_spa) |
| 216 | |
| 217 | def arp_tpa(_arp_tpa): |
| 218 | return ofb_field(type=ARP_TPA, arp_tpa=_arp_tpa) |
| 219 | |
| 220 | def arp_sha(_arp_sha): |
| 221 | return ofb_field(type=ARP_SHA, arp_sha=_arp_sha) |
| 222 | |
| 223 | def arp_tha(_arp_tha): |
| 224 | return ofb_field(type=ARP_THA, arp_tha=_arp_tha) |
| 225 | |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 226 | def metadata(_table_metadata): |
| 227 | return ofb_field(type=METADATA, table_metadata=_table_metadata) |
| 228 | |
| 229 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 230 | # TODO finish for rest of match fields |
| 231 | |
| 232 | |
| 233 | # frequently used extractors: |
| 234 | |
| 235 | def get_actions(flow): |
| 236 | """Extract list of ofp_action objects from flow spec object""" |
| 237 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 238 | # we have the following hard assumptions for now |
| 239 | for instruction in flow.instructions: |
| 240 | if instruction.type == ofp.OFPIT_APPLY_ACTIONS: |
| 241 | return instruction.actions.actions |
| 242 | |
| 243 | def get_ofb_fields(flow): |
| 244 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 245 | assert flow.match.type == ofp.OFPMT_OXM |
| 246 | ofb_fields = [] |
| 247 | for field in flow.match.oxm_fields: |
| 248 | assert field.oxm_class == ofp.OFPXMC_OPENFLOW_BASIC |
| 249 | ofb_fields.append(field.ofb_field) |
| 250 | return ofb_fields |
| 251 | |
| 252 | def get_out_port(flow): |
| 253 | for action in get_actions(flow): |
| 254 | if action.type == OUTPUT: |
| 255 | return action.output.port |
| 256 | return None |
| 257 | |
| 258 | def get_in_port(flow): |
| 259 | for field in get_ofb_fields(flow): |
| 260 | if field.type == IN_PORT: |
| 261 | return field.port |
| 262 | return None |
| 263 | |
| 264 | def get_goto_table_id(flow): |
| 265 | for instruction in flow.instructions: |
| 266 | if instruction.type == ofp.OFPIT_GOTO_TABLE: |
| 267 | return instruction.goto_table.table_id |
| 268 | return None |
| 269 | |
| 270 | |
| 271 | # test and extract next table and group information |
| 272 | |
| 273 | def has_next_table(flow): |
| 274 | return get_goto_table_id(flow) is not None |
| 275 | |
| 276 | def get_group(flow): |
| 277 | for action in get_actions(flow): |
| 278 | if action.type == GROUP: |
| 279 | return action.group.group_id |
| 280 | return None |
| 281 | |
| 282 | def has_group(flow): |
| 283 | return get_group(flow) is not None |
| 284 | |
| 285 | |
| 286 | def mk_simple_flow_mod(match_fields, actions, command=ofp.OFPFC_ADD, |
| 287 | next_table_id=None, **kw): |
| 288 | """ |
| 289 | Convenience function to generare ofp_flow_mod message with OXM BASIC match |
| 290 | composed from the match_fields, and single APPLY_ACTIONS instruction with |
| 291 | a list if ofp_action objects. |
| 292 | :param match_fields: list(ofp_oxm_ofb_field) |
| 293 | :param actions: list(ofp_action) |
| 294 | :param command: one of OFPFC_* |
| 295 | :param kw: additional keyword-based params to ofp_flow_mod |
| 296 | :return: initialized ofp_flow_mod object |
| 297 | """ |
| 298 | instructions = [ |
| 299 | ofp.ofp_instruction( |
| 300 | type=ofp.OFPIT_APPLY_ACTIONS, |
| 301 | actions=ofp.ofp_instruction_actions(actions=actions) |
| 302 | ) |
| 303 | ] |
| 304 | if next_table_id is not None: |
| 305 | instructions.append(ofp.ofp_instruction( |
| 306 | type=ofp.OFPIT_GOTO_TABLE, |
| 307 | goto_table=ofp.ofp_instruction_goto_table(table_id=next_table_id) |
| 308 | )) |
| 309 | |
| 310 | return ofp.ofp_flow_mod( |
| 311 | command=command, |
| 312 | match=ofp.ofp_match( |
| 313 | type=ofp.OFPMT_OXM, |
| 314 | oxm_fields=[ |
| 315 | ofp.ofp_oxm_field( |
| 316 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 317 | ofb_field=field |
| 318 | ) for field in match_fields |
| 319 | ] |
| 320 | ), |
| 321 | instructions=instructions, |
| 322 | **kw |
| 323 | ) |
| 324 | |
| 325 | |
| 326 | def mk_multicast_group_mod(group_id, buckets, command=ofp.OFPGC_ADD): |
| 327 | group = ofp.ofp_group_mod( |
| 328 | command=command, |
| 329 | type=ofp.OFPGT_ALL, |
| 330 | group_id=group_id, |
| 331 | buckets=buckets |
| 332 | ) |
| 333 | return group |
| 334 | |
| 335 | |
| 336 | def hash_flow_stats(flow): |
| 337 | """ |
| 338 | Return unique 64-bit integer hash for flow covering the following |
| 339 | attributes: 'table_id', 'priority', 'flags', 'cookie', 'match' |
| 340 | """ |
| 341 | hex = md5('{},{},{},{},{}'.format( |
| 342 | flow.table_id, |
| 343 | flow.priority, |
| 344 | flow.flags, |
| 345 | flow.cookie, |
| 346 | flow.match.SerializeToString() |
| 347 | )).hexdigest() |
| 348 | return int(hex[:16], 16) |
| 349 | |
| 350 | |
| 351 | def flow_stats_entry_from_flow_mod_message(mod): |
| 352 | flow = ofp.ofp_flow_stats( |
| 353 | table_id=mod.table_id, |
| 354 | priority=mod.priority, |
| 355 | idle_timeout=mod.idle_timeout, |
| 356 | hard_timeout=mod.hard_timeout, |
| 357 | flags=mod.flags, |
| 358 | cookie=mod.cookie, |
| 359 | match=mod.match, |
| 360 | instructions=mod.instructions |
| 361 | ) |
| 362 | flow.id = hash_flow_stats(flow) |
| 363 | return flow |
| 364 | |
| 365 | |
| 366 | def group_entry_from_group_mod(mod): |
| 367 | group = ofp.ofp_group_entry( |
| 368 | desc=ofp.ofp_group_desc( |
| 369 | type=mod.type, |
| 370 | group_id=mod.group_id, |
| 371 | buckets=mod.buckets |
| 372 | ), |
| 373 | stats=ofp.ofp_group_stats( |
| 374 | group_id=mod.group_id |
| 375 | # TODO do we need to instantiate bucket bins? |
| 376 | ) |
| 377 | ) |
| 378 | return group |
| 379 | |
| 380 | |
| 381 | def mk_flow_stat(**kw): |
| 382 | return flow_stats_entry_from_flow_mod_message(mk_simple_flow_mod(**kw)) |
| 383 | |
| 384 | |
| 385 | def mk_group_stat(**kw): |
| 386 | return group_entry_from_group_mod(mk_multicast_group_mod(**kw)) |
| 387 | |
| 388 | class RouteHop(object): |
| 389 | __slots__ = ('_device', '_ingress_port', '_egress_port') |
| 390 | def __init__(self, device, ingress_port, egress_port): |
| 391 | self._device = device |
| 392 | self._ingress_port = ingress_port |
| 393 | self._egress_port = egress_port |
| 394 | @property |
| 395 | def device(self): return self._device |
| 396 | @property |
| 397 | def ingress_port(self): return self._ingress_port |
| 398 | @property |
| 399 | def egress_port(self): return self._egress_port |
| 400 | def __eq__(self, other): |
| 401 | return ( |
| 402 | self._device == other._device and |
| 403 | self._ingress_port == other._ingress_port and |
| 404 | self._egress_port == other._egress_port) |
| 405 | |
| 406 | |
| 407 | class FlowDecomposer(object): |
| 408 | |
| 409 | def __init__(self, *args, **kw): |
| 410 | self.logical_device_id = 'this shall be overwritten in derived class' |
| 411 | super(FlowDecomposer, self).__init__(*args, **kw) |
| 412 | |
| 413 | # ~~~~~~~~~~~~~~~~~~~~ methods exposed *to* derived class ~~~~~~~~~~~~~~~~~ |
| 414 | |
| 415 | def decompose_rules(self, flows, groups): |
| 416 | """ |
| 417 | Generate per-device flows and flow-groups from the flows and groups |
| 418 | defined on a logical device |
| 419 | :param flows: logical device flows |
| 420 | :param groups: logical device flow groups |
| 421 | :return: dict(device_id -> |
| 422 | (OrderedDict-of-device-flows, OrderedDict-of-device-flow-groups)) |
| 423 | """ |
| 424 | |
| 425 | device_rules = deepcopy(self.get_all_default_rules()) |
| 426 | group_map = dict((g.desc.group_id, g) for g in groups) |
| 427 | |
| 428 | for flow in flows: |
| 429 | for device_id, (_flows, _groups) \ |
| 430 | in self.decompose_flow(flow, group_map).iteritems(): |
| 431 | fl_lst, gr_lst = device_rules.setdefault( |
| 432 | device_id, (OrderedDict(), OrderedDict())) |
| 433 | for _flow in _flows: |
| 434 | if _flow.id not in fl_lst: |
| 435 | fl_lst[_flow.id] = _flow |
| 436 | for _group in _groups: |
| 437 | if _group.group_id not in gr_lst: |
| 438 | gr_lst[_group.group_id] = _group |
| 439 | return device_rules |
| 440 | |
| 441 | def decompose_flow(self, flow, group_map): |
| 442 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 443 | |
| 444 | #################################################################### |
| 445 | # |
| 446 | # TODO this is a very limited, heuristics based implementation |
| 447 | # |
| 448 | #################################################################### |
| 449 | |
| 450 | in_port_no = get_in_port(flow) |
| 451 | out_port_no = get_out_port(flow) # may be None |
| 452 | |
Zsolt Haraszti | ee5c4c8 | 2017-01-09 14:37:57 -0800 | [diff] [blame] | 453 | device_rules = {} # accumulator |
| 454 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 455 | route = self.get_route(in_port_no, out_port_no) |
Zsolt Haraszti | ee5c4c8 | 2017-01-09 14:37:57 -0800 | [diff] [blame] | 456 | if route is None: |
| 457 | log.error('no-route', in_port_no=in_port_no, |
| 458 | out_port_no=out_port_no, comment='ignoring flow') |
| 459 | return device_rules |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 460 | |
| 461 | assert len(route) == 2 |
| 462 | ingress_hop, egress_hop = route |
| 463 | |
| 464 | def is_downstream(): |
| 465 | return ingress_hop.device.root |
| 466 | |
| 467 | def is_upstream(): |
| 468 | return not is_downstream() |
| 469 | |
Zsolt Haraszti | 91730da | 2016-12-12 12:54:38 -0800 | [diff] [blame] | 470 | if out_port_no is not None and \ |
| 471 | (out_port_no & 0x7fffffff) == ofp.OFPP_CONTROLLER: |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 472 | |
| 473 | # UPSTREAM CONTROLLER-BOUND FLOW |
| 474 | |
| 475 | # we assume that the ingress device is already pushing a |
| 476 | # customer-specific vlan (c-vid), based on its default flow |
| 477 | # rules so there is nothing else to do on the ONU |
| 478 | |
| 479 | # on the olt, we need to push a new tag and set it to 4000 |
| 480 | # which for now represents in-bound channel to the controller |
| 481 | # (via Voltha) |
| 482 | # TODO make the 4000 configurable |
| 483 | fl_lst, _ = device_rules.setdefault( |
| 484 | egress_hop.device.id, ([], [])) |
| 485 | fl_lst.append(mk_flow_stat( |
| 486 | priority=flow.priority, |
| 487 | cookie=flow.cookie, |
| 488 | match_fields=[ |
| 489 | in_port(egress_hop.ingress_port.port_no) |
| 490 | ] + [ |
| 491 | field for field in get_ofb_fields(flow) |
| 492 | if field.type not in (IN_PORT, VLAN_VID) |
| 493 | ], |
| 494 | actions=[ |
| 495 | push_vlan(0x8100), |
| 496 | set_field(vlan_vid(ofp.OFPVID_PRESENT | 4000)), |
| 497 | output(egress_hop.egress_port.port_no)] |
| 498 | )) |
| 499 | |
| 500 | else: |
| 501 | # NOT A CONTROLLER-BOUND FLOW |
| 502 | if is_upstream(): |
| 503 | |
| 504 | # We assume that anything that is upstream needs to get Q-in-Q |
| 505 | # treatment and that this is expressed via two flow rules, |
| 506 | # the first using the goto-statement. We also assume that the |
| 507 | # inner tag is applied at the ONU, while the outer tag is |
| 508 | # applied at the OLT |
| 509 | if has_next_table(flow): |
| 510 | assert out_port_no is None |
| 511 | fl_lst, _ = device_rules.setdefault( |
| 512 | ingress_hop.device.id, ([], [])) |
| 513 | fl_lst.append(mk_flow_stat( |
| 514 | priority=flow.priority, |
| 515 | cookie=flow.cookie, |
| 516 | match_fields=[ |
| 517 | in_port(ingress_hop.ingress_port.port_no) |
| 518 | ] + [ |
| 519 | field for field in get_ofb_fields(flow) |
| 520 | if field.type not in (IN_PORT,) |
| 521 | ], |
| 522 | actions=[ |
| 523 | action for action in get_actions(flow) |
| 524 | ] + [ |
| 525 | output(ingress_hop.egress_port.port_no) |
| 526 | ] |
| 527 | )) |
| 528 | |
| 529 | else: |
| 530 | assert out_port_no is not None |
| 531 | fl_lst, _ = device_rules.setdefault( |
| 532 | egress_hop.device.id, ([], [])) |
| 533 | fl_lst.append(mk_flow_stat( |
| 534 | priority=flow.priority, |
| 535 | cookie=flow.cookie, |
| 536 | match_fields=[ |
| 537 | in_port(egress_hop.ingress_port.port_no), |
| 538 | ] + [ |
| 539 | field for field in get_ofb_fields(flow) |
| 540 | if field.type not in (IN_PORT, ) |
| 541 | ], |
| 542 | actions=[ |
| 543 | action for action in get_actions(flow) |
| 544 | if action.type != OUTPUT |
| 545 | ] + [ |
| 546 | output(egress_hop.egress_port.port_no) |
| 547 | ] |
| 548 | )) |
| 549 | |
| 550 | else: # downstream |
| 551 | if has_next_table(flow): |
| 552 | assert out_port_no is None |
| 553 | fl_lst, _ = device_rules.setdefault( |
| 554 | ingress_hop.device.id, ([], [])) |
| 555 | fl_lst.append(mk_flow_stat( |
| 556 | priority=flow.priority, |
| 557 | cookie=flow.cookie, |
| 558 | match_fields=[ |
| 559 | in_port(ingress_hop.ingress_port.port_no) |
| 560 | ] + [ |
| 561 | field for field in get_ofb_fields(flow) |
| 562 | if field.type not in (IN_PORT,) |
| 563 | ], |
| 564 | actions=[ |
| 565 | action for action in get_actions(flow) |
| 566 | ] + [ |
| 567 | output(ingress_hop.egress_port.port_no) |
| 568 | ] |
| 569 | )) |
| 570 | elif out_port_no is not None: # unicast case |
| 571 | fl_lst, _ = device_rules.setdefault( |
| 572 | egress_hop.device.id, ([], [])) |
| 573 | fl_lst.append(mk_flow_stat( |
| 574 | priority=flow.priority, |
| 575 | cookie=flow.cookie, |
| 576 | match_fields=[ |
| 577 | in_port(egress_hop.ingress_port.port_no) |
| 578 | ] + [ |
| 579 | field for field in get_ofb_fields(flow) |
| 580 | if field.type not in (IN_PORT,) |
| 581 | ], |
| 582 | actions=[ |
| 583 | action for action in get_actions(flow) |
| 584 | if action.type not in (OUTPUT,) |
| 585 | ] + [ |
| 586 | output(egress_hop.egress_port.port_no) |
| 587 | ] |
| 588 | |
| 589 | )) |
| 590 | |
| 591 | else: # multicast case |
| 592 | grp_id = get_group(flow) |
| 593 | assert grp_id is not None |
| 594 | |
| 595 | fl_lst, _ = device_rules.setdefault( |
| 596 | ingress_hop.device.id, ([], [])) |
| 597 | fl_lst.append(mk_flow_stat( |
| 598 | priority=flow.priority, |
| 599 | cookie=flow.cookie, |
| 600 | match_fields=[ |
| 601 | in_port(ingress_hop.ingress_port.port_no) |
| 602 | ] + [ |
| 603 | field for field in get_ofb_fields(flow) |
| 604 | if field.type not in (IN_PORT, ETH_TYPE, IPV4_DST) |
| 605 | ], |
| 606 | actions=[ |
| 607 | action for action in get_actions(flow) |
| 608 | if action.type not in (GROUP,) |
| 609 | ] + [ |
| 610 | pop_vlan(), |
| 611 | output(ingress_hop.egress_port.port_no) |
| 612 | ] |
| 613 | )) |
| 614 | |
Zsolt Haraszti | 50de7c4 | 2016-12-13 15:39:07 -0800 | [diff] [blame] | 615 | # having no group yet is the same as having a group with |
| 616 | # no buckets |
| 617 | group = group_map.get(grp_id, ofp.ofp_group_entry()) |
| 618 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 619 | for bucket in group.desc.buckets: |
| 620 | found_pop_vlan = False |
| 621 | other_actions = [] |
| 622 | for action in bucket.actions: |
| 623 | if action.type == POP_VLAN: |
| 624 | found_pop_vlan = True |
| 625 | elif action.type == OUTPUT: |
| 626 | out_port_no = action.output.port |
| 627 | else: |
| 628 | other_actions.append(action) |
| 629 | # re-run route request to determine egress device and |
| 630 | # ports |
| 631 | route2 = self.get_route(in_port_no, out_port_no) |
| 632 | |
| 633 | assert len(route2) == 2 |
| 634 | ingress_hop2, egress_hop = route2 |
| 635 | assert ingress_hop == ingress_hop2 |
| 636 | |
| 637 | fl_lst, _ = device_rules.setdefault( |
| 638 | egress_hop.device.id, ([], [])) |
| 639 | fl_lst.append(mk_flow_stat( |
| 640 | priority=flow.priority, |
| 641 | cookie=flow.cookie, |
| 642 | match_fields=[ |
| 643 | in_port(egress_hop.ingress_port.port_no) |
| 644 | ] + [ |
| 645 | field for field in get_ofb_fields(flow) |
| 646 | if field.type not in (IN_PORT, VLAN_VID, VLAN_PCP) |
| 647 | ], |
| 648 | actions=other_actions + [ |
| 649 | output(egress_hop.egress_port.port_no) |
| 650 | ] |
| 651 | )) |
| 652 | |
| 653 | return device_rules |
| 654 | |
| 655 | # ~~~~~~~~~~~~ methods expected to be provided by derived class ~~~~~~~~~~~ |
| 656 | |
| 657 | def get_all_default_rules(self): |
| 658 | raise NotImplementedError('derived class must provide') |
| 659 | |
| 660 | def get_default_rules(self, device_id): |
| 661 | raise NotImplementedError('derived class must provide') |
| 662 | |
| 663 | def get_route(self, ingress_port_no, egress_port_no): |
| 664 | raise NotImplementedError('derived class must provide') |
| 665 | |
| 666 | |