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