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]"; |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 55 | private static final PiAction NOP = PiAction.builder().withId(FabricConstants.NOP).build(); |
| 56 | private static final PiAction NOP_ACL = PiAction.builder() |
| 57 | .withId(FabricConstants.FABRIC_INGRESS_FORWARDING_NOP_ACL).build(); |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 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())) { |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 144 | if (tableId.equals(FabricConstants.FABRIC_INGRESS_FORWARDING_ACL)) { |
| 145 | return NOP_ACL; |
| 146 | } else { |
| 147 | return NOP; |
| 148 | } |
Yi Tseng | df3eec5 | 2018-02-15 14:56:02 -0800 | [diff] [blame] | 149 | } |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 150 | PortNumber outPort = getOutputPort(treatment); |
| 151 | if (outPort == null |
| 152 | || !outPort.equals(PortNumber.CONTROLLER) |
| 153 | || treatment.allInstructions().size() > 1) { |
| 154 | throw new PiInterpreterException( |
| 155 | format(INVALID_TREATMENT_WITH_EXP, |
| 156 | "forwarding", "supports only punt/clone to CPU actions", |
| 157 | treatment)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 160 | final PiActionId actionId = treatment.clearedDeferred() |
| 161 | ? FabricConstants.FABRIC_INGRESS_FORWARDING_PUNT_TO_CPU |
| 162 | : FabricConstants.FABRIC_INGRESS_FORWARDING_CLONE_TO_CPU; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 163 | |
| 164 | return PiAction.builder() |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 165 | .withId(actionId) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 166 | .build(); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * In Next block, we need to implement these actions: |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 171 | * set_vlan |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 172 | * set_vlan_output |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 173 | * output_simple |
| 174 | * output_hashed |
| 175 | * l3_routing_simple |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 176 | * l3_routing_vlan |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 177 | * l3_routing_hashed |
| 178 | * mpls_routing_v4_simple |
| 179 | * mpls_routing_v6_simple |
| 180 | * mpls_routing_v4_hashed |
| 181 | * mpls_routing_v6_hashed |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 182 | * |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 183 | * Unsupported, need to find a way to implement it |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 184 | * |
| 185 | * set_mcast_group |
| 186 | * |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 187 | */ |
| 188 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 189 | public static PiAction mapNextTreatment(TrafficTreatment treatment, PiTableId tableId) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 190 | throws PiInterpreterException { |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 191 | // TODO: refactor this method |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 192 | List<Instruction> insts = treatment.allInstructions(); |
| 193 | OutputInstruction outInst = null; |
| 194 | ModEtherInstruction modEthDstInst = null; |
| 195 | ModEtherInstruction modEthSrcInst = null; |
| 196 | ModVlanIdInstruction modVlanIdInst = null; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 197 | ModMplsLabelInstruction modMplsInst = null; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 198 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 199 | // TODO: add NextFunctionType (like ForwardingFunctionType) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 200 | for (Instruction inst : insts) { |
| 201 | switch (inst.type()) { |
| 202 | case L2MODIFICATION: |
| 203 | L2ModificationInstruction l2Inst = (L2ModificationInstruction) inst; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 204 | switch (l2Inst.subtype()) { |
| 205 | case ETH_SRC: |
| 206 | modEthSrcInst = (ModEtherInstruction) l2Inst; |
| 207 | break; |
| 208 | case ETH_DST: |
| 209 | modEthDstInst = (ModEtherInstruction) l2Inst; |
| 210 | break; |
| 211 | case VLAN_ID: |
| 212 | modVlanIdInst = (ModVlanIdInstruction) l2Inst; |
| 213 | break; |
| 214 | case MPLS_LABEL: |
| 215 | modMplsInst = (ModMplsLabelInstruction) l2Inst; |
| 216 | break; |
Charles Chan | 8da5779 | 2018-08-16 16:59:42 -0700 | [diff] [blame^] | 217 | case VLAN_POP: |
| 218 | // VLAN_POP will be handled by mapEgressNextTreatment() |
| 219 | break; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 220 | default: |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 221 | log.warn("Unsupported l2 instruction sub type {} [table={}, {}]", |
| 222 | l2Inst.subtype(), tableId, treatment); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 223 | break; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 224 | } |
| 225 | break; |
| 226 | case OUTPUT: |
| 227 | outInst = (OutputInstruction) inst; |
| 228 | break; |
| 229 | default: |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 230 | log.warn("Unsupported instruction sub type {} [table={}, {}]", |
| 231 | inst.type(), tableId, treatment); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 236 | if (tableId.equals(FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META) && |
| 237 | modVlanIdInst != null) { |
| 238 | // set_vlan |
| 239 | VlanId vlanId = modVlanIdInst.vlanId(); |
| 240 | PiActionParam newVlanParam = |
| 241 | new PiActionParam(FabricConstants.NEW_VLAN_ID, |
| 242 | ImmutableByteSequence.copyFrom(vlanId.toShort())); |
| 243 | return PiAction.builder() |
| 244 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN) |
| 245 | .withParameter(newVlanParam) |
| 246 | .build(); |
| 247 | } |
| 248 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 249 | if (outInst == null) { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 250 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 251 | } |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 252 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 253 | short portNum = (short) outInst.port().toLong(); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 254 | PiActionParam portNumParam = new PiActionParam(FabricConstants.PORT_NUM, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 255 | ImmutableByteSequence.copyFrom(portNum)); |
| 256 | if (modEthDstInst == null && modEthSrcInst == null) { |
| 257 | if (modVlanIdInst != null) { |
| 258 | VlanId vlanId = modVlanIdInst.vlanId(); |
| 259 | PiActionParam vlanParam = |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 260 | new PiActionParam(FabricConstants.NEW_VLAN_ID, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 261 | ImmutableByteSequence.copyFrom(vlanId.toShort())); |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 262 | // set_vlan_output (simple table) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 263 | return PiAction.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 264 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_SET_VLAN_OUTPUT) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 265 | .withParameters(ImmutableList.of(portNumParam, vlanParam)) |
| 266 | .build(); |
| 267 | } else { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 268 | // output (simple or hashed table) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 269 | return PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 270 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_OUTPUT_SIMPLE) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 271 | .withParameter(portNumParam) |
| 272 | .build(); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if (modEthDstInst != null && modEthSrcInst != null) { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 277 | MacAddress srcMac = modEthSrcInst.mac(); |
| 278 | MacAddress dstMac = modEthDstInst.mac(); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 279 | PiActionParam srcMacParam = new PiActionParam(FabricConstants.SMAC, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 280 | ImmutableByteSequence.copyFrom(srcMac.toBytes())); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 281 | PiActionParam dstMacParam = new PiActionParam(FabricConstants.DMAC, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 282 | ImmutableByteSequence.copyFrom(dstMac.toBytes())); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 283 | |
| 284 | if (modMplsInst != null) { |
| 285 | // MPLS routing |
| 286 | MplsLabel mplsLabel = modMplsInst.label(); |
| 287 | try { |
| 288 | ImmutableByteSequence mplsValue = |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 289 | ImmutableByteSequence.copyFrom(mplsLabel.toInt()).fit(20); |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 290 | PiActionParam mplsParam = new PiActionParam(FabricConstants.LABEL, mplsValue); |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 291 | |
| 292 | PiActionId actionId; |
| 293 | // FIXME: finds a way to determine v4 or v6 |
| 294 | if (tableId.equals(FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE)) { |
| 295 | actionId = FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_V4_SIMPLE; |
| 296 | } else if (tableId.equals(FabricConstants.FABRIC_INGRESS_NEXT_HASHED)) { |
| 297 | actionId = FabricConstants.FABRIC_INGRESS_NEXT_MPLS_ROUTING_V4_HASHED; |
| 298 | } else { |
| 299 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
| 300 | } |
| 301 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 302 | return PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 303 | .withId(actionId) |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 304 | .withParameters(ImmutableList.of(portNumParam, |
| 305 | srcMacParam, |
| 306 | dstMacParam, |
| 307 | mplsParam)) |
| 308 | .build(); |
| 309 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 310 | // Basically this won't happened because we already limited |
| 311 | // size of mpls value to 20 bits (0xFFFFF) |
| 312 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
| 313 | } |
| 314 | } |
| 315 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 316 | if (modVlanIdInst != null) { |
| 317 | VlanId vlanId = modVlanIdInst.vlanId(); |
| 318 | PiActionParam vlanParam = |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 319 | new PiActionParam(FabricConstants.NEW_VLAN_ID, |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 320 | ImmutableByteSequence.copyFrom(vlanId.toShort())); |
| 321 | // L3 routing and set VLAN |
| 322 | return PiAction.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 323 | .withId(FabricConstants.FABRIC_INGRESS_NEXT_L3_ROUTING_VLAN) |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 324 | .withParameters(ImmutableList.of(srcMacParam, dstMacParam, portNumParam, vlanParam)) |
| 325 | .build(); |
| 326 | } else { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 327 | PiActionId actionId; |
| 328 | if (tableId.equals(FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE)) { |
| 329 | actionId = FabricConstants.FABRIC_INGRESS_NEXT_L3_ROUTING_SIMPLE; |
| 330 | } else if (tableId.equals(FabricConstants.FABRIC_INGRESS_NEXT_HASHED)) { |
| 331 | actionId = FabricConstants.FABRIC_INGRESS_NEXT_L3_ROUTING_HASHED; |
| 332 | } else { |
| 333 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
| 334 | } |
| 335 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 336 | // L3 routing |
| 337 | return PiAction.builder() |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 338 | .withId(actionId) |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 339 | .withParameters(ImmutableList.of(portNumParam, |
| 340 | srcMacParam, |
| 341 | dstMacParam)) |
| 342 | .build(); |
| 343 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | throw new PiInterpreterException(format(INVALID_TREATMENT, "next", treatment)); |
| 347 | } |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 348 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 349 | /* |
| 350 | * pop_vlan |
| 351 | */ |
| 352 | public static PiAction mapEgressNextTreatment(TrafficTreatment treatment, PiTableId tableId) { |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 353 | // Pop VLAN action for now, may add new action to this control block in the future. |
| 354 | return treatment.allInstructions() |
| 355 | .stream() |
| 356 | .filter(inst -> inst.type() == Instruction.Type.L2MODIFICATION) |
| 357 | .map(inst -> (L2ModificationInstruction) inst) |
| 358 | .filter(inst -> inst.subtype() == L2ModificationInstruction.L2SubType.VLAN_POP) |
| 359 | .findFirst() |
| 360 | .map(inst -> POP_VLAN) |
| 361 | .orElse(NOP); |
| 362 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 363 | } |