khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [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 | # |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 16 | import structlog |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 17 | |
| 18 | from adapters.protos import openflow_13_pb2 as ofp |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 19 | from hashlib import md5 |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 20 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 21 | log = structlog.get_logger() |
| 22 | |
| 23 | # aliases |
| 24 | ofb_field = ofp.ofp_oxm_ofb_field |
| 25 | action = ofp.ofp_action |
| 26 | |
| 27 | # OFPAT_* shortcuts |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 28 | OUTPUT = ofp.OFPAT_OUTPUT |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 29 | COPY_TTL_OUT = ofp.OFPAT_COPY_TTL_OUT |
| 30 | COPY_TTL_IN = ofp.OFPAT_COPY_TTL_IN |
| 31 | SET_MPLS_TTL = ofp.OFPAT_SET_MPLS_TTL |
| 32 | DEC_MPLS_TTL = ofp.OFPAT_DEC_MPLS_TTL |
| 33 | PUSH_VLAN = ofp.OFPAT_PUSH_VLAN |
| 34 | POP_VLAN = ofp.OFPAT_POP_VLAN |
| 35 | PUSH_MPLS = ofp.OFPAT_PUSH_MPLS |
| 36 | POP_MPLS = ofp.OFPAT_POP_MPLS |
| 37 | SET_QUEUE = ofp.OFPAT_SET_QUEUE |
| 38 | GROUP = ofp.OFPAT_GROUP |
| 39 | SET_NW_TTL = ofp.OFPAT_SET_NW_TTL |
| 40 | NW_TTL = ofp.OFPAT_DEC_NW_TTL |
| 41 | SET_FIELD = ofp.OFPAT_SET_FIELD |
| 42 | PUSH_PBB = ofp.OFPAT_PUSH_PBB |
| 43 | POP_PBB = ofp.OFPAT_POP_PBB |
| 44 | EXPERIMENTER = ofp.OFPAT_EXPERIMENTER |
| 45 | |
| 46 | # OFPXMT_OFB_* shortcuts (incomplete) |
| 47 | IN_PORT = ofp.OFPXMT_OFB_IN_PORT |
| 48 | IN_PHY_PORT = ofp.OFPXMT_OFB_IN_PHY_PORT |
| 49 | METADATA = ofp.OFPXMT_OFB_METADATA |
| 50 | ETH_DST = ofp.OFPXMT_OFB_ETH_DST |
| 51 | ETH_SRC = ofp.OFPXMT_OFB_ETH_SRC |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 52 | ETH_TYPE = ofp.OFPXMT_OFB_ETH_TYPE |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 53 | VLAN_VID = ofp.OFPXMT_OFB_VLAN_VID |
| 54 | VLAN_PCP = ofp.OFPXMT_OFB_VLAN_PCP |
| 55 | IP_DSCP = ofp.OFPXMT_OFB_IP_DSCP |
| 56 | IP_ECN = ofp.OFPXMT_OFB_IP_ECN |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 57 | IP_PROTO = ofp.OFPXMT_OFB_IP_PROTO |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 58 | IPV4_SRC = ofp.OFPXMT_OFB_IPV4_SRC |
| 59 | IPV4_DST = ofp.OFPXMT_OFB_IPV4_DST |
| 60 | TCP_SRC = ofp.OFPXMT_OFB_TCP_SRC |
| 61 | TCP_DST = ofp.OFPXMT_OFB_TCP_DST |
| 62 | UDP_SRC = ofp.OFPXMT_OFB_UDP_SRC |
| 63 | UDP_DST = ofp.OFPXMT_OFB_UDP_DST |
| 64 | SCTP_SRC = ofp.OFPXMT_OFB_SCTP_SRC |
| 65 | SCTP_DST = ofp.OFPXMT_OFB_SCTP_DST |
| 66 | ICMPV4_TYPE = ofp.OFPXMT_OFB_ICMPV4_TYPE |
| 67 | ICMPV4_CODE = ofp.OFPXMT_OFB_ICMPV4_CODE |
| 68 | ARP_OP = ofp.OFPXMT_OFB_ARP_OP |
| 69 | ARP_SPA = ofp.OFPXMT_OFB_ARP_SPA |
| 70 | ARP_TPA = ofp.OFPXMT_OFB_ARP_TPA |
| 71 | ARP_SHA = ofp.OFPXMT_OFB_ARP_SHA |
| 72 | ARP_THA = ofp.OFPXMT_OFB_ARP_THA |
| 73 | IPV6_SRC = ofp.OFPXMT_OFB_IPV6_SRC |
| 74 | IPV6_DST = ofp.OFPXMT_OFB_IPV6_DST |
| 75 | IPV6_FLABEL = ofp.OFPXMT_OFB_IPV6_FLABEL |
| 76 | ICMPV6_TYPE = ofp.OFPXMT_OFB_ICMPV6_TYPE |
| 77 | ICMPV6_CODE = ofp.OFPXMT_OFB_ICMPV6_CODE |
| 78 | IPV6_ND_TARGET = ofp.OFPXMT_OFB_IPV6_ND_TARGET |
| 79 | OFB_IPV6_ND_SLL = ofp.OFPXMT_OFB_IPV6_ND_SLL |
| 80 | IPV6_ND_TLL = ofp.OFPXMT_OFB_IPV6_ND_TLL |
| 81 | MPLS_LABEL = ofp.OFPXMT_OFB_MPLS_LABEL |
| 82 | MPLS_TC = ofp.OFPXMT_OFB_MPLS_TC |
| 83 | MPLS_BOS = ofp.OFPXMT_OFB_MPLS_BOS |
| 84 | PBB_ISID = ofp.OFPXMT_OFB_PBB_ISID |
| 85 | TUNNEL_ID = ofp.OFPXMT_OFB_TUNNEL_ID |
| 86 | IPV6_EXTHDR = ofp.OFPXMT_OFB_IPV6_EXTHDR |
| 87 | |
| 88 | # ofp_action_* shortcuts |
| 89 | |
| 90 | def output(port, max_len=ofp.OFPCML_MAX): |
| 91 | return action( |
| 92 | type=OUTPUT, |
| 93 | output=ofp.ofp_action_output(port=port, max_len=max_len) |
| 94 | ) |
| 95 | |
| 96 | def mpls_ttl(ttl): |
| 97 | return action( |
| 98 | type=SET_MPLS_TTL, |
| 99 | mpls_ttl=ofp.ofp_action_mpls_ttl(mpls_ttl=ttl) |
| 100 | ) |
| 101 | |
| 102 | def push_vlan(eth_type): |
| 103 | return action( |
| 104 | type=PUSH_VLAN, |
| 105 | push=ofp.ofp_action_push(ethertype=eth_type) |
| 106 | ) |
| 107 | |
| 108 | def pop_vlan(): |
| 109 | return action( |
| 110 | type=POP_VLAN |
| 111 | ) |
| 112 | |
| 113 | def pop_mpls(eth_type): |
| 114 | return action( |
| 115 | type=POP_MPLS, |
| 116 | pop_mpls=ofp.ofp_action_pop_mpls(ethertype=eth_type) |
| 117 | ) |
| 118 | |
| 119 | def group(group_id): |
| 120 | return action( |
| 121 | type=GROUP, |
| 122 | group=ofp.ofp_action_group(group_id=group_id) |
| 123 | ) |
| 124 | |
| 125 | def nw_ttl(nw_ttl): |
| 126 | return action( |
| 127 | type=NW_TTL, |
| 128 | nw_ttl=ofp.ofp_action_nw_ttl(nw_ttl=nw_ttl) |
| 129 | ) |
| 130 | |
| 131 | def set_field(field): |
| 132 | return action( |
| 133 | type=SET_FIELD, |
| 134 | set_field=ofp.ofp_action_set_field( |
| 135 | field=ofp.ofp_oxm_field( |
| 136 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 137 | ofb_field=field)) |
| 138 | ) |
| 139 | |
| 140 | def experimenter(experimenter, data): |
| 141 | return action( |
| 142 | type=EXPERIMENTER, |
| 143 | experimenter=ofp.ofp_action_experimenter( |
| 144 | experimenter=experimenter, data=data) |
| 145 | ) |
| 146 | |
| 147 | # ofb_field generators (incomplete set) |
| 148 | |
| 149 | def in_port(_in_port): |
| 150 | return ofb_field(type=IN_PORT, port=_in_port) |
| 151 | |
| 152 | def in_phy_port(_in_phy_port): |
| 153 | return ofb_field(type=IN_PHY_PORT, port=_in_phy_port) |
| 154 | |
| 155 | def metadata(_table_metadata): |
| 156 | return ofb_field(type=METADATA, table_metadata=_table_metadata) |
| 157 | |
| 158 | def eth_dst(_eth_dst): |
| 159 | return ofb_field(type=ETH_DST, table_metadata=_eth_dst) |
| 160 | |
| 161 | def eth_src(_eth_src): |
| 162 | return ofb_field(type=ETH_SRC, table_metadata=_eth_src) |
| 163 | |
| 164 | def eth_type(_eth_type): |
| 165 | return ofb_field(type=ETH_TYPE, eth_type=_eth_type) |
| 166 | |
| 167 | def vlan_vid(_vlan_vid): |
| 168 | return ofb_field(type=VLAN_VID, vlan_vid=_vlan_vid) |
| 169 | |
| 170 | def vlan_pcp(_vlan_pcp): |
| 171 | return ofb_field(type=VLAN_PCP, vlan_pcp=_vlan_pcp) |
| 172 | |
| 173 | def ip_dscp(_ip_dscp): |
| 174 | return ofb_field(type=IP_DSCP, ip_dscp=_ip_dscp) |
| 175 | |
| 176 | def ip_ecn(_ip_ecn): |
| 177 | return ofb_field(type=IP_ECN, ip_ecn=_ip_ecn) |
| 178 | |
| 179 | def ip_proto(_ip_proto): |
| 180 | return ofb_field(type=IP_PROTO, ip_proto=_ip_proto) |
| 181 | |
| 182 | def ipv4_src(_ipv4_src): |
| 183 | return ofb_field(type=IPV4_SRC, ipv4_src=_ipv4_src) |
| 184 | |
| 185 | def ipv4_dst(_ipv4_dst): |
| 186 | return ofb_field(type=IPV4_DST, ipv4_dst=_ipv4_dst) |
| 187 | |
| 188 | def tcp_src(_tcp_src): |
| 189 | return ofb_field(type=TCP_SRC, tcp_src=_tcp_src) |
| 190 | |
| 191 | def tcp_dst(_tcp_dst): |
| 192 | return ofb_field(type=TCP_DST, tcp_dst=_tcp_dst) |
| 193 | |
| 194 | def udp_src(_udp_src): |
| 195 | return ofb_field(type=UDP_SRC, udp_src=_udp_src) |
| 196 | |
| 197 | def udp_dst(_udp_dst): |
| 198 | return ofb_field(type=UDP_DST, udp_dst=_udp_dst) |
| 199 | |
| 200 | def sctp_src(_sctp_src): |
| 201 | return ofb_field(type=SCTP_SRC, sctp_src=_sctp_src) |
| 202 | |
| 203 | def sctp_dst(_sctp_dst): |
| 204 | return ofb_field(type=SCTP_DST, sctp_dst=_sctp_dst) |
| 205 | |
| 206 | def icmpv4_type(_icmpv4_type): |
| 207 | return ofb_field(type=ICMPV4_TYPE, icmpv4_type=_icmpv4_type) |
| 208 | |
| 209 | def icmpv4_code(_icmpv4_code): |
| 210 | return ofb_field(type=ICMPV4_CODE, icmpv4_code=_icmpv4_code) |
| 211 | |
| 212 | def arp_op(_arp_op): |
| 213 | return ofb_field(type=ARP_OP, arp_op=_arp_op) |
| 214 | |
| 215 | def arp_spa(_arp_spa): |
| 216 | return ofb_field(type=ARP_SPA, arp_spa=_arp_spa) |
| 217 | |
| 218 | def arp_tpa(_arp_tpa): |
| 219 | return ofb_field(type=ARP_TPA, arp_tpa=_arp_tpa) |
| 220 | |
| 221 | def arp_sha(_arp_sha): |
| 222 | return ofb_field(type=ARP_SHA, arp_sha=_arp_sha) |
| 223 | |
| 224 | def arp_tha(_arp_tha): |
| 225 | return ofb_field(type=ARP_THA, arp_tha=_arp_tha) |
| 226 | |
| 227 | def ipv6_src(_ipv6_src): |
| 228 | return ofb_field(type=IPV6_SRC, arp_tha=_ipv6_src) |
| 229 | |
| 230 | def ipv6_dst(_ipv6_dst): |
| 231 | return ofb_field(type=IPV6_DST, arp_tha=_ipv6_dst) |
| 232 | |
| 233 | def ipv6_flabel(_ipv6_flabel): |
| 234 | return ofb_field(type=IPV6_FLABEL, arp_tha=_ipv6_flabel) |
| 235 | |
| 236 | def ipmpv6_type(_icmpv6_type): |
| 237 | return ofb_field(type=ICMPV6_TYPE, arp_tha=_icmpv6_type) |
| 238 | |
| 239 | def icmpv6_code(_icmpv6_code): |
| 240 | return ofb_field(type=ICMPV6_CODE, arp_tha=_icmpv6_code) |
| 241 | |
| 242 | def ipv6_nd_target(_ipv6_nd_target): |
| 243 | return ofb_field(type=IPV6_ND_TARGET, arp_tha=_ipv6_nd_target) |
| 244 | |
| 245 | def ofb_ipv6_nd_sll(_ofb_ipv6_nd_sll): |
| 246 | return ofb_field(type=OFB_IPV6_ND_SLL, arp_tha=_ofb_ipv6_nd_sll) |
| 247 | |
| 248 | def ipv6_nd_tll(_ipv6_nd_tll): |
| 249 | return ofb_field(type=IPV6_ND_TLL, arp_tha=_ipv6_nd_tll) |
| 250 | |
| 251 | def mpls_label(_mpls_label): |
| 252 | return ofb_field(type=MPLS_LABEL, arp_tha=_mpls_label) |
| 253 | |
| 254 | def mpls_tc(_mpls_tc): |
| 255 | return ofb_field(type=MPLS_TC, arp_tha=_mpls_tc) |
| 256 | |
| 257 | def mpls_bos(_mpls_bos): |
| 258 | return ofb_field(type=MPLS_BOS, arp_tha=_mpls_bos) |
| 259 | |
| 260 | def pbb_isid(_pbb_isid): |
| 261 | return ofb_field(type=PBB_ISID, arp_tha=_pbb_isid) |
| 262 | |
| 263 | def tunnel_id(_tunnel_id): |
| 264 | return ofb_field(type=TUNNEL_ID, arp_tha=_tunnel_id) |
| 265 | |
| 266 | def ipv6_exthdr(_ipv6_exthdr): |
| 267 | return ofb_field(type=IPV6_EXTHDR, arp_tha=_ipv6_exthdr) |
| 268 | |
| 269 | |
| 270 | # frequently used extractors: |
| 271 | |
| 272 | def get_actions(flow): |
| 273 | """Extract list of ofp_action objects from flow spec object""" |
| 274 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 275 | # we have the following hard assumptions for now |
| 276 | for instruction in flow.instructions: |
| 277 | if instruction.type == ofp.OFPIT_APPLY_ACTIONS: |
| 278 | return instruction.actions.actions |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 279 | |
| 280 | def get_ofb_fields(flow): |
| 281 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 282 | assert flow.match.type == ofp.OFPMT_OXM |
| 283 | ofb_fields = [] |
| 284 | for field in flow.match.oxm_fields: |
| 285 | assert field.oxm_class == ofp.OFPXMC_OPENFLOW_BASIC |
| 286 | ofb_fields.append(field.ofb_field) |
| 287 | return ofb_fields |
| 288 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 289 | def get_out_port(flow): |
| 290 | for action in get_actions(flow): |
| 291 | if action.type == OUTPUT: |
| 292 | return action.output.port |
| 293 | return None |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame^] | 294 | |
| 295 | def get_in_port(flow): |
| 296 | for field in get_ofb_fields(flow): |
| 297 | if field.type == IN_PORT: |
| 298 | return field.port |
| 299 | return None |
| 300 | |
| 301 | def get_goto_table_id(flow): |
| 302 | for instruction in flow.instructions: |
| 303 | if instruction.type == ofp.OFPIT_GOTO_TABLE: |
| 304 | return instruction.goto_table.table_id |
| 305 | return None |
| 306 | |
| 307 | def get_metadata(flow): |
| 308 | ''' legacy get method (only want lower 32 bits ''' |
| 309 | for field in get_ofb_fields(flow): |
| 310 | if field.type == METADATA: |
| 311 | return field.table_metadata & 0xffffffff |
| 312 | return None |
| 313 | |
| 314 | def get_metadata_64_bit(flow): |
| 315 | for field in get_ofb_fields(flow): |
| 316 | if field.type == METADATA: |
| 317 | return field.table_metadata |
| 318 | return None |
| 319 | |
| 320 | |
| 321 | def get_port_number_from_metadata(flow): |
| 322 | """ |
| 323 | The port number (UNI on ONU) is in the lower 32-bits of metadata and |
| 324 | the inner_tag is in the upper 32-bits |
| 325 | |
| 326 | This is set in the ONOS OltPipeline as a metadata field |
| 327 | """ |
| 328 | md = get_metadata_64_bit(flow) |
| 329 | |
| 330 | if md is None: |
| 331 | return None |
| 332 | |
| 333 | if md <= 0xffffffff: |
| 334 | log.warn('onos-upgrade-suggested', |
| 335 | netadata=md, |
| 336 | message='Legacy MetaData detected form OltPipeline') |
| 337 | return md |
| 338 | |
| 339 | return md & 0xffffffff |
| 340 | |
| 341 | |
| 342 | def get_inner_tag_from_metadata(flow): |
| 343 | """ |
| 344 | The port number (UNI on ONU) is in the lower 32-bits of metadata and |
| 345 | the inner_tag is in the upper 32-bits |
| 346 | |
| 347 | This is set in the ONOS OltPipeline as a metadata field |
| 348 | """ |
| 349 | md = get_metadata_64_bit(flow) |
| 350 | |
| 351 | if md is None: |
| 352 | return None |
| 353 | |
| 354 | if md <= 0xffffffff: |
| 355 | log.warn('onos-upgrade-suggested', |
| 356 | netadata=md, |
| 357 | message='Legacy MetaData detected form OltPipeline') |
| 358 | return md |
| 359 | |
| 360 | return (md >> 32) & 0xffffffff |
| 361 | |
| 362 | |
| 363 | # test and extract next table and group information |
| 364 | def has_next_table(flow): |
| 365 | return get_goto_table_id(flow) is not None |
| 366 | |
| 367 | def get_group(flow): |
| 368 | for action in get_actions(flow): |
| 369 | if action.type == GROUP: |
| 370 | return action.group.group_id |
| 371 | return None |
| 372 | |
| 373 | def has_group(flow): |
| 374 | return get_group(flow) is not None |
| 375 | |
| 376 | def mk_oxm_fields(match_fields): |
| 377 | oxm_fields=[ |
| 378 | ofp.ofp_oxm_field( |
| 379 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 380 | ofb_field=field |
| 381 | ) for field in match_fields |
| 382 | ] |
| 383 | |
| 384 | return oxm_fields |
| 385 | |
| 386 | def mk_instructions_from_actions(actions): |
| 387 | instructions_action = ofp.ofp_instruction_actions() |
| 388 | instructions_action.actions.extend(actions) |
| 389 | instruction = ofp.ofp_instruction(type=ofp.OFPIT_APPLY_ACTIONS, |
| 390 | actions=instructions_action) |
| 391 | return [instruction] |
| 392 | |
| 393 | def mk_simple_flow_mod(match_fields, actions, command=ofp.OFPFC_ADD, |
| 394 | next_table_id=None, **kw): |
| 395 | """ |
| 396 | Convenience function to generare ofp_flow_mod message with OXM BASIC match |
| 397 | composed from the match_fields, and single APPLY_ACTIONS instruction with |
| 398 | a list if ofp_action objects. |
| 399 | :param match_fields: list(ofp_oxm_ofb_field) |
| 400 | :param actions: list(ofp_action) |
| 401 | :param command: one of OFPFC_* |
| 402 | :param kw: additional keyword-based params to ofp_flow_mod |
| 403 | :return: initialized ofp_flow_mod object |
| 404 | """ |
| 405 | instructions = [ |
| 406 | ofp.ofp_instruction( |
| 407 | type=ofp.OFPIT_APPLY_ACTIONS, |
| 408 | actions=ofp.ofp_instruction_actions(actions=actions) |
| 409 | ) |
| 410 | ] |
| 411 | if next_table_id is not None: |
| 412 | instructions.append(ofp.ofp_instruction( |
| 413 | type=ofp.OFPIT_GOTO_TABLE, |
| 414 | goto_table=ofp.ofp_instruction_goto_table(table_id=next_table_id) |
| 415 | )) |
| 416 | |
| 417 | return ofp.ofp_flow_mod( |
| 418 | command=command, |
| 419 | match=ofp.ofp_match( |
| 420 | type=ofp.OFPMT_OXM, |
| 421 | oxm_fields=[ |
| 422 | ofp.ofp_oxm_field( |
| 423 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 424 | ofb_field=field |
| 425 | ) for field in match_fields |
| 426 | ] |
| 427 | ), |
| 428 | instructions=instructions, |
| 429 | **kw |
| 430 | ) |
| 431 | |
| 432 | |
| 433 | def mk_multicast_group_mod(group_id, buckets, command=ofp.OFPGC_ADD): |
| 434 | group = ofp.ofp_group_mod( |
| 435 | command=command, |
| 436 | type=ofp.OFPGT_ALL, |
| 437 | group_id=group_id, |
| 438 | buckets=buckets |
| 439 | ) |
| 440 | return group |
| 441 | |
| 442 | |
| 443 | def hash_flow_stats(flow): |
| 444 | """ |
| 445 | Return unique 64-bit integer hash for flow covering the following |
| 446 | attributes: 'table_id', 'priority', 'flags', 'cookie', 'match', '_instruction_string' |
| 447 | """ |
| 448 | _instruction_string = "" |
| 449 | for _instruction in flow.instructions: |
| 450 | _instruction_string += _instruction.SerializeToString() |
| 451 | |
| 452 | hex = md5('{},{},{},{},{},{}'.format( |
| 453 | flow.table_id, |
| 454 | flow.priority, |
| 455 | flow.flags, |
| 456 | flow.cookie, |
| 457 | flow.match.SerializeToString(), |
| 458 | _instruction_string |
| 459 | )).hexdigest() |
| 460 | return int(hex[:16], 16) |
| 461 | |
| 462 | |
| 463 | def flow_stats_entry_from_flow_mod_message(mod): |
| 464 | flow = ofp.ofp_flow_stats( |
| 465 | table_id=mod.table_id, |
| 466 | priority=mod.priority, |
| 467 | idle_timeout=mod.idle_timeout, |
| 468 | hard_timeout=mod.hard_timeout, |
| 469 | flags=mod.flags, |
| 470 | cookie=mod.cookie, |
| 471 | match=mod.match, |
| 472 | instructions=mod.instructions |
| 473 | ) |
| 474 | flow.id = hash_flow_stats(flow) |
| 475 | return flow |
| 476 | |
| 477 | |
| 478 | def group_entry_from_group_mod(mod): |
| 479 | group = ofp.ofp_group_entry( |
| 480 | desc=ofp.ofp_group_desc( |
| 481 | type=mod.type, |
| 482 | group_id=mod.group_id, |
| 483 | buckets=mod.buckets |
| 484 | ), |
| 485 | stats=ofp.ofp_group_stats( |
| 486 | group_id=mod.group_id |
| 487 | # TODO do we need to instantiate bucket bins? |
| 488 | ) |
| 489 | ) |
| 490 | return group |
| 491 | |
| 492 | |
| 493 | def mk_flow_stat(**kw): |
| 494 | return flow_stats_entry_from_flow_mod_message(mk_simple_flow_mod(**kw)) |
| 495 | |
| 496 | |
| 497 | def mk_group_stat(**kw): |
| 498 | return group_entry_from_group_mod(mk_multicast_group_mod(**kw)) |