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; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 36 | import org.onosproject.net.pi.model.PiTableId; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 37 | import org.onosproject.net.pi.runtime.PiAction; |
| 38 | import org.onosproject.net.pi.runtime.PiActionParam; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 39 | import org.slf4j.Logger; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 40 | |
| 41 | import java.util.List; |
| 42 | |
| 43 | import static java.lang.String.format; |
| 44 | import static org.onosproject.net.flow.instructions.Instruction.Type.L2MODIFICATION; |
| 45 | import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 46 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.VLAN_ID; |
| 47 | import static org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType.VLAN_PUSH; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 48 | import static org.slf4j.LoggerFactory.getLogger; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 49 | |
| 50 | |
| 51 | final class FabricTreatmentInterpreter { |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 52 | private static final Logger log = getLogger(FabricTreatmentInterpreter.class); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 53 | private static final String INVALID_TREATMENT = "Invalid treatment for %s block: %s"; |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 54 | private static final PiAction NOP = PiAction.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 55 | .withId(FabricConstants.NOP) |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 56 | .build(); |
| 57 | |
| 58 | private static final PiAction POP_VLAN = PiAction.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 59 | .withId(FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_POP_VLAN) |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 60 | .build(); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 61 | |
| 62 | // Hide default constructor |
| 63 | protected FabricTreatmentInterpreter() { |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * In Filtering block, we need to implement these actions: |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 68 | * push_internal_vlan |
| 69 | * set_vlan |
| 70 | * nop |
| 71 | * |
| 72 | * Unsupported, using PiAction directly: |
| 73 | * set_forwarding_type |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 74 | * drop |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 75 | */ |
| 76 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 77 | static PiAction mapFilteringTreatment(TrafficTreatment treatment, PiTableId tableId) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 78 | throws PiInterpreterException { |
| 79 | List<Instruction> instructions = treatment.allInstructions(); |
| 80 | Instruction noActInst = Instructions.createNoAction(); |
| 81 | if (instructions.isEmpty() || instructions.contains(noActInst)) { |
| 82 | // nop |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 83 | return NOP; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | L2ModificationInstruction.ModVlanHeaderInstruction pushVlanInst = null; |
| 87 | ModVlanIdInstruction setVlanInst = null; |
| 88 | |
| 89 | for (Instruction inst : instructions) { |
| 90 | if (inst.type() == L2MODIFICATION) { |
| 91 | L2ModificationInstruction l2Inst = (L2ModificationInstruction) inst; |
| 92 | |
| 93 | if (l2Inst.subtype() == VLAN_PUSH) { |
| 94 | pushVlanInst = (L2ModificationInstruction.ModVlanHeaderInstruction) l2Inst; |
| 95 | |
| 96 | } else if (l2Inst.subtype() == VLAN_ID) { |
| 97 | setVlanInst = (ModVlanIdInstruction) l2Inst; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (setVlanInst == null) { |
| 103 | throw new PiInterpreterException(format(INVALID_TREATMENT, "filtering", treatment)); |
| 104 | } |
| 105 | |
| 106 | VlanId vlanId = setVlanInst.vlanId(); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 107 | PiActionParam param = new PiActionParam(FabricConstants.NEW_VLAN_ID, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 108 | ImmutableByteSequence.copyFrom(vlanId.toShort())); |
| 109 | PiActionId actionId; |
| 110 | if (pushVlanInst != null) { |
| 111 | // push_internal_vlan |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 112 | actionId = FabricConstants.FABRIC_INGRESS_FILTERING_PUSH_INTERNAL_VLAN; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 113 | } else { |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 114 | actionId = FabricConstants.FABRIC_INGRESS_FILTERING_SET_VLAN; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // set_vlan |
| 118 | return PiAction.builder() |
| 119 | .withId(actionId) |
| 120 | .withParameter(param) |
| 121 | .build(); |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | * In forwarding block, we need to implement these actions: |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 126 | * send_to_controller |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 127 | * |
| 128 | * Unsupported, using PiAction directly: |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 129 | * set_next_id_bridging |
| 130 | * pop_mpls_and_next |
| 131 | * set_next_id_unicast_v4 |
| 132 | * set_next_id_multicast_v4 |
| 133 | * set_next_id_acl |
| 134 | * drop |
| 135 | * set_next_id_unicast_v6 |
| 136 | * set_next_id_multicast_v6 |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 137 | */ |
| 138 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 139 | public static PiAction mapForwardingTreatment(TrafficTreatment treatment, PiTableId tableId) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 140 | throws PiInterpreterException { |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 141 | // Empty treatment, generate table entry with no action |
| 142 | if (treatment.equals(DefaultTrafficTreatment.emptyTreatment())) { |
| 143 | return null; |
| 144 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 145 | List<Instruction> insts = treatment.allInstructions(); |
| 146 | OutputInstruction outInst = insts.stream() |
| 147 | .filter(inst -> inst.type() == OUTPUT) |
| 148 | .map(inst -> (OutputInstruction) inst) |
| 149 | .findFirst() |
| 150 | .orElse(null); |
| 151 | |
| 152 | if (outInst == null) { |
| 153 | throw new PiInterpreterException(format(INVALID_TREATMENT, "forwarding", treatment)); |
| 154 | } |
| 155 | |
| 156 | PortNumber portNumber = outInst.port(); |
| 157 | if (!portNumber.equals(PortNumber.CONTROLLER)) { |
| 158 | throw new PiInterpreterException(format("Unsupported port number %s," + |
| 159 | "supports punt action only", |
| 160 | portNumber)); |
| 161 | } |
| 162 | |
| 163 | return PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 164 | .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_SEND_TO_CONTROLLER) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 165 | .build(); |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * In Next block, we need to implement these actions: |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 170 | * set_vlan |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 171 | * set_vlan_output |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 172 | * output_simple |
| 173 | * output_hashed |
| 174 | * l3_routing_simple |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 175 | * l3_routing_vlan |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 176 | * l3_routing_hashed |
| 177 | * mpls_routing_v4_simple |
| 178 | * mpls_routing_v6_simple |
| 179 | * mpls_routing_v4_hashed |
| 180 | * mpls_routing_v6_hashed |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 181 | * |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 182 | * Unsupported, need to find a way to implement it |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 183 | * |
| 184 | * set_mcast_group |
| 185 | * |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 186 | */ |
| 187 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 188 | public static PiAction mapNextTreatment(TrafficTreatment treatment, PiTableId tableId) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 189 | throws PiInterpreterException { |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 190 | // TODO: refactor this method |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 191 | List<Instruction> insts = treatment.allInstructions(); |
| 192 | OutputInstruction outInst = null; |
| 193 | ModEtherInstruction modEthDstInst = null; |
| 194 | ModEtherInstruction modEthSrcInst = null; |
| 195 | ModVlanIdInstruction modVlanIdInst = null; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 196 | ModMplsLabelInstruction modMplsInst = null; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 197 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 198 | // TODO: add NextFunctionType (like ForwardingFunctionType) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 199 | for (Instruction inst : insts) { |
| 200 | switch (inst.type()) { |
| 201 | case L2MODIFICATION: |
| 202 | L2ModificationInstruction l2Inst = (L2ModificationInstruction) inst; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 203 | switch (l2Inst.subtype()) { |
| 204 | case ETH_SRC: |
| 205 | modEthSrcInst = (ModEtherInstruction) l2Inst; |
| 206 | break; |
| 207 | case ETH_DST: |
| 208 | modEthDstInst = (ModEtherInstruction) l2Inst; |
| 209 | break; |
| 210 | case VLAN_ID: |
| 211 | modVlanIdInst = (ModVlanIdInstruction) l2Inst; |
| 212 | break; |
| 213 | case MPLS_LABEL: |
| 214 | modMplsInst = (ModMplsLabelInstruction) l2Inst; |
| 215 | break; |
| 216 | default: |
| 217 | log.warn("Unsupported l2 instruction sub type: {}", l2Inst.subtype()); |
| 218 | break; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 219 | } |
| 220 | break; |
| 221 | case OUTPUT: |
| 222 | outInst = (OutputInstruction) inst; |
| 223 | break; |
| 224 | default: |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 225 | log.warn("Unsupported instruction sub type: {}", inst.type()); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | } |
| 229 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 230 | if (tableId.equals(FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META) && |
| 231 | modVlanIdInst != null) { |
| 232 | // set_vlan |
| 233 | VlanId vlanId = modVlanIdInst.vlanId(); |
| 234 | PiActionParam newVlanParam = |
| 235 | new PiActionParam(FabricConstants.NEW_VLAN_ID, |
| 236 | ImmutableByteSequence.copyFrom(vlanId.toShort())); |
| 237 | return PiAction.builder() |
| 238 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN) |
| 239 | .withParameter(newVlanParam) |
| 240 | .build(); |
| 241 | } |
| 242 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 243 | if (outInst == null) { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 244 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 245 | } |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 246 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 247 | short portNum = (short) outInst.port().toLong(); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 248 | PiActionParam portNumParam = new PiActionParam(FabricConstants.PORT_NUM, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 249 | ImmutableByteSequence.copyFrom(portNum)); |
| 250 | if (modEthDstInst == null && modEthSrcInst == null) { |
| 251 | if (modVlanIdInst != null) { |
| 252 | VlanId vlanId = modVlanIdInst.vlanId(); |
| 253 | PiActionParam vlanParam = |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 254 | new PiActionParam(FabricConstants.NEW_VLAN_ID, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 255 | ImmutableByteSequence.copyFrom(vlanId.toShort())); |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 256 | // set_vlan_output (simple table) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 257 | return PiAction.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 258 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN_OUTPUT) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 259 | .withParameters(ImmutableList.of(portNumParam, vlanParam)) |
| 260 | .build(); |
| 261 | } else { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 262 | // output (simple or hashed table) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 263 | return PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 264 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 265 | .withParameter(portNumParam) |
| 266 | .build(); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | if (modEthDstInst != null && modEthSrcInst != null) { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 271 | MacAddress srcMac = modEthSrcInst.mac(); |
| 272 | MacAddress dstMac = modEthDstInst.mac(); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 273 | PiActionParam srcMacParam = new PiActionParam(FabricConstants.SMAC, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 274 | ImmutableByteSequence.copyFrom(srcMac.toBytes())); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 275 | PiActionParam dstMacParam = new PiActionParam(FabricConstants.DMAC, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 276 | ImmutableByteSequence.copyFrom(dstMac.toBytes())); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 277 | |
| 278 | if (modMplsInst != null) { |
| 279 | // MPLS routing |
| 280 | MplsLabel mplsLabel = modMplsInst.label(); |
| 281 | try { |
| 282 | ImmutableByteSequence mplsValue = |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 283 | ImmutableByteSequence.copyFrom(mplsLabel.toInt()).fit(20); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 284 | PiActionParam mplsParam = new PiActionParam(FabricConstants.LABEL, mplsValue); |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 285 | |
| 286 | PiActionId actionId; |
| 287 | // FIXME: finds a way to determine v4 or v6 |
| 288 | if (tableId.equals(FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE)) { |
| 289 | actionId = FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_V4_SIMPLE; |
| 290 | } else if (tableId.equals(FabricConstants.FABRIC_INGRESS_NEXT_HASHED)) { |
| 291 | actionId = FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_V4_HASHED; |
| 292 | } else { |
| 293 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
| 294 | } |
| 295 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 296 | return PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 297 | .withId(actionId) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 298 | .withParameters(ImmutableList.of(portNumParam, |
| 299 | srcMacParam, |
| 300 | dstMacParam, |
| 301 | mplsParam)) |
| 302 | .build(); |
| 303 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 304 | // Basically this won't happened because we already limited |
| 305 | // size of mpls value to 20 bits (0xFFFFF) |
| 306 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
| 307 | } |
| 308 | } |
| 309 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 310 | if (modVlanIdInst != null) { |
| 311 | VlanId vlanId = modVlanIdInst.vlanId(); |
| 312 | PiActionParam vlanParam = |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 313 | new PiActionParam(FabricConstants.NEW_VLAN_ID, |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 314 | ImmutableByteSequence.copyFrom(vlanId.toShort())); |
| 315 | // L3 routing and set VLAN |
| 316 | return PiAction.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 317 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_L3_ROUTING_VLAN) |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 318 | .withParameters(ImmutableList.of(srcMacParam, dstMacParam, portNumParam, vlanParam)) |
| 319 | .build(); |
| 320 | } else { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 321 | PiActionId actionId; |
| 322 | if (tableId.equals(FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE)) { |
| 323 | actionId = FabricConstants.FABRIC_INGRESS_NEXT_L3_ROUTING_SIMPLE; |
| 324 | } else if (tableId.equals(FabricConstants.FABRIC_INGRESS_NEXT_HASHED)) { |
| 325 | actionId = FabricConstants.FABRIC_INGRESS_NEXT_L3_ROUTING_HASHED; |
| 326 | } else { |
| 327 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
| 328 | } |
| 329 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 330 | // L3 routing |
| 331 | return PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 332 | .withId(actionId) |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 333 | .withParameters(ImmutableList.of(portNumParam, |
| 334 | srcMacParam, |
| 335 | dstMacParam)) |
| 336 | .build(); |
| 337 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
| 341 | } |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 342 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame^] | 343 | /* |
| 344 | * pop_vlan |
| 345 | */ |
| 346 | public static PiAction mapEgressNextTreatment(TrafficTreatment treatment, PiTableId tableId) { |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 347 | // Pop VLAN action for now, may add new action to this control block in the future. |
| 348 | return treatment.allInstructions() |
| 349 | .stream() |
| 350 | .filter(inst -> inst.type() == Instruction.Type.L2MODIFICATION) |
| 351 | .map(inst -> (L2ModificationInstruction) inst) |
| 352 | .filter(inst -> inst.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP) |
| 353 | .findFirst() |
| 354 | .map(inst -> POP_VLAN) |
| 355 | .orElse(NOP); |
| 356 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 357 | } |