Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 18 | |
| 19 | import org.onosproject.net.PortNumber; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 20 | import org.onosproject.net.flow.TrafficSelector; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 21 | import org.onosproject.net.flow.TrafficTreatment; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 22 | import org.onosproject.net.flow.criteria.Criterion; |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 23 | import org.onosproject.net.flow.criteria.MetadataCriterion; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 24 | import org.onosproject.net.flow.instructions.Instruction; |
| 25 | import org.onosproject.net.flow.instructions.Instructions; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 26 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 27 | import org.onosproject.net.flowobjective.DefaultNextTreatment; |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 28 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 29 | import org.onosproject.net.flowobjective.ForwardingObjective; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flowobjective.NextTreatment; |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 31 | import org.onosproject.net.flowobjective.Objective; |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 32 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 33 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 34 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 35 | import java.util.Collection; |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 36 | import java.util.List; |
| 37 | import java.util.stream.Collectors; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 38 | |
| 39 | import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | import static java.lang.String.format; |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 41 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.EDGE_PORT; |
| 42 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.INFRA_PORT; |
| 43 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.METADATA_MASK; |
| 44 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.METADATA_TO_PORT_TYPE; |
| 45 | import static org.onosproject.pipelines.fabric.impl.behaviour.Constants.PAIR_PORT; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 46 | |
| 47 | /** |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 48 | * Utility class with methods common to fabric pipeconf operations. |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 49 | */ |
| 50 | public final class FabricUtils { |
| 51 | |
| 52 | private FabricUtils() { |
| 53 | // Hides constructor. |
| 54 | } |
| 55 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 56 | public static Criterion criterion(Collection<Criterion> criteria, Criterion.Type type) { |
| 57 | return criteria.stream() |
| 58 | .filter(c -> c.type().equals(type)) |
| 59 | .findFirst().orElse(null); |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 62 | public static Criterion criterion(TrafficSelector selector, Criterion.Type type) { |
| 63 | return selector.getCriterion(type); |
| 64 | } |
| 65 | |
| 66 | public static Criterion criterionNotNull(TrafficSelector selector, Criterion.Type type) { |
| 67 | return checkNotNull(criterion(selector, type), |
| 68 | format("%s criterion cannot be null", type)); |
| 69 | } |
| 70 | |
| 71 | public static Criterion criterionNotNull(Collection<Criterion> criteria, Criterion.Type type) { |
| 72 | return checkNotNull(criterion(criteria, type), |
| 73 | format("%s criterion cannot be null", type)); |
| 74 | } |
| 75 | |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 76 | public static Instruction instruction(TrafficTreatment treatment, Instruction.Type type) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 77 | return treatment.allInstructions() |
| 78 | .stream() |
| 79 | .filter(inst -> inst.type() == type) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 80 | .findFirst().orElse(null); |
| 81 | } |
| 82 | |
| 83 | public static L2ModificationInstruction l2Instruction( |
| 84 | TrafficTreatment treatment, L2ModificationInstruction.L2SubType subType) { |
| 85 | return treatment.allInstructions().stream() |
| 86 | .filter(i -> i.type().equals(Instruction.Type.L2MODIFICATION)) |
| 87 | .map(i -> (L2ModificationInstruction) i) |
| 88 | .filter(i -> i.subtype().equals(subType)) |
| 89 | .findFirst().orElse(null); |
| 90 | } |
| 91 | |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 92 | public static Instruction l2InstructionOrFail( |
| 93 | TrafficTreatment treatment, |
| 94 | L2ModificationInstruction.L2SubType subType, PiTableId tableId) |
| 95 | throws PiPipelineInterpreter.PiInterpreterException { |
| 96 | final Instruction inst = l2Instruction(treatment, subType); |
| 97 | if (inst == null) { |
| 98 | treatmentException(tableId, treatment, format("missing %s instruction", subType)); |
| 99 | } |
| 100 | return inst; |
| 101 | } |
| 102 | |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 103 | public static List<L2ModificationInstruction> l2Instructions( |
| 104 | TrafficTreatment treatment, L2ModificationInstruction.L2SubType subType) { |
| 105 | return treatment.allInstructions().stream() |
| 106 | .filter(i -> i.type().equals(Instruction.Type.L2MODIFICATION)) |
| 107 | .map(i -> (L2ModificationInstruction) i) |
| 108 | .filter(i -> i.subtype().equals(subType)) |
| 109 | .collect(Collectors.toList()); |
| 110 | } |
| 111 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 112 | public static Instructions.OutputInstruction outputInstruction(TrafficTreatment treatment) { |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 113 | return (Instructions.OutputInstruction) instruction(treatment, Instruction.Type.OUTPUT); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public static PortNumber outputPort(TrafficTreatment treatment) { |
| 117 | final Instructions.OutputInstruction inst = outputInstruction(treatment); |
| 118 | return inst == null ? null : inst.port(); |
| 119 | } |
| 120 | |
| 121 | public static PortNumber outputPort(NextTreatment treatment) { |
| 122 | if (treatment.type() == NextTreatment.Type.TREATMENT) { |
| 123 | final DefaultNextTreatment t = (DefaultNextTreatment) treatment; |
| 124 | return outputPort(t.treatment()); |
| 125 | } |
| 126 | return null; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 127 | } |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 128 | |
| 129 | public static void treatmentException( |
| 130 | PiTableId tableId, TrafficTreatment treatment, String explanation) |
| 131 | throws PiPipelineInterpreter.PiInterpreterException { |
| 132 | throw new PiPipelineInterpreter.PiInterpreterException(format( |
| 133 | "Invalid treatment for table '%s', %s: %s", tableId, explanation, treatment)); |
| 134 | } |
pierventre | 4d29d3c | 2021-08-27 17:20:00 +0200 | [diff] [blame^] | 135 | |
| 136 | /** |
| 137 | * Port type metadata conversion. |
| 138 | * |
| 139 | * @param obj the objective |
| 140 | * @return the port type associated to the metadata, null otherwise |
| 141 | */ |
| 142 | public static Byte portType(Objective obj) { |
| 143 | Byte portType = null; |
| 144 | if (isSrMetadataSet(obj, PAIR_PORT) && METADATA_TO_PORT_TYPE.containsKey(PAIR_PORT)) { |
| 145 | portType = METADATA_TO_PORT_TYPE.get(PAIR_PORT); |
| 146 | } else if (isSrMetadataSet(obj, EDGE_PORT) && METADATA_TO_PORT_TYPE.containsKey(EDGE_PORT)) { |
| 147 | portType = METADATA_TO_PORT_TYPE.get(EDGE_PORT); |
| 148 | } else if (isSrMetadataSet(obj, INFRA_PORT) && METADATA_TO_PORT_TYPE.containsKey(INFRA_PORT)) { |
| 149 | portType = METADATA_TO_PORT_TYPE.get(INFRA_PORT); |
| 150 | } |
| 151 | return portType; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Check metadata passed from SegmentRouting app. |
| 156 | * |
| 157 | * @param obj the objective containing the metadata |
| 158 | * @return true if the objective contains valid metadata, false otherwise |
| 159 | */ |
| 160 | public static boolean isValidSrMetadata(Objective obj) { |
| 161 | long meta = 0; |
| 162 | if (obj instanceof FilteringObjective) { |
| 163 | FilteringObjective filtObj = (FilteringObjective) obj; |
| 164 | if (filtObj.meta() == null) { |
| 165 | return true; |
| 166 | } |
| 167 | Instructions.MetadataInstruction metaIns = filtObj.meta().writeMetadata(); |
| 168 | if (metaIns == null) { |
| 169 | return true; |
| 170 | } |
| 171 | meta = metaIns.metadata() & metaIns.metadataMask(); |
| 172 | } else if (obj instanceof ForwardingObjective) { |
| 173 | ForwardingObjective fwdObj = (ForwardingObjective) obj; |
| 174 | if (fwdObj.meta() == null) { |
| 175 | return true; |
| 176 | } |
| 177 | MetadataCriterion metaCrit = (MetadataCriterion) fwdObj.meta().getCriterion(Criterion.Type.METADATA); |
| 178 | if (metaCrit == null) { |
| 179 | return true; |
| 180 | } |
| 181 | meta = metaCrit.metadata(); |
| 182 | } |
| 183 | return meta != 0 && ((meta ^ METADATA_MASK) <= METADATA_MASK); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Verify if a given flag has been set into the metadata. |
| 188 | * |
| 189 | * @param obj the objective containing the metadata |
| 190 | * @param flag the flag to verify |
| 191 | * @return true if the flag is set, false otherwise |
| 192 | */ |
| 193 | public static boolean isSrMetadataSet(Objective obj, long flag) { |
| 194 | long meta = 0; |
| 195 | if (obj instanceof FilteringObjective) { |
| 196 | FilteringObjective filtObj = (FilteringObjective) obj; |
| 197 | if (filtObj.meta() == null) { |
| 198 | return false; |
| 199 | } |
| 200 | Instructions.MetadataInstruction metaIns = filtObj.meta().writeMetadata(); |
| 201 | if (metaIns == null) { |
| 202 | return false; |
| 203 | } |
| 204 | meta = metaIns.metadata() & metaIns.metadataMask(); |
| 205 | } else if (obj instanceof ForwardingObjective) { |
| 206 | ForwardingObjective fwdObj = (ForwardingObjective) obj; |
| 207 | if (fwdObj.meta() == null) { |
| 208 | return false; |
| 209 | } |
| 210 | MetadataCriterion metaCrit = (MetadataCriterion) fwdObj.meta().getCriterion(Criterion.Type.METADATA); |
| 211 | if (metaCrit == null) { |
| 212 | return false; |
| 213 | } |
| 214 | meta = metaCrit.metadata(); |
| 215 | } |
| 216 | return (meta & flag) == flag; |
| 217 | } |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 218 | } |