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