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); |
Carmelo Cascone | 2a308ff | 2021-06-01 18:31:57 -0700 | [diff] [blame] | 132 | |
| 133 | initializePipeline(); |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | protected void initializePipeline() { |
| 137 | // Set up rules for packet-out forwarding. We support only IPv4 routing. |
| 138 | final int cpuPort = capabilities.cpuPort().get(); |
| 139 | flowRuleService.applyFlowRules( |
| 140 | ingressVlanRule(cpuPort, false, DEFAULT_VLAN), |
| 141 | fwdClassifierRule(cpuPort, null, Ethernet.TYPE_IPV4, FWD_IPV4_ROUTING, |
| 142 | DEFAULT_FLOW_PRIORITY)); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 146 | public void filter(FilteringObjective obj) { |
| 147 | final ObjectiveTranslation result = filteringTranslator.translate(obj); |
| 148 | handleResult(obj, result); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 152 | public void forward(ForwardingObjective obj) { |
| 153 | final ObjectiveTranslation result = forwardingTranslator.translate(obj); |
| 154 | handleResult(obj, result); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 158 | public void next(NextObjective obj) { |
| 159 | if (obj.op() == Objective.Operation.VERIFY) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 160 | // TODO: support VERIFY operation |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 161 | log.debug("VERIFY operation not yet supported for NextObjective, will return success"); |
| 162 | success(obj); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 163 | return; |
| 164 | } |
| 165 | |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 166 | if (obj.op() == Objective.Operation.MODIFY && obj.type() != SIMPLE) { |
| 167 | log.warn("MODIFY operation not yet supported for NextObjective {}, will return failure :(", |
| 168 | obj.type()); |
| 169 | if (log.isTraceEnabled()) { |
| 170 | log.trace("Objective {}", obj); |
| 171 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 172 | fail(obj, ObjectiveError.UNSUPPORTED); |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 176 | final ObjectiveTranslation result = nextTranslator.translate(obj); |
| 177 | handleResult(obj, result); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public List<String> getNextMappings(NextGroup nextGroup) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 182 | final FabricNextGroup fabricNextGroup = KRYO.deserialize(nextGroup.data()); |
| 183 | return fabricNextGroup.nextMappings().stream() |
| 184 | .map(m -> format("%s -> %s", fabricNextGroup.type(), m)) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 185 | .collect(Collectors.toList()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 188 | private void handleResult(Objective obj, ObjectiveTranslation result) { |
| 189 | if (result.error().isPresent()) { |
| 190 | fail(obj, result.error().get()); |
| 191 | return; |
| 192 | } |
| 193 | processGroups(obj, result.groups()); |
| 194 | processFlows(obj, result.flowRules()); |
| 195 | if (obj instanceof NextObjective) { |
| 196 | handleNextGroup((NextObjective) obj); |
| 197 | } |
| 198 | success(obj); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 201 | private void handleNextGroup(NextObjective obj) { |
| 202 | switch (obj.op()) { |
| 203 | case REMOVE: |
| 204 | removeNextGroup(obj); |
| 205 | break; |
| 206 | case ADD: |
| 207 | case ADD_TO_EXISTING: |
| 208 | case REMOVE_FROM_EXISTING: |
| 209 | case MODIFY: |
| 210 | putNextGroup(obj); |
| 211 | break; |
| 212 | case VERIFY: |
| 213 | break; |
| 214 | default: |
| 215 | log.error("Unknown NextObjective operation '{}'", obj.op()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | private void processFlows(Objective objective, Collection<FlowRule> flowRules) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 220 | if (flowRules.isEmpty()) { |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 221 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 222 | } |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 223 | |
| 224 | if (log.isTraceEnabled()) { |
| 225 | log.trace("Objective {} -> Flows {}", objective, flowRules); |
| 226 | } |
| 227 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 228 | final FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 229 | switch (objective.op()) { |
| 230 | case ADD: |
| 231 | case ADD_TO_EXISTING: |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 232 | case MODIFY: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 233 | flowRules.forEach(ops::add); |
| 234 | break; |
| 235 | case REMOVE: |
| 236 | case REMOVE_FROM_EXISTING: |
| 237 | flowRules.forEach(ops::remove); |
| 238 | break; |
| 239 | default: |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 240 | log.warn("Unsupported Objective operation {}", objective.op()); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 241 | return; |
wu | 914ed23 | 2018-10-23 11:19:53 +0800 | [diff] [blame] | 242 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 243 | flowRuleService.apply(ops.build()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 246 | private void processGroups(Objective objective, Collection<GroupDescription> groups) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 247 | if (groups.isEmpty()) { |
Yi Tseng | f78e174 | 2018-04-08 19:57:17 +0800 | [diff] [blame] | 248 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 249 | } |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 250 | |
| 251 | if (log.isTraceEnabled()) { |
| 252 | log.trace("Objective {} -> Groups {}", objective, groups); |
| 253 | } |
| 254 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 255 | switch (objective.op()) { |
| 256 | case ADD: |
| 257 | groups.forEach(groupService::addGroup); |
| 258 | break; |
| 259 | case REMOVE: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 260 | groups.forEach(group -> groupService.removeGroup( |
| 261 | deviceId, group.appCookie(), objective.appId())); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 262 | break; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 263 | case ADD_TO_EXISTING: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 264 | groups.forEach(group -> groupService.addBucketsToGroup( |
| 265 | deviceId, group.appCookie(), group.buckets(), |
| 266 | group.appCookie(), group.appId()) |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 267 | ); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 268 | break; |
| 269 | case REMOVE_FROM_EXISTING: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 270 | groups.forEach(group -> groupService.removeBucketsFromGroup( |
| 271 | deviceId, group.appCookie(), group.buckets(), |
| 272 | group.appCookie(), group.appId()) |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 273 | ); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 274 | break; |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 275 | case MODIFY: |
| 276 | // Modify is only supported for simple next objective |
| 277 | // Replace group bucket directly |
| 278 | groups.forEach(group -> groupService.setBucketsForGroup( |
| 279 | deviceId, group.appCookie(), group.buckets(), |
| 280 | group.appCookie(), group.appId()) |
| 281 | ); |
| 282 | break; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 283 | default: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 284 | log.warn("Unsupported Objective operation {}", objective.op()); |
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 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 287 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 288 | private void fail(Objective objective, ObjectiveError error) { |
| 289 | CompletableFuture.runAsync( |
| 290 | () -> objective.context().ifPresent( |
| 291 | ctx -> ctx.onError(objective, error)), callbackExecutor); |
Yi Tseng | fe13f3e | 2018-08-19 03:09:54 +0800 | [diff] [blame] | 292 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 295 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 296 | private void success(Objective objective) { |
| 297 | CompletableFuture.runAsync( |
| 298 | () -> objective.context().ifPresent( |
| 299 | ctx -> ctx.onSuccess(objective)), callbackExecutor); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 302 | private void removeNextGroup(NextObjective obj) { |
| 303 | final NextGroup removed = flowObjectiveStore.removeNextGroup(obj.id()); |
| 304 | if (removed == null) { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 305 | log.debug("NextGroup {} was not found in FlowObjectiveStore", obj); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 306 | } |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 309 | private void putNextGroup(NextObjective obj) { |
| 310 | final List<String> nextMappings = obj.nextTreatments().stream() |
| 311 | .map(this::nextTreatmentToMappingString) |
| 312 | .filter(Objects::nonNull) |
| 313 | .collect(Collectors.toList()); |
| 314 | final FabricNextGroup nextGroup = new FabricNextGroup(obj.type(), nextMappings); |
| 315 | flowObjectiveStore.putNextGroup(obj.id(), nextGroup); |
| 316 | } |
| 317 | |
| 318 | private String nextTreatmentToMappingString(NextTreatment n) { |
| 319 | switch (n.type()) { |
| 320 | case TREATMENT: |
| 321 | final PortNumber p = outputPort(n); |
| 322 | return p == null ? "UNKNOWN" |
| 323 | : format("OUTPUT:%s", p.toString()); |
| 324 | case ID: |
| 325 | final IdNextTreatment id = (IdNextTreatment) n; |
| 326 | return format("NEXT_ID:%d", id.nextId()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 327 | default: |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 328 | log.warn("Unknown NextTreatment type '{}'", n.type()); |
| 329 | return "???"; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 330 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 333 | public FlowRule ingressVlanRule(long port, boolean vlanValid, int vlanId) { |
| 334 | final TrafficSelector selector = DefaultTrafficSelector.builder() |
| 335 | .add(Criteria.matchInPort(PortNumber.portNumber(port))) |
| 336 | .add(PiCriterion.builder() |
| 337 | .matchExact(FabricConstants.HDR_VLAN_IS_VALID, vlanValid ? ONE : ZERO) |
| 338 | .build()) |
| 339 | .build(); |
| 340 | final TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 341 | .piTableAction(PiAction.builder() |
| 342 | .withId(vlanValid ? FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT |
| 343 | : FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN) |
| 344 | .withParameter(new PiActionParam(FabricConstants.VLAN_ID, vlanId)) |
| 345 | .build()) |
| 346 | .build(); |
| 347 | return DefaultFlowRule.builder() |
| 348 | .withSelector(selector) |
| 349 | .withTreatment(treatment) |
| 350 | .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN) |
| 351 | .makePermanent() |
| 352 | .withPriority(DEFAULT_FLOW_PRIORITY) |
| 353 | .forDevice(deviceId) |
| 354 | .fromApp(appId) |
| 355 | .build(); |
| 356 | } |
| 357 | |
| 358 | public FlowRule fwdClassifierRule(int port, Short ethType, short ipEthType, byte fwdType, int priority) { |
| 359 | final TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder() |
| 360 | .matchInPort(PortNumber.portNumber(port)) |
| 361 | .matchPi(PiCriterion.builder() |
| 362 | .matchExact(FabricConstants.HDR_IP_ETH_TYPE, ipEthType) |
| 363 | .build()); |
| 364 | if (ethType != null) { |
| 365 | selectorBuilder.matchEthType(ethType); |
| 366 | } |
| 367 | final TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 368 | .piTableAction(PiAction.builder() |
| 369 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_SET_FORWARDING_TYPE) |
| 370 | .withParameter(new PiActionParam(FabricConstants.FWD_TYPE, fwdType)) |
| 371 | .build()) |
| 372 | .build(); |
| 373 | return DefaultFlowRule.builder() |
| 374 | .withSelector(selectorBuilder.build()) |
| 375 | .withTreatment(treatment) |
| 376 | .forTable(FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER) |
| 377 | .makePermanent() |
| 378 | .withPriority(priority) |
| 379 | .forDevice(deviceId) |
| 380 | .fromApp(appId) |
| 381 | .build(); |
| 382 | } |
| 383 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 384 | /** |
| 385 | * NextGroup implementation. |
| 386 | */ |
| 387 | private static class FabricNextGroup implements NextGroup { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 388 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 389 | private final NextObjective.Type type; |
| 390 | private final List<String> nextMappings; |
| 391 | |
| 392 | FabricNextGroup(NextObjective.Type type, List<String> nextMappings) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 393 | this.type = type; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 394 | this.nextMappings = ImmutableList.copyOf(nextMappings); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Charles Chan | 91ea972 | 2018-08-30 15:56:32 -0700 | [diff] [blame] | 397 | NextObjective.Type type() { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 398 | return type; |
| 399 | } |
| 400 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 401 | Collection<String> nextMappings() { |
| 402 | return nextMappings; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public byte[] data() { |
| 407 | return KRYO.serialize(this); |
| 408 | } |
| 409 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 410 | } |