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 | |
| 17 | package org.onosproject.pipelines.fabric.pipeliner; |
| 18 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 19 | import org.onlab.packet.VlanId; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 21 | import org.onosproject.net.PortNumber; |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 22 | import org.onosproject.net.driver.Driver; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 23 | import org.onosproject.net.flow.DefaultFlowRule; |
| 24 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 25 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 26 | import org.onosproject.net.flow.TrafficSelector; |
| 27 | import org.onosproject.net.flow.TrafficTreatment; |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 28 | import org.onosproject.net.flow.criteria.Criterion; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flow.criteria.PiCriterion; |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flow.instructions.Instruction; |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flowobjective.NextObjective; |
Yi Tseng | 6e9b6f5 | 2018-02-27 10:40:51 +0100 | [diff] [blame] | 35 | import org.onosproject.net.flowobjective.Objective; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flowobjective.ObjectiveError; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 37 | import org.onosproject.net.group.DefaultGroupBucket; |
| 38 | import org.onosproject.net.group.DefaultGroupDescription; |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 39 | import org.onosproject.net.group.DefaultGroupKey; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 40 | import org.onosproject.net.group.GroupBucket; |
| 41 | import org.onosproject.net.group.GroupBuckets; |
| 42 | import org.onosproject.net.group.GroupDescription; |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 43 | import org.onosproject.net.group.GroupKey; |
| 44 | import org.onosproject.net.pi.runtime.PiAction; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 45 | import org.onosproject.net.pi.runtime.PiActionGroupId; |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 46 | import org.onosproject.net.pi.runtime.PiActionParam; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 47 | import org.onosproject.net.pi.runtime.PiGroupKey; |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 48 | import org.onosproject.pipelines.fabric.FabricConstants; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 49 | import org.onosproject.pipelines.fabric.FabricUtils; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 52 | import java.util.List; |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 53 | import java.util.Optional; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 54 | import java.util.stream.Collectors; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 55 | |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 56 | import static org.onosproject.pipelines.fabric.FabricUtils.getOutputPort; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 57 | import static org.slf4j.LoggerFactory.getLogger; |
| 58 | |
| 59 | /** |
| 60 | * Handling next objective for fabric pipeliner. |
| 61 | */ |
| 62 | public class FabricNextPipeliner { |
| 63 | private static final Logger log = getLogger(FabricNextPipeliner.class); |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 64 | private static final String NO_HASHED_TABLE = "noHashedTable"; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 65 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 66 | protected DeviceId deviceId; |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 67 | protected Driver driver; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 68 | |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 69 | public FabricNextPipeliner(DeviceId deviceId, Driver driver) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 70 | this.deviceId = deviceId; |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 71 | this.driver = driver; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | public PipelinerTranslationResult next(NextObjective nextObjective) { |
| 75 | PipelinerTranslationResult.Builder resultBuilder = PipelinerTranslationResult.builder(); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 76 | |
Charles Chan | b1f02f2 | 2018-08-31 20:19:33 -0700 | [diff] [blame] | 77 | if (nextObjective.op() != Objective.Operation.ADD_TO_EXISTING && |
| 78 | nextObjective.op() != Objective.Operation.REMOVE_FROM_EXISTING) { |
| 79 | processNextVlanMeta(nextObjective, resultBuilder); |
| 80 | } |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 81 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 82 | switch (nextObjective.type()) { |
| 83 | case SIMPLE: |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 84 | processSimpleNext(nextObjective, resultBuilder); |
| 85 | break; |
| 86 | case HASHED: |
| 87 | processHashedNext(nextObjective, resultBuilder); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 88 | break; |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 89 | case BROADCAST: |
| 90 | processBroadcastNext(nextObjective, resultBuilder); |
| 91 | break; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 92 | default: |
| 93 | log.warn("Unsupported next type {}", nextObjective); |
| 94 | resultBuilder.setError(ObjectiveError.UNSUPPORTED); |
| 95 | break; |
| 96 | } |
| 97 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 98 | return resultBuilder.build(); |
| 99 | } |
| 100 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 101 | private void processNextVlanMeta(NextObjective next, |
| 102 | PipelinerTranslationResult.Builder resultBuilder) { |
| 103 | TrafficSelector meta = next.meta(); |
| 104 | if (meta == null) { |
| 105 | // do nothing if there is no metadata in the next objective. |
| 106 | return; |
| 107 | } |
| 108 | VlanIdCriterion vlanIdCriterion = |
| 109 | (VlanIdCriterion) meta.getCriterion(Criterion.Type.VLAN_VID); |
| 110 | |
| 111 | if (vlanIdCriterion == null) { |
| 112 | // do nothing if we can't find vlan from next objective metadata. |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | VlanId vlanId = vlanIdCriterion.vlanId(); |
| 117 | TrafficSelector selector = buildNextIdSelector(next.id()); |
| 118 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 119 | .setVlanId(vlanId) |
| 120 | .build(); |
| 121 | |
| 122 | resultBuilder.addFlowRule(DefaultFlowRule.builder() |
| 123 | .withSelector(selector) |
| 124 | .withTreatment(treatment) |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 125 | .forTable(FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META) |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 126 | .makePermanent() |
| 127 | .withPriority(next.priority()) |
| 128 | .forDevice(deviceId) |
| 129 | .fromApp(next.appId()) |
| 130 | .build()); |
| 131 | } |
| 132 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 133 | private void processSimpleNext(NextObjective next, |
| 134 | PipelinerTranslationResult.Builder resultBuilder) { |
| 135 | |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 136 | if (next.next().size() > 1) { |
| 137 | log.warn("Only one treatment in simple next objective"); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 138 | resultBuilder.setError(ObjectiveError.BADPARAMS); |
| 139 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | TrafficSelector selector = buildNextIdSelector(next.id()); |
| 143 | TrafficTreatment treatment = next.next().iterator().next(); |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 144 | PortNumber outputPort = getOutputPort(treatment); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 145 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 146 | if (outputPort == null) { |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 147 | log.warn("At least one output instruction in simple next objective"); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 148 | resultBuilder.setError(ObjectiveError.BADPARAMS); |
| 149 | return; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 150 | } |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 151 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 152 | resultBuilder.addFlowRule(DefaultFlowRule.builder() |
| 153 | .withSelector(selector) |
| 154 | .withTreatment(treatment) |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 155 | .forTable(FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 156 | .makePermanent() |
| 157 | .withPriority(next.priority()) |
| 158 | .forDevice(deviceId) |
| 159 | .fromApp(next.appId()) |
| 160 | .build()); |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 161 | |
| 162 | if (includesPopVlanInst(treatment)) { |
| 163 | processVlanPopRule(outputPort, next, resultBuilder); |
| 164 | } |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 167 | private boolean includesPopVlanInst(TrafficTreatment treatment) { |
| 168 | return treatment.allInstructions() |
| 169 | .stream() |
| 170 | .filter(inst -> inst.type() == Instruction.Type.L2MODIFICATION) |
| 171 | .map(inst -> (L2ModificationInstruction) inst) |
| 172 | .anyMatch(inst -> inst.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP); |
| 173 | } |
| 174 | |
| 175 | private void processVlanPopRule(PortNumber port, NextObjective next, |
| 176 | PipelinerTranslationResult.Builder resultBuilder) { |
| 177 | TrafficSelector meta = next.meta(); |
| 178 | VlanIdCriterion vlanIdCriterion = |
| 179 | (VlanIdCriterion) meta.getCriterion(Criterion.Type.VLAN_VID); |
| 180 | VlanId vlanId = vlanIdCriterion.vlanId(); |
| 181 | |
| 182 | PiCriterion egressVlanTableMatch = PiCriterion.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 183 | .matchExact(FabricConstants.STANDARD_METADATA_EGRESS_PORT, |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 184 | (short) port.toLong()) |
| 185 | .build(); |
| 186 | // Add VLAN pop rule to egress pipeline table |
| 187 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 188 | .matchPi(egressVlanTableMatch) |
| 189 | .matchVlanId(vlanId) |
| 190 | .build(); |
| 191 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 192 | .popVlan() |
| 193 | .build(); |
| 194 | resultBuilder.addFlowRule(DefaultFlowRule.builder() |
| 195 | .withSelector(selector) |
| 196 | .withTreatment(treatment) |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 197 | .forTable(FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN) |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 198 | .makePermanent() |
| 199 | .withPriority(next.priority()) |
| 200 | .forDevice(deviceId) |
| 201 | .fromApp(next.appId()) |
| 202 | .build()); |
| 203 | } |
| 204 | |
| 205 | private void processHashedNext(NextObjective next, PipelinerTranslationResult.Builder resultBuilder) { |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 206 | boolean noHashedTable = Boolean.parseBoolean(driver.getProperty(NO_HASHED_TABLE)); |
| 207 | |
| 208 | if (noHashedTable) { |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 209 | if (next.next().isEmpty()) { |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 210 | return; |
| 211 | } |
| 212 | // use first action if not support hashed group |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 213 | TrafficTreatment treatment = next.next().iterator().next(); |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 214 | |
| 215 | NextObjective.Builder simpleNext = DefaultNextObjective.builder() |
| 216 | .addTreatment(treatment) |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 217 | .withId(next.id()) |
| 218 | .fromApp(next.appId()) |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 219 | .makePermanent() |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 220 | .withMeta(next.meta()) |
| 221 | .withPriority(next.priority()) |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 222 | .withType(NextObjective.Type.SIMPLE); |
| 223 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 224 | if (next.context().isPresent()) { |
| 225 | processSimpleNext(simpleNext.add(next.context().get()), resultBuilder); |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 226 | } else { |
| 227 | processSimpleNext(simpleNext.add(), resultBuilder); |
| 228 | } |
| 229 | return; |
| 230 | } |
| 231 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 232 | // create hash groups |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 233 | int groupId = next.id(); |
| 234 | List<GroupBucket> bucketList = next.next().stream() |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 235 | .map(DefaultGroupBucket::createSelectGroupBucket) |
| 236 | .collect(Collectors.toList()); |
| 237 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 238 | // Egress VLAN handling |
| 239 | next.next().forEach(treatment -> { |
| 240 | PortNumber outputPort = getOutputPort(treatment); |
| 241 | if (includesPopVlanInst(treatment) && outputPort != null) { |
| 242 | processVlanPopRule(outputPort, next, resultBuilder); |
| 243 | } |
| 244 | }); |
| 245 | |
| 246 | if (bucketList.size() != next.next().size()) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 247 | // some action not converted |
| 248 | // set error |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 249 | log.warn("Expected bucket size {}, got {}", next.next().size(), bucketList.size()); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 250 | resultBuilder.setError(ObjectiveError.BADPARAMS); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | GroupBuckets buckets = new GroupBuckets(bucketList); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 255 | PiGroupKey groupKey = new PiGroupKey(FabricConstants.FABRIC_INGRESS_NEXT_HASHED, |
| 256 | FabricConstants.FABRIC_INGRESS_NEXT_ECMP_SELECTOR, |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 257 | groupId); |
| 258 | |
| 259 | resultBuilder.addGroup(new DefaultGroupDescription(deviceId, |
| 260 | GroupDescription.Type.SELECT, |
| 261 | buckets, |
| 262 | groupKey, |
| 263 | groupId, |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 264 | next.appId())); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 265 | |
| 266 | // flow |
Yi Tseng | 6e9b6f5 | 2018-02-27 10:40:51 +0100 | [diff] [blame] | 267 | // If operation is ADD_TO_EXIST or REMOVE_FROM_EXIST, means we modify |
| 268 | // group buckets only, no changes for flow rule |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 269 | if (next.op() == Objective.Operation.ADD_TO_EXISTING || |
| 270 | next.op() == Objective.Operation.REMOVE_FROM_EXISTING) { |
Yi Tseng | 6e9b6f5 | 2018-02-27 10:40:51 +0100 | [diff] [blame] | 271 | return; |
| 272 | } |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 273 | TrafficSelector selector = buildNextIdSelector(next.id()); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 274 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 275 | .piTableAction(PiActionGroupId.of(next.id())) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 276 | .build(); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 277 | |
| 278 | resultBuilder.addFlowRule(DefaultFlowRule.builder() |
| 279 | .withSelector(selector) |
| 280 | .withTreatment(treatment) |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 281 | .forTable(FabricConstants.FABRIC_INGRESS_NEXT_HASHED) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 282 | .makePermanent() |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 283 | .withPriority(next.priority()) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 284 | .forDevice(deviceId) |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 285 | .fromApp(next.appId()) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 286 | .build()); |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | private TrafficSelector buildNextIdSelector(int nextId) { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 290 | PiCriterion nextIdCriterion = PiCriterion.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 291 | .matchExact(FabricConstants.FABRIC_METADATA_NEXT_ID, nextId) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 292 | .build(); |
| 293 | return DefaultTrafficSelector.builder() |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 294 | .matchPi(nextIdCriterion) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 295 | .build(); |
| 296 | } |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 297 | |
| 298 | private void processBroadcastNext(NextObjective next, PipelinerTranslationResult.Builder resultBuilder) { |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 299 | final GroupDescription allGroup = getAllGroup(next); |
| 300 | if (allGroup == null) { |
| 301 | // Error already logged. |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 302 | resultBuilder.setError(ObjectiveError.BADPARAMS); |
| 303 | return; |
| 304 | } |
| 305 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 306 | resultBuilder.addGroup(allGroup); |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 307 | //flow rule |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 308 | final TrafficSelector selector = buildNextIdSelector(next.id()); |
| 309 | final PiActionParam groupIdParam = new PiActionParam( |
| 310 | FabricConstants.GID, allGroup.givenGroupId()); |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 311 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 312 | final PiAction setMcGroupAction = PiAction.builder() |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 313 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_MCAST_GROUP) |
| 314 | .withParameter(groupIdParam) |
| 315 | .build(); |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 316 | final TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 317 | .piTableAction(setMcGroupAction) |
| 318 | .build(); |
| 319 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 320 | resultBuilder.addFlowRule( |
| 321 | DefaultFlowRule.builder() |
| 322 | .withSelector(selector) |
| 323 | .withTreatment(treatment) |
| 324 | .forTable(FabricConstants.FABRIC_INGRESS_NEXT_MULTICAST) |
| 325 | .makePermanent() |
| 326 | .withPriority(next.priority()) |
| 327 | .forDevice(deviceId) |
| 328 | .fromApp(next.appId()) |
| 329 | .build()); |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 330 | |
| 331 | // Egress VLAN handling |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 332 | next.next().forEach(t -> { |
| 333 | PortNumber outputPort = getOutputPort(t); |
| 334 | if (includesPopVlanInst(t) && outputPort != null) { |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 335 | processVlanPopRule(outputPort, next, resultBuilder); |
| 336 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 337 | if (t.allInstructions().size() > 2) { |
| 338 | // More than OUTPUT and VLAN_POP... |
| 339 | log.warn("Some instructions of BROADCAST NextObjective might" + |
| 340 | "not have been applied, supported only " + |
| 341 | "OUTPUT and VLAN_POP, but found {}", t); |
| 342 | } |
Esin Karaman | 24fda8a | 2018-01-26 11:52:28 +0000 | [diff] [blame] | 343 | }); |
| 344 | } |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 345 | |
| 346 | private GroupDescription getAllGroup(NextObjective next) { |
| 347 | final List<GroupBucket> bucketList = next.next().stream() |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 348 | .map(FabricUtils::getOutputInstruction) |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 349 | .filter(Optional::isPresent) |
| 350 | .map(Optional::get) |
| 351 | .map(i -> DefaultTrafficTreatment.builder().add(i).build()) |
| 352 | .map(DefaultGroupBucket::createAllGroupBucket) |
| 353 | .collect(Collectors.toList()); |
| 354 | |
| 355 | if (bucketList.size() != next.next().size()) { |
| 356 | log.warn("Got BROADCAST NextObjective with {} treatments but " + |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 357 | "found only {} OUTPUT instructions, cannot " + |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 358 | "translate to ALL groups", |
| 359 | next.next().size(), bucketList.size()); |
| 360 | return null; |
| 361 | } |
| 362 | |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 363 | // FIXME: remove once support for clone sessions is available |
| 364 | // Right now we add a CPU port to all multicast groups. The egress |
| 365 | // pipeline is expected to drop replicated packets to the CPU if a clone |
| 366 | // was not requested in the ingress pipeline. |
| 367 | bucketList.add( |
| 368 | DefaultGroupBucket.createAllGroupBucket( |
| 369 | DefaultTrafficTreatment.builder() |
| 370 | .setOutput(PortNumber.CONTROLLER) |
| 371 | .build())); |
| 372 | |
Carmelo Cascone | 5813681 | 2018-07-19 03:40:16 +0200 | [diff] [blame] | 373 | final int groupId = next.id(); |
| 374 | final GroupBuckets buckets = new GroupBuckets(bucketList); |
| 375 | // Used DefaultGroupKey instead of PiGroupKey |
| 376 | // as we don't have any action profile to apply to the groups of ALL type |
| 377 | final GroupKey groupKey = new DefaultGroupKey(FabricPipeliner.KRYO.serialize(groupId)); |
| 378 | |
| 379 | return new DefaultGroupDescription(deviceId, |
| 380 | GroupDescription.Type.ALL, |
| 381 | buckets, |
| 382 | groupKey, |
| 383 | groupId, |
| 384 | next.appId()); |
| 385 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 386 | } |