Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -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; |
| 18 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 20 | import org.onosproject.net.PortNumber; |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 21 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 22 | import org.onosproject.net.flow.TrafficTreatment; |
| 23 | import org.onosproject.net.flow.instructions.Instruction; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 24 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 25 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 26 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 28 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 29 | import org.onosproject.net.pi.model.PiActionId; |
| 30 | import org.onosproject.net.pi.model.PiPipelineInterpreter.PiInterpreterException; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 31 | import org.onosproject.net.pi.model.PiTableId; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 32 | import org.onosproject.net.pi.runtime.PiAction; |
| 33 | import org.onosproject.net.pi.runtime.PiActionParam; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 34 | |
| 35 | import static java.lang.String.format; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 36 | import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT; |
| 37 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.ETH_DST; |
| 38 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.ETH_SRC; |
| 39 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.MPLS_LABEL; |
| 40 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.MPLS_PUSH; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 41 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.VLAN_ID; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 42 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.VLAN_POP; |
| 43 | import static org.onosproject.pipelines.fabric.FabricUtils.instruction; |
| 44 | import static org.onosproject.pipelines.fabric.FabricUtils.l2Instruction; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 45 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 46 | /** |
| 47 | * Treatment translation logic. |
| 48 | */ |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 49 | final class FabricTreatmentInterpreter { |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 50 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 51 | private static final ImmutableMap<PiTableId, PiActionId> NOP_ACTIONS = |
| 52 | ImmutableMap.<PiTableId, PiActionId>builder() |
| 53 | .put(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN, |
| 54 | FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT) |
| 55 | .put(FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4, |
| 56 | FabricConstants.FABRIC_INGRESS_FORWARDING_NOP_ROUTING_V4) |
| 57 | .put(FabricConstants.FABRIC_INGRESS_ACL_ACL, |
| 58 | FabricConstants.FABRIC_INGRESS_ACL_NOP_ACL) |
| 59 | .put(FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN, |
| 60 | FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_POP_VLAN) |
| 61 | .build(); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 62 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 63 | private FabricTreatmentInterpreter() { |
| 64 | // Hide default constructor |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 67 | static PiAction mapFilteringTreatment(TrafficTreatment treatment, PiTableId tableId) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 68 | throws PiInterpreterException { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 69 | |
| 70 | if (!tableId.equals(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)) { |
| 71 | // Mapping for other tables of the filtering block must be handled |
| 72 | // in the pipeliner. |
| 73 | tableException(tableId); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 76 | if (isNoAction(treatment)) { |
| 77 | // Permit action if table is ingress_port_vlan; |
| 78 | return nop(tableId); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 81 | final ModVlanIdInstruction setVlanInst = (ModVlanIdInstruction) l2InstructionOrFail( |
| 82 | treatment, VLAN_ID, tableId); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 83 | return PiAction.builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 84 | .withId(FabricConstants.FABRIC_INGRESS_FILTERING_PERMIT_WITH_INTERNAL_VLAN) |
| 85 | .withParameter(new PiActionParam( |
| 86 | FabricConstants.VLAN_ID, setVlanInst.vlanId().toShort())) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 87 | .build(); |
| 88 | } |
| 89 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 90 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 91 | static PiAction mapForwardingTreatment(TrafficTreatment treatment, PiTableId tableId) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 92 | throws PiInterpreterException { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 93 | if (isNoAction(treatment)) { |
| 94 | return nop(tableId); |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 95 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 96 | treatmentException( |
| 97 | tableId, treatment, |
| 98 | "supports mapping only for empty/no-action treatments"); |
| 99 | return null; |
| 100 | } |
| 101 | |
| 102 | static PiAction mapNextTreatment(TrafficTreatment treatment, PiTableId tableId) |
| 103 | throws PiInterpreterException { |
| 104 | if (tableId == FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN) { |
| 105 | return mapNextVlanTreatment(treatment, tableId); |
| 106 | } else if (tableId == FabricConstants.FABRIC_INGRESS_NEXT_HASHED) { |
| 107 | return mapNextHashedOrSimpleTreatment(treatment, tableId, false); |
| 108 | } else if (tableId == FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE) { |
| 109 | return mapNextHashedOrSimpleTreatment(treatment, tableId, true); |
Carmelo Cascone | 45cc086 | 2018-11-26 11:50:41 -0800 | [diff] [blame] | 110 | } else if (tableId == FabricConstants.FABRIC_INGRESS_NEXT_XCONNECT) { |
| 111 | return mapNextXconnect(treatment, tableId); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 112 | } |
| 113 | throw new PiInterpreterException(format( |
| 114 | "Treatment mapping not supported for table '%s'", tableId)); |
| 115 | } |
| 116 | |
| 117 | private static PiAction mapNextVlanTreatment(TrafficTreatment treatment, PiTableId tableId) |
| 118 | throws PiInterpreterException { |
| 119 | final ModVlanIdInstruction modVlanIdInst = (ModVlanIdInstruction) |
| 120 | l2InstructionOrFail(treatment, VLAN_ID, tableId); |
| 121 | return PiAction.builder() |
| 122 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN) |
| 123 | .withParameter(new PiActionParam( |
| 124 | FabricConstants.VLAN_ID, |
| 125 | modVlanIdInst.vlanId().toShort())) |
| 126 | .build(); |
| 127 | } |
| 128 | |
| 129 | private static PiAction mapNextHashedOrSimpleTreatment( |
| 130 | TrafficTreatment treatment, PiTableId tableId, boolean simple) |
| 131 | throws PiInterpreterException { |
| 132 | // Provide mapping for output_hashed, routing_hashed, and |
| 133 | // mpls_routing_hashed. multicast_hashed can only be invoked with |
| 134 | // PiAction, hence no mapping. outPort required for all actions. Presence |
| 135 | // of other instructions will determine which action to map to. |
| 136 | final PortNumber outPort = ((OutputInstruction) instructionOrFail( |
| 137 | treatment, OUTPUT, tableId)).port(); |
| 138 | final ModEtherInstruction ethDst = (ModEtherInstruction) l2Instruction( |
| 139 | treatment, ETH_DST); |
| 140 | final ModEtherInstruction ethSrc = (ModEtherInstruction) l2Instruction( |
| 141 | treatment, ETH_SRC); |
| 142 | final Instruction mplsPush = l2Instruction( |
| 143 | treatment, MPLS_PUSH); |
| 144 | final ModMplsLabelInstruction mplsLabel = (ModMplsLabelInstruction) l2Instruction( |
| 145 | treatment, MPLS_LABEL); |
| 146 | |
| 147 | final PiAction.Builder actionBuilder = PiAction.builder() |
| 148 | .withParameter(new PiActionParam(FabricConstants.PORT_NUM, outPort.toLong())); |
| 149 | |
| 150 | if (ethDst != null && ethSrc != null) { |
| 151 | actionBuilder.withParameter(new PiActionParam( |
| 152 | FabricConstants.SMAC, ethSrc.mac().toBytes())); |
| 153 | actionBuilder.withParameter(new PiActionParam( |
| 154 | FabricConstants.DMAC, ethDst.mac().toBytes())); |
| 155 | if (mplsLabel != null) { |
| 156 | // mpls_routing_hashed |
| 157 | return actionBuilder |
| 158 | .withParameter(new PiActionParam(FabricConstants.LABEL, mplsLabel.label().toInt())) |
| 159 | .withId(simple ? FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_SIMPLE |
| 160 | : FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_HASHED) |
| 161 | .build(); |
| 162 | } else { |
| 163 | // routing_hashed |
| 164 | return actionBuilder |
| 165 | .withId(simple ? FabricConstants.FABRIC_INGRESS_NEXT_ROUTING_SIMPLE |
| 166 | : FabricConstants.FABRIC_INGRESS_NEXT_ROUTING_HASHED) |
| 167 | .build(); |
| 168 | } |
| 169 | } else { |
| 170 | // output_hashed |
| 171 | return actionBuilder |
| 172 | .withId(simple ? FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE |
| 173 | : FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_HASHED) |
| 174 | .build(); |
| 175 | } |
| 176 | } |
| 177 | |
Carmelo Cascone | 45cc086 | 2018-11-26 11:50:41 -0800 | [diff] [blame] | 178 | private static PiAction mapNextXconnect( |
| 179 | TrafficTreatment treatment, PiTableId tableId) |
| 180 | throws PiInterpreterException { |
| 181 | final PortNumber outPort = ((OutputInstruction) instructionOrFail( |
| 182 | treatment, OUTPUT, tableId)).port(); |
| 183 | return PiAction.builder() |
| 184 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_XCONNECT) |
| 185 | .withParameter(new PiActionParam( |
| 186 | FabricConstants.PORT_NUM, outPort.toLong())) |
| 187 | .build(); |
| 188 | } |
| 189 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 190 | static PiAction mapAclTreatment(TrafficTreatment treatment, PiTableId tableId) |
| 191 | throws PiInterpreterException { |
| 192 | if (isNoAction(treatment)) { |
| 193 | return nop(tableId); |
| 194 | } |
Daniele Moro | 01ca2ab | 2019-06-25 11:48:48 -0700 | [diff] [blame^] | 195 | treatmentException( |
| 196 | tableId, treatment, |
| 197 | "unsupported treatment"); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 198 | |
Daniele Moro | 01ca2ab | 2019-06-25 11:48:48 -0700 | [diff] [blame^] | 199 | // This function will never return null |
| 200 | return null; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 203 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 204 | static PiAction mapEgressNextTreatment( |
| 205 | TrafficTreatment treatment, PiTableId tableId) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 206 | throws PiInterpreterException { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 207 | l2InstructionOrFail(treatment, VLAN_POP, tableId); |
| 208 | return PiAction.builder() |
| 209 | .withId(FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_POP_VLAN) |
| 210 | .build(); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 211 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 212 | } |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 213 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 214 | private static PiAction nop(PiTableId tableId) throws PiInterpreterException { |
| 215 | if (!NOP_ACTIONS.containsKey(tableId)) { |
| 216 | throw new PiInterpreterException(format("table '%s' doe not specify a nop action", tableId)); |
| 217 | } |
| 218 | return PiAction.builder().withId(NOP_ACTIONS.get(tableId)).build(); |
| 219 | } |
| 220 | |
| 221 | private static boolean isNoAction(TrafficTreatment treatment) { |
| 222 | return treatment.equals(DefaultTrafficTreatment.emptyTreatment()) || |
| 223 | treatment.allInstructions().isEmpty(); |
| 224 | } |
| 225 | |
| 226 | private static Instruction l2InstructionOrFail( |
| 227 | TrafficTreatment treatment, |
| 228 | L2ModificationInstruction.L2SubType subType, PiTableId tableId) |
| 229 | throws PiInterpreterException { |
| 230 | final Instruction inst = l2Instruction(treatment, subType); |
| 231 | if (inst == null) { |
| 232 | treatmentException(tableId, treatment, format("missing %s instruction", subType)); |
| 233 | } |
| 234 | return inst; |
| 235 | } |
| 236 | |
| 237 | private static Instruction instructionOrFail( |
| 238 | TrafficTreatment treatment, Instruction.Type type, PiTableId tableId) |
| 239 | throws PiInterpreterException { |
| 240 | final Instruction inst = instruction(treatment, type); |
| 241 | if (inst == null) { |
| 242 | treatmentException(tableId, treatment, format("missing %s instruction", type)); |
| 243 | } |
| 244 | return inst; |
| 245 | } |
| 246 | |
| 247 | private static void tableException(PiTableId tableId) |
| 248 | throws PiInterpreterException { |
| 249 | throw new PiInterpreterException(format("Table '%s' not supported", tableId)); |
| 250 | } |
| 251 | |
| 252 | private static void treatmentException( |
| 253 | PiTableId tableId, TrafficTreatment treatment, String explanation) |
| 254 | throws PiInterpreterException { |
| 255 | throw new PiInterpreterException(format( |
| 256 | "Invalid treatment for table '%s', %s: %s", tableId, explanation, treatment)); |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 257 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 258 | } |