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 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.onlab.packet.MacAddress; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 21 | import org.onlab.packet.MplsLabel; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onlab.util.ImmutableByteSequence; |
| 24 | import org.onosproject.net.PortNumber; |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 25 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 26 | import org.onosproject.net.flow.TrafficTreatment; |
| 27 | import org.onosproject.net.flow.instructions.Instruction; |
| 28 | import org.onosproject.net.flow.instructions.Instructions; |
| 29 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 30 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 31 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 34 | import org.onosproject.net.pi.model.PiActionId; |
| 35 | import org.onosproject.net.pi.model.PiPipelineInterpreter.PiInterpreterException; |
| 36 | import org.onosproject.net.pi.runtime.PiAction; |
| 37 | import org.onosproject.net.pi.runtime.PiActionParam; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 38 | import org.slf4j.Logger; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 39 | |
| 40 | import java.util.List; |
| 41 | |
| 42 | import static java.lang.String.format; |
| 43 | import static org.onosproject.net.flow.instructions.Instruction.Type.L2MODIFICATION; |
| 44 | import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 45 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.VLAN_ID; |
| 46 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.VLAN_PUSH; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 47 | import static org.slf4j.LoggerFactory.getLogger; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 48 | |
| 49 | |
| 50 | final class FabricTreatmentInterpreter { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 51 | private static final Logger log = getLogger(FabricTreatmentInterpreter.class); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 52 | private static final String INVALID_TREATMENT = "Invalid treatment for %s block: %s"; |
| 53 | |
| 54 | // Hide default constructor |
| 55 | protected FabricTreatmentInterpreter() { |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * In Filtering block, we need to implement these actions: |
| 60 | * |
| 61 | * push_internal_vlan |
| 62 | * set_vlan |
| 63 | * nop |
| 64 | * |
| 65 | * Unsupported, using PiAction directly: |
| 66 | * set_forwarding_type |
| 67 | */ |
| 68 | |
| 69 | static PiAction mapFilteringTreatment(TrafficTreatment treatment) |
| 70 | throws PiInterpreterException { |
| 71 | List<Instruction> instructions = treatment.allInstructions(); |
| 72 | Instruction noActInst = Instructions.createNoAction(); |
| 73 | if (instructions.isEmpty() || instructions.contains(noActInst)) { |
| 74 | // nop |
| 75 | return PiAction.builder() |
| 76 | .withId(FabricConstants.ACT_NOP_ID) |
| 77 | .build(); |
| 78 | } |
| 79 | |
| 80 | L2ModificationInstruction.ModVlanHeaderInstruction pushVlanInst = null; |
| 81 | ModVlanIdInstruction setVlanInst = null; |
| 82 | |
| 83 | for (Instruction inst : instructions) { |
| 84 | if (inst.type() == L2MODIFICATION) { |
| 85 | L2ModificationInstruction l2Inst = (L2ModificationInstruction) inst; |
| 86 | |
| 87 | if (l2Inst.subtype() == VLAN_PUSH) { |
| 88 | pushVlanInst = (L2ModificationInstruction.ModVlanHeaderInstruction) l2Inst; |
| 89 | |
| 90 | } else if (l2Inst.subtype() == VLAN_ID) { |
| 91 | setVlanInst = (ModVlanIdInstruction) l2Inst; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (setVlanInst == null) { |
| 97 | throw new PiInterpreterException(format(INVALID_TREATMENT, "filtering", treatment)); |
| 98 | } |
| 99 | |
| 100 | VlanId vlanId = setVlanInst.vlanId(); |
| 101 | PiActionParam param = new PiActionParam(FabricConstants.ACT_PRM_NEW_VLAN_ID_ID, |
| 102 | ImmutableByteSequence.copyFrom(vlanId.toShort())); |
| 103 | PiActionId actionId; |
| 104 | if (pushVlanInst != null) { |
| 105 | // push_internal_vlan |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 106 | actionId = FabricConstants.ACT_FILTERING_PUSH_INTERNAL_VLAN_ID; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 107 | } else { |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 108 | actionId = FabricConstants.ACT_FILTERING_SET_VLAN_ID; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // set_vlan |
| 112 | return PiAction.builder() |
| 113 | .withId(actionId) |
| 114 | .withParameter(param) |
| 115 | .build(); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * In forwarding block, we need to implement these actions: |
| 120 | * duplicate_to_controller |
| 121 | * |
| 122 | * Unsupported, using PiAction directly: |
| 123 | * set_next_id |
| 124 | * push_mpls_and_next_v4 |
| 125 | * push_mpls_and_next_v6 |
| 126 | */ |
| 127 | |
| 128 | public static PiAction mapForwardingTreatment(TrafficTreatment treatment) |
| 129 | throws PiInterpreterException { |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 130 | // Empty treatment, generate table entry with no action |
| 131 | if (treatment.equals(DefaultTrafficTreatment.emptyTreatment())) { |
| 132 | return null; |
| 133 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 134 | List<Instruction> insts = treatment.allInstructions(); |
| 135 | OutputInstruction outInst = insts.stream() |
| 136 | .filter(inst -> inst.type() == OUTPUT) |
| 137 | .map(inst -> (OutputInstruction) inst) |
| 138 | .findFirst() |
| 139 | .orElse(null); |
| 140 | |
| 141 | if (outInst == null) { |
| 142 | throw new PiInterpreterException(format(INVALID_TREATMENT, "forwarding", treatment)); |
| 143 | } |
| 144 | |
| 145 | PortNumber portNumber = outInst.port(); |
| 146 | if (!portNumber.equals(PortNumber.CONTROLLER)) { |
| 147 | throw new PiInterpreterException(format("Unsupported port number %s," + |
| 148 | "supports punt action only", |
| 149 | portNumber)); |
| 150 | } |
| 151 | |
| 152 | return PiAction.builder() |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 153 | .withId(FabricConstants.ACT_FORWARDING_DUPLICATE_TO_CONTROLLER_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 154 | .build(); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * In Next block, we need to implement these actions: |
| 159 | * output |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 160 | * set_vlan_output |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 161 | * l3_routing |
| 162 | * mpls_routing_v4 |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 163 | * |
| 164 | * Unsupported, using PiAction directly: |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 165 | * set_next_type |
| 166 | * |
| 167 | * Unsupported, need to find a way to implement it |
| 168 | * mpls_routing_v6 |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 169 | */ |
| 170 | |
| 171 | public static PiAction mapNextTreatment(TrafficTreatment treatment) |
| 172 | throws PiInterpreterException { |
| 173 | List<Instruction> insts = treatment.allInstructions(); |
| 174 | OutputInstruction outInst = null; |
| 175 | ModEtherInstruction modEthDstInst = null; |
| 176 | ModEtherInstruction modEthSrcInst = null; |
| 177 | ModVlanIdInstruction modVlanIdInst = null; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 178 | ModMplsLabelInstruction modMplsInst = null; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 179 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 180 | // TODO: add NextFunctionType (like ForwardingFunctionType) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 181 | for (Instruction inst : insts) { |
| 182 | switch (inst.type()) { |
| 183 | case L2MODIFICATION: |
| 184 | L2ModificationInstruction l2Inst = (L2ModificationInstruction) inst; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 185 | switch (l2Inst.subtype()) { |
| 186 | case ETH_SRC: |
| 187 | modEthSrcInst = (ModEtherInstruction) l2Inst; |
| 188 | break; |
| 189 | case ETH_DST: |
| 190 | modEthDstInst = (ModEtherInstruction) l2Inst; |
| 191 | break; |
| 192 | case VLAN_ID: |
| 193 | modVlanIdInst = (ModVlanIdInstruction) l2Inst; |
| 194 | break; |
| 195 | case MPLS_LABEL: |
| 196 | modMplsInst = (ModMplsLabelInstruction) l2Inst; |
| 197 | break; |
Yi Tseng | 4fd2843 | 2018-02-01 14:48:03 -0800 | [diff] [blame] | 198 | case VLAN_PUSH: |
| 199 | break; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 200 | default: |
| 201 | log.warn("Unsupported l2 instruction sub type: {}", l2Inst.subtype()); |
| 202 | break; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 203 | } |
| 204 | break; |
| 205 | case OUTPUT: |
| 206 | outInst = (OutputInstruction) inst; |
| 207 | break; |
| 208 | default: |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 209 | log.warn("Unsupported instruction sub type: {}", inst.type()); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (outInst == null) { |
| 215 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
| 216 | } |
| 217 | short portNum = (short) outInst.port().toLong(); |
| 218 | PiActionParam portNumParam = new PiActionParam(FabricConstants.ACT_PRM_PORT_NUM_ID, |
| 219 | ImmutableByteSequence.copyFrom(portNum)); |
| 220 | if (modEthDstInst == null && modEthSrcInst == null) { |
| 221 | if (modVlanIdInst != null) { |
| 222 | VlanId vlanId = modVlanIdInst.vlanId(); |
| 223 | PiActionParam vlanParam = |
| 224 | new PiActionParam(FabricConstants.ACT_PRM_NEW_VLAN_ID_ID, |
| 225 | ImmutableByteSequence.copyFrom(vlanId.toShort())); |
| 226 | // set_vlan_output |
| 227 | return PiAction.builder() |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 228 | .withId(FabricConstants.ACT_NEXT_SET_VLAN_OUTPUT_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 229 | .withParameters(ImmutableList.of(portNumParam, vlanParam)) |
| 230 | .build(); |
| 231 | } else { |
| 232 | // output |
| 233 | return PiAction.builder() |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 234 | .withId(FabricConstants.ACT_NEXT_OUTPUT_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 235 | .withParameter(portNumParam) |
| 236 | .build(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (modEthDstInst != null && modEthSrcInst != null) { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 241 | MacAddress srcMac = modEthSrcInst.mac(); |
| 242 | MacAddress dstMac = modEthDstInst.mac(); |
| 243 | PiActionParam srcMacParam = new PiActionParam(FabricConstants.ACT_PRM_SMAC_ID, |
| 244 | ImmutableByteSequence.copyFrom(srcMac.toBytes())); |
| 245 | PiActionParam dstMacParam = new PiActionParam(FabricConstants.ACT_PRM_DMAC_ID, |
| 246 | ImmutableByteSequence.copyFrom(dstMac.toBytes())); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 247 | |
| 248 | if (modMplsInst != null) { |
| 249 | // MPLS routing |
| 250 | MplsLabel mplsLabel = modMplsInst.label(); |
| 251 | try { |
| 252 | ImmutableByteSequence mplsValue = |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 253 | ImmutableByteSequence.copyFrom(mplsLabel.toInt()).fit(20); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 254 | PiActionParam mplsParam = new PiActionParam(FabricConstants.ACT_PRM_LABEL_ID, mplsValue); |
| 255 | return PiAction.builder() |
| 256 | // FIXME: fins a way to determine v4 or v6 |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 257 | .withId(FabricConstants.ACT_NEXT_MPLS_ROUTING_V4_ID) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 258 | .withParameters(ImmutableList.of(portNumParam, |
| 259 | srcMacParam, |
| 260 | dstMacParam, |
| 261 | mplsParam)) |
| 262 | .build(); |
| 263 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 264 | // Basically this won't happened because we already limited |
| 265 | // size of mpls value to 20 bits (0xFFFFF) |
| 266 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // L3 routing |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 271 | return PiAction.builder() |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 272 | .withId(FabricConstants.ACT_NEXT_L3_ROUTING_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 273 | .withParameters(ImmutableList.of(portNumParam, |
| 274 | srcMacParam, |
| 275 | dstMacParam)) |
| 276 | .build(); |
| 277 | } |
| 278 | |
| 279 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
| 280 | } |
| 281 | } |