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 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 88 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 89 | # ofp_action_* shortcuts |
| 90 | |
| 91 | def output(port, max_len=ofp.OFPCML_MAX): |
| 92 | return action( |
| 93 | type=OUTPUT, |
| 94 | output=ofp.ofp_action_output(port=port, max_len=max_len) |
| 95 | ) |
| 96 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 97 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 98 | def mpls_ttl(ttl): |
| 99 | return action( |
| 100 | type=SET_MPLS_TTL, |
| 101 | mpls_ttl=ofp.ofp_action_mpls_ttl(mpls_ttl=ttl) |
| 102 | ) |
| 103 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 104 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 105 | def push_vlan(eth_type): |
| 106 | return action( |
| 107 | type=PUSH_VLAN, |
| 108 | push=ofp.ofp_action_push(ethertype=eth_type) |
| 109 | ) |
| 110 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 111 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 112 | def pop_vlan(): |
| 113 | return action( |
| 114 | type=POP_VLAN |
| 115 | ) |
| 116 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 117 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 118 | def pop_mpls(eth_type): |
| 119 | return action( |
| 120 | type=POP_MPLS, |
| 121 | pop_mpls=ofp.ofp_action_pop_mpls(ethertype=eth_type) |
| 122 | ) |
| 123 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 124 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 125 | def group(group_id): |
| 126 | return action( |
| 127 | type=GROUP, |
| 128 | group=ofp.ofp_action_group(group_id=group_id) |
| 129 | ) |
| 130 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 131 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 132 | def nw_ttl(nw_ttl): |
| 133 | return action( |
| 134 | type=NW_TTL, |
| 135 | nw_ttl=ofp.ofp_action_nw_ttl(nw_ttl=nw_ttl) |
| 136 | ) |
| 137 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 138 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 139 | def set_field(field): |
| 140 | return action( |
| 141 | type=SET_FIELD, |
| 142 | set_field=ofp.ofp_action_set_field( |
| 143 | field=ofp.ofp_oxm_field( |
| 144 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 145 | ofb_field=field)) |
| 146 | ) |
| 147 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 148 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 149 | def experimenter(experimenter, data): |
| 150 | return action( |
| 151 | type=EXPERIMENTER, |
| 152 | experimenter=ofp.ofp_action_experimenter( |
| 153 | experimenter=experimenter, data=data) |
| 154 | ) |
| 155 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 156 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 157 | # ofb_field generators (incomplete set) |
| 158 | |
| 159 | def in_port(_in_port): |
| 160 | return ofb_field(type=IN_PORT, port=_in_port) |
| 161 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 162 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 163 | def in_phy_port(_in_phy_port): |
| 164 | return ofb_field(type=IN_PHY_PORT, port=_in_phy_port) |
| 165 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 166 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 167 | def metadata(_table_metadata): |
| 168 | return ofb_field(type=METADATA, table_metadata=_table_metadata) |
| 169 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 170 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 171 | def eth_dst(_eth_dst): |
| 172 | return ofb_field(type=ETH_DST, table_metadata=_eth_dst) |
| 173 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 174 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 175 | def eth_src(_eth_src): |
| 176 | return ofb_field(type=ETH_SRC, table_metadata=_eth_src) |
| 177 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 178 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 179 | def eth_type(_eth_type): |
| 180 | return ofb_field(type=ETH_TYPE, eth_type=_eth_type) |
| 181 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 182 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 183 | def vlan_vid(_vlan_vid): |
| 184 | return ofb_field(type=VLAN_VID, vlan_vid=_vlan_vid) |
| 185 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 186 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 187 | def vlan_pcp(_vlan_pcp): |
| 188 | return ofb_field(type=VLAN_PCP, vlan_pcp=_vlan_pcp) |
| 189 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 190 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 191 | def ip_dscp(_ip_dscp): |
| 192 | return ofb_field(type=IP_DSCP, ip_dscp=_ip_dscp) |
| 193 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 194 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 195 | def ip_ecn(_ip_ecn): |
| 196 | return ofb_field(type=IP_ECN, ip_ecn=_ip_ecn) |
| 197 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 198 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 199 | def ip_proto(_ip_proto): |
| 200 | return ofb_field(type=IP_PROTO, ip_proto=_ip_proto) |
| 201 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 202 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 203 | def ipv4_src(_ipv4_src): |
| 204 | return ofb_field(type=IPV4_SRC, ipv4_src=_ipv4_src) |
| 205 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 206 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 207 | def ipv4_dst(_ipv4_dst): |
| 208 | return ofb_field(type=IPV4_DST, ipv4_dst=_ipv4_dst) |
| 209 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 210 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 211 | def tcp_src(_tcp_src): |
| 212 | return ofb_field(type=TCP_SRC, tcp_src=_tcp_src) |
| 213 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 214 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 215 | def tcp_dst(_tcp_dst): |
| 216 | return ofb_field(type=TCP_DST, tcp_dst=_tcp_dst) |
| 217 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 218 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 219 | def udp_src(_udp_src): |
| 220 | return ofb_field(type=UDP_SRC, udp_src=_udp_src) |
| 221 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 222 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 223 | def udp_dst(_udp_dst): |
| 224 | return ofb_field(type=UDP_DST, udp_dst=_udp_dst) |
| 225 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 226 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 227 | def sctp_src(_sctp_src): |
| 228 | return ofb_field(type=SCTP_SRC, sctp_src=_sctp_src) |
| 229 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 230 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 231 | def sctp_dst(_sctp_dst): |
| 232 | return ofb_field(type=SCTP_DST, sctp_dst=_sctp_dst) |
| 233 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 234 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 235 | def icmpv4_type(_icmpv4_type): |
| 236 | return ofb_field(type=ICMPV4_TYPE, icmpv4_type=_icmpv4_type) |
| 237 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 238 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 239 | def icmpv4_code(_icmpv4_code): |
| 240 | return ofb_field(type=ICMPV4_CODE, icmpv4_code=_icmpv4_code) |
| 241 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 242 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 243 | def arp_op(_arp_op): |
| 244 | return ofb_field(type=ARP_OP, arp_op=_arp_op) |
| 245 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 246 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 247 | def arp_spa(_arp_spa): |
| 248 | return ofb_field(type=ARP_SPA, arp_spa=_arp_spa) |
| 249 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 250 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 251 | def arp_tpa(_arp_tpa): |
| 252 | return ofb_field(type=ARP_TPA, arp_tpa=_arp_tpa) |
| 253 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 254 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 255 | def arp_sha(_arp_sha): |
| 256 | return ofb_field(type=ARP_SHA, arp_sha=_arp_sha) |
| 257 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 258 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 259 | def arp_tha(_arp_tha): |
| 260 | return ofb_field(type=ARP_THA, arp_tha=_arp_tha) |
| 261 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 262 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 263 | def ipv6_src(_ipv6_src): |
| 264 | return ofb_field(type=IPV6_SRC, arp_tha=_ipv6_src) |
| 265 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 266 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 267 | def ipv6_dst(_ipv6_dst): |
| 268 | return ofb_field(type=IPV6_DST, arp_tha=_ipv6_dst) |
| 269 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 270 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 271 | def ipv6_flabel(_ipv6_flabel): |
| 272 | return ofb_field(type=IPV6_FLABEL, arp_tha=_ipv6_flabel) |
| 273 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 274 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 275 | def ipmpv6_type(_icmpv6_type): |
| 276 | return ofb_field(type=ICMPV6_TYPE, arp_tha=_icmpv6_type) |
| 277 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 278 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 279 | def icmpv6_code(_icmpv6_code): |
| 280 | return ofb_field(type=ICMPV6_CODE, arp_tha=_icmpv6_code) |
| 281 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 282 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 283 | def ipv6_nd_target(_ipv6_nd_target): |
| 284 | return ofb_field(type=IPV6_ND_TARGET, arp_tha=_ipv6_nd_target) |
| 285 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 286 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 287 | def ofb_ipv6_nd_sll(_ofb_ipv6_nd_sll): |
| 288 | return ofb_field(type=OFB_IPV6_ND_SLL, arp_tha=_ofb_ipv6_nd_sll) |
| 289 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 290 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 291 | def ipv6_nd_tll(_ipv6_nd_tll): |
| 292 | return ofb_field(type=IPV6_ND_TLL, arp_tha=_ipv6_nd_tll) |
| 293 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 294 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 295 | def mpls_label(_mpls_label): |
| 296 | return ofb_field(type=MPLS_LABEL, arp_tha=_mpls_label) |
| 297 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 298 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 299 | def mpls_tc(_mpls_tc): |
| 300 | return ofb_field(type=MPLS_TC, arp_tha=_mpls_tc) |
| 301 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 302 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 303 | def mpls_bos(_mpls_bos): |
| 304 | return ofb_field(type=MPLS_BOS, arp_tha=_mpls_bos) |
| 305 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 306 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 307 | def pbb_isid(_pbb_isid): |
| 308 | return ofb_field(type=PBB_ISID, arp_tha=_pbb_isid) |
| 309 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 310 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 311 | def tunnel_id(_tunnel_id): |
| 312 | return ofb_field(type=TUNNEL_ID, arp_tha=_tunnel_id) |
| 313 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 314 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 315 | def ipv6_exthdr(_ipv6_exthdr): |
| 316 | return ofb_field(type=IPV6_EXTHDR, arp_tha=_ipv6_exthdr) |
| 317 | |
| 318 | |
| 319 | # frequently used extractors: |
| 320 | |
| 321 | def get_actions(flow): |
| 322 | """Extract list of ofp_action objects from flow spec object""" |
| 323 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 324 | # we have the following hard assumptions for now |
| 325 | for instruction in flow.instructions: |
| 326 | if instruction.type == ofp.OFPIT_APPLY_ACTIONS: |
| 327 | return instruction.actions.actions |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 328 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 329 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 330 | def get_ofb_fields(flow): |
| 331 | assert isinstance(flow, ofp.ofp_flow_stats) |
| 332 | assert flow.match.type == ofp.OFPMT_OXM |
| 333 | ofb_fields = [] |
| 334 | for field in flow.match.oxm_fields: |
| 335 | assert field.oxm_class == ofp.OFPXMC_OPENFLOW_BASIC |
| 336 | ofb_fields.append(field.ofb_field) |
| 337 | return ofb_fields |
| 338 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 339 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 340 | def get_out_port(flow): |
| 341 | for action in get_actions(flow): |
| 342 | if action.type == OUTPUT: |
| 343 | return action.output.port |
| 344 | return None |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 345 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 346 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 347 | def get_in_port(flow): |
| 348 | for field in get_ofb_fields(flow): |
| 349 | if field.type == IN_PORT: |
| 350 | return field.port |
| 351 | return None |
| 352 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 353 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 354 | def get_goto_table_id(flow): |
| 355 | for instruction in flow.instructions: |
| 356 | if instruction.type == ofp.OFPIT_GOTO_TABLE: |
| 357 | return instruction.goto_table.table_id |
| 358 | return None |
| 359 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 360 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 361 | def get_metadata(flow): |
| 362 | ''' legacy get method (only want lower 32 bits ''' |
| 363 | for field in get_ofb_fields(flow): |
| 364 | if field.type == METADATA: |
| 365 | return field.table_metadata & 0xffffffff |
| 366 | return None |
| 367 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 368 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 369 | def get_metadata_64_bit(flow): |
| 370 | for field in get_ofb_fields(flow): |
| 371 | if field.type == METADATA: |
| 372 | return field.table_metadata |
| 373 | return None |
| 374 | |
| 375 | |
| 376 | def get_port_number_from_metadata(flow): |
| 377 | """ |
| 378 | The port number (UNI on ONU) is in the lower 32-bits of metadata and |
| 379 | the inner_tag is in the upper 32-bits |
| 380 | |
| 381 | This is set in the ONOS OltPipeline as a metadata field |
| 382 | """ |
| 383 | md = get_metadata_64_bit(flow) |
| 384 | |
| 385 | if md is None: |
| 386 | return None |
| 387 | |
| 388 | if md <= 0xffffffff: |
| 389 | log.warn('onos-upgrade-suggested', |
| 390 | netadata=md, |
| 391 | message='Legacy MetaData detected form OltPipeline') |
| 392 | return md |
| 393 | |
| 394 | return md & 0xffffffff |
| 395 | |
| 396 | |
| 397 | def get_inner_tag_from_metadata(flow): |
| 398 | """ |
| 399 | The port number (UNI on ONU) is in the lower 32-bits of metadata and |
| 400 | the inner_tag is in the upper 32-bits |
| 401 | |
| 402 | This is set in the ONOS OltPipeline as a metadata field |
| 403 | """ |
| 404 | md = get_metadata_64_bit(flow) |
| 405 | |
| 406 | if md is None: |
| 407 | return None |
| 408 | |
| 409 | if md <= 0xffffffff: |
| 410 | log.warn('onos-upgrade-suggested', |
| 411 | netadata=md, |
| 412 | message='Legacy MetaData detected form OltPipeline') |
| 413 | return md |
| 414 | |
| 415 | return (md >> 32) & 0xffffffff |
| 416 | |
| 417 | |
| 418 | # test and extract next table and group information |
| 419 | def has_next_table(flow): |
| 420 | return get_goto_table_id(flow) is not None |
| 421 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 422 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 423 | def get_group(flow): |
| 424 | for action in get_actions(flow): |
| 425 | if action.type == GROUP: |
| 426 | return action.group.group_id |
| 427 | return None |
| 428 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 429 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 430 | def has_group(flow): |
| 431 | return get_group(flow) is not None |
| 432 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 433 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 434 | def mk_oxm_fields(match_fields): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 435 | oxm_fields = [ |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 436 | ofp.ofp_oxm_field( |
| 437 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 438 | ofb_field=field |
| 439 | ) for field in match_fields |
| 440 | ] |
| 441 | |
| 442 | return oxm_fields |
| 443 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 444 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 445 | def mk_instructions_from_actions(actions): |
| 446 | instructions_action = ofp.ofp_instruction_actions() |
| 447 | instructions_action.actions.extend(actions) |
| 448 | instruction = ofp.ofp_instruction(type=ofp.OFPIT_APPLY_ACTIONS, |
| 449 | actions=instructions_action) |
| 450 | return [instruction] |
| 451 | |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 452 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 453 | def mk_simple_flow_mod(match_fields, actions, command=ofp.OFPFC_ADD, |
| 454 | next_table_id=None, **kw): |
| 455 | """ |
| 456 | Convenience function to generare ofp_flow_mod message with OXM BASIC match |
| 457 | composed from the match_fields, and single APPLY_ACTIONS instruction with |
| 458 | a list if ofp_action objects. |
| 459 | :param match_fields: list(ofp_oxm_ofb_field) |
| 460 | :param actions: list(ofp_action) |
| 461 | :param command: one of OFPFC_* |
| 462 | :param kw: additional keyword-based params to ofp_flow_mod |
| 463 | :return: initialized ofp_flow_mod object |
| 464 | """ |
| 465 | instructions = [ |
| 466 | ofp.ofp_instruction( |
| 467 | type=ofp.OFPIT_APPLY_ACTIONS, |
| 468 | actions=ofp.ofp_instruction_actions(actions=actions) |
| 469 | ) |
| 470 | ] |
| 471 | if next_table_id is not None: |
| 472 | instructions.append(ofp.ofp_instruction( |
| 473 | type=ofp.OFPIT_GOTO_TABLE, |
| 474 | goto_table=ofp.ofp_instruction_goto_table(table_id=next_table_id) |
| 475 | )) |
| 476 | |
| 477 | return ofp.ofp_flow_mod( |
| 478 | command=command, |
| 479 | match=ofp.ofp_match( |
| 480 | type=ofp.OFPMT_OXM, |
| 481 | oxm_fields=[ |
| 482 | ofp.ofp_oxm_field( |
| 483 | oxm_class=ofp.OFPXMC_OPENFLOW_BASIC, |
| 484 | ofb_field=field |
| 485 | ) for field in match_fields |
| 486 | ] |
| 487 | ), |
| 488 | instructions=instructions, |
| 489 | **kw |
| 490 | ) |
| 491 | |
| 492 | |
| 493 | def mk_multicast_group_mod(group_id, buckets, command=ofp.OFPGC_ADD): |
| 494 | group = ofp.ofp_group_mod( |
| 495 | command=command, |
| 496 | type=ofp.OFPGT_ALL, |
| 497 | group_id=group_id, |
| 498 | buckets=buckets |
| 499 | ) |
| 500 | return group |
| 501 | |
| 502 | |
| 503 | def hash_flow_stats(flow): |
| 504 | """ |
| 505 | Return unique 64-bit integer hash for flow covering the following |
| 506 | attributes: 'table_id', 'priority', 'flags', 'cookie', 'match', '_instruction_string' |
| 507 | """ |
| 508 | _instruction_string = "" |
| 509 | for _instruction in flow.instructions: |
| 510 | _instruction_string += _instruction.SerializeToString() |
| 511 | |
| 512 | hex = md5('{},{},{},{},{},{}'.format( |
| 513 | flow.table_id, |
| 514 | flow.priority, |
| 515 | flow.flags, |
| 516 | flow.cookie, |
| 517 | flow.match.SerializeToString(), |
| 518 | _instruction_string |
| 519 | )).hexdigest() |
| 520 | return int(hex[:16], 16) |
| 521 | |
| 522 | |
| 523 | def flow_stats_entry_from_flow_mod_message(mod): |
| 524 | flow = ofp.ofp_flow_stats( |
| 525 | table_id=mod.table_id, |
| 526 | priority=mod.priority, |
| 527 | idle_timeout=mod.idle_timeout, |
| 528 | hard_timeout=mod.hard_timeout, |
| 529 | flags=mod.flags, |
| 530 | cookie=mod.cookie, |
| 531 | match=mod.match, |
| 532 | instructions=mod.instructions |
| 533 | ) |
| 534 | flow.id = hash_flow_stats(flow) |
| 535 | return flow |
| 536 | |
| 537 | |
| 538 | def group_entry_from_group_mod(mod): |
| 539 | group = ofp.ofp_group_entry( |
| 540 | desc=ofp.ofp_group_desc( |
| 541 | type=mod.type, |
| 542 | group_id=mod.group_id, |
| 543 | buckets=mod.buckets |
| 544 | ), |
| 545 | stats=ofp.ofp_group_stats( |
| 546 | group_id=mod.group_id |
| 547 | # TODO do we need to instantiate bucket bins? |
| 548 | ) |
| 549 | ) |
| 550 | return group |
| 551 | |
| 552 | |
| 553 | def mk_flow_stat(**kw): |
| 554 | return flow_stats_entry_from_flow_mod_message(mk_simple_flow_mod(**kw)) |
| 555 | |
| 556 | |
| 557 | def mk_group_stat(**kw): |
khenaidoo | 6fdf0ba | 2018-11-02 14:38:33 -0400 | [diff] [blame] | 558 | return group_entry_from_group_mod(mk_multicast_group_mod(**kw)) |