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