Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [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; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.Lists; |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 20 | import org.onlab.packet.VlanId; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
| 22 | import org.onosproject.net.PortNumber; |
| 23 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 24 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 25 | import org.onosproject.net.flow.TrafficSelector; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
| 27 | import org.onosproject.net.flow.criteria.Criterion; |
| 28 | import org.onosproject.net.flow.criteria.PiCriterion; |
| 29 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 30 | import org.onosproject.net.flow.instructions.Instruction; |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flowobjective.DefaultNextTreatment; |
| 33 | import org.onosproject.net.flowobjective.NextObjective; |
| 34 | import org.onosproject.net.flowobjective.NextTreatment; |
| 35 | import org.onosproject.net.flowobjective.Objective; |
| 36 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 37 | import org.onosproject.net.group.DefaultGroupBucket; |
| 38 | import org.onosproject.net.group.DefaultGroupDescription; |
| 39 | import org.onosproject.net.group.DefaultGroupKey; |
| 40 | import org.onosproject.net.group.GroupBucket; |
| 41 | import org.onosproject.net.group.GroupBuckets; |
| 42 | import org.onosproject.net.group.GroupDescription; |
| 43 | import org.onosproject.net.group.GroupKey; |
| 44 | import org.onosproject.net.pi.model.PiTableId; |
| 45 | import org.onosproject.net.pi.runtime.PiAction; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 46 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 47 | import org.onosproject.net.pi.runtime.PiActionProfileGroupId; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 48 | import org.onosproject.net.pi.runtime.PiGroupKey; |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 49 | import org.onosproject.pipelines.fabric.impl.behaviour.FabricCapabilities; |
Carmelo Cascone | 2102bfb | 2020-12-04 16:54:24 -0800 | [diff] [blame] | 50 | import org.onosproject.pipelines.fabric.FabricConstants; |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 51 | import org.onosproject.pipelines.fabric.impl.behaviour.FabricUtils; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 52 | |
| 53 | import java.util.Collection; |
| 54 | import java.util.List; |
| 55 | import java.util.Objects; |
| 56 | import java.util.Set; |
| 57 | import java.util.stream.Collectors; |
| 58 | |
| 59 | import static java.lang.String.format; |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 60 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.VLAN_ID; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 61 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.VLAN_POP; |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 62 | import static org.onosproject.pipelines.fabric.impl.behaviour.FabricUtils.criterion; |
| 63 | import static org.onosproject.pipelines.fabric.impl.behaviour.FabricUtils.l2Instruction; |
| 64 | import static org.onosproject.pipelines.fabric.impl.behaviour.FabricUtils.l2Instructions; |
| 65 | import static org.onosproject.pipelines.fabric.impl.behaviour.FabricUtils.outputPort; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * ObjectiveTranslator implementation for NextObjective. |
| 69 | */ |
| 70 | class NextObjectiveTranslator |
| 71 | extends AbstractObjectiveTranslator<NextObjective> { |
| 72 | |
Carmelo Cascone | 45cc086 | 2018-11-26 11:50:41 -0800 | [diff] [blame] | 73 | private static final String XCONNECT = "xconnect"; |
| 74 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 75 | NextObjectiveTranslator(DeviceId deviceId, FabricCapabilities capabilities) { |
| 76 | super(deviceId, capabilities); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public ObjectiveTranslation doTranslate(NextObjective obj) |
| 81 | throws FabricPipelinerException { |
| 82 | |
| 83 | final ObjectiveTranslation.Builder resultBuilder = |
| 84 | ObjectiveTranslation.builder(); |
| 85 | |
| 86 | switch (obj.type()) { |
| 87 | case SIMPLE: |
| 88 | simpleNext(obj, resultBuilder, false); |
| 89 | break; |
| 90 | case HASHED: |
| 91 | hashedNext(obj, resultBuilder); |
| 92 | break; |
| 93 | case BROADCAST: |
Carmelo Cascone | 45cc086 | 2018-11-26 11:50:41 -0800 | [diff] [blame] | 94 | if (isXconnect(obj)) { |
| 95 | xconnectNext(obj, resultBuilder); |
| 96 | } else { |
| 97 | multicastNext(obj, resultBuilder); |
| 98 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 99 | break; |
| 100 | default: |
| 101 | log.warn("Unsupported NextObjective type '{}'", obj); |
| 102 | return ObjectiveTranslation.ofError(ObjectiveError.UNSUPPORTED); |
| 103 | } |
| 104 | |
| 105 | if (!isGroupModifyOp(obj)) { |
| 106 | // Generate next VLAN rules. |
| 107 | nextVlan(obj, resultBuilder); |
| 108 | } |
| 109 | |
| 110 | return resultBuilder.build(); |
| 111 | } |
| 112 | |
| 113 | private void nextVlan(NextObjective obj, |
| 114 | ObjectiveTranslation.Builder resultBuilder) |
| 115 | throws FabricPipelinerException { |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 116 | // We expect NextObjective treatments to contain one or two VLAN instructions. |
| 117 | // If two, this treatment should be mapped to an action for double-vlan push. |
| 118 | // In fabric.p4, mapping next IDs to VLAN IDs is done by a direct table (next_vlan), |
| 119 | // for this reason, we also make sure that all treatments in the NextObjective |
| 120 | // have exactly the same VLAN instructions, as they will be mapped to a single action |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 121 | |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 122 | // Try to extract VLAN instructions in the treatment, |
| 123 | // later we check if we support multiple VLAN termination. |
| 124 | final List<List<ModVlanIdInstruction>> vlanInstructions = defaultNextTreatments( |
Carmelo Cascone | 1c8a4ed | 2019-04-08 15:37:03 -0700 | [diff] [blame] | 125 | obj.nextTreatments(), false).stream() |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 126 | .map(defaultNextTreatment -> |
| 127 | l2Instructions(defaultNextTreatment.treatment(), VLAN_ID) |
| 128 | .stream().map(v -> (ModVlanIdInstruction) v) |
| 129 | .collect(Collectors.toList())) |
| 130 | .filter(l -> !l.isEmpty()) |
Carmelo Cascone | 1c8a4ed | 2019-04-08 15:37:03 -0700 | [diff] [blame] | 131 | .collect(Collectors.toList()); |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 132 | |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 133 | final VlanIdCriterion vlanIdCriterion = obj.meta() == null ? null |
| 134 | : (VlanIdCriterion) criterion(obj.meta().criteria(), Criterion.Type.VLAN_VID); |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 135 | |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 136 | final List<VlanId> vlanIdList; |
Carmelo Cascone | 1c8a4ed | 2019-04-08 15:37:03 -0700 | [diff] [blame] | 137 | if (vlanInstructions.isEmpty() && vlanIdCriterion == null) { |
| 138 | // No VLAN_ID to apply. |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 139 | return; |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 140 | } |
| 141 | if (!vlanInstructions.isEmpty()) { |
Carmelo Cascone | 1c8a4ed | 2019-04-08 15:37:03 -0700 | [diff] [blame] | 142 | // Give priority to what found in the instructions. |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 143 | // Expect the same VLAN ID (or two VLAN IDs in the same order) for all instructions. |
| 144 | final Set<List<VlanId>> vlanIds = vlanInstructions.stream() |
| 145 | .map(l -> l.stream().map(ModVlanIdInstruction::vlanId).collect(Collectors.toList())) |
Carmelo Cascone | 1c8a4ed | 2019-04-08 15:37:03 -0700 | [diff] [blame] | 146 | .collect(Collectors.toSet()); |
| 147 | if (obj.nextTreatments().size() != vlanInstructions.size() || |
| 148 | vlanIds.size() != 1) { |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 149 | throw new FabricPipelinerException( |
Carmelo Cascone | 1c8a4ed | 2019-04-08 15:37:03 -0700 | [diff] [blame] | 150 | "Inconsistent VLAN_ID instructions, cannot process " + |
| 151 | "next_vlan rule. It is required that all " + |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 152 | "treatments have the same VLAN_ID instructions."); |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 153 | } |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 154 | vlanIdList = vlanIds.iterator().next(); |
Carmelo Cascone | 1c8a4ed | 2019-04-08 15:37:03 -0700 | [diff] [blame] | 155 | } else { |
| 156 | // Use the value in meta. |
| 157 | // FIXME: there should be no need to generate a next_vlan rule for |
| 158 | // the value found in meta. Meta describes the fields that were |
| 159 | // expected to be matched in previous pipeline stages, i.e. |
| 160 | // existing packet fields. But, for some reason, if we remove this |
| 161 | // rule, traffic is not forwarded at spines. We might need to look |
| 162 | // at the way default VLANs are handled in fabric.p4. |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 163 | vlanIdList = List.of(vlanIdCriterion.vlanId()); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 166 | final TrafficSelector selector = nextIdSelector(obj.id()); |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 167 | final TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 168 | vlanIdList.stream().forEach(vlanId -> treatmentBuilder.setVlanId(vlanId)); |
| 169 | final TrafficTreatment treatment = treatmentBuilder.build(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 170 | |
| 171 | resultBuilder.addFlowRule(flowRule( |
| 172 | obj, FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN, |
| 173 | selector, treatment)); |
| 174 | } |
| 175 | |
| 176 | private void simpleNext(NextObjective obj, |
| 177 | ObjectiveTranslation.Builder resultBuilder, |
| 178 | boolean forceSimple) |
| 179 | throws FabricPipelinerException { |
| 180 | |
| 181 | if (capabilities.hasHashedTable()) { |
| 182 | // Use hashed table when possible. |
| 183 | hashedNext(obj, resultBuilder); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (obj.nextTreatments().isEmpty()) { |
| 188 | // Do nothing. |
| 189 | return; |
| 190 | } else if (!forceSimple && obj.nextTreatments().size() != 1) { |
| 191 | throw new FabricPipelinerException(format( |
| 192 | "SIMPLE NextObjective should contain only 1 treatment, found %d", |
| 193 | obj.nextTreatments().size()), ObjectiveError.BADPARAMS); |
| 194 | } |
| 195 | |
| 196 | final TrafficSelector selector = nextIdSelector(obj.id()); |
| 197 | |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 198 | final List<DefaultNextTreatment> treatments = defaultNextTreatments( |
| 199 | obj.nextTreatments(), true); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 200 | |
| 201 | if (forceSimple && treatments.size() > 1) { |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 202 | log.warn("Forcing SIMPLE behavior for NextObjective with {} treatments {}", |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 203 | treatments.size(), obj); |
| 204 | } |
| 205 | |
| 206 | // If not forcing, we are essentially extracting the only available treatment. |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 207 | final TrafficTreatment treatment = defaultNextTreatments( |
| 208 | obj.nextTreatments(), true).get(0).treatment(); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 209 | |
| 210 | resultBuilder.addFlowRule(flowRule( |
| 211 | obj, FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE, |
| 212 | selector, treatment)); |
| 213 | |
| 214 | handleEgress(obj, treatment, resultBuilder, false); |
| 215 | } |
| 216 | |
| 217 | private void hashedNext(NextObjective obj, |
| 218 | ObjectiveTranslation.Builder resultBuilder) |
| 219 | throws FabricPipelinerException { |
| 220 | |
| 221 | if (!capabilities.hasHashedTable()) { |
| 222 | simpleNext(obj, resultBuilder, true); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // Updated result builder with hashed group. |
| 227 | final int groupId = selectGroup(obj, resultBuilder); |
| 228 | |
| 229 | if (isGroupModifyOp(obj)) { |
| 230 | // No changes to flow rules. |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | final TrafficSelector selector = nextIdSelector(obj.id()); |
| 235 | final TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 236 | .piTableAction(PiActionProfileGroupId.of(groupId)) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 237 | .build(); |
| 238 | |
| 239 | resultBuilder.addFlowRule(flowRule( |
| 240 | obj, FabricConstants.FABRIC_INGRESS_NEXT_HASHED, |
| 241 | selector, treatment)); |
| 242 | } |
| 243 | |
| 244 | private void handleEgress(NextObjective obj, TrafficTreatment treatment, |
| 245 | ObjectiveTranslation.Builder resultBuilder, |
| 246 | boolean strict) |
| 247 | throws FabricPipelinerException { |
| 248 | final PortNumber outPort = outputPort(treatment); |
| 249 | final Instruction popVlanInst = l2Instruction(treatment, VLAN_POP); |
pierventre | 48e7882 | 2020-12-15 17:34:54 +0100 | [diff] [blame] | 250 | if (outPort != null) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 251 | if (strict && treatment.allInstructions().size() > 2) { |
| 252 | throw new FabricPipelinerException( |
| 253 | "Treatment contains instructions other " + |
| 254 | "than OUTPUT and VLAN_POP, cannot generate " + |
| 255 | "egress rules"); |
| 256 | } |
pierventre | 48e7882 | 2020-12-15 17:34:54 +0100 | [diff] [blame] | 257 | // We cannot program if there are no proper metadata in the objective |
| 258 | if (obj.meta() != null && obj.meta().getCriterion(Criterion.Type.VLAN_VID) != null) { |
| 259 | egressVlan(outPort, obj, popVlanInst, resultBuilder); |
| 260 | } else { |
| 261 | log.warn("NextObjective {} is trying to program {} without {} information", |
| 262 | obj, FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN, |
| 263 | obj.meta() == null ? "metadata" : "vlanId"); |
| 264 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
pierventre | 48e7882 | 2020-12-15 17:34:54 +0100 | [diff] [blame] | 268 | private void egressVlan(PortNumber outPort, NextObjective obj, Instruction popVlanInst, |
| 269 | ObjectiveTranslation.Builder resultBuilder) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 270 | throws FabricPipelinerException { |
| 271 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 272 | final VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) criterion( |
| 273 | obj.meta(), Criterion.Type.VLAN_VID); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 274 | |
| 275 | final PiCriterion egressVlanTableMatch = PiCriterion.builder() |
| 276 | .matchExact(FabricConstants.HDR_EG_PORT, outPort.toLong()) |
| 277 | .build(); |
| 278 | final TrafficSelector selector = DefaultTrafficSelector.builder() |
| 279 | .matchPi(egressVlanTableMatch) |
| 280 | .matchVlanId(vlanIdCriterion.vlanId()) |
| 281 | .build(); |
pierventre | 48e7882 | 2020-12-15 17:34:54 +0100 | [diff] [blame] | 282 | final TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 283 | if (popVlanInst == null) { |
| 284 | treatmentBuilder.pushVlan(); |
| 285 | } else { |
| 286 | treatmentBuilder.popVlan(); |
| 287 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 288 | |
| 289 | resultBuilder.addFlowRule(flowRule( |
| 290 | obj, FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN, |
pierventre | 48e7882 | 2020-12-15 17:34:54 +0100 | [diff] [blame] | 291 | selector, treatmentBuilder.build())); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | private TrafficSelector nextIdSelector(int nextId) { |
Carmelo Cascone | 45cc086 | 2018-11-26 11:50:41 -0800 | [diff] [blame] | 295 | return nextIdSelectorBuilder(nextId).build(); |
| 296 | } |
| 297 | |
| 298 | private TrafficSelector.Builder nextIdSelectorBuilder(int nextId) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 299 | final PiCriterion nextIdCriterion = PiCriterion.builder() |
| 300 | .matchExact(FabricConstants.HDR_NEXT_ID, nextId) |
| 301 | .build(); |
| 302 | return DefaultTrafficSelector.builder() |
Carmelo Cascone | 45cc086 | 2018-11-26 11:50:41 -0800 | [diff] [blame] | 303 | .matchPi(nextIdCriterion); |
| 304 | } |
| 305 | |
| 306 | private void xconnectNext(NextObjective obj, ObjectiveTranslation.Builder resultBuilder) |
| 307 | throws FabricPipelinerException { |
| 308 | |
| 309 | final Collection<DefaultNextTreatment> defaultNextTreatments = |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 310 | defaultNextTreatments(obj.nextTreatments(), true); |
Carmelo Cascone | 45cc086 | 2018-11-26 11:50:41 -0800 | [diff] [blame] | 311 | |
| 312 | final List<PortNumber> outPorts = defaultNextTreatments.stream() |
| 313 | .map(DefaultNextTreatment::treatment) |
| 314 | .map(FabricUtils::outputPort) |
| 315 | .filter(Objects::nonNull) |
| 316 | .collect(Collectors.toList()); |
| 317 | |
| 318 | if (outPorts.size() != 2) { |
| 319 | throw new FabricPipelinerException(format( |
| 320 | "Handling XCONNECT with %d treatments (ports), but expected is 2", |
| 321 | defaultNextTreatments.size()), ObjectiveError.UNSUPPORTED); |
| 322 | } |
| 323 | |
| 324 | final PortNumber port1 = outPorts.get(0); |
| 325 | final PortNumber port2 = outPorts.get(1); |
| 326 | final TrafficSelector selector1 = nextIdSelectorBuilder(obj.id()) |
| 327 | .matchInPort(port1) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 328 | .build(); |
Carmelo Cascone | 45cc086 | 2018-11-26 11:50:41 -0800 | [diff] [blame] | 329 | final TrafficTreatment treatment1 = DefaultTrafficTreatment.builder() |
| 330 | .setOutput(port2) |
| 331 | .build(); |
| 332 | final TrafficSelector selector2 = nextIdSelectorBuilder(obj.id()) |
| 333 | .matchInPort(port2) |
| 334 | .build(); |
| 335 | final TrafficTreatment treatment2 = DefaultTrafficTreatment.builder() |
| 336 | .setOutput(port1) |
| 337 | .build(); |
| 338 | |
| 339 | resultBuilder.addFlowRule(flowRule( |
| 340 | obj, FabricConstants.FABRIC_INGRESS_NEXT_XCONNECT, |
| 341 | selector1, treatment1)); |
| 342 | resultBuilder.addFlowRule(flowRule( |
| 343 | obj, FabricConstants.FABRIC_INGRESS_NEXT_XCONNECT, |
| 344 | selector2, treatment2)); |
| 345 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | private void multicastNext(NextObjective obj, |
| 349 | ObjectiveTranslation.Builder resultBuilder) |
| 350 | throws FabricPipelinerException { |
| 351 | |
| 352 | // Create ALL group that will be translated to a PRE multicast entry. |
| 353 | final int groupId = allGroup(obj, resultBuilder); |
| 354 | |
| 355 | if (isGroupModifyOp(obj)) { |
| 356 | // No changes to flow rules. |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | final TrafficSelector selector = nextIdSelector(obj.id()); |
| 361 | final PiActionParam groupIdParam = new PiActionParam( |
| 362 | FabricConstants.GROUP_ID, groupId); |
| 363 | final PiAction setMcGroupAction = PiAction.builder() |
| 364 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_MCAST_GROUP_ID) |
| 365 | .withParameter(groupIdParam) |
| 366 | .build(); |
| 367 | final TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 368 | .piTableAction(setMcGroupAction) |
| 369 | .build(); |
| 370 | |
| 371 | resultBuilder.addFlowRule(flowRule( |
| 372 | obj, FabricConstants.FABRIC_INGRESS_NEXT_MULTICAST, |
| 373 | selector, treatment)); |
| 374 | } |
| 375 | |
| 376 | private int selectGroup(NextObjective obj, |
| 377 | ObjectiveTranslation.Builder resultBuilder) |
| 378 | throws FabricPipelinerException { |
| 379 | |
| 380 | final PiTableId hashedTableId = FabricConstants.FABRIC_INGRESS_NEXT_HASHED; |
| 381 | final List<DefaultNextTreatment> defaultNextTreatments = |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 382 | defaultNextTreatments(obj.nextTreatments(), true); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 383 | final List<TrafficTreatment> piTreatments = Lists.newArrayList(); |
| 384 | |
| 385 | for (DefaultNextTreatment t : defaultNextTreatments) { |
| 386 | // Map treatment to PI... |
| 387 | piTreatments.add(mapTreatmentToPiIfNeeded(t.treatment(), hashedTableId)); |
| 388 | // ...and handle egress if necessary. |
| 389 | handleEgress(obj, t.treatment(), resultBuilder, false); |
| 390 | } |
| 391 | |
| 392 | final List<GroupBucket> bucketList = piTreatments.stream() |
| 393 | .map(DefaultGroupBucket::createSelectGroupBucket) |
| 394 | .collect(Collectors.toList()); |
| 395 | |
| 396 | final int groupId = obj.id(); |
| 397 | final PiGroupKey groupKey = new PiGroupKey( |
| 398 | hashedTableId, |
| 399 | FabricConstants.FABRIC_INGRESS_NEXT_HASHED_SELECTOR, |
| 400 | groupId); |
| 401 | |
| 402 | resultBuilder.addGroup(new DefaultGroupDescription( |
| 403 | deviceId, |
| 404 | GroupDescription.Type.SELECT, |
| 405 | new GroupBuckets(bucketList), |
| 406 | groupKey, |
| 407 | groupId, |
| 408 | obj.appId())); |
| 409 | |
| 410 | return groupId; |
| 411 | } |
| 412 | |
| 413 | private int allGroup(NextObjective obj, |
| 414 | ObjectiveTranslation.Builder resultBuilder) |
| 415 | throws FabricPipelinerException { |
| 416 | |
| 417 | final Collection<DefaultNextTreatment> defaultNextTreatments = |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 418 | defaultNextTreatments(obj.nextTreatments(), true); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 419 | // No need to map treatments to PI as translation of ALL groups to PRE |
| 420 | // multicast entries is based solely on the output port. |
| 421 | for (DefaultNextTreatment t : defaultNextTreatments) { |
| 422 | handleEgress(obj, t.treatment(), resultBuilder, true); |
| 423 | } |
| 424 | |
| 425 | // FIXME: this implementation supports only the case in which each |
| 426 | // switch interface is associated with only one VLAN, otherwise we would |
| 427 | // need to support replicating multiple times the same packet for the |
| 428 | // same port while setting different VLAN IDs. Hence, collect in a set. |
| 429 | final Set<PortNumber> outPorts = defaultNextTreatments.stream() |
| 430 | .map(DefaultNextTreatment::treatment) |
| 431 | .map(FabricUtils::outputPort) |
| 432 | .filter(Objects::nonNull) |
| 433 | .collect(Collectors.toSet()); |
| 434 | |
| 435 | if (outPorts.size() != defaultNextTreatments.size()) { |
| 436 | throw new FabricPipelinerException(format( |
| 437 | "Found BROADCAST NextObjective with %d treatments but " + |
| 438 | "found only %d distinct OUTPUT port numbers, cannot " + |
| 439 | "translate to ALL groups", |
| 440 | defaultNextTreatments.size(), outPorts.size()), |
| 441 | ObjectiveError.UNSUPPORTED); |
| 442 | } |
| 443 | |
| 444 | final List<GroupBucket> bucketList = outPorts.stream() |
| 445 | .map(p -> DefaultTrafficTreatment.builder().setOutput(p).build()) |
| 446 | .map(DefaultGroupBucket::createAllGroupBucket) |
| 447 | .collect(Collectors.toList()); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 448 | |
| 449 | final int groupId = obj.id(); |
| 450 | // Use DefaultGroupKey instead of PiGroupKey as we don't have any |
| 451 | // action profile to apply to the groups of ALL type. |
| 452 | final GroupKey groupKey = new DefaultGroupKey( |
| 453 | FabricPipeliner.KRYO.serialize(groupId)); |
| 454 | |
| 455 | resultBuilder.addGroup( |
| 456 | new DefaultGroupDescription( |
| 457 | deviceId, |
| 458 | GroupDescription.Type.ALL, |
| 459 | new GroupBuckets(bucketList), |
| 460 | groupKey, |
| 461 | groupId, |
| 462 | obj.appId())); |
| 463 | |
| 464 | return groupId; |
| 465 | } |
| 466 | |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 467 | private List<DefaultNextTreatment> defaultNextTreatments( |
| 468 | Collection<NextTreatment> nextTreatments, boolean strict) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 469 | throws FabricPipelinerException { |
| 470 | final List<DefaultNextTreatment> defaultNextTreatments = Lists.newArrayList(); |
| 471 | final List<NextTreatment> unsupportedNextTreatments = Lists.newArrayList(); |
| 472 | for (NextTreatment n : nextTreatments) { |
| 473 | if (n.type() == NextTreatment.Type.TREATMENT) { |
| 474 | defaultNextTreatments.add((DefaultNextTreatment) n); |
| 475 | } else { |
| 476 | unsupportedNextTreatments.add(n); |
| 477 | } |
| 478 | } |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 479 | if (strict && !unsupportedNextTreatments.isEmpty()) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 480 | throw new FabricPipelinerException(format( |
| 481 | "Unsupported NextTreatments: %s", |
| 482 | unsupportedNextTreatments)); |
| 483 | } |
| 484 | return defaultNextTreatments; |
| 485 | } |
| 486 | |
Carmelo Cascone | d06a851 | 2018-12-02 16:34:20 -0800 | [diff] [blame] | 487 | private TrafficTreatment getFirstDefaultNextTreatmentIfAny( |
| 488 | Collection<NextTreatment> nextTreatments) |
| 489 | throws FabricPipelinerException { |
| 490 | final Collection<DefaultNextTreatment> nexts = defaultNextTreatments(nextTreatments, false); |
| 491 | return nexts.isEmpty() ? null : nexts.iterator().next().treatment(); |
| 492 | } |
| 493 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 494 | private boolean isGroupModifyOp(NextObjective obj) { |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 495 | // If operation is ADD_TO_EXIST, REMOVE_FROM_EXIST or MODIFY, it means we modify |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 496 | // group buckets only, no changes for flow rules. |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 497 | // FIXME Please note that for MODIFY op this could not apply in future if we extend the scope of MODIFY |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 498 | return obj.op() == Objective.Operation.ADD_TO_EXISTING || |
pierventre | 4e02fb5 | 2020-08-13 16:35:15 +0200 | [diff] [blame] | 499 | obj.op() == Objective.Operation.REMOVE_FROM_EXISTING || |
| 500 | obj.op() == Objective.Operation.MODIFY; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 501 | } |
Carmelo Cascone | 45cc086 | 2018-11-26 11:50:41 -0800 | [diff] [blame] | 502 | |
| 503 | private boolean isXconnect(NextObjective obj) { |
| 504 | return obj.appId().name().contains(XCONNECT); |
| 505 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 506 | } |