Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 1 | /* |
Joey Armstrong | 7f6d6d2 | 2023-01-09 17:09:50 -0500 | [diff] [blame] | 2 | * Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.opencord.olt.impl; |
| 18 | |
| 19 | import org.onlab.packet.EthType; |
| 20 | import org.onlab.packet.IPv4; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.net.PortNumber; |
| 24 | import org.onosproject.net.flow.FlowRule; |
| 25 | import org.onosproject.net.flow.FlowRuleEvent; |
| 26 | import org.onosproject.net.flow.criteria.Criterion; |
| 27 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 28 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
| 29 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 30 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
| 31 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 32 | import org.onosproject.net.meter.MeterId; |
Gustavo Silva | 29fb20e | 2022-05-26 09:59:54 -0300 | [diff] [blame] | 33 | import org.opencord.olt.OltFlowsStatus; |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 34 | import org.opencord.sadis.UniTagInformation; |
| 35 | |
| 36 | import java.util.concurrent.atomic.AtomicBoolean; |
| 37 | |
| 38 | import static org.onosproject.net.flow.instructions.Instruction.Type.L2MODIFICATION; |
| 39 | import static org.opencord.olt.impl.OltFlowService.EAPOL_DEFAULT_VLAN; |
| 40 | import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_TP_ID_DEFAULT; |
| 41 | |
| 42 | /** |
| 43 | * Utility class for Flow service utility methods. |
| 44 | */ |
| 45 | public final class OltFlowServiceUtils { |
| 46 | |
| 47 | public static final int NONE_TP_ID = -1; |
| 48 | |
| 49 | private OltFlowServiceUtils() { |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Constructs and returns the metadata from cVlan, techProfileId and upstreamOltMeterId. |
| 54 | * |
| 55 | * @param cVlan the customer vlan |
| 56 | * @param techProfileId the technology profile |
| 57 | * @param upstreamOltMeterId the upstream olt meter id |
| 58 | * @return Metadata |
| 59 | */ |
| 60 | public static Long createTechProfValueForWriteMetadata(VlanId cVlan, int techProfileId, |
| 61 | MeterId upstreamOltMeterId) { |
| 62 | Long writeMetadata; |
| 63 | |
| 64 | if (cVlan == null || VlanId.NONE.equals(cVlan)) { |
| 65 | writeMetadata = (long) techProfileId << 32; |
| 66 | } else { |
| 67 | writeMetadata = ((long) (cVlan.id()) << 48 | (long) techProfileId << 32); |
| 68 | } |
| 69 | if (upstreamOltMeterId == null) { |
| 70 | return writeMetadata; |
| 71 | } else { |
| 72 | return writeMetadata | upstreamOltMeterId.id(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Converts FlowRuleEvent.Type to OltFlowService.OltFlowsStatus. |
| 78 | * |
| 79 | * @param type FlowRuleEvent type |
| 80 | * @return OltFlowService.OltFlowsStatus |
| 81 | */ |
Gustavo Silva | 29fb20e | 2022-05-26 09:59:54 -0300 | [diff] [blame] | 82 | public static OltFlowsStatus flowRuleStatusToOltFlowStatus(FlowRuleEvent.Type type) { |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 83 | switch (type) { |
| 84 | case RULE_ADD_REQUESTED: |
Gustavo Silva | 29fb20e | 2022-05-26 09:59:54 -0300 | [diff] [blame] | 85 | return OltFlowsStatus.PENDING_ADD; |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 86 | case RULE_ADDED: |
Gustavo Silva | 29fb20e | 2022-05-26 09:59:54 -0300 | [diff] [blame] | 87 | return OltFlowsStatus.ADDED; |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 88 | case RULE_REMOVE_REQUESTED: |
Gustavo Silva | 29fb20e | 2022-05-26 09:59:54 -0300 | [diff] [blame] | 89 | return OltFlowsStatus.PENDING_REMOVE; |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 90 | case RULE_REMOVED: |
Gustavo Silva | 29fb20e | 2022-05-26 09:59:54 -0300 | [diff] [blame] | 91 | return OltFlowsStatus.REMOVED; |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 92 | default: |
Gustavo Silva | 29fb20e | 2022-05-26 09:59:54 -0300 | [diff] [blame] | 93 | return OltFlowsStatus.NONE; |
Harsh Awasthi | c1e4bf5 | 2022-02-09 14:14:14 +0530 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Checks if the configured Mac address is valid for a UniTagInformation. |
| 99 | * |
| 100 | * @param tagInformation UniTagInformation |
| 101 | * @return true if the mac address is valid |
| 102 | */ |
| 103 | public static boolean isMacAddressValid(UniTagInformation tagInformation) { |
| 104 | return tagInformation.getConfiguredMacAddress() != null && |
| 105 | !tagInformation.getConfiguredMacAddress().trim().equals("") && |
| 106 | !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress())); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns true if the flow is a DHCP flow. |
| 111 | * Matches both upstream and downstream flows. |
| 112 | * |
| 113 | * @param flowRule The FlowRule to evaluate |
| 114 | * @return boolean |
| 115 | */ |
| 116 | public static boolean isDhcpFlow(FlowRule flowRule) { |
| 117 | IPProtocolCriterion ipCriterion = (IPProtocolCriterion) flowRule.selector() |
| 118 | .getCriterion(Criterion.Type.IP_PROTO); |
| 119 | if (ipCriterion == null) { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | UdpPortCriterion src = (UdpPortCriterion) flowRule.selector().getCriterion(Criterion.Type.UDP_SRC); |
| 124 | |
| 125 | if (src == null) { |
| 126 | return false; |
| 127 | } |
| 128 | return ipCriterion.protocol() == IPv4.PROTOCOL_UDP && |
| 129 | (src.udpPort().toInt() == 68 || src.udpPort().toInt() == 67); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns true if the flow is a Pppoe flow. |
| 134 | * |
| 135 | * @param flowRule The FlowRule to evaluate |
| 136 | * @return boolean |
| 137 | */ |
| 138 | public static boolean isPppoeFlow(FlowRule flowRule) { |
| 139 | EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) flowRule.selector() |
| 140 | .getCriterion(Criterion.Type.ETH_TYPE); |
| 141 | |
| 142 | if (ethTypeCriterion == null) { |
| 143 | return false; |
| 144 | } |
| 145 | return EthType.EtherType.PPPoED.ethType().equals(ethTypeCriterion.ethType()); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Return true if the flow is a Data flow. |
| 150 | * @param flowRule The FlowRule to evaluate |
| 151 | * @return boolean |
| 152 | */ |
| 153 | public static boolean isDataFlow(FlowRule flowRule) { |
| 154 | // we consider subscriber flows the one that matches on VLAN_VID |
| 155 | // method is valid only because it's the last check after EAPOL and DHCP. |
| 156 | // this matches mcast flows as well, if we want to avoid that we can |
| 157 | // filter out the elements that have groups in the treatment or |
| 158 | // mcastIp in the selector |
| 159 | // IPV4_DST:224.0.0.22/32 |
| 160 | // treatment=[immediate=[GROUP:0x1]] |
| 161 | |
| 162 | return flowRule.selector().getCriterion(Criterion.Type.VLAN_VID) != null; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Extracts and returns inPort selector from the FlowRule. |
| 167 | * |
| 168 | * @param flowRule The FlowRule to evaluate |
| 169 | * @return PortNumber |
| 170 | */ |
| 171 | public static PortNumber getPortNumberFromFlowRule(FlowRule flowRule) { |
| 172 | PortCriterion inPort = (PortCriterion) flowRule.selector().getCriterion(Criterion.Type.IN_PORT); |
| 173 | if (inPort != null) { |
| 174 | return inPort.port(); |
| 175 | } |
| 176 | return null; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Constructs and returns the metadata from innerVlan, techProfileId and egressPort. |
| 181 | * |
| 182 | * @param innerVlan inner vlan tag |
| 183 | * @param techProfileId technology profile |
| 184 | * @param egressPort outport |
| 185 | * @return Metadata |
| 186 | */ |
| 187 | public static Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) { |
| 188 | if (techProfileId == NONE_TP_ID) { |
| 189 | techProfileId = DEFAULT_TP_ID_DEFAULT; |
| 190 | } |
| 191 | |
| 192 | Long writeMetadata = (long) techProfileId << 32 | egressPort.toLong(); |
| 193 | |
| 194 | if (innerVlan != null && !VlanId.NONE.equals(innerVlan)) { |
| 195 | writeMetadata |= (long) (innerVlan.id()) << 48; |
| 196 | } |
| 197 | |
| 198 | return writeMetadata; |
| 199 | } |
| 200 | |
| 201 | /*** |
| 202 | * Checks if the FlowRule is default eapol. |
| 203 | * @param flowRule FlowRule to check. |
| 204 | * @return true if FlowRule is default eapol. |
| 205 | */ |
| 206 | public static boolean isDefaultEapolFlow(FlowRule flowRule) { |
| 207 | EthTypeCriterion c = (EthTypeCriterion) flowRule.selector().getCriterion(Criterion.Type.ETH_TYPE); |
| 208 | if (c == null) { |
| 209 | return false; |
| 210 | } |
| 211 | if (c.ethType().equals(EthType.EtherType.EAPOL.ethType())) { |
| 212 | AtomicBoolean isDefault = new AtomicBoolean(false); |
| 213 | flowRule.treatment().allInstructions().forEach(instruction -> { |
| 214 | if (instruction.type() == L2MODIFICATION) { |
| 215 | L2ModificationInstruction modificationInstruction = (L2ModificationInstruction) instruction; |
| 216 | if (modificationInstruction.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) { |
| 217 | L2ModificationInstruction.ModVlanIdInstruction vlanInstruction = |
| 218 | (L2ModificationInstruction.ModVlanIdInstruction) modificationInstruction; |
| 219 | if (vlanInstruction.vlanId().id().equals(EAPOL_DEFAULT_VLAN)) { |
| 220 | isDefault.set(true); |
| 221 | return; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | }); |
| 226 | return isDefault.get(); |
| 227 | } |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | /*** |
| 232 | * Checks if the FlowRule is Subscriber eapol. |
| 233 | * @param flowRule Flow Rule |
| 234 | * @return true if FlowRule is Subscriber eapol. |
| 235 | */ |
| 236 | public static boolean isSubscriberEapolFlow(FlowRule flowRule) { |
| 237 | EthTypeCriterion c = (EthTypeCriterion) flowRule.selector().getCriterion(Criterion.Type.ETH_TYPE); |
| 238 | if (c == null) { |
| 239 | return false; |
| 240 | } |
| 241 | if (c.ethType().equals(EthType.EtherType.EAPOL.ethType())) { |
| 242 | AtomicBoolean isSubscriber = new AtomicBoolean(false); |
| 243 | flowRule.treatment().allInstructions().forEach(instruction -> { |
| 244 | if (instruction.type() == L2MODIFICATION) { |
| 245 | L2ModificationInstruction modificationInstruction = (L2ModificationInstruction) instruction; |
| 246 | if (modificationInstruction.subtype() == L2ModificationInstruction.L2SubType.VLAN_ID) { |
| 247 | L2ModificationInstruction.ModVlanIdInstruction vlanInstruction = |
| 248 | (L2ModificationInstruction.ModVlanIdInstruction) modificationInstruction; |
| 249 | if (!vlanInstruction.vlanId().id().equals(EAPOL_DEFAULT_VLAN)) { |
| 250 | isSubscriber.set(true); |
| 251 | return; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | }); |
| 256 | return isSubscriber.get(); |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | } |