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 | |
ggowdru | a015559 | 2017-02-17 09:05:47 -0800 | [diff] [blame] | 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 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 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 | |
ggowdru | a015559 | 2017-02-17 09:05:47 -0800 | [diff] [blame] | 238 | def ipv6_src(_ipv6_src): |
| 239 | return ofb_field(type=IPV6_SRC, arp_tha=_ipv6_src) |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 240 | |
ggowdru | a015559 | 2017-02-17 09:05:47 -0800 | [diff] [blame] | 241 | def ipv6_dst(_ipv6_dst): |
| 242 | return ofb_field(type=IPV6_DST, arp_tha=_ipv6_dst) |
Zsolt Haraszti | 6a5107c | 2017-01-09 23:42:41 -0800 | [diff] [blame] | 243 | |
ggowdru | a015559 | 2017-02-17 09:05:47 -0800 | [diff] [blame] | 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) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 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 | for instruction in flow.instructions: |
| 288 | if instruction.type == ofp.OFPIT_APPLY_ACTIONS: |
| 289 | return instruction.actions.actions |
| 290 | |
| 291 | def get_ofb_fields(flow): |
| 292 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 293 | assert flow.match.type == ofp.OFPMT_OXM |
| 294 | ofb_fields = [] |
| 295 | for field in flow.match.oxm_fields: |
| 296 | assert field.oxm_class == ofp.OFPXMC_OPENFLOW_BASIC |
| 297 | ofb_fields.append(field.ofb_field) |
| 298 | return ofb_fields |
| 299 | |
| 300 | def get_out_port(flow): |
| 301 | for action in get_actions(flow): |
| 302 | if action.type == OUTPUT: |
| 303 | return action.output.port |
| 304 | return None |
| 305 | |
| 306 | def get_in_port(flow): |
| 307 | for field in get_ofb_fields(flow): |
| 308 | if field.type == IN_PORT: |
| 309 | return field.port |
| 310 | return None |
| 311 | |
| 312 | def get_goto_table_id(flow): |
| 313 | for instruction in flow.instructions: |
| 314 | if instruction.type == ofp.OFPIT_GOTO_TABLE: |
| 315 | return instruction.goto_table.table_id |
| 316 | return None |
| 317 | |
Chip Boling | 8be8384 | 2017-09-18 18:06:37 -0500 | [diff] [blame] | 318 | def get_metadata(flow): |
| 319 | for field in get_ofb_fields(flow): |
| 320 | if field.type == METADATA: |
| 321 | return field.table_metadata |
| 322 | return None |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 323 | |
| 324 | # test and extract next table and group information |
| 325 | |
| 326 | def has_next_table(flow): |
| 327 | return get_goto_table_id(flow) is not None |
| 328 | |
| 329 | def get_group(flow): |
| 330 | for action in get_actions(flow): |
| 331 | if action.type == GROUP: |
| 332 | return action.group.group_id |
| 333 | return None |
| 334 | |
| 335 | def has_group(flow): |
| 336 | return get_group(flow) is not None |
| 337 | |
| 338 | |
| 339 | def mk_simple_flow_mod(match_fields, actions, command=ofp.OFPFC_ADD, |
| 340 | next_table_id=None, **kw): |
| 341 | """ |
| 342 | Convenience function to generare ofp_flow_mod message with OXM BASIC match |
| 343 | composed from the match_fields, and single APPLY_ACTIONS instruction with |
| 344 | a list if ofp_action objects. |
| 345 | :param match_fields: list(ofp_oxm_ofb_field) |
| 346 | :param actions: list(ofp_action) |
| 347 | :param command: one of OFPFC_* |
| 348 | :param kw: additional keyword-based params to ofp_flow_mod |
| 349 | :return: initialized ofp_flow_mod object |
| 350 | """ |
| 351 | instructions = [ |
| 352 | ofp.ofp_instruction( |
| 353 | type=ofp.OFPIT_APPLY_ACTIONS, |
| 354 | actions=ofp.ofp_instruction_actions(actions=actions) |
| 355 | ) |
| 356 | ] |
| 357 | if next_table_id is not None: |
| 358 | instructions.append(ofp.ofp_instruction( |
| 359 | type=ofp.OFPIT_GOTO_TABLE, |
| 360 | goto_table=ofp.ofp_instruction_goto_table(table_id=next_table_id) |
| 361 | )) |
| 362 | |
| 363 | return ofp.ofp_flow_mod( |
| 364 | command=command, |
| 365 | match=ofp.ofp_match( |
| 366 | type=ofp.OFPMT_OXM, |
| 367 | oxm_fields=[ |
| 368 | ofp.ofp_oxm_field( |
| 369 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 370 | ofb_field=field |
| 371 | ) for field in match_fields |
| 372 | ] |
| 373 | ), |
| 374 | instructions=instructions, |
| 375 | **kw |
| 376 | ) |
| 377 | |
| 378 | |
| 379 | def mk_multicast_group_mod(group_id, buckets, command=ofp.OFPGC_ADD): |
| 380 | group = ofp.ofp_group_mod( |
| 381 | command=command, |
| 382 | type=ofp.OFPGT_ALL, |
| 383 | group_id=group_id, |
| 384 | buckets=buckets |
| 385 | ) |
| 386 | return group |
| 387 | |
| 388 | |
| 389 | def hash_flow_stats(flow): |
| 390 | """ |
| 391 | Return unique 64-bit integer hash for flow covering the following |
| 392 | attributes: 'table_id', 'priority', 'flags', 'cookie', 'match' |
| 393 | """ |
| 394 | hex = md5('{},{},{},{},{}'.format( |
| 395 | flow.table_id, |
| 396 | flow.priority, |
| 397 | flow.flags, |
| 398 | flow.cookie, |
| 399 | flow.match.SerializeToString() |
| 400 | )).hexdigest() |
| 401 | return int(hex[:16], 16) |
| 402 | |
| 403 | |
| 404 | def flow_stats_entry_from_flow_mod_message(mod): |
| 405 | flow = ofp.ofp_flow_stats( |
| 406 | table_id=mod.table_id, |
| 407 | priority=mod.priority, |
| 408 | idle_timeout=mod.idle_timeout, |
| 409 | hard_timeout=mod.hard_timeout, |
| 410 | flags=mod.flags, |
| 411 | cookie=mod.cookie, |
| 412 | match=mod.match, |
| 413 | instructions=mod.instructions |
| 414 | ) |
| 415 | flow.id = hash_flow_stats(flow) |
| 416 | return flow |
| 417 | |
| 418 | |
| 419 | def group_entry_from_group_mod(mod): |
| 420 | group = ofp.ofp_group_entry( |
| 421 | desc=ofp.ofp_group_desc( |
| 422 | type=mod.type, |
| 423 | group_id=mod.group_id, |
| 424 | buckets=mod.buckets |
| 425 | ), |
| 426 | stats=ofp.ofp_group_stats( |
| 427 | group_id=mod.group_id |
| 428 | # TODO do we need to instantiate bucket bins? |
| 429 | ) |
| 430 | ) |
| 431 | return group |
| 432 | |
| 433 | |
| 434 | def mk_flow_stat(**kw): |
| 435 | return flow_stats_entry_from_flow_mod_message(mk_simple_flow_mod(**kw)) |
| 436 | |
| 437 | |
| 438 | def mk_group_stat(**kw): |
| 439 | return group_entry_from_group_mod(mk_multicast_group_mod(**kw)) |
| 440 | |
| 441 | class RouteHop(object): |
| 442 | __slots__ = ('_device', '_ingress_port', '_egress_port') |
| 443 | def __init__(self, device, ingress_port, egress_port): |
| 444 | self._device = device |
| 445 | self._ingress_port = ingress_port |
| 446 | self._egress_port = egress_port |
| 447 | @property |
| 448 | def device(self): return self._device |
| 449 | @property |
| 450 | def ingress_port(self): return self._ingress_port |
| 451 | @property |
| 452 | def egress_port(self): return self._egress_port |
| 453 | def __eq__(self, other): |
| 454 | return ( |
| 455 | self._device == other._device and |
| 456 | self._ingress_port == other._ingress_port and |
| 457 | self._egress_port == other._egress_port) |
Gertjan Van Droogenbroeck | da36670 | 2017-10-06 18:36:52 -0400 | [diff] [blame] | 458 | def __ne__(self, other): |
| 459 | return not self.__eq__(other) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 460 | |
| 461 | |
| 462 | class FlowDecomposer(object): |
| 463 | |
| 464 | def __init__(self, *args, **kw): |
| 465 | self.logical_device_id = 'this shall be overwritten in derived class' |
| 466 | super(FlowDecomposer, self).__init__(*args, **kw) |
| 467 | |
| 468 | # ~~~~~~~~~~~~~~~~~~~~ methods exposed *to* derived class ~~~~~~~~~~~~~~~~~ |
| 469 | |
| 470 | def decompose_rules(self, flows, groups): |
| 471 | """ |
| 472 | Generate per-device flows and flow-groups from the flows and groups |
| 473 | defined on a logical device |
| 474 | :param flows: logical device flows |
| 475 | :param groups: logical device flow groups |
| 476 | :return: dict(device_id -> |
| 477 | (OrderedDict-of-device-flows, OrderedDict-of-device-flow-groups)) |
| 478 | """ |
| 479 | |
| 480 | device_rules = deepcopy(self.get_all_default_rules()) |
| 481 | group_map = dict((g.desc.group_id, g) for g in groups) |
| 482 | |
| 483 | for flow in flows: |
| 484 | for device_id, (_flows, _groups) \ |
| 485 | in self.decompose_flow(flow, group_map).iteritems(): |
| 486 | fl_lst, gr_lst = device_rules.setdefault( |
| 487 | device_id, (OrderedDict(), OrderedDict())) |
| 488 | for _flow in _flows: |
| 489 | if _flow.id not in fl_lst: |
| 490 | fl_lst[_flow.id] = _flow |
| 491 | for _group in _groups: |
| 492 | if _group.group_id not in gr_lst: |
| 493 | gr_lst[_group.group_id] = _group |
| 494 | return device_rules |
| 495 | |
| 496 | def decompose_flow(self, flow, group_map): |
| 497 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 498 | |
| 499 | #################################################################### |
| 500 | # |
alshabib | b5d7781 | 2017-02-01 20:21:49 -0800 | [diff] [blame] | 501 | # limited, heuristics based implementation |
| 502 | # needs to be replaced, see https://jira.opencord.org/browse/CORD-841 |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 503 | # |
| 504 | #################################################################### |
| 505 | |
| 506 | in_port_no = get_in_port(flow) |
| 507 | out_port_no = get_out_port(flow) # may be None |
| 508 | |
Zsolt Haraszti | ee5c4c8 | 2017-01-09 14:37:57 -0800 | [diff] [blame] | 509 | device_rules = {} # accumulator |
| 510 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 511 | route = self.get_route(in_port_no, out_port_no) |
Zsolt Haraszti | ee5c4c8 | 2017-01-09 14:37:57 -0800 | [diff] [blame] | 512 | if route is None: |
| 513 | log.error('no-route', in_port_no=in_port_no, |
| 514 | out_port_no=out_port_no, comment='ignoring flow') |
| 515 | return device_rules |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 516 | |
| 517 | assert len(route) == 2 |
| 518 | ingress_hop, egress_hop = route |
| 519 | |
| 520 | def is_downstream(): |
| 521 | return ingress_hop.device.root |
| 522 | |
| 523 | def is_upstream(): |
| 524 | return not is_downstream() |
| 525 | |
Zsolt Haraszti | 91730da | 2016-12-12 12:54:38 -0800 | [diff] [blame] | 526 | if out_port_no is not None and \ |
| 527 | (out_port_no & 0x7fffffff) == ofp.OFPP_CONTROLLER: |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 528 | |
| 529 | # UPSTREAM CONTROLLER-BOUND FLOW |
| 530 | |
| 531 | # we assume that the ingress device is already pushing a |
| 532 | # customer-specific vlan (c-vid), based on its default flow |
| 533 | # rules so there is nothing else to do on the ONU |
| 534 | |
| 535 | # on the olt, we need to push a new tag and set it to 4000 |
| 536 | # which for now represents in-bound channel to the controller |
| 537 | # (via Voltha) |
| 538 | # TODO make the 4000 configurable |
| 539 | fl_lst, _ = device_rules.setdefault( |
| 540 | egress_hop.device.id, ([], [])) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 541 | |
Chip Boling | 8be8384 | 2017-09-18 18:06:37 -0500 | [diff] [blame] | 542 | # in_port_no is None for wildcard input case, do not include |
| 543 | # upstream port for 4000 flow in input |
| 544 | if in_port_no is None: |
| 545 | in_ports = self.get_wildcard_input_ports(exclude_port= |
| 546 | egress_hop.egress_port.port_no) |
| 547 | else: |
| 548 | in_ports = [in_port_no] |
| 549 | |
| 550 | for input_port in in_ports: |
| 551 | fl_lst.append(mk_flow_stat( # Upstream flow |
| 552 | priority=flow.priority, |
| 553 | cookie=flow.cookie, |
| 554 | match_fields=[ |
| 555 | in_port(egress_hop.ingress_port.port_no), |
| 556 | vlan_vid(ofp.OFPVID_PRESENT | input_port) |
| 557 | ] + [ |
| 558 | field for field in get_ofb_fields(flow) |
| 559 | if field.type not in (IN_PORT, VLAN_VID) |
| 560 | ], |
| 561 | actions=[ |
| 562 | push_vlan(0x8100), |
| 563 | set_field(vlan_vid(ofp.OFPVID_PRESENT | 4000)), |
| 564 | output(egress_hop.egress_port.port_no)] |
| 565 | )) |
| 566 | fl_lst.append(mk_flow_stat( # Downstream flow |
| 567 | priority=flow.priority, |
| 568 | match_fields=[ |
| 569 | in_port(egress_hop.egress_port.port_no), |
| 570 | vlan_vid(ofp.OFPVID_PRESENT | 4000), |
| 571 | vlan_pcp(0), |
| 572 | metadata(input_port) |
| 573 | ], |
| 574 | actions=[ |
| 575 | pop_vlan(), |
| 576 | output(egress_hop.ingress_port.port_no)] |
| 577 | )) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 578 | else: |
| 579 | # NOT A CONTROLLER-BOUND FLOW |
| 580 | if is_upstream(): |
| 581 | |
| 582 | # We assume that anything that is upstream needs to get Q-in-Q |
| 583 | # treatment and that this is expressed via two flow rules, |
| 584 | # the first using the goto-statement. We also assume that the |
| 585 | # inner tag is applied at the ONU, while the outer tag is |
| 586 | # applied at the OLT |
| 587 | if has_next_table(flow): |
| 588 | assert out_port_no is None |
| 589 | fl_lst, _ = device_rules.setdefault( |
| 590 | ingress_hop.device.id, ([], [])) |
| 591 | fl_lst.append(mk_flow_stat( |
| 592 | priority=flow.priority, |
| 593 | cookie=flow.cookie, |
| 594 | match_fields=[ |
| 595 | in_port(ingress_hop.ingress_port.port_no) |
| 596 | ] + [ |
| 597 | field for field in get_ofb_fields(flow) |
| 598 | if field.type not in (IN_PORT,) |
| 599 | ], |
| 600 | actions=[ |
| 601 | action for action in get_actions(flow) |
| 602 | ] + [ |
| 603 | output(ingress_hop.egress_port.port_no) |
| 604 | ] |
| 605 | )) |
| 606 | |
| 607 | else: |
| 608 | assert out_port_no is not None |
| 609 | fl_lst, _ = device_rules.setdefault( |
| 610 | egress_hop.device.id, ([], [])) |
| 611 | fl_lst.append(mk_flow_stat( |
| 612 | priority=flow.priority, |
| 613 | cookie=flow.cookie, |
| 614 | match_fields=[ |
| 615 | in_port(egress_hop.ingress_port.port_no), |
| 616 | ] + [ |
| 617 | field for field in get_ofb_fields(flow) |
| 618 | if field.type not in (IN_PORT, ) |
| 619 | ], |
| 620 | actions=[ |
| 621 | action for action in get_actions(flow) |
| 622 | if action.type != OUTPUT |
| 623 | ] + [ |
| 624 | output(egress_hop.egress_port.port_no) |
| 625 | ] |
| 626 | )) |
| 627 | |
| 628 | else: # downstream |
| 629 | if has_next_table(flow): |
| 630 | assert out_port_no is None |
Chip Boling | 8be8384 | 2017-09-18 18:06:37 -0500 | [diff] [blame] | 631 | |
| 632 | # For downstream flows with dual-tags, recalculate route with |
| 633 | # inner-tag as logical output port. Otherwise PON-0 is always |
| 634 | # selected. |
| 635 | inner_tag = get_metadata(flow) |
| 636 | if inner_tag is not None: |
| 637 | route = self.get_route(in_port_no, inner_tag) |
| 638 | if route is None: |
| 639 | log.error('no-route-double-tag', in_port_no=in_port_no, |
| 640 | out_port_no=inner_tag, comment='ignoring flow') |
| 641 | return device_rules |
| 642 | assert len(route) == 2 |
| 643 | ingress_hop, egress_hop = route |
| 644 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 645 | fl_lst, _ = device_rules.setdefault( |
| 646 | ingress_hop.device.id, ([], [])) |
| 647 | fl_lst.append(mk_flow_stat( |
| 648 | priority=flow.priority, |
| 649 | cookie=flow.cookie, |
| 650 | match_fields=[ |
| 651 | in_port(ingress_hop.ingress_port.port_no) |
| 652 | ] + [ |
| 653 | field for field in get_ofb_fields(flow) |
| 654 | if field.type not in (IN_PORT,) |
| 655 | ], |
| 656 | actions=[ |
| 657 | action for action in get_actions(flow) |
| 658 | ] + [ |
| 659 | output(ingress_hop.egress_port.port_no) |
| 660 | ] |
| 661 | )) |
| 662 | elif out_port_no is not None: # unicast case |
| 663 | fl_lst, _ = device_rules.setdefault( |
| 664 | egress_hop.device.id, ([], [])) |
| 665 | fl_lst.append(mk_flow_stat( |
| 666 | priority=flow.priority, |
| 667 | cookie=flow.cookie, |
| 668 | match_fields=[ |
| 669 | in_port(egress_hop.ingress_port.port_no) |
| 670 | ] + [ |
| 671 | field for field in get_ofb_fields(flow) |
| 672 | if field.type not in (IN_PORT,) |
| 673 | ], |
| 674 | actions=[ |
| 675 | action for action in get_actions(flow) |
| 676 | if action.type not in (OUTPUT,) |
| 677 | ] + [ |
| 678 | output(egress_hop.egress_port.port_no) |
| 679 | ] |
| 680 | |
| 681 | )) |
| 682 | |
| 683 | else: # multicast case |
| 684 | grp_id = get_group(flow) |
| 685 | assert grp_id is not None |
| 686 | |
| 687 | fl_lst, _ = device_rules.setdefault( |
| 688 | ingress_hop.device.id, ([], [])) |
| 689 | fl_lst.append(mk_flow_stat( |
| 690 | priority=flow.priority, |
| 691 | cookie=flow.cookie, |
| 692 | match_fields=[ |
| 693 | in_port(ingress_hop.ingress_port.port_no) |
| 694 | ] + [ |
| 695 | field for field in get_ofb_fields(flow) |
Gertjan Van Droogenbroeck | 0a0ac4a | 2017-09-29 12:21:14 +0000 | [diff] [blame] | 696 | if field.type not in (IN_PORT,) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 697 | ], |
| 698 | actions=[ |
| 699 | action for action in get_actions(flow) |
| 700 | if action.type not in (GROUP,) |
| 701 | ] + [ |
| 702 | pop_vlan(), |
| 703 | output(ingress_hop.egress_port.port_no) |
| 704 | ] |
| 705 | )) |
| 706 | |
Zsolt Haraszti | 50de7c4 | 2016-12-13 15:39:07 -0800 | [diff] [blame] | 707 | # having no group yet is the same as having a group with |
| 708 | # no buckets |
| 709 | group = group_map.get(grp_id, ofp.ofp_group_entry()) |
| 710 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 711 | for bucket in group.desc.buckets: |
| 712 | found_pop_vlan = False |
| 713 | other_actions = [] |
| 714 | for action in bucket.actions: |
| 715 | if action.type == POP_VLAN: |
| 716 | found_pop_vlan = True |
| 717 | elif action.type == OUTPUT: |
| 718 | out_port_no = action.output.port |
| 719 | else: |
| 720 | other_actions.append(action) |
| 721 | # re-run route request to determine egress device and |
| 722 | # ports |
| 723 | route2 = self.get_route(in_port_no, out_port_no) |
Gertjan Van Droogenbroeck | da36670 | 2017-10-06 18:36:52 -0400 | [diff] [blame] | 724 | if route2 is None: |
| 725 | log.error('mc-no-route', in_port_no=in_port_no, |
| 726 | out_port_no=out_port_no, route2=route2, |
| 727 | comment='ignoring flow') |
| 728 | continue |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 729 | |
| 730 | assert len(route2) == 2 |
| 731 | ingress_hop2, egress_hop = route2 |
Chip Boling | 8be8384 | 2017-09-18 18:06:37 -0500 | [diff] [blame] | 732 | assert ingress_hop.ingress_port == ingress_hop2.ingress_port |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 733 | |
| 734 | fl_lst, _ = device_rules.setdefault( |
| 735 | egress_hop.device.id, ([], [])) |
| 736 | fl_lst.append(mk_flow_stat( |
| 737 | priority=flow.priority, |
| 738 | cookie=flow.cookie, |
| 739 | match_fields=[ |
| 740 | in_port(egress_hop.ingress_port.port_no) |
| 741 | ] + [ |
| 742 | field for field in get_ofb_fields(flow) |
| 743 | if field.type not in (IN_PORT, VLAN_VID, VLAN_PCP) |
| 744 | ], |
| 745 | actions=other_actions + [ |
| 746 | output(egress_hop.egress_port.port_no) |
| 747 | ] |
| 748 | )) |
| 749 | |
| 750 | return device_rules |
| 751 | |
| 752 | # ~~~~~~~~~~~~ methods expected to be provided by derived class ~~~~~~~~~~~ |
| 753 | |
| 754 | def get_all_default_rules(self): |
| 755 | raise NotImplementedError('derived class must provide') |
| 756 | |
| 757 | def get_default_rules(self, device_id): |
| 758 | raise NotImplementedError('derived class must provide') |
| 759 | |
| 760 | def get_route(self, ingress_port_no, egress_port_no): |
| 761 | raise NotImplementedError('derived class must provide') |
| 762 | |
Chip Boling | 8be8384 | 2017-09-18 18:06:37 -0500 | [diff] [blame] | 763 | def get_wildcard_input_ports(self, exclude_port=None): |
| 764 | raise NotImplementedError('derived class must provide') |