alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [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 | package org.onosproject.driver.pipeline; |
| 17 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalCause; |
| 21 | import com.google.common.cache.RemovalNotification; |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Lists; |
| 24 | import org.apache.commons.lang3.tuple.ImmutablePair; |
| 25 | import org.apache.commons.lang3.tuple.Pair; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 26 | import org.onlab.osgi.ServiceDirectory; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 27 | import org.onlab.packet.EthType; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 28 | import org.onlab.packet.IPv4; |
Matteo Scandolo | 9f4f007 | 2018-11-02 16:09:39 -0700 | [diff] [blame] | 29 | import org.onlab.packet.IPv6; |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 30 | import org.onlab.packet.IpPrefix; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 31 | import org.onlab.packet.VlanId; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 32 | import org.onlab.util.KryoNamespace; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreService; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.PortNumber; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 37 | import org.onosproject.net.behaviour.NextGroup; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 38 | import org.onosproject.net.behaviour.Pipeliner; |
| 39 | import org.onosproject.net.behaviour.PipelinerContext; |
| 40 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 41 | import org.onosproject.net.flow.DefaultFlowRule; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 42 | import org.onosproject.net.flow.DefaultTrafficSelector; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 44 | import org.onosproject.net.flow.FlowRule; |
| 45 | import org.onosproject.net.flow.FlowRuleOperations; |
| 46 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 47 | import org.onosproject.net.flow.FlowRuleService; |
| 48 | import org.onosproject.net.flow.TrafficSelector; |
| 49 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.criteria.Criteria; |
| 51 | import org.onosproject.net.flow.criteria.Criterion; |
| 52 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 53 | import org.onosproject.net.flow.criteria.IPCriterion; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flow.criteria.IPProtocolCriterion; |
| 55 | import org.onosproject.net.flow.criteria.PortCriterion; |
Amit Ghosh | cbaf867 | 2016-12-23 21:36:19 +0000 | [diff] [blame] | 56 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
alshabib | 2f74f2c | 2016-01-14 13:29:35 -0800 | [diff] [blame] | 57 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 58 | import org.onosproject.net.flow.instructions.Instruction; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 59 | import org.onosproject.net.flow.instructions.Instructions; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 60 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 61 | import org.onosproject.net.flowobjective.FilteringObjective; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 62 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 63 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 64 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 65 | import org.onosproject.net.flowobjective.Objective; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 66 | import org.onosproject.net.flowobjective.ObjectiveError; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 67 | import org.onosproject.net.group.DefaultGroupBucket; |
| 68 | import org.onosproject.net.group.DefaultGroupDescription; |
| 69 | import org.onosproject.net.group.DefaultGroupKey; |
| 70 | import org.onosproject.net.group.Group; |
| 71 | import org.onosproject.net.group.GroupBucket; |
| 72 | import org.onosproject.net.group.GroupBuckets; |
| 73 | import org.onosproject.net.group.GroupDescription; |
| 74 | import org.onosproject.net.group.GroupEvent; |
| 75 | import org.onosproject.net.group.GroupKey; |
| 76 | import org.onosproject.net.group.GroupListener; |
| 77 | import org.onosproject.net.group.GroupService; |
| 78 | import org.onosproject.store.serializers.KryoNamespaces; |
alshabib | 5ccbe3f | 2016-03-02 22:36:02 -0800 | [diff] [blame] | 79 | import org.onosproject.store.service.StorageService; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 80 | import org.slf4j.Logger; |
| 81 | |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 82 | import java.util.Arrays; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 83 | import java.util.Collection; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 84 | import java.util.Collections; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 85 | import java.util.List; |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 86 | import java.util.Objects; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 87 | import java.util.Optional; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 88 | import java.util.concurrent.TimeUnit; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 89 | import java.util.stream.Collectors; |
| 90 | |
Andrea Campanella | 969e8af | 2020-07-09 11:39:08 +0200 | [diff] [blame] | 91 | import static org.onosproject.core.CoreService.CORE_APP_NAME; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 92 | import static org.slf4j.LoggerFactory.getLogger; |
| 93 | |
| 94 | /** |
Jonathan Hart | 64da69d | 2015-07-15 15:10:28 -0700 | [diff] [blame] | 95 | * Pipeliner for OLT device. |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 96 | */ |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 97 | |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 98 | public class OltPipeline extends AbstractHandlerBehaviour implements Pipeliner { |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 99 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 100 | private static final Integer QQ_TABLE = 1; |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 101 | private static final int NO_ACTION_PRIORITY = 500; |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 102 | private static final String DOWNSTREAM = "downstream"; |
| 103 | private static final String UPSTREAM = "upstream"; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 104 | private final Logger log = getLogger(getClass()); |
| 105 | |
| 106 | private ServiceDirectory serviceDirectory; |
| 107 | private FlowRuleService flowRuleService; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 108 | private GroupService groupService; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 109 | private CoreService coreService; |
alshabib | 5ccbe3f | 2016-03-02 22:36:02 -0800 | [diff] [blame] | 110 | private StorageService storageService; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 111 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 112 | private DeviceId deviceId; |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 113 | private ApplicationId appId; |
alshabib | 8336447 | 2016-03-25 09:59:55 -0700 | [diff] [blame] | 114 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 115 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 116 | protected FlowObjectiveStore flowObjectiveStore; |
| 117 | |
| 118 | private Cache<GroupKey, NextObjective> pendingGroups; |
| 119 | |
| 120 | protected static KryoNamespace appKryo = new KryoNamespace.Builder() |
| 121 | .register(KryoNamespaces.API) |
| 122 | .register(GroupKey.class) |
| 123 | .register(DefaultGroupKey.class) |
| 124 | .register(OLTPipelineGroup.class) |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 125 | .build("OltPipeline"); |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 126 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 127 | @Override |
| 128 | public void init(DeviceId deviceId, PipelinerContext context) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 129 | log.debug("Initiate OLT pipeline"); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 130 | this.serviceDirectory = context.directory(); |
| 131 | this.deviceId = deviceId; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 132 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 133 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 134 | coreService = serviceDirectory.get(CoreService.class); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 135 | groupService = serviceDirectory.get(GroupService.class); |
| 136 | flowObjectiveStore = context.store(); |
alshabib | 5ccbe3f | 2016-03-02 22:36:02 -0800 | [diff] [blame] | 137 | storageService = serviceDirectory.get(StorageService.class); |
| 138 | |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 139 | appId = coreService.registerApplication( |
| 140 | "org.onosproject.driver.OLTPipeline"); |
| 141 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 142 | |
| 143 | pendingGroups = CacheBuilder.newBuilder() |
| 144 | .expireAfterWrite(20, TimeUnit.SECONDS) |
| 145 | .removalListener((RemovalNotification<GroupKey, NextObjective> notification) -> { |
| 146 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 147 | fail(notification.getValue(), ObjectiveError.GROUPINSTALLATIONFAILED); |
| 148 | } |
| 149 | }).build(); |
| 150 | |
| 151 | groupService.addListener(new InnerGroupListener()); |
| 152 | |
alshabib | b32cefe | 2015-06-08 18:15:05 -0700 | [diff] [blame] | 153 | } |
| 154 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 155 | @Override |
| 156 | public void filter(FilteringObjective filter) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 157 | Instructions.OutputInstruction output; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 158 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 159 | if (filter.meta() != null && !filter.meta().immediate().isEmpty()) { |
| 160 | output = (Instructions.OutputInstruction) filter.meta().immediate().stream() |
| 161 | .filter(t -> t.type().equals(Instruction.Type.OUTPUT)) |
| 162 | .limit(1) |
| 163 | .findFirst().get(); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 164 | |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 165 | if (output == null || !output.port().equals(PortNumber.CONTROLLER)) { |
dvaddire | 8e6b89a | 2017-08-31 21:54:03 +0530 | [diff] [blame] | 166 | log.warn("OLT can only filter packet to controller"); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 167 | fail(filter, ObjectiveError.UNSUPPORTED); |
| 168 | return; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 169 | } |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 170 | } else { |
| 171 | fail(filter, ObjectiveError.BADPARAMS); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 175 | if (filter.key().type() != Criterion.Type.IN_PORT) { |
| 176 | fail(filter, ObjectiveError.BADPARAMS); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | EthTypeCriterion ethType = (EthTypeCriterion) |
| 181 | filterForCriterion(filter.conditions(), Criterion.Type.ETH_TYPE); |
| 182 | |
| 183 | if (ethType == null) { |
| 184 | fail(filter, ObjectiveError.BADPARAMS); |
| 185 | return; |
| 186 | } |
| 187 | |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 188 | if (ethType.ethType().equals(EthType.EtherType.EAPOL.ethType())) { |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 189 | provisionEthTypeBasedFilter(filter, ethType, output); |
| 190 | } else if (ethType.ethType().equals(EthType.EtherType.LLDP.ethType())) { |
| 191 | provisionEthTypeBasedFilter(filter, ethType, output); |
| 192 | |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 193 | } else if (ethType.ethType().equals(EthType.EtherType.IPV4.ethType())) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 194 | IPProtocolCriterion ipProto = (IPProtocolCriterion) |
| 195 | filterForCriterion(filter.conditions(), Criterion.Type.IP_PROTO); |
dvaddire | 8e6b89a | 2017-08-31 21:54:03 +0530 | [diff] [blame] | 196 | if (ipProto == null) { |
| 197 | log.warn("OLT can only filter IGMP and DHCP"); |
| 198 | fail(filter, ObjectiveError.UNSUPPORTED); |
| 199 | return; |
| 200 | } |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 201 | if (ipProto.protocol() == IPv4.PROTOCOL_IGMP) { |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 202 | provisionIgmp(filter, ethType, ipProto, output); |
Amit Ghosh | cbaf867 | 2016-12-23 21:36:19 +0000 | [diff] [blame] | 203 | } else if (ipProto.protocol() == IPv4.PROTOCOL_UDP) { |
| 204 | UdpPortCriterion udpSrcPort = (UdpPortCriterion) |
| 205 | filterForCriterion(filter.conditions(), Criterion.Type.UDP_SRC); |
| 206 | |
| 207 | UdpPortCriterion udpDstPort = (UdpPortCriterion) |
| 208 | filterForCriterion(filter.conditions(), Criterion.Type.UDP_DST); |
| 209 | |
Matteo Scandolo | 9f4f007 | 2018-11-02 16:09:39 -0700 | [diff] [blame] | 210 | if ((udpSrcPort.udpPort().toInt() == 67 && udpDstPort.udpPort().toInt() == 68) || |
| 211 | (udpSrcPort.udpPort().toInt() == 68 && udpDstPort.udpPort().toInt() == 67)) { |
| 212 | provisionDhcp(filter, ethType, ipProto, udpSrcPort, udpDstPort, output); |
| 213 | } else { |
| 214 | log.warn("Filtering rule with unsupported UDP src {} or dst {} port", udpSrcPort, udpDstPort); |
Amit Ghosh | cbaf867 | 2016-12-23 21:36:19 +0000 | [diff] [blame] | 215 | fail(filter, ObjectiveError.UNSUPPORTED); |
| 216 | } |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 217 | } else { |
Matteo Scandolo | 9f4f007 | 2018-11-02 16:09:39 -0700 | [diff] [blame] | 218 | log.warn("Currently supporting only IGMP and DHCP filters for IPv4 packets"); |
| 219 | fail(filter, ObjectiveError.UNSUPPORTED); |
| 220 | } |
| 221 | } else if (ethType.ethType().equals(EthType.EtherType.IPV6.ethType())) { |
| 222 | IPProtocolCriterion ipProto = (IPProtocolCriterion) |
| 223 | filterForCriterion(filter.conditions(), Criterion.Type.IP_PROTO); |
| 224 | if (ipProto == null) { |
| 225 | log.warn("OLT can only filter DHCP"); |
| 226 | fail(filter, ObjectiveError.UNSUPPORTED); |
| 227 | return; |
| 228 | } |
| 229 | if (ipProto.protocol() == IPv6.PROTOCOL_UDP) { |
| 230 | UdpPortCriterion udpSrcPort = (UdpPortCriterion) |
| 231 | filterForCriterion(filter.conditions(), Criterion.Type.UDP_SRC); |
| 232 | |
| 233 | UdpPortCriterion udpDstPort = (UdpPortCriterion) |
| 234 | filterForCriterion(filter.conditions(), Criterion.Type.UDP_DST); |
| 235 | |
| 236 | if ((udpSrcPort.udpPort().toInt() == 546 && udpDstPort.udpPort().toInt() == 547) || |
| 237 | (udpSrcPort.udpPort().toInt() == 547 && udpDstPort.udpPort().toInt() == 546)) { |
| 238 | provisionDhcp(filter, ethType, ipProto, udpSrcPort, udpDstPort, output); |
| 239 | } else { |
| 240 | log.warn("Filtering rule with unsupported UDP src {} or dst {} port", udpSrcPort, udpDstPort); |
| 241 | fail(filter, ObjectiveError.UNSUPPORTED); |
| 242 | } |
| 243 | } else { |
| 244 | log.warn("Currently supporting only DHCP filters for IPv6 packets"); |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 245 | fail(filter, ObjectiveError.UNSUPPORTED); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 246 | } |
| 247 | } else { |
dvaddire | 8e6b89a | 2017-08-31 21:54:03 +0530 | [diff] [blame] | 248 | log.warn("\nOnly the following are Supported in OLT for filter ->\n" |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 249 | + "ETH TYPE : EAPOL, LLDP and IPV4\n" |
Matteo Scandolo | 9f4f007 | 2018-11-02 16:09:39 -0700 | [diff] [blame] | 250 | + "IPV4 TYPE: IGMP and UDP (for DHCP)" |
| 251 | + "IPV6 TYPE: UDP (for DHCP)"); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 252 | fail(filter, ObjectiveError.UNSUPPORTED); |
| 253 | } |
| 254 | |
| 255 | } |
| 256 | |
| 257 | |
| 258 | @Override |
| 259 | public void forward(ForwardingObjective fwd) { |
Andrea Campanella | 6edac32 | 2020-01-29 14:41:42 +0100 | [diff] [blame] | 260 | log.debug("Installing forwarding objective {}", fwd); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 261 | if (checkForMulticast(fwd)) { |
| 262 | processMulticastRule(fwd); |
| 263 | return; |
| 264 | } |
| 265 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 266 | TrafficTreatment treatment = fwd.treatment(); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 267 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 268 | List<Instruction> instructions = treatment.allInstructions(); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 269 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 270 | Optional<Instruction> vlanInstruction = instructions.stream() |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 271 | .filter(i -> i.type() == Instruction.Type.L2MODIFICATION) |
| 272 | .filter(i -> ((L2ModificationInstruction) i).subtype() == |
| 273 | L2ModificationInstruction.L2SubType.VLAN_PUSH || |
| 274 | ((L2ModificationInstruction) i).subtype() == |
| 275 | L2ModificationInstruction.L2SubType.VLAN_POP) |
| 276 | .findAny(); |
| 277 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 278 | |
| 279 | if (!vlanInstruction.isPresent()) { |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 280 | installNoModificationRules(fwd); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 281 | } else { |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 282 | L2ModificationInstruction vlanIns = |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 283 | (L2ModificationInstruction) vlanInstruction.get(); |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 284 | if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_PUSH) { |
| 285 | installUpstreamRules(fwd); |
| 286 | } else if (vlanIns.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP) { |
| 287 | installDownstreamRules(fwd); |
| 288 | } else { |
| 289 | log.error("Unknown OLT operation: {}", fwd); |
| 290 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 291 | return; |
| 292 | } |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 295 | pass(fwd); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 296 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 299 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 300 | @Override |
| 301 | public void next(NextObjective nextObjective) { |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 302 | if (nextObjective.type() != NextObjective.Type.BROADCAST) { |
| 303 | log.error("OLT only supports broadcast groups."); |
| 304 | fail(nextObjective, ObjectiveError.BADPARAMS); |
Andrea Campanella | 5908b56 | 2020-05-06 16:09:10 +0200 | [diff] [blame] | 305 | return; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Andrea Campanella | 5908b56 | 2020-05-06 16:09:10 +0200 | [diff] [blame] | 308 | if (nextObjective.next().size() != 1 && !nextObjective.op().equals(Objective.Operation.REMOVE)) { |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 309 | log.error("OLT only supports singleton broadcast groups."); |
| 310 | fail(nextObjective, ObjectiveError.BADPARAMS); |
Andrea Campanella | 5908b56 | 2020-05-06 16:09:10 +0200 | [diff] [blame] | 311 | return; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Andrea Campanella | 5908b56 | 2020-05-06 16:09:10 +0200 | [diff] [blame] | 314 | Optional<TrafficTreatment> treatmentOpt = nextObjective.next().stream().findFirst(); |
| 315 | if (treatmentOpt.isEmpty() && !nextObjective.op().equals(Objective.Operation.REMOVE)) { |
| 316 | log.error("Next objective {} does not have a treatment", nextObjective); |
| 317 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 318 | return; |
| 319 | } |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 320 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 321 | GroupKey key = new DefaultGroupKey(appKryo.serialize(nextObjective.id())); |
| 322 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 323 | pendingGroups.put(key, nextObjective); |
Andrea Campanella | 5908b56 | 2020-05-06 16:09:10 +0200 | [diff] [blame] | 324 | log.trace("NextObjective Operation {}", nextObjective.op()); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 325 | switch (nextObjective.op()) { |
| 326 | case ADD: |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 327 | GroupDescription groupDesc = |
| 328 | new DefaultGroupDescription(deviceId, |
| 329 | GroupDescription.Type.ALL, |
Andrea Campanella | 5908b56 | 2020-05-06 16:09:10 +0200 | [diff] [blame] | 330 | new GroupBuckets( |
| 331 | Collections.singletonList( |
| 332 | buildBucket(treatmentOpt.get()))), |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 333 | key, |
| 334 | null, |
| 335 | nextObjective.appId()); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 336 | groupService.addGroup(groupDesc); |
| 337 | break; |
| 338 | case REMOVE: |
| 339 | groupService.removeGroup(deviceId, key, nextObjective.appId()); |
| 340 | break; |
| 341 | case ADD_TO_EXISTING: |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 342 | groupService.addBucketsToGroup(deviceId, key, |
Andrea Campanella | 5908b56 | 2020-05-06 16:09:10 +0200 | [diff] [blame] | 343 | new GroupBuckets( |
| 344 | Collections.singletonList( |
| 345 | buildBucket(treatmentOpt.get()))), |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 346 | key, nextObjective.appId()); |
| 347 | break; |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 348 | case REMOVE_FROM_EXISTING: |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 349 | groupService.removeBucketsFromGroup(deviceId, key, |
Andrea Campanella | 5908b56 | 2020-05-06 16:09:10 +0200 | [diff] [blame] | 350 | new GroupBuckets( |
| 351 | Collections.singletonList( |
| 352 | buildBucket(treatmentOpt.get()))), |
alshabib | 56efe43 | 2016-02-25 17:57:24 -0500 | [diff] [blame] | 353 | key, nextObjective.appId()); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 354 | break; |
| 355 | default: |
| 356 | log.warn("Unknown next objective operation: {}", nextObjective.op()); |
| 357 | } |
| 358 | |
| 359 | |
| 360 | } |
| 361 | |
Andrea Campanella | 5908b56 | 2020-05-06 16:09:10 +0200 | [diff] [blame] | 362 | private GroupBucket buildBucket(TrafficTreatment treatment) { |
| 363 | return DefaultGroupBucket.createAllGroupBucket(treatment); |
| 364 | } |
| 365 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 366 | private void processMulticastRule(ForwardingObjective fwd) { |
| 367 | if (fwd.nextId() == null) { |
| 368 | log.error("Multicast objective does not have a next id"); |
| 369 | fail(fwd, ObjectiveError.BADPARAMS); |
| 370 | } |
| 371 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 372 | GroupKey key = getGroupForNextObjective(fwd.nextId()); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 373 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 374 | if (key == null) { |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 375 | log.error("Group for forwarding objective missing: {}", fwd); |
| 376 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 377 | } |
| 378 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 379 | Group group = groupService.getGroup(deviceId, key); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 380 | TrafficTreatment treatment = |
| 381 | buildTreatment(Instructions.createGroup(group.id())); |
| 382 | |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 383 | TrafficSelector.Builder selectorBuilder = buildIpv4SelectorForMulticast(fwd); |
| 384 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 385 | FlowRule rule = DefaultFlowRule.builder() |
alshabib | 8336447 | 2016-03-25 09:59:55 -0700 | [diff] [blame] | 386 | .fromApp(fwd.appId()) |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 387 | .forDevice(deviceId) |
| 388 | .forTable(0) |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 389 | .makePermanent() |
| 390 | .withPriority(fwd.priority()) |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 391 | .withSelector(selectorBuilder.build()) |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 392 | .withTreatment(treatment) |
| 393 | .build(); |
| 394 | |
| 395 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
| 396 | switch (fwd.op()) { |
| 397 | |
| 398 | case ADD: |
| 399 | builder.add(rule); |
| 400 | break; |
| 401 | case REMOVE: |
| 402 | builder.remove(rule); |
| 403 | break; |
| 404 | case ADD_TO_EXISTING: |
| 405 | case REMOVE_FROM_EXISTING: |
| 406 | break; |
| 407 | default: |
| 408 | log.warn("Unknown forwarding operation: {}", fwd.op()); |
| 409 | } |
| 410 | |
| 411 | applyFlowRules(builder, fwd); |
| 412 | |
| 413 | } |
| 414 | |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 415 | private TrafficSelector.Builder buildIpv4SelectorForMulticast(ForwardingObjective fwd) { |
| 416 | TrafficSelector.Builder builderToUpdate = DefaultTrafficSelector.builder(); |
| 417 | |
| 418 | Optional<Criterion> vlanIdCriterion = readFromSelector(fwd.meta(), Criterion.Type.VLAN_VID); |
| 419 | if (vlanIdCriterion.isPresent()) { |
| 420 | VlanId assignedVlan = ((VlanIdCriterion) vlanIdCriterion.get()).vlanId(); |
| 421 | builderToUpdate.matchVlanId(assignedVlan); |
| 422 | } |
| 423 | |
Esin Karaman | ec25a0a | 2020-03-18 14:29:29 +0000 | [diff] [blame] | 424 | Optional<Criterion> innerVlanIdCriterion = readFromSelector(fwd.meta(), Criterion.Type.INNER_VLAN_VID); |
| 425 | if (innerVlanIdCriterion.isPresent()) { |
| 426 | VlanId assignedInnerVlan = ((VlanIdCriterion) innerVlanIdCriterion.get()).vlanId(); |
| 427 | builderToUpdate.matchMetadata(assignedInnerVlan.toShort()); |
| 428 | } |
| 429 | |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 430 | Optional<Criterion> ethTypeCriterion = readFromSelector(fwd.selector(), Criterion.Type.ETH_TYPE); |
| 431 | if (ethTypeCriterion.isPresent()) { |
| 432 | EthType ethType = ((EthTypeCriterion) ethTypeCriterion.get()).ethType(); |
| 433 | builderToUpdate.matchEthType(ethType.toShort()); |
| 434 | } |
| 435 | |
| 436 | Optional<Criterion> ipv4DstCriterion = readFromSelector(fwd.selector(), Criterion.Type.IPV4_DST); |
| 437 | if (ipv4DstCriterion.isPresent()) { |
| 438 | IpPrefix ipv4Dst = ((IPCriterion) ipv4DstCriterion.get()).ip(); |
| 439 | builderToUpdate.matchIPDst(ipv4Dst); |
| 440 | } |
| 441 | |
| 442 | return builderToUpdate; |
| 443 | } |
| 444 | |
| 445 | static Optional<Criterion> readFromSelector(TrafficSelector selector, Criterion.Type type) { |
| 446 | if (selector == null) { |
| 447 | return Optional.empty(); |
| 448 | } |
| 449 | Criterion criterion = selector.getCriterion(type); |
| 450 | return (criterion == null) |
| 451 | ? Optional.empty() : Optional.of(criterion); |
| 452 | } |
| 453 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 454 | private boolean checkForMulticast(ForwardingObjective fwd) { |
| 455 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 456 | IPCriterion ip = (IPCriterion) filterForCriterion(fwd.selector().criteria(), |
alshabib | 56efe43 | 2016-02-25 17:57:24 -0500 | [diff] [blame] | 457 | Criterion.Type.IPV4_DST); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 458 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 459 | if (ip == null) { |
| 460 | return false; |
| 461 | } |
| 462 | |
Charles Chan | aedabfd | 2016-02-26 09:31:48 -0800 | [diff] [blame] | 463 | return ip.ip().isMulticast(); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 464 | |
| 465 | } |
| 466 | |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 467 | private GroupKey getGroupForNextObjective(Integer nextId) { |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 468 | NextGroup next = flowObjectiveStore.getNextGroup(nextId); |
alshabib | 1aa5814 | 2016-02-17 15:37:56 -0800 | [diff] [blame] | 469 | return appKryo.deserialize(next.data()); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 470 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 473 | private void installNoModificationRules(ForwardingObjective fwd) { |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 474 | Instructions.OutputInstruction output = (Instructions.OutputInstruction) fetchOutput(fwd, DOWNSTREAM); |
Gamze Abaka | a34469f | 2019-04-19 08:30:16 +0000 | [diff] [blame] | 475 | Instructions.MetadataInstruction writeMetadata = fetchWriteMetadata(fwd); |
| 476 | Instructions.MeterInstruction meter = (Instructions.MeterInstruction) fetchMeter(fwd); |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 477 | |
| 478 | TrafficSelector selector = fwd.selector(); |
| 479 | |
| 480 | Criterion inport = selector.getCriterion(Criterion.Type.IN_PORT); |
| 481 | Criterion outerVlan = selector.getCriterion(Criterion.Type.VLAN_VID); |
| 482 | Criterion innerVlan = selector.getCriterion(Criterion.Type.INNER_VLAN_VID); |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 483 | |
| 484 | if (inport == null || output == null || innerVlan == null || outerVlan == null) { |
Andrea Campanella | 969e8af | 2020-07-09 11:39:08 +0200 | [diff] [blame] | 485 | // Avoid logging a non-error from lldp, bbdp and eapol core flows. |
| 486 | if (!fwd.appId().name().equals(CORE_APP_NAME)) { |
| 487 | log.error("Forwarding objective is underspecified: {}", fwd); |
| 488 | } else { |
| 489 | log.debug("Not installing unsupported core generated flow {}", fwd); |
| 490 | } |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 491 | fail(fwd, ObjectiveError.BADPARAMS); |
| 492 | return; |
| 493 | } |
| 494 | |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 495 | |
| 496 | FlowRule.Builder outer = DefaultFlowRule.builder() |
| 497 | .fromApp(fwd.appId()) |
| 498 | .forDevice(deviceId) |
| 499 | .makePermanent() |
| 500 | .withPriority(fwd.priority()) |
Gamze Abaka | a34469f | 2019-04-19 08:30:16 +0000 | [diff] [blame] | 501 | .withSelector(buildSelector(inport, outerVlan)) |
| 502 | .withTreatment(buildTreatment(output, writeMetadata, meter)); |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 503 | |
| 504 | applyRules(fwd, outer); |
| 505 | } |
| 506 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 507 | private void installDownstreamRules(ForwardingObjective fwd) { |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 508 | Instructions.OutputInstruction output = (Instructions.OutputInstruction) fetchOutput(fwd, DOWNSTREAM); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 509 | |
| 510 | if (output == null) { |
| 511 | return; |
| 512 | } |
| 513 | |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 514 | TrafficSelector selector = fwd.selector(); |
| 515 | |
| 516 | Criterion outerVlan = selector.getCriterion(Criterion.Type.VLAN_VID); |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 517 | Criterion outerPbit = selector.getCriterion(Criterion.Type.VLAN_PCP); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 518 | Criterion innerVlanCriterion = selector.getCriterion(Criterion.Type.INNER_VLAN_VID); |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 519 | Criterion inport = selector.getCriterion(Criterion.Type.IN_PORT); |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 520 | Criterion dstMac = selector.getCriterion(Criterion.Type.ETH_DST); |
cboling | 651c687 | 2018-10-05 11:09:22 -0500 | [diff] [blame] | 521 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 522 | if (outerVlan == null || innerVlanCriterion == null || inport == null) { |
Andrea Campanella | 969e8af | 2020-07-09 11:39:08 +0200 | [diff] [blame] | 523 | // Avoid logging a non-error from lldp, bbdp and eapol core flows. |
| 524 | if (!fwd.appId().name().equals(CORE_APP_NAME)) { |
| 525 | log.error("Forwarding objective is underspecified: {}", fwd); |
| 526 | } else { |
| 527 | log.debug("Not installing unsupported core generated flow {}", fwd); |
| 528 | } |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 529 | fail(fwd, ObjectiveError.BADPARAMS); |
| 530 | return; |
| 531 | } |
| 532 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 533 | VlanId innerVlan = ((VlanIdCriterion) innerVlanCriterion).vlanId(); |
| 534 | Criterion innerVid = Criteria.matchVlanId(innerVlan); |
alshabib | 2f74f2c | 2016-01-14 13:29:35 -0800 | [diff] [blame] | 535 | |
Amit Ghosh | a685998 | 2020-02-12 09:35:45 +0000 | [diff] [blame] | 536 | // In the case where the C-tag is the same for all the subscribers, |
| 537 | // we add a metadata with the outport in the selector to make the flow unique |
| 538 | Criterion innerSelectorMeta = Criteria.matchMetadata(output.port().toLong()); |
| 539 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 540 | if (innerVlan.toShort() == VlanId.ANY_VALUE) { |
Andrea Campanella | 6edac32 | 2020-01-29 14:41:42 +0100 | [diff] [blame] | 541 | TrafficSelector outerSelector = buildSelector(inport, outerVlan, outerPbit, dstMac); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 542 | installDownstreamRulesForAnyVlan(fwd, output, outerSelector, buildSelector(inport, |
Amit Ghosh | a685998 | 2020-02-12 09:35:45 +0000 | [diff] [blame] | 543 | Criteria.matchVlanId(VlanId.ANY), innerSelectorMeta)); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 544 | } else { |
Andrea Campanella | 6edac32 | 2020-01-29 14:41:42 +0100 | [diff] [blame] | 545 | // Required to differentiate the same match flows |
| 546 | // Please note that S tag and S p bit values will be same for the same service - so conflict flows! |
| 547 | // Metadata match criteria solves the conflict issue - but not used by the voltha |
| 548 | // Maybe - find a better way to solve the above problem |
| 549 | Criterion metadata = Criteria.matchMetadata(innerVlan.toShort()); |
| 550 | TrafficSelector outerSelector = buildSelector(inport, metadata, outerVlan, outerPbit, dstMac); |
Amit Ghosh | a685998 | 2020-02-12 09:35:45 +0000 | [diff] [blame] | 551 | installDownstreamRulesForVlans(fwd, output, outerSelector, buildSelector(inport, innerVid, |
| 552 | innerSelectorMeta)); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | private void installDownstreamRulesForVlans(ForwardingObjective fwd, Instruction output, |
| 557 | TrafficSelector outerSelector, TrafficSelector innerSelector) { |
| 558 | |
| 559 | List<Pair<Instruction, Instruction>> vlanOps = |
| 560 | vlanOps(fwd, |
| 561 | L2ModificationInstruction.L2SubType.VLAN_POP); |
| 562 | |
| 563 | if (vlanOps == null || vlanOps.isEmpty()) { |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | Pair<Instruction, Instruction> popAndRewrite = vlanOps.remove(0); |
| 568 | |
| 569 | TrafficTreatment innerTreatment; |
| 570 | VlanId setVlanId = ((L2ModificationInstruction.ModVlanIdInstruction) popAndRewrite.getRight()).vlanId(); |
| 571 | if (VlanId.NONE.equals(setVlanId)) { |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 572 | innerTreatment = (buildTreatment(popAndRewrite.getLeft(), fetchMeter(fwd), |
| 573 | writeMetadataIncludingOnlyTp(fwd), output)); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 574 | } else { |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 575 | innerTreatment = (buildTreatment(popAndRewrite.getRight(), |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 576 | fetchMeter(fwd), writeMetadataIncludingOnlyTp(fwd), output)); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 579 | List<Instruction> setVlanPcps = findL2Instructions(L2ModificationInstruction.L2SubType.VLAN_PCP, |
| 580 | fwd.treatment().allInstructions()); |
| 581 | |
| 582 | Instruction innerPbitSet = null; |
| 583 | |
| 584 | if (setVlanPcps != null && !setVlanPcps.isEmpty()) { |
| 585 | innerPbitSet = setVlanPcps.get(0); |
| 586 | } |
| 587 | |
| 588 | VlanId remarkInnerVlan = null; |
| 589 | Optional<Criterion> vlanIdCriterion = readFromSelector(innerSelector, Criterion.Type.VLAN_VID); |
| 590 | if (vlanIdCriterion.isPresent()) { |
| 591 | remarkInnerVlan = ((VlanIdCriterion) vlanIdCriterion.get()).vlanId(); |
| 592 | } |
| 593 | |
| 594 | Instruction modVlanId = null; |
| 595 | if (innerPbitSet != null) { |
| 596 | modVlanId = Instructions.modVlanId(remarkInnerVlan); |
| 597 | } |
| 598 | |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 599 | //match: in port (nni), s-tag |
| 600 | //action: pop vlan (s-tag), write metadata, go to table 1, meter |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 601 | FlowRule.Builder outer = DefaultFlowRule.builder() |
alshabib | 8336447 | 2016-03-25 09:59:55 -0700 | [diff] [blame] | 602 | .fromApp(fwd.appId()) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 603 | .forDevice(deviceId) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 604 | .makePermanent() |
| 605 | .withPriority(fwd.priority()) |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 606 | .withSelector(outerSelector) |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 607 | .withTreatment(buildTreatment(popAndRewrite.getLeft(), modVlanId, |
| 608 | innerPbitSet, fetchMeter(fwd), fetchWriteMetadata(fwd), Instructions.transition(QQ_TABLE))); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 609 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 610 | //match: in port (nni), c-tag |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 611 | //action: immediate: write metadata and pop, meter, output |
alshabib | fa0dc66 | 2016-01-13 11:23:53 -0800 | [diff] [blame] | 612 | FlowRule.Builder inner = DefaultFlowRule.builder() |
alshabib | 8336447 | 2016-03-25 09:59:55 -0700 | [diff] [blame] | 613 | .fromApp(fwd.appId()) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 614 | .forDevice(deviceId) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 615 | .forTable(QQ_TABLE) |
| 616 | .makePermanent() |
| 617 | .withPriority(fwd.priority()) |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 618 | .withSelector(innerSelector) |
| 619 | .withTreatment(innerTreatment); |
| 620 | applyRules(fwd, inner, outer); |
| 621 | } |
| 622 | |
| 623 | private void installDownstreamRulesForAnyVlan(ForwardingObjective fwd, Instruction output, |
| 624 | TrafficSelector outerSelector, TrafficSelector innerSelector) { |
| 625 | |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 626 | //match: in port (nni), s-tag |
| 627 | //action: immediate: write metadata, pop vlan, meter and go to table 1 |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 628 | FlowRule.Builder outer = DefaultFlowRule.builder() |
| 629 | .fromApp(fwd.appId()) |
| 630 | .forDevice(deviceId) |
| 631 | .makePermanent() |
| 632 | .withPriority(fwd.priority()) |
| 633 | .withSelector(outerSelector) |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 634 | .withTreatment(buildTreatment(Instructions.popVlan(), fetchMeter(fwd), |
| 635 | fetchWriteMetadata(fwd), Instructions.transition(QQ_TABLE))); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 636 | |
| 637 | //match: in port (nni) and s-tag |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 638 | //action: immediate : write metadata, meter and output |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 639 | FlowRule.Builder inner = DefaultFlowRule.builder() |
| 640 | .fromApp(fwd.appId()) |
| 641 | .forDevice(deviceId) |
| 642 | .forTable(QQ_TABLE) |
| 643 | .makePermanent() |
| 644 | .withPriority(fwd.priority()) |
| 645 | .withSelector(innerSelector) |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 646 | .withTreatment(buildTreatment(fetchMeter(fwd), |
| 647 | writeMetadataIncludingOnlyTp(fwd), output)); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 648 | |
| 649 | applyRules(fwd, inner, outer); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | private void installUpstreamRules(ForwardingObjective fwd) { |
| 653 | List<Pair<Instruction, Instruction>> vlanOps = |
| 654 | vlanOps(fwd, |
| 655 | L2ModificationInstruction.L2SubType.VLAN_PUSH); |
| 656 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 657 | if (vlanOps == null || vlanOps.isEmpty()) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 658 | return; |
| 659 | } |
| 660 | |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 661 | Instruction output = fetchOutput(fwd, UPSTREAM); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 662 | |
| 663 | if (output == null) { |
| 664 | return; |
| 665 | } |
| 666 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 667 | Pair<Instruction, Instruction> outerPair = vlanOps.remove(0); |
| 668 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 669 | boolean noneValueVlanStatus = checkNoneVlanCriteria(fwd); |
| 670 | boolean anyValueVlanStatus = checkAnyVlanMatchCriteria(fwd); |
Jonathan Hart | 3dc474c | 2018-08-15 18:15:45 -0700 | [diff] [blame] | 671 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 672 | if (anyValueVlanStatus) { |
| 673 | installUpstreamRulesForAnyVlan(fwd, output, outerPair); |
| 674 | } else { |
Andrea Campanella | 6edac32 | 2020-01-29 14:41:42 +0100 | [diff] [blame] | 675 | Pair<Instruction, Instruction> innerPair = outerPair; |
| 676 | outerPair = vlanOps.remove(0); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 677 | installUpstreamRulesForVlans(fwd, output, innerPair, outerPair, noneValueVlanStatus); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | private void installUpstreamRulesForVlans(ForwardingObjective fwd, Instruction output, |
| 682 | Pair<Instruction, Instruction> innerPair, |
| 683 | Pair<Instruction, Instruction> outerPair, Boolean noneValueVlanStatus) { |
| 684 | |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 685 | List<Instruction> setVlanPcps = findL2Instructions(L2ModificationInstruction.L2SubType.VLAN_PCP, |
| 686 | fwd.treatment().allInstructions()); |
| 687 | |
| 688 | Instruction innerPbitSet = null; |
| 689 | Instruction outerPbitSet = null; |
| 690 | |
| 691 | if (setVlanPcps != null && !setVlanPcps.isEmpty()) { |
| 692 | innerPbitSet = setVlanPcps.get(0); |
| 693 | outerPbitSet = setVlanPcps.get(1); |
| 694 | } |
| 695 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 696 | TrafficTreatment innerTreatment; |
| 697 | if (noneValueVlanStatus) { |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 698 | innerTreatment = buildTreatment(innerPair.getLeft(), innerPair.getRight(), fetchMeter(fwd), |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 699 | fetchWriteMetadata(fwd), innerPbitSet, |
| 700 | Instructions.transition(QQ_TABLE)); |
Jonathan Hart | 3dc474c | 2018-08-15 18:15:45 -0700 | [diff] [blame] | 701 | } else { |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 702 | innerTreatment = buildTreatment(innerPair.getRight(), fetchMeter(fwd), fetchWriteMetadata(fwd), |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 703 | innerPbitSet, Instructions.transition(QQ_TABLE)); |
Jonathan Hart | 3dc474c | 2018-08-15 18:15:45 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 706 | //match: in port, vlanId (0 or None) |
| 707 | //action: |
| 708 | //if vlanId None, push & set c-tag go to table 1 |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 709 | //if vlanId 0 or any specific vlan, set c-tag, write metadata, meter and go to table 1 |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 710 | FlowRule.Builder inner = DefaultFlowRule.builder() |
alshabib | 8336447 | 2016-03-25 09:59:55 -0700 | [diff] [blame] | 711 | .fromApp(fwd.appId()) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 712 | .forDevice(deviceId) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 713 | .makePermanent() |
| 714 | .withPriority(fwd.priority()) |
| 715 | .withSelector(fwd.selector()) |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 716 | .withTreatment(innerTreatment); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 717 | |
| 718 | PortCriterion inPort = (PortCriterion) |
| 719 | fwd.selector().getCriterion(Criterion.Type.IN_PORT); |
| 720 | |
| 721 | VlanId cVlanId = ((L2ModificationInstruction.ModVlanIdInstruction) |
| 722 | innerPair.getRight()).vlanId(); |
| 723 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 724 | //match: in port, c-tag |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 725 | //action: immediate: push s-tag, write metadata, meter and output |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 726 | FlowRule.Builder outer = DefaultFlowRule.builder() |
alshabib | 8336447 | 2016-03-25 09:59:55 -0700 | [diff] [blame] | 727 | .fromApp(fwd.appId()) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 728 | .forDevice(deviceId) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 729 | .forTable(QQ_TABLE) |
| 730 | .makePermanent() |
| 731 | .withPriority(fwd.priority()) |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 732 | .withTreatment(buildTreatment(outerPair.getLeft(), outerPair.getRight(), |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 733 | fetchMeter(fwd), writeMetadataIncludingOnlyTp(fwd), |
| 734 | outerPbitSet, output)); |
| 735 | |
| 736 | if (innerPbitSet != null) { |
| 737 | byte innerPbit = ((L2ModificationInstruction.ModVlanPcpInstruction) |
| 738 | innerPbitSet).vlanPcp(); |
| 739 | outer.withSelector(buildSelector(inPort, Criteria.matchVlanId(cVlanId), Criteria.matchVlanPcp(innerPbit))); |
| 740 | } else { |
| 741 | outer.withSelector(buildSelector(inPort, Criteria.matchVlanId(cVlanId))); |
| 742 | } |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 743 | |
| 744 | applyRules(fwd, inner, outer); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 745 | } |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 746 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 747 | private void installUpstreamRulesForAnyVlan(ForwardingObjective fwd, Instruction output, |
| 748 | Pair<Instruction, Instruction> outerPair) { |
| 749 | |
| 750 | log.debug("Installing upstream rules for any value vlan"); |
| 751 | |
| 752 | //match: in port and any-vlan (coming from OLT app.) |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 753 | //action: write metadata, go to table 1 and meter |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 754 | FlowRule.Builder inner = DefaultFlowRule.builder() |
| 755 | .fromApp(fwd.appId()) |
| 756 | .forDevice(deviceId) |
| 757 | .makePermanent() |
| 758 | .withPriority(fwd.priority()) |
| 759 | .withSelector(fwd.selector()) |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 760 | .withTreatment(buildTreatment(Instructions.transition(QQ_TABLE), fetchMeter(fwd), |
| 761 | fetchWriteMetadata(fwd))); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 762 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 763 | //match: in port and any-vlan (coming from OLT app.) |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 764 | //action: immediate: push:QinQ, vlanId (s-tag), write metadata, meter and output |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 765 | FlowRule.Builder outer = DefaultFlowRule.builder() |
| 766 | .fromApp(fwd.appId()) |
| 767 | .forDevice(deviceId) |
| 768 | .forTable(QQ_TABLE) |
| 769 | .makePermanent() |
| 770 | .withPriority(fwd.priority()) |
| 771 | .withSelector(fwd.selector()) |
Andrea Campanella | 6edac32 | 2020-01-29 14:41:42 +0100 | [diff] [blame] | 772 | .withTreatment(buildTreatment(outerPair.getLeft(), outerPair.getRight(), |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 773 | fetchMeter(fwd), writeMetadataIncludingOnlyTp(fwd), output)); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 774 | |
Andrea Campanella | 6edac32 | 2020-01-29 14:41:42 +0100 | [diff] [blame] | 775 | applyRules(fwd, inner, outer); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | private boolean checkNoneVlanCriteria(ForwardingObjective fwd) { |
| 779 | // Add the VLAN_PUSH treatment if we're matching on VlanId.NONE |
| 780 | Criterion vlanMatchCriterion = filterForCriterion(fwd.selector().criteria(), Criterion.Type.VLAN_VID); |
| 781 | boolean noneValueVlanStatus = false; |
| 782 | if (vlanMatchCriterion != null) { |
| 783 | noneValueVlanStatus = ((VlanIdCriterion) vlanMatchCriterion).vlanId().equals(VlanId.NONE); |
| 784 | } |
| 785 | return noneValueVlanStatus; |
| 786 | } |
| 787 | |
| 788 | private boolean checkAnyVlanMatchCriteria(ForwardingObjective fwd) { |
| 789 | Criterion anyValueVlanCriterion = fwd.selector().criteria().stream() |
| 790 | .filter(c -> c.type().equals(Criterion.Type.VLAN_VID)) |
| 791 | .filter(vc -> ((VlanIdCriterion) vc).vlanId().toShort() == VlanId.ANY_VALUE) |
| 792 | .findAny().orElse(null); |
| 793 | |
| 794 | if (anyValueVlanCriterion == null) { |
Andrea Campanella | 6edac32 | 2020-01-29 14:41:42 +0100 | [diff] [blame] | 795 | log.debug("Any value vlan match criteria is not found, criteria {}", |
| 796 | fwd.selector().criteria()); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 797 | return false; |
| 798 | } |
| 799 | |
| 800 | return true; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | private Instruction fetchOutput(ForwardingObjective fwd, String direction) { |
| 804 | Instruction output = fwd.treatment().allInstructions().stream() |
| 805 | .filter(i -> i.type() == Instruction.Type.OUTPUT) |
| 806 | .findFirst().orElse(null); |
| 807 | |
| 808 | if (output == null) { |
| 809 | log.error("OLT {} rule has no output", direction); |
| 810 | fail(fwd, ObjectiveError.BADPARAMS); |
| 811 | return null; |
| 812 | } |
| 813 | return output; |
| 814 | } |
| 815 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 816 | private Instruction fetchMeter(ForwardingObjective fwd) { |
| 817 | Instruction meter = fwd.treatment().metered(); |
| 818 | |
| 819 | if (meter == null) { |
| 820 | log.debug("Meter instruction is not found for the forwarding objective {}", fwd); |
| 821 | return null; |
| 822 | } |
| 823 | |
| 824 | log.debug("Meter instruction is found."); |
| 825 | return meter; |
| 826 | } |
| 827 | |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 828 | private Instructions.MetadataInstruction fetchWriteMetadata(ForwardingObjective fwd) { |
| 829 | Instructions.MetadataInstruction writeMetadata = fwd.treatment().writeMetadata(); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 830 | |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 831 | if (writeMetadata == null) { |
| 832 | log.warn("Write metadata is not found for the forwarding obj"); |
| 833 | fail(fwd, ObjectiveError.BADPARAMS); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 834 | return null; |
| 835 | } |
| 836 | |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 837 | log.debug("Write metadata is found {}", writeMetadata); |
| 838 | return writeMetadata; |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 839 | } |
| 840 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 841 | private List<Pair<Instruction, Instruction>> vlanOps(ForwardingObjective fwd, |
| 842 | L2ModificationInstruction.L2SubType type) { |
| 843 | |
| 844 | List<Pair<Instruction, Instruction>> vlanOps = findVlanOps( |
| 845 | fwd.treatment().allInstructions(), type); |
| 846 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 847 | if (vlanOps == null || vlanOps.isEmpty()) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 848 | String direction = type == L2ModificationInstruction.L2SubType.VLAN_POP |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 849 | ? DOWNSTREAM : UPSTREAM; |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 850 | log.error("Missing vlan operations in {} forwarding: {}", direction, fwd); |
| 851 | fail(fwd, ObjectiveError.BADPARAMS); |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 852 | return ImmutableList.of(); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 853 | } |
| 854 | return vlanOps; |
| 855 | } |
| 856 | |
| 857 | |
| 858 | private List<Pair<Instruction, Instruction>> findVlanOps(List<Instruction> instructions, |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 859 | L2ModificationInstruction.L2SubType type) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 860 | |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 861 | List<Instruction> vlanOperations = findL2Instructions( |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 862 | type, |
| 863 | instructions); |
| 864 | List<Instruction> vlanSets = findL2Instructions( |
| 865 | L2ModificationInstruction.L2SubType.VLAN_ID, |
| 866 | instructions); |
| 867 | |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 868 | if (vlanOperations.size() != vlanSets.size()) { |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 869 | return ImmutableList.of(); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | List<Pair<Instruction, Instruction>> pairs = Lists.newArrayList(); |
| 873 | |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 874 | for (int i = 0; i < vlanOperations.size(); i++) { |
| 875 | pairs.add(new ImmutablePair<>(vlanOperations.get(i), vlanSets.get(i))); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 876 | } |
| 877 | return pairs; |
| 878 | } |
| 879 | |
| 880 | private List<Instruction> findL2Instructions(L2ModificationInstruction.L2SubType subType, |
| 881 | List<Instruction> actions) { |
| 882 | return actions.stream() |
| 883 | .filter(i -> i.type() == Instruction.Type.L2MODIFICATION) |
| 884 | .filter(i -> ((L2ModificationInstruction) i).subtype() == subType) |
| 885 | .collect(Collectors.toList()); |
| 886 | } |
| 887 | |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 888 | private void provisionEthTypeBasedFilter(FilteringObjective filter, |
| 889 | EthTypeCriterion ethType, |
| 890 | Instructions.OutputInstruction output) { |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 891 | |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 892 | Instruction meter = filter.meta().metered(); |
| 893 | Instruction writeMetadata = filter.meta().writeMetadata(); |
| 894 | |
Gamze Abaka | f57ef60 | 2019-03-11 06:52:48 +0000 | [diff] [blame] | 895 | Criterion vlanId = filterForCriterion(filter.conditions(), Criterion.Type.VLAN_VID); |
| 896 | |
| 897 | TrafficSelector selector = buildSelector(filter.key(), ethType, vlanId); |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 898 | TrafficTreatment treatment = buildTreatment(output, meter, writeMetadata); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 899 | buildAndApplyRule(filter, selector, treatment); |
| 900 | |
| 901 | } |
| 902 | |
Jonathan Hart | 51539b8 | 2015-10-29 09:53:04 -0700 | [diff] [blame] | 903 | private void provisionIgmp(FilteringObjective filter, EthTypeCriterion ethType, |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 904 | IPProtocolCriterion ipProto, |
| 905 | Instructions.OutputInstruction output) { |
Gamze Abaka | 7c070c8 | 2019-02-21 14:40:18 +0000 | [diff] [blame] | 906 | |
| 907 | Instruction meter = filter.meta().metered(); |
| 908 | Instruction writeMetadata = filter.meta().writeMetadata(); |
| 909 | |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 910 | // cTag |
| 911 | VlanIdCriterion vlanId = (VlanIdCriterion) filterForCriterion(filter.conditions(), |
| 912 | Criterion.Type.VLAN_VID); |
| 913 | Criterion cTagPriority = filterForCriterion(filter.conditions(), Criterion.Type.VLAN_PCP); |
| 914 | |
| 915 | TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto, vlanId, cTagPriority); |
Gamze Abaka | 7c070c8 | 2019-02-21 14:40:18 +0000 | [diff] [blame] | 916 | TrafficTreatment treatment = buildTreatment(output, meter, writeMetadata); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 917 | buildAndApplyRule(filter, selector, treatment); |
| 918 | } |
| 919 | |
Amit Ghosh | cbaf867 | 2016-12-23 21:36:19 +0000 | [diff] [blame] | 920 | private void provisionDhcp(FilteringObjective filter, EthTypeCriterion ethType, |
| 921 | IPProtocolCriterion ipProto, |
| 922 | UdpPortCriterion udpSrcPort, |
| 923 | UdpPortCriterion udpDstPort, |
| 924 | Instructions.OutputInstruction output) { |
Gamze Abaka | 7c070c8 | 2019-02-21 14:40:18 +0000 | [diff] [blame] | 925 | |
| 926 | Instruction meter = filter.meta().metered(); |
| 927 | Instruction writeMetadata = filter.meta().writeMetadata(); |
| 928 | |
Gamze Abaka | fe9d54f | 2019-12-02 06:42:39 +0000 | [diff] [blame] | 929 | VlanIdCriterion vlanId = (VlanIdCriterion) |
| 930 | filterForCriterion(filter.conditions(), Criterion.Type.VLAN_VID); |
| 931 | |
| 932 | TrafficSelector selector = buildSelector(filter.key(), ethType, ipProto, udpSrcPort, udpDstPort, vlanId); |
Gamze Abaka | 7c070c8 | 2019-02-21 14:40:18 +0000 | [diff] [blame] | 933 | TrafficTreatment treatment = buildTreatment(output, meter, writeMetadata); |
Amit Ghosh | cbaf867 | 2016-12-23 21:36:19 +0000 | [diff] [blame] | 934 | buildAndApplyRule(filter, selector, treatment); |
| 935 | } |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 936 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 937 | private void buildAndApplyRule(FilteringObjective filter, TrafficSelector selector, |
| 938 | TrafficTreatment treatment) { |
| 939 | FlowRule rule = DefaultFlowRule.builder() |
alshabib | 8336447 | 2016-03-25 09:59:55 -0700 | [diff] [blame] | 940 | .fromApp(filter.appId()) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 941 | .forDevice(deviceId) |
| 942 | .forTable(0) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 943 | .makePermanent() |
| 944 | .withSelector(selector) |
| 945 | .withTreatment(treatment) |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 946 | .withPriority(filter.priority()) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 947 | .build(); |
| 948 | |
| 949 | FlowRuleOperations.Builder opsBuilder = FlowRuleOperations.builder(); |
| 950 | |
| 951 | switch (filter.type()) { |
| 952 | case PERMIT: |
| 953 | opsBuilder.add(rule); |
| 954 | break; |
| 955 | case DENY: |
| 956 | opsBuilder.remove(rule); |
| 957 | break; |
| 958 | default: |
| 959 | log.warn("Unknown filter type : {}", filter.type()); |
| 960 | fail(filter, ObjectiveError.UNSUPPORTED); |
| 961 | } |
| 962 | |
| 963 | applyFlowRules(opsBuilder, filter); |
| 964 | } |
| 965 | |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 966 | private void applyRules(ForwardingObjective fwd, FlowRule.Builder... fwdBuilders) { |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 967 | FlowRuleOperations.Builder builder = FlowRuleOperations.builder(); |
| 968 | switch (fwd.op()) { |
| 969 | case ADD: |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 970 | for (FlowRule.Builder fwdBuilder : fwdBuilders) { |
| 971 | builder.add(fwdBuilder.build()); |
| 972 | } |
Amit Ghosh | f1f2275 | 2018-08-14 07:28:01 +0100 | [diff] [blame] | 973 | break; |
| 974 | case REMOVE: |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 975 | for (FlowRule.Builder fwdBuilder : fwdBuilders) { |
| 976 | builder.remove(fwdBuilder.build()); |
| 977 | } |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 978 | break; |
| 979 | case ADD_TO_EXISTING: |
| 980 | break; |
| 981 | case REMOVE_FROM_EXISTING: |
| 982 | break; |
| 983 | default: |
| 984 | log.warn("Unknown forwarding operation: {}", fwd.op()); |
| 985 | } |
| 986 | |
| 987 | applyFlowRules(builder, fwd); |
| 988 | } |
| 989 | |
| 990 | private void applyFlowRules(FlowRuleOperations.Builder builder, |
| 991 | Objective objective) { |
| 992 | flowRuleService.apply(builder.build(new FlowRuleOperationsContext() { |
| 993 | @Override |
| 994 | public void onSuccess(FlowRuleOperations ops) { |
| 995 | pass(objective); |
| 996 | } |
| 997 | |
| 998 | @Override |
| 999 | public void onError(FlowRuleOperations ops) { |
| 1000 | fail(objective, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 1001 | } |
| 1002 | })); |
| 1003 | } |
| 1004 | |
| 1005 | private Criterion filterForCriterion(Collection<Criterion> criteria, Criterion.Type type) { |
| 1006 | return criteria.stream() |
alshabib | bb42423 | 2016-01-15 12:20:25 -0800 | [diff] [blame] | 1007 | .filter(c -> c.type().equals(type)) |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 1008 | .limit(1) |
| 1009 | .findFirst().orElse(null); |
| 1010 | } |
| 1011 | |
| 1012 | private TrafficSelector buildSelector(Criterion... criteria) { |
| 1013 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 1014 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 1015 | |
Gamze Abaka | f57ef60 | 2019-03-11 06:52:48 +0000 | [diff] [blame] | 1016 | Arrays.stream(criteria).filter(Objects::nonNull).forEach(sBuilder::add); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 1017 | |
| 1018 | return sBuilder.build(); |
| 1019 | } |
| 1020 | |
| 1021 | private TrafficTreatment buildTreatment(Instruction... instructions) { |
| 1022 | |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 1023 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 1024 | |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 1025 | Arrays.stream(instructions).filter(Objects::nonNull).forEach(tBuilder::add); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 1026 | |
| 1027 | return tBuilder.build(); |
| 1028 | } |
| 1029 | |
Gamze Abaka | 6c8a71b | 2018-12-24 13:17:12 +0000 | [diff] [blame] | 1030 | private Instruction writeMetadataIncludingOnlyTp(ForwardingObjective fwd) { |
| 1031 | |
| 1032 | return Instructions.writeMetadata( |
| 1033 | fetchWriteMetadata(fwd).metadata() & 0xFFFF00000000L, 0L); |
| 1034 | } |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 1035 | |
| 1036 | private void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 1037 | obj.context().ifPresent(context -> context.onError(obj, error)); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | private void pass(Objective obj) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 1041 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
alshabib | fd430b6 | 2015-12-16 18:56:38 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 1044 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 1045 | private class InnerGroupListener implements GroupListener { |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 1046 | @Override |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 1047 | public void event(GroupEvent event) { |
Andrea Campanella | 5908b56 | 2020-05-06 16:09:10 +0200 | [diff] [blame] | 1048 | GroupKey key = event.subject().appCookie(); |
| 1049 | NextObjective obj = pendingGroups.getIfPresent(key); |
| 1050 | if (obj == null) { |
| 1051 | log.debug("No pending group for {}, moving on", key); |
| 1052 | return; |
| 1053 | } |
| 1054 | log.trace("Event {} for group {}, handling pending" + |
| 1055 | "NextGroup {}", event.type(), key, obj.id()); |
ke han | f508667 | 2016-08-12 11:09:17 +0800 | [diff] [blame] | 1056 | if (event.type() == GroupEvent.Type.GROUP_ADDED || |
Gamze Abaka | dadae72 | 2018-09-12 10:55:35 +0000 | [diff] [blame] | 1057 | event.type() == GroupEvent.Type.GROUP_UPDATED) { |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 1058 | flowObjectiveStore.putNextGroup(obj.id(), new OLTPipelineGroup(key)); |
| 1059 | pass(obj); |
| 1060 | pendingGroups.invalidate(key); |
Andrea Campanella | 5908b56 | 2020-05-06 16:09:10 +0200 | [diff] [blame] | 1061 | } else if (event.type() == GroupEvent.Type.GROUP_REMOVED) { |
| 1062 | flowObjectiveStore.removeNextGroup(obj.id()); |
| 1063 | pass(obj); |
| 1064 | pendingGroups.invalidate(key); |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 1065 | } |
alshabib | 2cc73cb | 2015-06-30 20:26:56 -0700 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | |
alshabib | d61b77b | 2016-02-01 23:30:53 -0800 | [diff] [blame] | 1069 | private static class OLTPipelineGroup implements NextGroup { |
| 1070 | |
| 1071 | private final GroupKey key; |
| 1072 | |
| 1073 | public OLTPipelineGroup(GroupKey key) { |
| 1074 | this.key = key; |
| 1075 | } |
| 1076 | |
| 1077 | public GroupKey key() { |
| 1078 | return key; |
| 1079 | } |
| 1080 | |
| 1081 | @Override |
| 1082 | public byte[] data() { |
| 1083 | return appKryo.serialize(key); |
| 1084 | } |
| 1085 | |
| 1086 | } |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1087 | |
| 1088 | @Override |
| 1089 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 1090 | // TODO Implementation deferred to vendor |
| 1091 | return null; |
| 1092 | } |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 1093 | } |