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