khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | package utils |
| 17 | |
| 18 | import ( |
| 19 | "bytes" |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 20 | "crypto/md5" |
| 21 | "fmt" |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 22 | "github.com/cevaris/ordered_map" |
| 23 | "github.com/gogo/protobuf/proto" |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/common/log" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 25 | ofp "github.com/opencord/voltha-protos/go/openflow_13" |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 26 | "math/big" |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 27 | "strings" |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 28 | ) |
| 29 | |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 30 | var ( |
| 31 | // Instructions shortcut |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 32 | APPLY_ACTIONS = ofp.OfpInstructionType_OFPIT_APPLY_ACTIONS |
| 33 | WRITE_METADATA = ofp.OfpInstructionType_OFPIT_WRITE_METADATA |
| 34 | METER_ACTION = ofp.OfpInstructionType_OFPIT_METER |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 35 | |
| 36 | //OFPAT_* shortcuts |
| 37 | OUTPUT = ofp.OfpActionType_OFPAT_OUTPUT |
| 38 | COPY_TTL_OUT = ofp.OfpActionType_OFPAT_COPY_TTL_OUT |
| 39 | COPY_TTL_IN = ofp.OfpActionType_OFPAT_COPY_TTL_IN |
| 40 | SET_MPLS_TTL = ofp.OfpActionType_OFPAT_SET_MPLS_TTL |
| 41 | DEC_MPLS_TTL = ofp.OfpActionType_OFPAT_DEC_MPLS_TTL |
| 42 | PUSH_VLAN = ofp.OfpActionType_OFPAT_PUSH_VLAN |
| 43 | POP_VLAN = ofp.OfpActionType_OFPAT_POP_VLAN |
| 44 | PUSH_MPLS = ofp.OfpActionType_OFPAT_PUSH_MPLS |
| 45 | POP_MPLS = ofp.OfpActionType_OFPAT_POP_MPLS |
| 46 | SET_QUEUE = ofp.OfpActionType_OFPAT_SET_QUEUE |
| 47 | GROUP = ofp.OfpActionType_OFPAT_GROUP |
| 48 | SET_NW_TTL = ofp.OfpActionType_OFPAT_SET_NW_TTL |
| 49 | NW_TTL = ofp.OfpActionType_OFPAT_DEC_NW_TTL |
| 50 | SET_FIELD = ofp.OfpActionType_OFPAT_SET_FIELD |
| 51 | PUSH_PBB = ofp.OfpActionType_OFPAT_PUSH_PBB |
| 52 | POP_PBB = ofp.OfpActionType_OFPAT_POP_PBB |
| 53 | EXPERIMENTER = ofp.OfpActionType_OFPAT_EXPERIMENTER |
| 54 | |
| 55 | //OFPXMT_OFB_* shortcuts (incomplete) |
| 56 | IN_PORT = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT |
| 57 | IN_PHY_PORT = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IN_PHY_PORT |
| 58 | METADATA = ofp.OxmOfbFieldTypes_OFPXMT_OFB_METADATA |
| 59 | ETH_DST = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ETH_DST |
| 60 | ETH_SRC = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ETH_SRC |
| 61 | ETH_TYPE = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ETH_TYPE |
| 62 | VLAN_VID = ofp.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID |
| 63 | VLAN_PCP = ofp.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_PCP |
| 64 | IP_DSCP = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IP_DSCP |
| 65 | IP_ECN = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IP_ECN |
| 66 | IP_PROTO = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IP_PROTO |
| 67 | IPV4_SRC = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IPV4_SRC |
| 68 | IPV4_DST = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IPV4_DST |
| 69 | TCP_SRC = ofp.OxmOfbFieldTypes_OFPXMT_OFB_TCP_SRC |
| 70 | TCP_DST = ofp.OxmOfbFieldTypes_OFPXMT_OFB_TCP_DST |
| 71 | UDP_SRC = ofp.OxmOfbFieldTypes_OFPXMT_OFB_UDP_SRC |
| 72 | UDP_DST = ofp.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST |
| 73 | SCTP_SRC = ofp.OxmOfbFieldTypes_OFPXMT_OFB_SCTP_SRC |
| 74 | SCTP_DST = ofp.OxmOfbFieldTypes_OFPXMT_OFB_SCTP_DST |
| 75 | ICMPV4_TYPE = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ICMPV4_TYPE |
| 76 | ICMPV4_CODE = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ICMPV4_CODE |
| 77 | ARP_OP = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ARP_OP |
| 78 | ARP_SPA = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ARP_SPA |
| 79 | ARP_TPA = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ARP_TPA |
| 80 | ARP_SHA = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ARP_SHA |
| 81 | ARP_THA = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ARP_THA |
| 82 | IPV6_SRC = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IPV6_SRC |
| 83 | IPV6_DST = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IPV6_DST |
| 84 | IPV6_FLABEL = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IPV6_FLABEL |
| 85 | ICMPV6_TYPE = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ICMPV6_TYPE |
| 86 | ICMPV6_CODE = ofp.OxmOfbFieldTypes_OFPXMT_OFB_ICMPV6_CODE |
| 87 | IPV6_ND_TARGET = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IPV6_ND_TARGET |
| 88 | OFB_IPV6_ND_SLL = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IPV6_ND_SLL |
| 89 | IPV6_ND_TLL = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IPV6_ND_TLL |
| 90 | MPLS_LABEL = ofp.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_LABEL |
| 91 | MPLS_TC = ofp.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_TC |
| 92 | MPLS_BOS = ofp.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_BOS |
| 93 | PBB_ISID = ofp.OxmOfbFieldTypes_OFPXMT_OFB_PBB_ISID |
| 94 | TUNNEL_ID = ofp.OxmOfbFieldTypes_OFPXMT_OFB_TUNNEL_ID |
| 95 | IPV6_EXTHDR = ofp.OxmOfbFieldTypes_OFPXMT_OFB_IPV6_EXTHDR |
| 96 | ) |
| 97 | |
| 98 | //ofp_action_* shortcuts |
| 99 | |
| 100 | func Output(port uint32, maxLen ...ofp.OfpControllerMaxLen) *ofp.OfpAction { |
| 101 | maxLength := ofp.OfpControllerMaxLen_OFPCML_MAX |
| 102 | if len(maxLen) > 0 { |
| 103 | maxLength = maxLen[0] |
| 104 | } |
| 105 | return &ofp.OfpAction{Type: OUTPUT, Action: &ofp.OfpAction_Output{Output: &ofp.OfpActionOutput{Port: port, MaxLen: uint32(maxLength)}}} |
| 106 | } |
| 107 | |
| 108 | func MplsTtl(ttl uint32) *ofp.OfpAction { |
| 109 | return &ofp.OfpAction{Type: SET_MPLS_TTL, Action: &ofp.OfpAction_MplsTtl{MplsTtl: &ofp.OfpActionMplsTtl{MplsTtl: ttl}}} |
| 110 | } |
| 111 | |
| 112 | func PushVlan(ethType uint32) *ofp.OfpAction { |
| 113 | return &ofp.OfpAction{Type: PUSH_VLAN, Action: &ofp.OfpAction_Push{Push: &ofp.OfpActionPush{Ethertype: ethType}}} |
| 114 | } |
| 115 | |
| 116 | func PopVlan() *ofp.OfpAction { |
| 117 | return &ofp.OfpAction{Type: POP_VLAN} |
| 118 | } |
| 119 | |
| 120 | func PopMpls(ethType uint32) *ofp.OfpAction { |
| 121 | return &ofp.OfpAction{Type: POP_MPLS, Action: &ofp.OfpAction_PopMpls{PopMpls: &ofp.OfpActionPopMpls{Ethertype: ethType}}} |
| 122 | } |
| 123 | |
| 124 | func Group(groupId uint32) *ofp.OfpAction { |
| 125 | return &ofp.OfpAction{Type: GROUP, Action: &ofp.OfpAction_Group{Group: &ofp.OfpActionGroup{GroupId: groupId}}} |
| 126 | } |
| 127 | |
| 128 | func NwTtl(nwTtl uint32) *ofp.OfpAction { |
| 129 | return &ofp.OfpAction{Type: NW_TTL, Action: &ofp.OfpAction_NwTtl{NwTtl: &ofp.OfpActionNwTtl{NwTtl: nwTtl}}} |
| 130 | } |
| 131 | |
| 132 | func SetField(field *ofp.OfpOxmOfbField) *ofp.OfpAction { |
| 133 | actionSetField := &ofp.OfpOxmField{OxmClass: ofp.OfpOxmClass_OFPXMC_OPENFLOW_BASIC, Field: &ofp.OfpOxmField_OfbField{OfbField: field}} |
| 134 | return &ofp.OfpAction{Type: SET_FIELD, Action: &ofp.OfpAction_SetField{SetField: &ofp.OfpActionSetField{Field: actionSetField}}} |
| 135 | } |
| 136 | |
| 137 | func Experimenter(experimenter uint32, data []byte) *ofp.OfpAction { |
| 138 | return &ofp.OfpAction{Type: EXPERIMENTER, Action: &ofp.OfpAction_Experimenter{Experimenter: &ofp.OfpActionExperimenter{Experimenter: experimenter, Data: data}}} |
| 139 | } |
| 140 | |
| 141 | //ofb_field generators (incomplete set) |
| 142 | |
| 143 | func InPort(inPort uint32) *ofp.OfpOxmOfbField { |
| 144 | return &ofp.OfpOxmOfbField{Type: IN_PORT, Value: &ofp.OfpOxmOfbField_Port{Port: inPort}} |
| 145 | } |
| 146 | |
| 147 | func InPhyPort(inPhyPort uint32) *ofp.OfpOxmOfbField { |
| 148 | return &ofp.OfpOxmOfbField{Type: IN_PHY_PORT, Value: &ofp.OfpOxmOfbField_Port{Port: inPhyPort}} |
| 149 | } |
| 150 | |
| 151 | func Metadata_ofp(tableMetadata uint64) *ofp.OfpOxmOfbField { |
| 152 | return &ofp.OfpOxmOfbField{Type: METADATA, Value: &ofp.OfpOxmOfbField_TableMetadata{TableMetadata: tableMetadata}} |
| 153 | } |
| 154 | |
| 155 | // should Metadata_ofp used here ????? |
| 156 | func EthDst(ethDst uint64) *ofp.OfpOxmOfbField { |
| 157 | return &ofp.OfpOxmOfbField{Type: ETH_DST, Value: &ofp.OfpOxmOfbField_TableMetadata{TableMetadata: ethDst}} |
| 158 | } |
| 159 | |
| 160 | // should Metadata_ofp used here ????? |
| 161 | func EthSrc(ethSrc uint64) *ofp.OfpOxmOfbField { |
| 162 | return &ofp.OfpOxmOfbField{Type: ETH_SRC, Value: &ofp.OfpOxmOfbField_TableMetadata{TableMetadata: ethSrc}} |
| 163 | } |
| 164 | |
| 165 | func EthType(ethType uint32) *ofp.OfpOxmOfbField { |
| 166 | return &ofp.OfpOxmOfbField{Type: ETH_TYPE, Value: &ofp.OfpOxmOfbField_EthType{EthType: ethType}} |
| 167 | } |
| 168 | |
| 169 | func VlanVid(vlanVid uint32) *ofp.OfpOxmOfbField { |
| 170 | return &ofp.OfpOxmOfbField{Type: VLAN_VID, Value: &ofp.OfpOxmOfbField_VlanVid{VlanVid: vlanVid}} |
| 171 | } |
| 172 | |
| 173 | func VlanPcp(vlanPcp uint32) *ofp.OfpOxmOfbField { |
| 174 | return &ofp.OfpOxmOfbField{Type: VLAN_PCP, Value: &ofp.OfpOxmOfbField_VlanPcp{VlanPcp: vlanPcp}} |
| 175 | } |
| 176 | |
| 177 | func IpDscp(ipDscp uint32) *ofp.OfpOxmOfbField { |
| 178 | return &ofp.OfpOxmOfbField{Type: IP_DSCP, Value: &ofp.OfpOxmOfbField_IpDscp{IpDscp: ipDscp}} |
| 179 | } |
| 180 | |
| 181 | func IpEcn(ipEcn uint32) *ofp.OfpOxmOfbField { |
| 182 | return &ofp.OfpOxmOfbField{Type: IP_ECN, Value: &ofp.OfpOxmOfbField_IpEcn{IpEcn: ipEcn}} |
| 183 | } |
| 184 | |
| 185 | func IpProto(ipProto uint32) *ofp.OfpOxmOfbField { |
| 186 | return &ofp.OfpOxmOfbField{Type: IP_PROTO, Value: &ofp.OfpOxmOfbField_IpProto{IpProto: ipProto}} |
| 187 | } |
| 188 | |
| 189 | func Ipv4Src(ipv4Src uint32) *ofp.OfpOxmOfbField { |
| 190 | return &ofp.OfpOxmOfbField{Type: IPV4_SRC, Value: &ofp.OfpOxmOfbField_Ipv4Src{Ipv4Src: ipv4Src}} |
| 191 | } |
| 192 | |
| 193 | func Ipv4Dst(ipv4Dst uint32) *ofp.OfpOxmOfbField { |
| 194 | return &ofp.OfpOxmOfbField{Type: IPV4_DST, Value: &ofp.OfpOxmOfbField_Ipv4Dst{Ipv4Dst: ipv4Dst}} |
| 195 | } |
| 196 | |
| 197 | func TcpSrc(tcpSrc uint32) *ofp.OfpOxmOfbField { |
| 198 | return &ofp.OfpOxmOfbField{Type: TCP_SRC, Value: &ofp.OfpOxmOfbField_TcpSrc{TcpSrc: tcpSrc}} |
| 199 | } |
| 200 | |
| 201 | func TcpDst(tcpDst uint32) *ofp.OfpOxmOfbField { |
| 202 | return &ofp.OfpOxmOfbField{Type: TCP_DST, Value: &ofp.OfpOxmOfbField_TcpDst{TcpDst: tcpDst}} |
| 203 | } |
| 204 | |
| 205 | func UdpSrc(udpSrc uint32) *ofp.OfpOxmOfbField { |
| 206 | return &ofp.OfpOxmOfbField{Type: UDP_SRC, Value: &ofp.OfpOxmOfbField_UdpSrc{UdpSrc: udpSrc}} |
| 207 | } |
| 208 | |
| 209 | func UdpDst(udpDst uint32) *ofp.OfpOxmOfbField { |
| 210 | return &ofp.OfpOxmOfbField{Type: UDP_DST, Value: &ofp.OfpOxmOfbField_UdpDst{UdpDst: udpDst}} |
| 211 | } |
| 212 | |
| 213 | func SctpSrc(sctpSrc uint32) *ofp.OfpOxmOfbField { |
| 214 | return &ofp.OfpOxmOfbField{Type: SCTP_SRC, Value: &ofp.OfpOxmOfbField_SctpSrc{SctpSrc: sctpSrc}} |
| 215 | } |
| 216 | |
| 217 | func SctpDst(sctpDst uint32) *ofp.OfpOxmOfbField { |
| 218 | return &ofp.OfpOxmOfbField{Type: SCTP_DST, Value: &ofp.OfpOxmOfbField_SctpDst{SctpDst: sctpDst}} |
| 219 | } |
| 220 | |
| 221 | func Icmpv4Type(icmpv4Type uint32) *ofp.OfpOxmOfbField { |
| 222 | return &ofp.OfpOxmOfbField{Type: ICMPV4_TYPE, Value: &ofp.OfpOxmOfbField_Icmpv4Type{Icmpv4Type: icmpv4Type}} |
| 223 | } |
| 224 | |
| 225 | func Icmpv4Code(icmpv4Code uint32) *ofp.OfpOxmOfbField { |
| 226 | return &ofp.OfpOxmOfbField{Type: ICMPV4_CODE, Value: &ofp.OfpOxmOfbField_Icmpv4Code{Icmpv4Code: icmpv4Code}} |
| 227 | } |
| 228 | |
| 229 | func ArpOp(arpOp uint32) *ofp.OfpOxmOfbField { |
| 230 | return &ofp.OfpOxmOfbField{Type: ARP_OP, Value: &ofp.OfpOxmOfbField_ArpOp{ArpOp: arpOp}} |
| 231 | } |
| 232 | |
| 233 | func ArpSpa(arpSpa uint32) *ofp.OfpOxmOfbField { |
| 234 | return &ofp.OfpOxmOfbField{Type: ARP_SPA, Value: &ofp.OfpOxmOfbField_ArpSpa{ArpSpa: arpSpa}} |
| 235 | } |
| 236 | |
| 237 | func ArpTpa(arpTpa uint32) *ofp.OfpOxmOfbField { |
| 238 | return &ofp.OfpOxmOfbField{Type: ARP_TPA, Value: &ofp.OfpOxmOfbField_ArpTpa{ArpTpa: arpTpa}} |
| 239 | } |
| 240 | |
| 241 | func ArpSha(arpSha []byte) *ofp.OfpOxmOfbField { |
| 242 | return &ofp.OfpOxmOfbField{Type: ARP_SHA, Value: &ofp.OfpOxmOfbField_ArpSha{ArpSha: arpSha}} |
| 243 | } |
| 244 | |
| 245 | func ArpTha(arpTha []byte) *ofp.OfpOxmOfbField { |
| 246 | return &ofp.OfpOxmOfbField{Type: ARP_THA, Value: &ofp.OfpOxmOfbField_ArpTha{ArpTha: arpTha}} |
| 247 | } |
| 248 | |
| 249 | func Ipv6Src(ipv6Src []byte) *ofp.OfpOxmOfbField { |
| 250 | return &ofp.OfpOxmOfbField{Type: IPV6_SRC, Value: &ofp.OfpOxmOfbField_Ipv6Src{Ipv6Src: ipv6Src}} |
| 251 | } |
| 252 | |
| 253 | func Ipv6Dst(ipv6Dst []byte) *ofp.OfpOxmOfbField { |
| 254 | return &ofp.OfpOxmOfbField{Type: IPV6_DST, Value: &ofp.OfpOxmOfbField_Ipv6Dst{Ipv6Dst: ipv6Dst}} |
| 255 | } |
| 256 | |
| 257 | func Ipv6Flabel(ipv6Flabel uint32) *ofp.OfpOxmOfbField { |
| 258 | return &ofp.OfpOxmOfbField{Type: IPV6_FLABEL, Value: &ofp.OfpOxmOfbField_Ipv6Flabel{Ipv6Flabel: ipv6Flabel}} |
| 259 | } |
| 260 | |
| 261 | func Icmpv6Type(icmpv6Type uint32) *ofp.OfpOxmOfbField { |
| 262 | return &ofp.OfpOxmOfbField{Type: ICMPV6_TYPE, Value: &ofp.OfpOxmOfbField_Icmpv6Type{Icmpv6Type: icmpv6Type}} |
| 263 | } |
| 264 | |
| 265 | func Icmpv6Code(icmpv6Code uint32) *ofp.OfpOxmOfbField { |
| 266 | return &ofp.OfpOxmOfbField{Type: ICMPV6_CODE, Value: &ofp.OfpOxmOfbField_Icmpv6Code{Icmpv6Code: icmpv6Code}} |
| 267 | } |
| 268 | |
| 269 | func Ipv6NdTarget(ipv6NdTarget []byte) *ofp.OfpOxmOfbField { |
| 270 | return &ofp.OfpOxmOfbField{Type: IPV6_ND_TARGET, Value: &ofp.OfpOxmOfbField_Ipv6NdTarget{Ipv6NdTarget: ipv6NdTarget}} |
| 271 | } |
| 272 | |
| 273 | func OfbIpv6NdSll(ofbIpv6NdSll []byte) *ofp.OfpOxmOfbField { |
| 274 | return &ofp.OfpOxmOfbField{Type: OFB_IPV6_ND_SLL, Value: &ofp.OfpOxmOfbField_Ipv6NdSsl{Ipv6NdSsl: ofbIpv6NdSll}} |
| 275 | } |
| 276 | |
| 277 | func Ipv6NdTll(ipv6NdTll []byte) *ofp.OfpOxmOfbField { |
| 278 | return &ofp.OfpOxmOfbField{Type: IPV6_ND_TLL, Value: &ofp.OfpOxmOfbField_Ipv6NdTll{Ipv6NdTll: ipv6NdTll}} |
| 279 | } |
| 280 | |
| 281 | func MplsLabel(mplsLabel uint32) *ofp.OfpOxmOfbField { |
| 282 | return &ofp.OfpOxmOfbField{Type: MPLS_LABEL, Value: &ofp.OfpOxmOfbField_MplsLabel{MplsLabel: mplsLabel}} |
| 283 | } |
| 284 | |
| 285 | func MplsTc(mplsTc uint32) *ofp.OfpOxmOfbField { |
| 286 | return &ofp.OfpOxmOfbField{Type: MPLS_TC, Value: &ofp.OfpOxmOfbField_MplsTc{MplsTc: mplsTc}} |
| 287 | } |
| 288 | |
| 289 | func MplsBos(mplsBos uint32) *ofp.OfpOxmOfbField { |
| 290 | return &ofp.OfpOxmOfbField{Type: MPLS_BOS, Value: &ofp.OfpOxmOfbField_MplsBos{MplsBos: mplsBos}} |
| 291 | } |
| 292 | |
| 293 | func PbbIsid(pbbIsid uint32) *ofp.OfpOxmOfbField { |
| 294 | return &ofp.OfpOxmOfbField{Type: PBB_ISID, Value: &ofp.OfpOxmOfbField_PbbIsid{PbbIsid: pbbIsid}} |
| 295 | } |
| 296 | |
| 297 | func TunnelId(tunnelId uint64) *ofp.OfpOxmOfbField { |
| 298 | return &ofp.OfpOxmOfbField{Type: TUNNEL_ID, Value: &ofp.OfpOxmOfbField_TunnelId{TunnelId: tunnelId}} |
| 299 | } |
| 300 | |
| 301 | func Ipv6Exthdr(ipv6Exthdr uint32) *ofp.OfpOxmOfbField { |
| 302 | return &ofp.OfpOxmOfbField{Type: IPV6_EXTHDR, Value: &ofp.OfpOxmOfbField_Ipv6Exthdr{Ipv6Exthdr: ipv6Exthdr}} |
| 303 | } |
| 304 | |
| 305 | //frequently used extractors |
| 306 | |
| 307 | func excludeAction(action *ofp.OfpAction, exclude ...ofp.OfpActionType) bool { |
| 308 | for _, actionToExclude := range exclude { |
| 309 | if action.Type == actionToExclude { |
| 310 | return true |
| 311 | } |
| 312 | } |
| 313 | return false |
| 314 | } |
| 315 | |
| 316 | func GetActions(flow *ofp.OfpFlowStats, exclude ...ofp.OfpActionType) []*ofp.OfpAction { |
| 317 | if flow == nil { |
| 318 | return nil |
| 319 | } |
| 320 | for _, instruction := range flow.Instructions { |
| 321 | if instruction.Type == uint32(ofp.OfpInstructionType_OFPIT_APPLY_ACTIONS) { |
| 322 | instActions := instruction.GetActions() |
| 323 | if instActions == nil { |
| 324 | return nil |
| 325 | } |
| 326 | if len(exclude) == 0 { |
| 327 | return instActions.Actions |
| 328 | } else { |
| 329 | filteredAction := make([]*ofp.OfpAction, 0) |
| 330 | for _, action := range instActions.Actions { |
| 331 | if !excludeAction(action, exclude...) { |
| 332 | filteredAction = append(filteredAction, action) |
| 333 | } |
| 334 | } |
| 335 | return filteredAction |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | return nil |
| 340 | } |
| 341 | |
| 342 | func UpdateOutputPortByActionType(flow *ofp.OfpFlowStats, actionType uint32, toPort uint32) *ofp.OfpFlowStats { |
| 343 | if flow == nil { |
| 344 | return nil |
| 345 | } |
| 346 | nFlow := (proto.Clone(flow)).(*ofp.OfpFlowStats) |
| 347 | nFlow.Instructions = nil |
| 348 | nInsts := make([]*ofp.OfpInstruction, 0) |
| 349 | for _, instruction := range flow.Instructions { |
| 350 | if instruction.Type == actionType { |
| 351 | instActions := instruction.GetActions() |
| 352 | if instActions == nil { |
| 353 | return nil |
| 354 | } |
| 355 | nActions := make([]*ofp.OfpAction, 0) |
| 356 | for _, action := range instActions.Actions { |
| 357 | if action.GetOutput() != nil { |
| 358 | nActions = append(nActions, Output(toPort)) |
| 359 | } else { |
| 360 | nActions = append(nActions, action) |
| 361 | } |
| 362 | } |
| 363 | instructionAction := ofp.OfpInstruction_Actions{Actions: &ofp.OfpInstructionActions{Actions: nActions}} |
| 364 | nInsts = append(nInsts, &ofp.OfpInstruction{Type: uint32(APPLY_ACTIONS), Data: &instructionAction}) |
| 365 | } else { |
| 366 | nInsts = append(nInsts, instruction) |
| 367 | } |
| 368 | } |
| 369 | nFlow.Instructions = nInsts |
| 370 | return nFlow |
| 371 | } |
| 372 | |
| 373 | func excludeOxmOfbField(field *ofp.OfpOxmOfbField, exclude ...ofp.OxmOfbFieldTypes) bool { |
| 374 | for _, fieldToExclude := range exclude { |
| 375 | if field.Type == fieldToExclude { |
| 376 | return true |
| 377 | } |
| 378 | } |
| 379 | return false |
| 380 | } |
| 381 | |
| 382 | func GetOfbFields(flow *ofp.OfpFlowStats, exclude ...ofp.OxmOfbFieldTypes) []*ofp.OfpOxmOfbField { |
| 383 | if flow == nil || flow.Match == nil || flow.Match.Type != ofp.OfpMatchType_OFPMT_OXM { |
| 384 | return nil |
| 385 | } |
| 386 | ofbFields := make([]*ofp.OfpOxmOfbField, 0) |
| 387 | for _, field := range flow.Match.OxmFields { |
| 388 | if field.OxmClass == ofp.OfpOxmClass_OFPXMC_OPENFLOW_BASIC { |
| 389 | ofbFields = append(ofbFields, field.GetOfbField()) |
| 390 | } |
| 391 | } |
| 392 | if len(exclude) == 0 { |
| 393 | return ofbFields |
| 394 | } else { |
| 395 | filteredFields := make([]*ofp.OfpOxmOfbField, 0) |
| 396 | for _, ofbField := range ofbFields { |
| 397 | if !excludeOxmOfbField(ofbField, exclude...) { |
| 398 | filteredFields = append(filteredFields, ofbField) |
| 399 | } |
| 400 | } |
| 401 | return filteredFields |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | func GetPacketOutPort(packet *ofp.OfpPacketOut) uint32 { |
| 406 | if packet == nil { |
| 407 | return 0 |
| 408 | } |
| 409 | for _, action := range packet.GetActions() { |
| 410 | if action.Type == OUTPUT { |
| 411 | return action.GetOutput().Port |
| 412 | } |
| 413 | } |
| 414 | return 0 |
| 415 | } |
| 416 | |
| 417 | func GetOutPort(flow *ofp.OfpFlowStats) uint32 { |
| 418 | if flow == nil { |
| 419 | return 0 |
| 420 | } |
| 421 | for _, action := range GetActions(flow) { |
| 422 | if action.Type == OUTPUT { |
| 423 | out := action.GetOutput() |
| 424 | if out == nil { |
| 425 | return 0 |
| 426 | } |
| 427 | return out.GetPort() |
| 428 | } |
| 429 | } |
| 430 | return 0 |
| 431 | } |
| 432 | |
| 433 | func GetInPort(flow *ofp.OfpFlowStats) uint32 { |
| 434 | if flow == nil { |
| 435 | return 0 |
| 436 | } |
| 437 | for _, field := range GetOfbFields(flow) { |
| 438 | if field.Type == IN_PORT { |
| 439 | return field.GetPort() |
| 440 | } |
| 441 | } |
| 442 | return 0 |
| 443 | } |
| 444 | |
| 445 | func GetGotoTableId(flow *ofp.OfpFlowStats) uint32 { |
| 446 | if flow == nil { |
| 447 | return 0 |
| 448 | } |
| 449 | for _, instruction := range flow.Instructions { |
| 450 | if instruction.Type == uint32(ofp.OfpInstructionType_OFPIT_GOTO_TABLE) { |
| 451 | gotoTable := instruction.GetGotoTable() |
| 452 | if gotoTable == nil { |
| 453 | return 0 |
| 454 | } |
| 455 | return gotoTable.GetTableId() |
| 456 | } |
| 457 | } |
| 458 | return 0 |
| 459 | } |
| 460 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 461 | func GetMeterId(flow *ofp.OfpFlowStats) uint32 { |
| 462 | if flow == nil { |
| 463 | return 0 |
| 464 | } |
| 465 | for _, instruction := range flow.Instructions { |
| 466 | if instruction.Type == uint32(ofp.OfpInstructionType_OFPIT_METER) { |
| 467 | MeterInstruction := instruction.GetMeter() |
| 468 | if MeterInstruction == nil { |
| 469 | return 0 |
| 470 | } |
| 471 | return MeterInstruction.GetMeterId() |
| 472 | } |
| 473 | } |
| 474 | return 0 |
| 475 | } |
| 476 | |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 477 | func GetTunnelId(flow *ofp.OfpFlowStats) uint64 { |
| 478 | if flow == nil { |
| 479 | return 0 |
| 480 | } |
| 481 | for _, field := range GetOfbFields(flow) { |
| 482 | if field.Type == TUNNEL_ID { |
| 483 | return field.GetTunnelId() |
| 484 | } |
| 485 | } |
| 486 | return 0 |
| 487 | } |
| 488 | |
| 489 | //GetMetaData - legacy get method (only want lower 32 bits) |
| 490 | func GetMetaData(flow *ofp.OfpFlowStats) uint32 { |
| 491 | if flow == nil { |
| 492 | return 0 |
| 493 | } |
| 494 | for _, field := range GetOfbFields(flow) { |
| 495 | if field.Type == METADATA { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 496 | return uint32(field.GetTableMetadata() & 0xFFFFFFFF) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 497 | } |
| 498 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 499 | log.Debug("No-metadata-present") |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 500 | return 0 |
| 501 | } |
| 502 | |
| 503 | func GetMetaData64Bit(flow *ofp.OfpFlowStats) uint64 { |
| 504 | if flow == nil { |
| 505 | return 0 |
| 506 | } |
| 507 | for _, field := range GetOfbFields(flow) { |
| 508 | if field.Type == METADATA { |
| 509 | return field.GetTableMetadata() |
| 510 | } |
| 511 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 512 | log.Debug("No-metadata-present") |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 513 | return 0 |
| 514 | } |
| 515 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 516 | // function returns write metadata value from write_metadata action field |
| 517 | func GetMetadataFromWriteMetadataAction(flow *ofp.OfpFlowStats) uint64 { |
| 518 | if flow != nil { |
| 519 | for _, instruction := range flow.Instructions { |
| 520 | if instruction.Type == uint32(WRITE_METADATA) { |
| 521 | if writeMetadata := instruction.GetWriteMetadata(); writeMetadata != nil { |
| 522 | return writeMetadata.GetMetadata() |
| 523 | } |
| 524 | } |
| 525 | } |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 526 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 527 | log.Debugw("No-write-metadata-present", log.Fields{"flow": flow}) |
| 528 | return 0 |
| 529 | } |
| 530 | |
| 531 | func GetTechProfileIDFromWriteMetaData(metadata uint64) uint16 { |
| 532 | /* |
| 533 | Write metadata instruction value (metadata) is 8 bytes: |
| 534 | MS 2 bytes: C Tag |
| 535 | Next 2 bytes: Technology Profile Id |
| 536 | Next 4 bytes: Port number (uni or nni) |
| 537 | |
| 538 | This is set in the ONOS OltPipeline as a write metadata instruction |
| 539 | */ |
| 540 | var tpId uint16 = 0 |
| 541 | log.Debugw("Write metadata value for Techprofile ID", log.Fields{"metadata": metadata}) |
| 542 | if metadata != 0 { |
| 543 | tpId = uint16((metadata >> 32) & 0xFFFF) |
| 544 | log.Debugw("Found techprofile ID from write metadata action", log.Fields{"tpid": tpId}) |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 545 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 546 | return tpId |
| 547 | } |
| 548 | |
| 549 | func GetEgressPortNumberFromWriteMetadata(flow *ofp.OfpFlowStats) uint32 { |
| 550 | /* |
| 551 | Write metadata instruction value (metadata) is 8 bytes: |
| 552 | MS 2 bytes: C Tag |
| 553 | Next 2 bytes: Technology Profile Id |
| 554 | Next 4 bytes: Port number (uni or nni) |
| 555 | This is set in the ONOS OltPipeline as a write metadata instruction |
| 556 | */ |
| 557 | var uniPort uint32 = 0 |
| 558 | md := GetMetadataFromWriteMetadataAction(flow) |
| 559 | log.Debugw("Metadata found for egress/uni port ", log.Fields{"metadata": md}) |
| 560 | if md != 0 { |
| 561 | uniPort = uint32(md & 0xFFFFFFFF) |
| 562 | log.Debugw("Found EgressPort from write metadata action", log.Fields{"egress_port": uniPort}) |
| 563 | } |
| 564 | return uniPort |
| 565 | |
| 566 | } |
| 567 | |
| 568 | func GetInnerTagFromMetaData(flow *ofp.OfpFlowStats) uint16 { |
| 569 | /* |
| 570 | Write metadata instruction value (metadata) is 8 bytes: |
| 571 | MS 2 bytes: C Tag |
| 572 | Next 2 bytes: Technology Profile Id |
| 573 | Next 4 bytes: Port number (uni or nni) |
| 574 | This is set in the ONOS OltPipeline as a write metadata instruction |
| 575 | */ |
| 576 | var innerTag uint16 = 0 |
| 577 | md := GetMetadataFromWriteMetadataAction(flow) |
| 578 | if md != 0 { |
| 579 | innerTag = uint16((md >> 48) & 0xFFFF) |
| 580 | log.Debugw("Found CVLAN from write metadate action", log.Fields{"c_vlan": innerTag}) |
| 581 | } |
| 582 | return innerTag |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | //GetInnerTagFromMetaData retrieves the inner tag from the Metadata_ofp. The port number (UNI on ONU) is in the |
| 586 | // lower 32-bits of Metadata_ofp and the inner_tag is in the upper 32-bits. This is set in the ONOS OltPipeline as |
| 587 | //// a Metadata_ofp field |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 588 | /*func GetInnerTagFromMetaData(flow *ofp.OfpFlowStats) uint64 { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 589 | md := GetMetaData64Bit(flow) |
| 590 | if md == 0 { |
| 591 | return 0 |
| 592 | } |
| 593 | if md <= 0xffffffff { |
| 594 | log.Debugw("onos-upgrade-suggested", log.Fields{"Metadata_ofp": md, "message": "Legacy MetaData detected form OltPipeline"}) |
| 595 | return md |
| 596 | } |
| 597 | return (md >> 32) & 0xffffffff |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 598 | }*/ |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 599 | |
| 600 | // Extract the child device port from a flow that contains the parent device peer port. Typically the UNI port of an |
| 601 | // ONU child device. Per TST agreement this will be the lower 32 bits of tunnel id reserving upper 32 bits for later |
| 602 | // use |
| 603 | func GetChildPortFromTunnelId(flow *ofp.OfpFlowStats) uint32 { |
| 604 | tid := GetTunnelId(flow) |
| 605 | if tid == 0 { |
| 606 | return 0 |
| 607 | } |
| 608 | // Per TST agreement we are keeping any child port id (uni port id) in the lower 32 bits |
| 609 | return uint32(tid & 0xffffffff) |
| 610 | } |
| 611 | |
| 612 | func HasNextTable(flow *ofp.OfpFlowStats) bool { |
| 613 | if flow == nil { |
| 614 | return false |
| 615 | } |
| 616 | return GetGotoTableId(flow) != 0 |
| 617 | } |
| 618 | |
| 619 | func GetGroup(flow *ofp.OfpFlowStats) uint32 { |
| 620 | if flow == nil { |
| 621 | return 0 |
| 622 | } |
| 623 | for _, action := range GetActions(flow) { |
| 624 | if action.Type == GROUP { |
| 625 | grp := action.GetGroup() |
| 626 | if grp == nil { |
| 627 | return 0 |
| 628 | } |
| 629 | return grp.GetGroupId() |
| 630 | } |
| 631 | } |
| 632 | return 0 |
| 633 | } |
| 634 | |
| 635 | func HasGroup(flow *ofp.OfpFlowStats) bool { |
| 636 | return GetGroup(flow) != 0 |
| 637 | } |
| 638 | |
| 639 | // GetNextTableId returns the next table ID if the "table_id" is present in the map, otherwise return nil |
| 640 | func GetNextTableId(kw OfpFlowModArgs) *uint32 { |
| 641 | if val, exist := kw["table_id"]; exist { |
| 642 | ret := uint32(val) |
| 643 | return &ret |
| 644 | } |
| 645 | return nil |
| 646 | } |
| 647 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 648 | // GetMeterIdFlowModArgs returns the meterId if the "meter_id" is present in the map, otherwise return 0 |
| 649 | func GetMeterIdFlowModArgs(kw OfpFlowModArgs) uint32 { |
| 650 | if val, exist := kw["meter_id"]; exist { |
| 651 | return uint32(val) |
| 652 | } |
| 653 | return 0 |
| 654 | } |
| 655 | |
| 656 | // Function returns the metadata if the "write_metadata" is present in the map, otherwise return nil |
| 657 | func GetMetadataFlowModArgs(kw OfpFlowModArgs) uint64 { |
| 658 | if val, exist := kw["write_metadata"]; exist { |
| 659 | ret := uint64(val) |
| 660 | return ret |
| 661 | } |
| 662 | return 0 |
| 663 | } |
| 664 | |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 665 | // Return unique 64-bit integer hash for flow covering the following attributes: |
| 666 | // 'table_id', 'priority', 'flags', 'cookie', 'match', '_instruction_string' |
| 667 | func HashFlowStats(flow *ofp.OfpFlowStats) uint64 { |
| 668 | if flow == nil { // Should never happen |
| 669 | return 0 |
| 670 | } |
| 671 | // Create string with the instructions field first |
| 672 | var instructionString bytes.Buffer |
| 673 | for _, instruction := range flow.Instructions { |
| 674 | instructionString.WriteString(instruction.String()) |
| 675 | } |
| 676 | var flowString = fmt.Sprintf("%d%d%d%d%s%s", flow.TableId, flow.Priority, flow.Flags, flow.Cookie, flow.Match.String(), instructionString.String()) |
| 677 | h := md5.New() |
| 678 | h.Write([]byte(flowString)) |
| 679 | hash := big.NewInt(0) |
| 680 | hash.SetBytes(h.Sum(nil)) |
| 681 | return hash.Uint64() |
| 682 | } |
| 683 | |
| 684 | // flowStatsEntryFromFlowModMessage maps an ofp_flow_mod message to an ofp_flow_stats message |
| 685 | func FlowStatsEntryFromFlowModMessage(mod *ofp.OfpFlowMod) *ofp.OfpFlowStats { |
| 686 | flow := &ofp.OfpFlowStats{} |
| 687 | if mod == nil { |
| 688 | return flow |
| 689 | } |
| 690 | flow.TableId = mod.TableId |
| 691 | flow.Priority = mod.Priority |
| 692 | flow.IdleTimeout = mod.IdleTimeout |
| 693 | flow.HardTimeout = mod.HardTimeout |
| 694 | flow.Flags = mod.Flags |
| 695 | flow.Cookie = mod.Cookie |
| 696 | flow.Match = mod.Match |
| 697 | flow.Instructions = mod.Instructions |
| 698 | flow.Id = HashFlowStats(flow) |
| 699 | return flow |
| 700 | } |
| 701 | |
| 702 | func GroupEntryFromGroupMod(mod *ofp.OfpGroupMod) *ofp.OfpGroupEntry { |
| 703 | group := &ofp.OfpGroupEntry{} |
| 704 | if mod == nil { |
| 705 | return group |
| 706 | } |
| 707 | group.Desc = &ofp.OfpGroupDesc{Type: mod.Type, GroupId: mod.GroupId, Buckets: mod.Buckets} |
| 708 | group.Stats = &ofp.OfpGroupStats{GroupId: mod.GroupId} |
| 709 | //TODO do we need to instantiate bucket bins? |
| 710 | return group |
| 711 | } |
| 712 | |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 713 | // flowStatsEntryFromFlowModMessage maps an ofp_flow_mod message to an ofp_flow_stats message |
| 714 | func MeterEntryFromMeterMod(meterMod *ofp.OfpMeterMod) *ofp.OfpMeterEntry { |
| 715 | bandStats := make([]*ofp.OfpMeterBandStats, 0) |
| 716 | meter := &ofp.OfpMeterEntry{Config: &ofp.OfpMeterConfig{}, |
| 717 | Stats: &ofp.OfpMeterStats{BandStats: bandStats}} |
| 718 | if meterMod == nil { |
| 719 | log.Error("Invalid meter mod command") |
| 720 | return meter |
| 721 | } |
| 722 | // config init |
| 723 | meter.Config.MeterId = meterMod.MeterId |
| 724 | meter.Config.Flags = meterMod.Flags |
| 725 | meter.Config.Bands = meterMod.Bands |
| 726 | // meter stats init |
| 727 | meter.Stats.MeterId = meterMod.MeterId |
| 728 | meter.Stats.FlowCount = 0 |
| 729 | meter.Stats.PacketInCount = 0 |
| 730 | meter.Stats.ByteInCount = 0 |
| 731 | meter.Stats.DurationSec = 0 |
| 732 | meter.Stats.DurationNsec = 0 |
| 733 | // band stats init |
| 734 | for _, _ = range meterMod.Bands { |
| 735 | band := &ofp.OfpMeterBandStats{} |
| 736 | band.PacketBandCount = 0 |
| 737 | band.ByteBandCount = 0 |
| 738 | bandStats = append(bandStats, band) |
| 739 | } |
| 740 | meter.Stats.BandStats = bandStats |
| 741 | log.Debugw("Allocated meter entry", log.Fields{"meter": *meter}) |
| 742 | return meter |
| 743 | |
| 744 | } |
| 745 | |
| 746 | func GetMeterIdFromFlow(flow *ofp.OfpFlowStats) uint32 { |
| 747 | if flow != nil { |
| 748 | for _, instruction := range flow.Instructions { |
| 749 | if instruction.Type == uint32(METER_ACTION) { |
| 750 | if meterInst := instruction.GetMeter(); meterInst != nil { |
| 751 | return meterInst.GetMeterId() |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | return uint32(0) |
| 758 | } |
| 759 | |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 760 | func MkOxmFields(matchFields []ofp.OfpOxmField) []*ofp.OfpOxmField { |
| 761 | oxmFields := make([]*ofp.OfpOxmField, 0) |
| 762 | for _, matchField := range matchFields { |
| 763 | oxmField := ofp.OfpOxmField{OxmClass: ofp.OfpOxmClass_OFPXMC_OPENFLOW_BASIC, Field: matchField.Field} |
| 764 | oxmFields = append(oxmFields, &oxmField) |
| 765 | } |
| 766 | return oxmFields |
| 767 | } |
| 768 | |
| 769 | func MkInstructionsFromActions(actions []*ofp.OfpAction) []*ofp.OfpInstruction { |
| 770 | instructions := make([]*ofp.OfpInstruction, 0) |
| 771 | instructionAction := ofp.OfpInstruction_Actions{Actions: &ofp.OfpInstructionActions{Actions: actions}} |
| 772 | instruction := ofp.OfpInstruction{Type: uint32(APPLY_ACTIONS), Data: &instructionAction} |
| 773 | instructions = append(instructions, &instruction) |
| 774 | return instructions |
| 775 | } |
| 776 | |
| 777 | // Convenience function to generare ofp_flow_mod message with OXM BASIC match composed from the match_fields, and |
| 778 | // single APPLY_ACTIONS instruction with a list if ofp_action objects. |
| 779 | func MkSimpleFlowMod(matchFields []*ofp.OfpOxmField, actions []*ofp.OfpAction, command *ofp.OfpFlowModCommand, kw OfpFlowModArgs) *ofp.OfpFlowMod { |
| 780 | |
| 781 | // Process actions instructions |
| 782 | instructions := make([]*ofp.OfpInstruction, 0) |
| 783 | instructionAction := ofp.OfpInstruction_Actions{Actions: &ofp.OfpInstructionActions{Actions: actions}} |
| 784 | instruction := ofp.OfpInstruction{Type: uint32(APPLY_ACTIONS), Data: &instructionAction} |
| 785 | instructions = append(instructions, &instruction) |
| 786 | |
| 787 | // Process next table |
| 788 | if tableId := GetNextTableId(kw); tableId != nil { |
| 789 | var instGotoTable ofp.OfpInstruction_GotoTable |
| 790 | instGotoTable.GotoTable = &ofp.OfpInstructionGotoTable{TableId: *tableId} |
| 791 | inst := ofp.OfpInstruction{Type: uint32(ofp.OfpInstructionType_OFPIT_GOTO_TABLE), Data: &instGotoTable} |
| 792 | instructions = append(instructions, &inst) |
| 793 | } |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 794 | // Process meter action |
| 795 | if meterId := GetMeterIdFlowModArgs(kw); meterId != 0 { |
| 796 | var instMeter ofp.OfpInstruction_Meter |
| 797 | instMeter.Meter = &ofp.OfpInstructionMeter{MeterId: meterId} |
| 798 | inst := ofp.OfpInstruction{Type: uint32(METER_ACTION), Data: &instMeter} |
| 799 | instructions = append(instructions, &inst) |
| 800 | } |
| 801 | //process write_metadata action |
| 802 | if metadata := GetMetadataFlowModArgs(kw); metadata != 0 { |
| 803 | var instWriteMetadata ofp.OfpInstruction_WriteMetadata |
| 804 | instWriteMetadata.WriteMetadata = &ofp.OfpInstructionWriteMetadata{Metadata: metadata} |
| 805 | inst := ofp.OfpInstruction{Type: uint32(WRITE_METADATA), Data: &instWriteMetadata} |
| 806 | instructions = append(instructions, &inst) |
| 807 | } |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 808 | |
| 809 | // Process match fields |
| 810 | oxmFields := make([]*ofp.OfpOxmField, 0) |
| 811 | for _, matchField := range matchFields { |
| 812 | oxmField := ofp.OfpOxmField{OxmClass: ofp.OfpOxmClass_OFPXMC_OPENFLOW_BASIC, Field: matchField.Field} |
| 813 | oxmFields = append(oxmFields, &oxmField) |
| 814 | } |
| 815 | var match ofp.OfpMatch |
| 816 | match.Type = ofp.OfpMatchType_OFPMT_OXM |
| 817 | match.OxmFields = oxmFields |
| 818 | |
| 819 | // Create ofp_flow_message |
| 820 | msg := &ofp.OfpFlowMod{} |
| 821 | if command == nil { |
| 822 | msg.Command = ofp.OfpFlowModCommand_OFPFC_ADD |
| 823 | } else { |
| 824 | msg.Command = *command |
| 825 | } |
| 826 | msg.Instructions = instructions |
| 827 | msg.Match = &match |
| 828 | |
| 829 | // Set the variadic argument values |
| 830 | msg = setVariadicModAttributes(msg, kw) |
| 831 | |
| 832 | return msg |
| 833 | } |
| 834 | |
| 835 | func MkMulticastGroupMod(groupId uint32, buckets []*ofp.OfpBucket, command *ofp.OfpGroupModCommand) *ofp.OfpGroupMod { |
| 836 | group := &ofp.OfpGroupMod{} |
| 837 | if command == nil { |
| 838 | group.Command = ofp.OfpGroupModCommand_OFPGC_ADD |
| 839 | } else { |
| 840 | group.Command = *command |
| 841 | } |
| 842 | group.Type = ofp.OfpGroupType_OFPGT_ALL |
| 843 | group.GroupId = groupId |
| 844 | group.Buckets = buckets |
| 845 | return group |
| 846 | } |
| 847 | |
| 848 | //SetVariadicModAttributes sets only uint64 or uint32 fields of the ofp_flow_mod message |
| 849 | func setVariadicModAttributes(mod *ofp.OfpFlowMod, args OfpFlowModArgs) *ofp.OfpFlowMod { |
| 850 | if args == nil { |
| 851 | return mod |
| 852 | } |
| 853 | for key, val := range args { |
| 854 | switch key { |
| 855 | case "cookie": |
| 856 | mod.Cookie = val |
| 857 | case "cookie_mask": |
| 858 | mod.CookieMask = val |
| 859 | case "table_id": |
| 860 | mod.TableId = uint32(val) |
| 861 | case "idle_timeout": |
| 862 | mod.IdleTimeout = uint32(val) |
| 863 | case "hard_timeout": |
| 864 | mod.HardTimeout = uint32(val) |
| 865 | case "priority": |
| 866 | mod.Priority = uint32(val) |
| 867 | case "buffer_id": |
| 868 | mod.BufferId = uint32(val) |
| 869 | case "out_port": |
| 870 | mod.OutPort = uint32(val) |
| 871 | case "out_group": |
| 872 | mod.OutGroup = uint32(val) |
| 873 | case "flags": |
| 874 | mod.Flags = uint32(val) |
| 875 | } |
| 876 | } |
| 877 | return mod |
| 878 | } |
| 879 | |
| 880 | func MkPacketIn(port uint32, packet []byte) *ofp.OfpPacketIn { |
| 881 | packetIn := &ofp.OfpPacketIn{ |
| 882 | Reason: ofp.OfpPacketInReason_OFPR_ACTION, |
| 883 | Match: &ofp.OfpMatch{ |
| 884 | Type: ofp.OfpMatchType_OFPMT_OXM, |
| 885 | OxmFields: []*ofp.OfpOxmField{ |
| 886 | { |
| 887 | OxmClass: ofp.OfpOxmClass_OFPXMC_OPENFLOW_BASIC, |
| 888 | Field: &ofp.OfpOxmField_OfbField{ |
| 889 | OfbField: InPort(port)}, |
| 890 | }, |
| 891 | }, |
| 892 | }, |
| 893 | Data: packet, |
| 894 | } |
| 895 | return packetIn |
| 896 | } |
| 897 | |
| 898 | // MkFlowStat is a helper method to build flows |
| 899 | func MkFlowStat(fa *FlowArgs) *ofp.OfpFlowStats { |
Manikkaraj k | b1a1092 | 2019-07-29 12:10:34 -0400 | [diff] [blame] | 900 | //Build the match-fields |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 901 | matchFields := make([]*ofp.OfpOxmField, 0) |
| 902 | for _, val := range fa.MatchFields { |
| 903 | matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}}) |
| 904 | } |
| 905 | return FlowStatsEntryFromFlowModMessage(MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)) |
| 906 | } |
| 907 | |
| 908 | func MkGroupStat(ga *GroupArgs) *ofp.OfpGroupEntry { |
| 909 | return GroupEntryFromGroupMod(MkMulticastGroupMod(ga.GroupId, ga.Buckets, ga.Command)) |
| 910 | } |
| 911 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 912 | type OfpFlowModArgs map[string]uint64 |
| 913 | |
| 914 | type FlowArgs struct { |
| 915 | MatchFields []*ofp.OfpOxmOfbField |
| 916 | Actions []*ofp.OfpAction |
| 917 | Command *ofp.OfpFlowModCommand |
| 918 | Priority uint32 |
| 919 | KV OfpFlowModArgs |
| 920 | } |
| 921 | |
khenaidoo | d20a585 | 2018-10-22 22:09:55 -0400 | [diff] [blame] | 922 | type GroupArgs struct { |
| 923 | GroupId uint32 |
| 924 | Buckets []*ofp.OfpBucket |
| 925 | Command *ofp.OfpGroupModCommand |
| 926 | } |
| 927 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 928 | type FlowsAndGroups struct { |
| 929 | Flows *ordered_map.OrderedMap |
| 930 | Groups *ordered_map.OrderedMap |
| 931 | } |
| 932 | |
| 933 | func NewFlowsAndGroups() *FlowsAndGroups { |
| 934 | var fg FlowsAndGroups |
| 935 | fg.Flows = ordered_map.NewOrderedMap() |
| 936 | fg.Groups = ordered_map.NewOrderedMap() |
| 937 | return &fg |
| 938 | } |
| 939 | |
| 940 | func (fg *FlowsAndGroups) Copy() *FlowsAndGroups { |
| 941 | copyFG := NewFlowsAndGroups() |
| 942 | iter := fg.Flows.IterFunc() |
| 943 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 944 | if protoMsg, isMsg := kv.Value.(*ofp.OfpFlowStats); isMsg { |
| 945 | copyFG.Flows.Set(kv.Key, proto.Clone(protoMsg)) |
| 946 | } |
| 947 | } |
| 948 | iter = fg.Groups.IterFunc() |
| 949 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 950 | if protoMsg, isMsg := kv.Value.(*ofp.OfpGroupEntry); isMsg { |
| 951 | copyFG.Groups.Set(kv.Key, proto.Clone(protoMsg)) |
| 952 | } |
| 953 | } |
| 954 | return copyFG |
| 955 | } |
| 956 | |
| 957 | func (fg *FlowsAndGroups) GetFlow(index int) *ofp.OfpFlowStats { |
| 958 | iter := fg.Flows.IterFunc() |
| 959 | pos := 0 |
| 960 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 961 | if pos == index { |
| 962 | if protoMsg, isMsg := kv.Value.(*ofp.OfpFlowStats); isMsg { |
| 963 | return protoMsg |
| 964 | } |
| 965 | return nil |
| 966 | } |
| 967 | pos += 1 |
| 968 | } |
| 969 | return nil |
| 970 | } |
| 971 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 972 | func (fg *FlowsAndGroups) ListFlows() []*ofp.OfpFlowStats { |
| 973 | flows := make([]*ofp.OfpFlowStats, 0) |
| 974 | iter := fg.Flows.IterFunc() |
| 975 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 976 | if protoMsg, isMsg := kv.Value.(*ofp.OfpFlowStats); isMsg { |
| 977 | flows = append(flows, protoMsg) |
| 978 | } |
| 979 | } |
| 980 | return flows |
| 981 | } |
| 982 | |
| 983 | func (fg *FlowsAndGroups) ListGroups() []*ofp.OfpGroupEntry { |
| 984 | groups := make([]*ofp.OfpGroupEntry, 0) |
| 985 | iter := fg.Groups.IterFunc() |
| 986 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 987 | if protoMsg, isMsg := kv.Value.(*ofp.OfpGroupEntry); isMsg { |
| 988 | groups = append(groups, protoMsg) |
| 989 | } |
| 990 | } |
| 991 | return groups |
| 992 | } |
| 993 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 994 | func (fg *FlowsAndGroups) String() string { |
| 995 | var buffer bytes.Buffer |
| 996 | iter := fg.Flows.IterFunc() |
| 997 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 998 | if protoMsg, isMsg := kv.Value.(*ofp.OfpFlowStats); isMsg { |
| 999 | buffer.WriteString("\nFlow:\n") |
| 1000 | buffer.WriteString(proto.MarshalTextString(protoMsg)) |
| 1001 | buffer.WriteString("\n") |
| 1002 | } |
| 1003 | } |
| 1004 | iter = fg.Groups.IterFunc() |
| 1005 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 1006 | if protoMsg, isMsg := kv.Value.(*ofp.OfpGroupEntry); isMsg { |
| 1007 | buffer.WriteString("\nGroup:\n") |
| 1008 | buffer.WriteString(proto.MarshalTextString(protoMsg)) |
| 1009 | buffer.WriteString("\n") |
| 1010 | } |
| 1011 | } |
| 1012 | return buffer.String() |
| 1013 | } |
| 1014 | |
| 1015 | func (fg *FlowsAndGroups) AddFlow(flow *ofp.OfpFlowStats) { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1016 | if flow == nil { |
| 1017 | return |
| 1018 | } |
| 1019 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1020 | if fg.Flows == nil { |
| 1021 | fg.Flows = ordered_map.NewOrderedMap() |
| 1022 | } |
| 1023 | if fg.Groups == nil { |
| 1024 | fg.Groups = ordered_map.NewOrderedMap() |
| 1025 | } |
| 1026 | //Add flow only if absent |
| 1027 | if _, exist := fg.Flows.Get(flow.Id); !exist { |
| 1028 | fg.Flows.Set(flow.Id, flow) |
| 1029 | } |
| 1030 | } |
| 1031 | |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1032 | func (fg *FlowsAndGroups) AddGroup(group *ofp.OfpGroupEntry) { |
| 1033 | if group == nil { |
| 1034 | return |
| 1035 | } |
| 1036 | |
| 1037 | if fg.Flows == nil { |
| 1038 | fg.Flows = ordered_map.NewOrderedMap() |
| 1039 | } |
| 1040 | if fg.Groups == nil { |
| 1041 | fg.Groups = ordered_map.NewOrderedMap() |
| 1042 | } |
| 1043 | //Add group only if absent |
| 1044 | if _, exist := fg.Groups.Get(group.Desc.GroupId); !exist { |
| 1045 | fg.Groups.Set(group.Desc.GroupId, group) |
| 1046 | } |
| 1047 | } |
| 1048 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1049 | //AddFrom add flows and groups from the argument into this structure only if they do not already exist |
| 1050 | func (fg *FlowsAndGroups) AddFrom(from *FlowsAndGroups) { |
| 1051 | iter := from.Flows.IterFunc() |
| 1052 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 1053 | if protoMsg, isMsg := kv.Value.(*ofp.OfpFlowStats); isMsg { |
| 1054 | if _, exist := fg.Flows.Get(protoMsg.Id); !exist { |
| 1055 | fg.Flows.Set(protoMsg.Id, protoMsg) |
| 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | iter = from.Groups.IterFunc() |
| 1060 | for kv, ok := iter(); ok; kv, ok = iter() { |
| 1061 | if protoMsg, isMsg := kv.Value.(*ofp.OfpGroupEntry); isMsg { |
| 1062 | if _, exist := fg.Groups.Get(protoMsg.Stats.GroupId); !exist { |
| 1063 | fg.Groups.Set(protoMsg.Stats.GroupId, protoMsg) |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | type DeviceRules struct { |
| 1070 | Rules map[string]*FlowsAndGroups |
| 1071 | } |
| 1072 | |
| 1073 | func NewDeviceRules() *DeviceRules { |
| 1074 | var dr DeviceRules |
| 1075 | dr.Rules = make(map[string]*FlowsAndGroups) |
| 1076 | return &dr |
| 1077 | } |
| 1078 | |
| 1079 | func (dr *DeviceRules) Copy() *DeviceRules { |
| 1080 | copyDR := NewDeviceRules() |
manikkaraj k | 6c9689d | 2019-05-09 12:59:52 -0400 | [diff] [blame] | 1081 | if dr != nil { |
| 1082 | for key, val := range dr.Rules { |
| 1083 | if val != nil { |
| 1084 | copyDR.Rules[key] = val.Copy() |
| 1085 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1086 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1087 | } |
| 1088 | return copyDR |
| 1089 | } |
| 1090 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1091 | func (dr *DeviceRules) ClearFlows(deviceId string) { |
| 1092 | if _, exist := dr.Rules[deviceId]; exist { |
| 1093 | dr.Rules[deviceId].Flows = ordered_map.NewOrderedMap() |
| 1094 | } |
| 1095 | } |
| 1096 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 1097 | func (dr *DeviceRules) FilterRules(deviceIds map[string]string) *DeviceRules { |
| 1098 | filteredDR := NewDeviceRules() |
| 1099 | for key, val := range dr.Rules { |
| 1100 | if _, exist := deviceIds[key]; exist { |
| 1101 | filteredDR.Rules[key] = val.Copy() |
| 1102 | } |
| 1103 | } |
| 1104 | return filteredDR |
| 1105 | } |
| 1106 | |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1107 | func (dr *DeviceRules) AddFlow(deviceId string, flow *ofp.OfpFlowStats) { |
| 1108 | if _, exist := dr.Rules[deviceId]; !exist { |
| 1109 | dr.Rules[deviceId] = NewFlowsAndGroups() |
| 1110 | } |
| 1111 | dr.Rules[deviceId].AddFlow(flow) |
| 1112 | } |
| 1113 | |
| 1114 | func (dr *DeviceRules) GetRules() map[string]*FlowsAndGroups { |
| 1115 | return dr.Rules |
| 1116 | } |
| 1117 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1118 | func (dr *DeviceRules) String() string { |
| 1119 | var buffer bytes.Buffer |
| 1120 | for key, value := range dr.Rules { |
| 1121 | buffer.WriteString("DeviceId:") |
| 1122 | buffer.WriteString(key) |
| 1123 | buffer.WriteString(value.String()) |
| 1124 | buffer.WriteString("\n\n") |
| 1125 | } |
| 1126 | return buffer.String() |
| 1127 | } |
| 1128 | |
| 1129 | func (dr *DeviceRules) AddFlowsAndGroup(deviceId string, fg *FlowsAndGroups) { |
khenaidoo | d20a585 | 2018-10-22 22:09:55 -0400 | [diff] [blame] | 1130 | if _, ok := dr.Rules[deviceId]; !ok { |
| 1131 | dr.Rules[deviceId] = NewFlowsAndGroups() |
| 1132 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1133 | dr.Rules[deviceId] = fg |
| 1134 | } |
khenaidoo | d20a585 | 2018-10-22 22:09:55 -0400 | [diff] [blame] | 1135 | |
| 1136 | // CreateEntryIfNotExist creates a new deviceId in the Map if it does not exist and assigns an |
| 1137 | // empty FlowsAndGroups to it. Otherwise, it does nothing. |
| 1138 | func (dr *DeviceRules) CreateEntryIfNotExist(deviceId string) { |
| 1139 | if _, ok := dr.Rules[deviceId]; !ok { |
| 1140 | dr.Rules[deviceId] = NewFlowsAndGroups() |
| 1141 | } |
| 1142 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1143 | |
| 1144 | /* |
| 1145 | * Common flow routines |
| 1146 | */ |
| 1147 | |
| 1148 | //FindOverlappingFlows return a list of overlapping flow(s) where mod is the flow request |
| 1149 | func FindOverlappingFlows(flows []*ofp.OfpFlowStats, mod *ofp.OfpFlowMod) []*ofp.OfpFlowStats { |
| 1150 | return nil //TODO - complete implementation |
| 1151 | } |
| 1152 | |
| 1153 | // FindFlowById returns the index of the flow in the flows array if present. Otherwise, it returns -1 |
| 1154 | func FindFlowById(flows []*ofp.OfpFlowStats, flow *ofp.OfpFlowStats) int { |
| 1155 | for idx, f := range flows { |
| 1156 | if flow.Id == f.Id { |
| 1157 | return idx |
| 1158 | } |
| 1159 | } |
| 1160 | return -1 |
| 1161 | } |
| 1162 | |
| 1163 | // FindFlows returns the index in flows where flow if present. Otherwise, it returns -1 |
| 1164 | func FindFlows(flows []*ofp.OfpFlowStats, flow *ofp.OfpFlowStats) int { |
| 1165 | for idx, f := range flows { |
| 1166 | if FlowMatch(f, flow) { |
| 1167 | return idx |
| 1168 | } |
| 1169 | } |
| 1170 | return -1 |
| 1171 | } |
| 1172 | |
| 1173 | //FlowMatch returns true if two flows matches on the following flow attributes: |
| 1174 | //TableId, Priority, Flags, Cookie, Match |
| 1175 | func FlowMatch(f1 *ofp.OfpFlowStats, f2 *ofp.OfpFlowStats) bool { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1176 | if f1 == nil || f2 == nil { |
| 1177 | return false |
| 1178 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1179 | keysMatter := []string{"TableId", "Priority", "Flags", "Cookie", "Match"} |
| 1180 | for _, key := range keysMatter { |
| 1181 | switch key { |
| 1182 | case "TableId": |
| 1183 | if f1.TableId != f2.TableId { |
| 1184 | return false |
| 1185 | } |
| 1186 | case "Priority": |
| 1187 | if f1.Priority != f2.Priority { |
| 1188 | return false |
| 1189 | } |
| 1190 | case "Flags": |
| 1191 | if f1.Flags != f2.Flags { |
| 1192 | return false |
| 1193 | } |
| 1194 | case "Cookie": |
| 1195 | if f1.Cookie != f2.Cookie { |
| 1196 | return false |
| 1197 | } |
| 1198 | case "Match": |
| 1199 | if strings.Compare(f1.Match.String(), f2.Match.String()) != 0 { |
| 1200 | return false |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | return true |
| 1205 | } |
| 1206 | |
| 1207 | //FlowMatchesMod returns True if given flow is "covered" by the wildcard flow_mod, taking into consideration of |
| 1208 | //both exact matches as well as masks-based match fields if any. Otherwise return False |
| 1209 | func FlowMatchesMod(flow *ofp.OfpFlowStats, mod *ofp.OfpFlowMod) bool { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1210 | if flow == nil || mod == nil { |
| 1211 | return false |
| 1212 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1213 | //Check if flow.cookie is covered by mod.cookie and mod.cookie_mask |
| 1214 | if (flow.Cookie & mod.CookieMask) != (mod.Cookie & mod.CookieMask) { |
| 1215 | return false |
| 1216 | } |
| 1217 | |
| 1218 | //Check if flow.table_id is covered by flow_mod.table_id |
| 1219 | if mod.TableId != uint32(ofp.OfpTable_OFPTT_ALL) && flow.TableId != mod.TableId { |
| 1220 | return false |
| 1221 | } |
| 1222 | |
| 1223 | //Check out_port |
| 1224 | if (mod.OutPort&0x7fffffff) != uint32(ofp.OfpPortNo_OFPP_ANY) && !FlowHasOutPort(flow, mod.OutPort) { |
| 1225 | return false |
| 1226 | } |
| 1227 | |
| 1228 | // Check out_group |
| 1229 | if (mod.OutGroup&0x7fffffff) != uint32(ofp.OfpGroup_OFPG_ANY) && !FlowHasOutGroup(flow, mod.OutGroup) { |
| 1230 | return false |
| 1231 | } |
| 1232 | |
| 1233 | //Priority is ignored |
| 1234 | |
| 1235 | //Check match condition |
| 1236 | //If the flow_mod match field is empty, that is a special case and indicates the flow entry matches |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1237 | if (mod.Match == nil) || (mod.Match.OxmFields == nil) || (len(mod.Match.OxmFields) == 0) { |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1238 | //If we got this far and the match is empty in the flow spec, than the flow matches |
| 1239 | return true |
| 1240 | } // TODO : implement the flow match analysis |
| 1241 | return false |
| 1242 | |
| 1243 | } |
| 1244 | |
| 1245 | //FlowHasOutPort returns True if flow has a output command with the given out_port |
| 1246 | func FlowHasOutPort(flow *ofp.OfpFlowStats, outPort uint32) bool { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1247 | if flow == nil { |
| 1248 | return false |
| 1249 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1250 | for _, instruction := range flow.Instructions { |
| 1251 | if instruction.Type == uint32(ofp.OfpInstructionType_OFPIT_APPLY_ACTIONS) { |
| 1252 | if instruction.GetActions() == nil { |
| 1253 | return false |
| 1254 | } |
| 1255 | for _, action := range instruction.GetActions().Actions { |
| 1256 | if action.Type == ofp.OfpActionType_OFPAT_OUTPUT { |
| 1257 | if (action.GetOutput() != nil) && (action.GetOutput().Port == outPort) { |
| 1258 | return true |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | } |
| 1263 | } |
| 1264 | } |
| 1265 | return false |
| 1266 | } |
| 1267 | |
| 1268 | //FlowHasOutGroup return True if flow has a output command with the given out_group |
| 1269 | func FlowHasOutGroup(flow *ofp.OfpFlowStats, groupID uint32) bool { |
khenaidoo | 68c930b | 2019-05-13 11:46:51 -0400 | [diff] [blame] | 1270 | if flow == nil { |
| 1271 | return false |
| 1272 | } |
khenaidoo | 19d7b63 | 2018-10-30 10:49:50 -0400 | [diff] [blame] | 1273 | for _, instruction := range flow.Instructions { |
| 1274 | if instruction.Type == uint32(ofp.OfpInstructionType_OFPIT_APPLY_ACTIONS) { |
| 1275 | if instruction.GetActions() == nil { |
| 1276 | return false |
| 1277 | } |
| 1278 | for _, action := range instruction.GetActions().Actions { |
| 1279 | if action.Type == ofp.OfpActionType_OFPAT_GROUP { |
| 1280 | if (action.GetGroup() != nil) && (action.GetGroup().GroupId == groupID) { |
| 1281 | return true |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | return false |
| 1289 | } |
| 1290 | |
| 1291 | //FindGroup returns index of group if found, else returns -1 |
| 1292 | func FindGroup(groups []*ofp.OfpGroupEntry, groupId uint32) int { |
| 1293 | for idx, group := range groups { |
| 1294 | if group.Desc.GroupId == groupId { |
| 1295 | return idx |
| 1296 | } |
| 1297 | } |
| 1298 | return -1 |
| 1299 | } |
| 1300 | |
| 1301 | func FlowsDeleteByGroupId(flows []*ofp.OfpFlowStats, groupId uint32) (bool, []*ofp.OfpFlowStats) { |
| 1302 | toKeep := make([]*ofp.OfpFlowStats, 0) |
| 1303 | |
| 1304 | for _, f := range flows { |
| 1305 | if !FlowHasOutGroup(f, groupId) { |
| 1306 | toKeep = append(toKeep, f) |
| 1307 | } |
| 1308 | } |
| 1309 | return len(toKeep) < len(flows), toKeep |
| 1310 | } |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 1311 | |
| 1312 | func ToOfpOxmField(from []*ofp.OfpOxmOfbField) []*ofp.OfpOxmField { |
| 1313 | matchFields := make([]*ofp.OfpOxmField, 0) |
| 1314 | for _, val := range from { |
| 1315 | matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}}) |
| 1316 | } |
| 1317 | return matchFields |
| 1318 | } |