Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 17 | package org.onosproject.pipelines.fabric.impl.behaviour.pipeliner; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 20 | import org.onlab.packet.Ethernet; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 21 | import org.onlab.util.KryoNamespace; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 22 | import org.onlab.util.SharedExecutors; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.behaviour.NextGroup; |
| 28 | import org.onosproject.net.behaviour.Pipeliner; |
| 29 | import org.onosproject.net.behaviour.PipelinerContext; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 30 | import org.onosproject.net.flow.DefaultFlowRule; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flow.FlowRule; |
| 34 | import org.onosproject.net.flow.FlowRuleOperations; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flow.FlowRuleService; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.net.flow.criteria.Criteria; |
| 39 | import org.onosproject.net.flow.criteria.PiCriterion; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 41 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 42 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 43 | import org.onosproject.net.flowobjective.IdNextTreatment; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 44 | import org.onosproject.net.flowobjective.NextObjective; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 45 | import org.onosproject.net.flowobjective.NextTreatment; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 46 | import org.onosproject.net.flowobjective.Objective; |
| 47 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 48 | import org.onosproject.net.group.GroupDescription; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 49 | import org.onosproject.net.group.GroupService; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 50 | import org.onosproject.net.pi.runtime.PiAction; |
| 51 | import org.onosproject.net.pi.runtime.PiActionParam; |
| 52 | import org.onosproject.pipelines.fabric.FabricConstants; |
| 53 | import org.onosproject.pipelines.fabric.impl.FabricPipeconfLoader; |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 54 | import org.onosproject.pipelines.fabric.impl.behaviour.AbstractFabricHandlerBehavior; |
| 55 | import org.onosproject.pipelines.fabric.impl.behaviour.FabricCapabilities; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 56 | import org.onosproject.store.serializers.KryoNamespaces; |
| 57 | import org.slf4j.Logger; |
| 58 | |
| 59 | import java.util.Collection; |
| 60 | import java.util.List; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 61 | import java.util.Objects; |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 62 | import java.util.concurrent.CompletableFuture; |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 63 | import java.util.concurrent.ExecutorService; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 64 | import java.util.stream.Collectors; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 65 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 66 | import static java.lang.String.format; |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 67 | import static org.onosproject.net.flowobjective.NextObjective.Type.SIMPLE; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 68 | import static org.onosproject.pipelines.fabric.impl.behaviour.FabricInterpreter.ONE; |
| 69 | import static org.onosproject.pipelines.fabric.impl.behaviour.FabricInterpreter.ZERO; |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 70 | import static org.onosproject.pipelines.fabric.impl.behaviour.FabricUtils.outputPort; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 71 | import static org.onosproject.pipelines.fabric.impl.behaviour.pipeliner.FilteringObjectiveTranslator.FWD_IPV4_ROUTING; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 72 | import static org.slf4j.LoggerFactory.getLogger; |
| 73 | |
| 74 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 75 | * Pipeliner implementation for fabric pipeline which uses ObjectiveTranslator |
| 76 | * implementations to translate flow objectives for the different blocks, |
| 77 | * filtering, forwarding and next. |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 78 | */ |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 79 | public class FabricPipeliner extends AbstractFabricHandlerBehavior |
| 80 | implements Pipeliner { |
| 81 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 82 | private static final Logger log = getLogger(FabricPipeliner.class); |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 83 | private static final int DEFAULT_FLOW_PRIORITY = 100; |
| 84 | public static final int DEFAULT_VLAN = 4094; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 85 | |
| 86 | protected static final KryoNamespace KRYO = new KryoNamespace.Builder() |
| 87 | .register(KryoNamespaces.API) |
| 88 | .register(FabricNextGroup.class) |
| 89 | .build("FabricPipeliner"); |
| 90 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 91 | protected DeviceId deviceId; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 92 | protected ApplicationId appId; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 93 | protected FlowRuleService flowRuleService; |
| 94 | protected GroupService groupService; |
| 95 | protected FlowObjectiveStore flowObjectiveStore; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 96 | protected CoreService coreService; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 97 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 98 | private FilteringObjectiveTranslator filteringTranslator; |
| 99 | private ForwardingObjectiveTranslator forwardingTranslator; |
| 100 | private NextObjectiveTranslator nextTranslator; |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 101 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 102 | private final ExecutorService callbackExecutor = SharedExecutors.getPoolThreadExecutor(); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 103 | |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Creates a new instance of this behavior with the given capabilities. |
| 106 | * |
| 107 | * @param capabilities capabilities |
| 108 | */ |
| 109 | public FabricPipeliner(FabricCapabilities capabilities) { |
| 110 | super(capabilities); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Create a new instance of this behaviour. Used by the abstract projectable |
| 115 | * model (i.e., {@link org.onosproject.net.Device#as(Class)}. |
| 116 | */ |
| 117 | public FabricPipeliner() { |
| 118 | super(); |
| 119 | } |
| 120 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 121 | @Override |
| 122 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 123 | this.deviceId = deviceId; |
| 124 | this.flowRuleService = context.directory().get(FlowRuleService.class); |
| 125 | this.groupService = context.directory().get(GroupService.class); |
| 126 | this.flowObjectiveStore = context.directory().get(FlowObjectiveStore.class); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 127 | this.filteringTranslator = new FilteringObjectiveTranslator(deviceId, capabilities); |
| 128 | this.forwardingTranslator = new ForwardingObjectiveTranslator(deviceId, capabilities); |
| 129 | this.nextTranslator = new NextObjectiveTranslator(deviceId, capabilities); |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 130 | this.coreService = context.directory().get(CoreService.class); |
| 131 | this.appId = coreService.getAppId(FabricPipeconfLoader.PIPELINE_APP_NAME); |
| 132 | } |
| 133 | |
| 134 | protected void initializePipeline() { |
| 135 | // Set up rules for packet-out forwarding. We support only IPv4 routing. |
| 136 | final int cpuPort = capabilities.cpuPort().get(); |
| 137 | flowRuleService.applyFlowRules( |
| 138 | ingressVlanRule(cpuPort, false, DEFAULT_VLAN), |
| 139 | fwdClassifierRule(cpuPort, null, Ethernet.TYPE_IPV4, FWD_IPV4_ROUTING, |
| 140 | DEFAULT_FLOW_PRIORITY)); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 144 | public void filter(FilteringObjective obj) { |
| 145 | final ObjectiveTranslation result = filteringTranslator.translate(obj); |
| 146 | handleResult(obj, result); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 150 | public void forward(ForwardingObjective obj) { |
| 151 | final ObjectiveTranslation result = forwardingTranslator.translate(obj); |
| 152 | handleResult(obj, result); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 156 | public void next(NextObjective obj) { |
| 157 | if (obj.op() == Objective.Operation.VERIFY) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 158 | // TODO: support VERIFY operation |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 159 | log.debug("VERIFY operation not yet supported for NextObjective, will return success"); |
| 160 | success(obj); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 164 | if (obj.op() == Objective.Operation.MODIFY && obj.type() != SIMPLE) { |
| 165 | log.warn("MODIFY operation not yet supported for NextObjective {}, will return failure :(", |
| 166 | obj.type()); |
| 167 | if (log.isTraceEnabled()) { |
| 168 | log.trace("Objective {}", obj); |
| 169 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 170 | fail(obj, ObjectiveError.UNSUPPORTED); |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 174 | final ObjectiveTranslation result = nextTranslator.translate(obj); |
| 175 | handleResult(obj, result); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public List<String> getNextMappings(NextGroup nextGroup) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 180 | final FabricNextGroup fabricNextGroup = KRYO.deserialize(nextGroup.data()); |
| 181 | return fabricNextGroup.nextMappings().stream() |
| 182 | .map(m -> format("%s -> %s", fabricNextGroup.type(), m)) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 183 | .collect(Collectors.toList()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 186 | private void handleResult(Objective obj, ObjectiveTranslation result) { |
| 187 | if (result.error().isPresent()) { |
| 188 | fail(obj, result.error().get()); |
| 189 | return; |
| 190 | } |
| 191 | processGroups(obj, result.groups()); |
| 192 | processFlows(obj, result.flowRules()); |
| 193 | if (obj instanceof NextObjective) { |
| 194 | handleNextGroup((NextObjective) obj); |
| 195 | } |
| 196 | success(obj); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 199 | private void handleNextGroup(NextObjective obj) { |
| 200 | switch (obj.op()) { |
| 201 | case REMOVE: |
| 202 | removeNextGroup(obj); |
| 203 | break; |
| 204 | case ADD: |
| 205 | case ADD_TO_EXISTING: |
| 206 | case REMOVE_FROM_EXISTING: |
| 207 | case MODIFY: |
| 208 | putNextGroup(obj); |
| 209 | break; |
| 210 | case VERIFY: |
| 211 | break; |
| 212 | default: |
| 213 | log.error("Unknown NextObjective operation '{}'", obj.op()); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private void processFlows(Objective objective, Collection<FlowRule> flowRules) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 218 | if (flowRules.isEmpty()) { |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 219 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 220 | } |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 221 | |
| 222 | if (log.isTraceEnabled()) { |
| 223 | log.trace("Objective {} -> Flows {}", objective, flowRules); |
| 224 | } |
| 225 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 226 | final FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 227 | switch (objective.op()) { |
| 228 | case ADD: |
| 229 | case ADD_TO_EXISTING: |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 230 | case MODIFY: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 231 | flowRules.forEach(ops::add); |
| 232 | break; |
| 233 | case REMOVE: |
| 234 | case REMOVE_FROM_EXISTING: |
| 235 | flowRules.forEach(ops::remove); |
| 236 | break; |
| 237 | default: |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 238 | log.warn("Unsupported Objective operation {}", objective.op()); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 239 | return; |
wu | 914ed23 | 2018-10-23 11:19:53 +0800 | [diff] [blame] | 240 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 241 | flowRuleService.apply(ops.build()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 244 | private void processGroups(Objective objective, Collection<GroupDescription> groups) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 245 | if (groups.isEmpty()) { |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 246 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 247 | } |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 248 | |
| 249 | if (log.isTraceEnabled()) { |
| 250 | log.trace("Objective {} -> Groups {}", objective, groups); |
| 251 | } |
| 252 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 253 | switch (objective.op()) { |
| 254 | case ADD: |
| 255 | groups.forEach(groupService::addGroup); |
| 256 | break; |
| 257 | case REMOVE: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 258 | groups.forEach(group -> groupService.removeGroup( |
| 259 | deviceId, group.appCookie(), objective.appId())); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 260 | break; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 261 | case ADD_TO_EXISTING: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 262 | groups.forEach(group -> groupService.addBucketsToGroup( |
| 263 | deviceId, group.appCookie(), group.buckets(), |
| 264 | group.appCookie(), group.appId()) |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 265 | ); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 266 | break; |
| 267 | case REMOVE_FROM_EXISTING: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 268 | groups.forEach(group -> groupService.removeBucketsFromGroup( |
| 269 | deviceId, group.appCookie(), group.buckets(), |
| 270 | group.appCookie(), group.appId()) |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 271 | ); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 272 | break; |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 273 | case MODIFY: |
| 274 | // Modify is only supported for simple next objective |
| 275 | // Replace group bucket directly |
| 276 | groups.forEach(group -> groupService.setBucketsForGroup( |
| 277 | deviceId, group.appCookie(), group.buckets(), |
| 278 | group.appCookie(), group.appId()) |
| 279 | ); |
| 280 | break; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 281 | default: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 282 | log.warn("Unsupported Objective operation {}", objective.op()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 283 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 284 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 285 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 286 | private void fail(Objective objective, ObjectiveError error) { |
| 287 | CompletableFuture.runAsync( |
| 288 | () -> objective.context().ifPresent( |
| 289 | ctx -> ctx.onError(objective, error)), callbackExecutor); |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 290 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 293 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 294 | private void success(Objective objective) { |
| 295 | CompletableFuture.runAsync( |
| 296 | () -> objective.context().ifPresent( |
| 297 | ctx -> ctx.onSuccess(objective)), callbackExecutor); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 300 | private void removeNextGroup(NextObjective obj) { |
| 301 | final NextGroup removed = flowObjectiveStore.removeNextGroup(obj.id()); |
| 302 | if (removed == null) { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 303 | log.debug("NextGroup {} was not found in FlowObjectiveStore", obj); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 304 | } |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 307 | private void putNextGroup(NextObjective obj) { |
| 308 | final List<String> nextMappings = obj.nextTreatments().stream() |
| 309 | .map(this::nextTreatmentToMappingString) |
| 310 | .filter(Objects::nonNull) |
| 311 | .collect(Collectors.toList()); |
| 312 | final FabricNextGroup nextGroup = new FabricNextGroup(obj.type(), nextMappings); |
| 313 | flowObjectiveStore.putNextGroup(obj.id(), nextGroup); |
| 314 | } |
| 315 | |
| 316 | private String nextTreatmentToMappingString(NextTreatment n) { |
| 317 | switch (n.type()) { |
| 318 | case TREATMENT: |
| 319 | final PortNumber p = outputPort(n); |
| 320 | return p == null ? "UNKNOWN" |
| 321 | : format("OUTPUT:%s", p.toString()); |
| 322 | case ID: |
| 323 | final IdNextTreatment id = (IdNextTreatment) n; |
| 324 | return format("NEXT_ID:%d", id.nextId()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 325 | default: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 326 | log.warn("Unknown NextTreatment type '{}'", n.type()); |
| 327 | return "???"; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 328 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 331 | public FlowRule ingressVlanRule(long port, boolean vlanValid, int vlanId) { |
| 332 | final TrafficSelector selector = DefaultTrafficSelector.builder() |
| 333 | .add(Criteria.matchInPort(PortNumber.portNumber(port))) |
| 334 | .add(PiCriterion.builder() |
| 335 | .matchExact(FabricConstants.HDR_VLAN_IS_VALID, vlanValid ? ONE : ZERO) |
| 336 | .build()) |
| 337 | .build(); |
| 338 | final TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 339 | .piTableAction(PiAction.builder() |
| 340 | .withId(vlanValid ? FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT |
| 341 | : FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN) |
| 342 | .withParameter(new PiActionParam(FabricConstants.VLAN_ID, vlanId)) |
| 343 | .build()) |
| 344 | .build(); |
| 345 | return DefaultFlowRule.builder() |
| 346 | .withSelector(selector) |
| 347 | .withTreatment(treatment) |
| 348 | .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN) |
| 349 | .makePermanent() |
| 350 | .withPriority(DEFAULT_FLOW_PRIORITY) |
| 351 | .forDevice(deviceId) |
| 352 | .fromApp(appId) |
| 353 | .build(); |
| 354 | } |
| 355 | |
| 356 | public FlowRule fwdClassifierRule(int port, Short ethType, short ipEthType, byte fwdType, int priority) { |
| 357 | final TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder() |
| 358 | .matchInPort(PortNumber.portNumber(port)) |
| 359 | .matchPi(PiCriterion.builder() |
| 360 | .matchExact(FabricConstants.HDR_IP_ETH_TYPE, ipEthType) |
| 361 | .build()); |
| 362 | if (ethType != null) { |
| 363 | selectorBuilder.matchEthType(ethType); |
| 364 | } |
| 365 | final TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 366 | .piTableAction(PiAction.builder() |
| 367 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_FORWARDING_TYPE) |
| 368 | .withParameter(new PiActionParam(FabricConstants.FWD_TYPE, fwdType)) |
| 369 | .build()) |
| 370 | .build(); |
| 371 | return DefaultFlowRule.builder() |
| 372 | .withSelector(selectorBuilder.build()) |
| 373 | .withTreatment(treatment) |
| 374 | .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER) |
| 375 | .makePermanent() |
| 376 | .withPriority(priority) |
| 377 | .forDevice(deviceId) |
| 378 | .fromApp(appId) |
| 379 | .build(); |
| 380 | } |
| 381 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 382 | /** |
| 383 | * NextGroup implementation. |
| 384 | */ |
| 385 | private static class FabricNextGroup implements NextGroup { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 386 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 387 | private final NextObjective.Type type; |
| 388 | private final List<String> nextMappings; |
| 389 | |
| 390 | FabricNextGroup(NextObjective.Type type, List<String> nextMappings) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 391 | this.type = type; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 392 | this.nextMappings = ImmutableList.copyOf(nextMappings); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 395 | NextObjective.Type type() { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 396 | return type; |
| 397 | } |
| 398 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 399 | Collection<String> nextMappings() { |
| 400 | return nextMappings; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public byte[] data() { |
| 405 | return KRYO.serialize(this); |
| 406 | } |
| 407 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 408 | } |