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