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