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