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 | |
Carmelo Cascone | 356ab8b | 2019-09-25 01:02:53 -0700 | [diff] [blame] | 17 | package org.onosproject.pipelines.fabric.impl.behaviour; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 18 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onlab.packet.DeserializationException; |
| 23 | import org.onlab.packet.Ethernet; |
| 24 | import org.onlab.util.ImmutableByteSequence; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Port; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.device.DeviceService; |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 30 | import org.onosproject.net.driver.DriverHandler; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flow.TrafficTreatment; |
| 32 | import org.onosproject.net.flow.criteria.Criterion; |
| 33 | import org.onosproject.net.flow.instructions.Instructions; |
| 34 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 35 | import org.onosproject.net.packet.InboundPacket; |
| 36 | import org.onosproject.net.packet.OutboundPacket; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 37 | import org.onosproject.net.pi.model.PiMatchFieldId; |
| 38 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 39 | import org.onosproject.net.pi.model.PiTableId; |
| 40 | import org.onosproject.net.pi.runtime.PiAction; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 41 | import org.onosproject.net.pi.runtime.PiPacketMetadata; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 42 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
Carmelo Cascone | 2102bfb | 2020-12-04 16:54:24 -0800 | [diff] [blame] | 43 | import org.onosproject.pipelines.fabric.FabricConstants; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 44 | |
| 45 | import java.nio.ByteBuffer; |
| 46 | import java.util.Collection; |
| 47 | import java.util.List; |
| 48 | import java.util.Optional; |
| 49 | import java.util.Set; |
| 50 | |
| 51 | import static java.lang.String.format; |
| 52 | import static java.util.stream.Collectors.toList; |
| 53 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Carmelo Cascone | 6880ba6 | 2018-09-06 00:04:34 -0700 | [diff] [blame] | 54 | import static org.onosproject.net.PortNumber.CONTROLLER; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 55 | import static org.onosproject.net.PortNumber.FLOOD; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 56 | import static org.onosproject.net.PortNumber.TABLE; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 57 | import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT; |
| 58 | import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT; |
| 59 | |
| 60 | /** |
| 61 | * Interpreter for fabric pipeline. |
| 62 | */ |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 63 | public class FabricInterpreter extends AbstractFabricHandlerBehavior |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 64 | implements PiPipelineInterpreter { |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 65 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 66 | private static final int PORT_BITWIDTH = 9; |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 67 | public static final byte[] ONE = new byte[]{1}; |
| 68 | public static final byte[] ZERO = new byte[]{0}; |
Carmelo Cascone | 6880ba6 | 2018-09-06 00:04:34 -0700 | [diff] [blame] | 69 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 70 | // Group tables by control block. |
| 71 | private static final Set<PiTableId> FILTERING_CTRL_TBLS = ImmutableSet.of( |
| 72 | FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN, |
| 73 | FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER); |
| 74 | private static final Set<PiTableId> FORWARDING_CTRL_TBLS = ImmutableSet.of( |
| 75 | FabricConstants.FABRIC_INGRESS_FORWARDING_MPLS, |
| 76 | FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4, |
| 77 | FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V6, |
| 78 | FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING); |
| 79 | private static final Set<PiTableId> ACL_CTRL_TBLS = ImmutableSet.of( |
| 80 | FabricConstants.FABRIC_INGRESS_ACL_ACL); |
| 81 | private static final Set<PiTableId> NEXT_CTRL_TBLS = ImmutableSet.of( |
| 82 | FabricConstants.FABRIC_INGRESS_NEXT_NEXT_VLAN, |
| 83 | FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE, |
Carmelo Cascone | 45cc086 | 2018-11-26 11:50:41 -0800 | [diff] [blame] | 84 | FabricConstants.FABRIC_INGRESS_NEXT_HASHED, |
| 85 | FabricConstants.FABRIC_INGRESS_NEXT_XCONNECT); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 86 | private static final Set<PiTableId> E_NEXT_CTRL_TBLS = ImmutableSet.of( |
| 87 | FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 88 | |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 89 | private static final ImmutableMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP = |
| 90 | ImmutableMap.<Criterion.Type, PiMatchFieldId>builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 91 | .put(Criterion.Type.IN_PORT, FabricConstants.HDR_IG_PORT) |
| 92 | .put(Criterion.Type.ETH_DST, FabricConstants.HDR_ETH_DST) |
| 93 | .put(Criterion.Type.ETH_SRC, FabricConstants.HDR_ETH_SRC) |
| 94 | .put(Criterion.Type.ETH_DST_MASKED, FabricConstants.HDR_ETH_DST) |
| 95 | .put(Criterion.Type.ETH_SRC_MASKED, FabricConstants.HDR_ETH_SRC) |
| 96 | .put(Criterion.Type.ETH_TYPE, FabricConstants.HDR_ETH_TYPE) |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 97 | .put(Criterion.Type.MPLS_LABEL, FabricConstants.HDR_MPLS_LABEL) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 98 | .put(Criterion.Type.VLAN_VID, FabricConstants.HDR_VLAN_ID) |
Daniele Moro | 7c3a002 | 2019-07-12 13:38:34 -0700 | [diff] [blame] | 99 | .put(Criterion.Type.INNER_VLAN_VID, FabricConstants.HDR_INNER_VLAN_ID) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 100 | .put(Criterion.Type.IPV4_DST, FabricConstants.HDR_IPV4_DST) |
| 101 | .put(Criterion.Type.IPV4_SRC, FabricConstants.HDR_IPV4_SRC) |
| 102 | .put(Criterion.Type.IPV6_DST, FabricConstants.HDR_IPV6_DST) |
| 103 | .put(Criterion.Type.IP_PROTO, FabricConstants.HDR_IP_PROTO) |
| 104 | .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HDR_ICMP_TYPE) |
| 105 | .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HDR_ICMP_CODE) |
Daniele Moro | 59020df | 2019-08-05 15:37:45 -0700 | [diff] [blame] | 106 | .put(Criterion.Type.UDP_DST, FabricConstants.HDR_L4_DPORT) |
| 107 | .put(Criterion.Type.UDP_SRC, FabricConstants.HDR_L4_SPORT) |
| 108 | .put(Criterion.Type.UDP_DST_MASKED, FabricConstants.HDR_L4_DPORT) |
| 109 | .put(Criterion.Type.UDP_SRC_MASKED, FabricConstants.HDR_L4_SPORT) |
| 110 | .put(Criterion.Type.TCP_DST, FabricConstants.HDR_L4_DPORT) |
| 111 | .put(Criterion.Type.TCP_SRC, FabricConstants.HDR_L4_SPORT) |
| 112 | .put(Criterion.Type.TCP_DST_MASKED, FabricConstants.HDR_L4_DPORT) |
| 113 | .put(Criterion.Type.TCP_SRC_MASKED, FabricConstants.HDR_L4_SPORT) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 114 | .build(); |
| 115 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 116 | private static final PiAction NOP = PiAction.builder() |
| 117 | .withId(FabricConstants.NOP).build(); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 118 | |
| 119 | private static final ImmutableMap<PiTableId, PiAction> DEFAULT_ACTIONS = |
| 120 | ImmutableMap.<PiTableId, PiAction>builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 121 | .put(FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4, NOP) |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 122 | .build(); |
| 123 | |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 124 | private FabricTreatmentInterpreter treatmentInterpreter; |
| 125 | |
| 126 | /** |
| 127 | * Creates a new instance of this behavior with the given capabilities. |
| 128 | * |
| 129 | * @param capabilities capabilities |
| 130 | */ |
| 131 | public FabricInterpreter(FabricCapabilities capabilities) { |
| 132 | super(capabilities); |
| 133 | instantiateTreatmentInterpreter(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Create a new instance of this behaviour. Used by the abstract projectable |
| 138 | * model (i.e., {@link org.onosproject.net.Device#as(Class)}. |
| 139 | */ |
| 140 | public FabricInterpreter() { |
| 141 | super(); |
| 142 | } |
| 143 | |
| 144 | private void instantiateTreatmentInterpreter() { |
| 145 | this.treatmentInterpreter = new FabricTreatmentInterpreter(this.capabilities); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void setHandler(DriverHandler handler) { |
| 150 | super.setHandler(handler); |
| 151 | instantiateTreatmentInterpreter(); |
| 152 | } |
| 153 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) { |
| 156 | return Optional.ofNullable(CRITERION_MAP.get(type)); |
| 157 | } |
| 158 | |
| 159 | @Override |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 160 | public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 161 | // The only use case for Index ID->PiTableId is when using the single |
| 162 | // table pipeliner. fabric.p4 is never used with such pipeliner. |
| 163 | return Optional.empty(); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 167 | public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId) |
| 168 | throws PiInterpreterException { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 169 | if (FILTERING_CTRL_TBLS.contains(piTableId)) { |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 170 | return treatmentInterpreter.mapFilteringTreatment(treatment, piTableId); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 171 | } else if (FORWARDING_CTRL_TBLS.contains(piTableId)) { |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 172 | return treatmentInterpreter.mapForwardingTreatment(treatment, piTableId); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 173 | } else if (ACL_CTRL_TBLS.contains(piTableId)) { |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 174 | return treatmentInterpreter.mapAclTreatment(treatment, piTableId); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 175 | } else if (NEXT_CTRL_TBLS.contains(piTableId)) { |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 176 | return treatmentInterpreter.mapNextTreatment(treatment, piTableId); |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 177 | } else if (E_NEXT_CTRL_TBLS.contains(piTableId)) { |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 178 | return treatmentInterpreter.mapEgressNextTreatment(treatment, piTableId); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 179 | } else { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 180 | throw new PiInterpreterException(format( |
| 181 | "Treatment mapping not supported for table '%s'", piTableId)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 185 | private PiPacketOperation createPiPacketOperation( |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 186 | ByteBuffer data, long portNumber, boolean doForwarding) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 187 | throws PiInterpreterException { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 188 | return PiPacketOperation.builder() |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 189 | .withType(PACKET_OUT) |
| 190 | .withData(copyFrom(data)) |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 191 | .withMetadatas(createPacketMetadata(portNumber, doForwarding)) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 192 | .build(); |
| 193 | } |
| 194 | |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 195 | private Collection<PiPacketMetadata> createPacketMetadata( |
| 196 | long portNumber, boolean doForwarding) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 197 | throws PiInterpreterException { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 198 | try { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 199 | ImmutableList.Builder<PiPacketMetadata> builder = ImmutableList.builder(); |
| 200 | builder.add(PiPacketMetadata.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 201 | .withId(FabricConstants.EGRESS_PORT) |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 202 | .withValue(copyFrom(portNumber) |
| 203 | .fit(PORT_BITWIDTH)) |
| 204 | .build()); |
| 205 | builder.add(PiPacketMetadata.builder() |
| 206 | .withId(FabricConstants.DO_FORWARDING) |
| 207 | .withValue(copyFrom(doForwarding ? ONE : ZERO)) |
| 208 | .build()); |
| 209 | return builder.build(); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 210 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 211 | throw new PiInterpreterException(format( |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 212 | "Port number '%d' too big, %s", portNumber, e.getMessage())); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) |
| 218 | throws PiInterpreterException { |
| 219 | DeviceId deviceId = packet.sendThrough(); |
| 220 | TrafficTreatment treatment = packet.treatment(); |
| 221 | |
| 222 | // fabric.p4 supports only OUTPUT instructions. |
| 223 | List<Instructions.OutputInstruction> outInstructions = treatment |
| 224 | .allInstructions() |
| 225 | .stream() |
| 226 | .filter(i -> i.type().equals(OUTPUT)) |
| 227 | .map(i -> (Instructions.OutputInstruction) i) |
| 228 | .collect(toList()); |
| 229 | |
| 230 | if (treatment.allInstructions().size() != outInstructions.size()) { |
| 231 | // There are other instructions that are not of type OUTPUT. |
| 232 | throw new PiInterpreterException("Treatment not supported: " + treatment); |
| 233 | } |
| 234 | |
| 235 | ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder(); |
| 236 | for (Instructions.OutputInstruction outInst : outInstructions) { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 237 | if (outInst.port().equals(TABLE)) { |
| 238 | // Logical port. Forward using the switch tables like a regular packet. |
| 239 | builder.add(createPiPacketOperation(packet.data(), 0, true)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 240 | } else if (outInst.port().equals(FLOOD)) { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 241 | // Logical port. Create a packet operation for each switch port. |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 242 | final DeviceService deviceService = handler().get(DeviceService.class); |
| 243 | for (Port port : deviceService.getPorts(packet.sendThrough())) { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 244 | builder.add(createPiPacketOperation(packet.data(), port.number().toLong(), false)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 245 | } |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 246 | } else if (outInst.port().isLogical()) { |
| 247 | throw new PiInterpreterException(format( |
| 248 | "Output on logical port '%s' not supported", outInst.port())); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 249 | } else { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame^] | 250 | // Send as-is to given port bypassing all switch tables. |
| 251 | builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong(), false)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | return builder.build(); |
| 255 | } |
| 256 | |
| 257 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 258 | public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId) throws PiInterpreterException { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 259 | // Assuming that the packet is ethernet, which is fine since fabric.p4 |
| 260 | // can deparse only ethernet packets. |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 261 | Ethernet ethPkt; |
| 262 | try { |
| 263 | ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0, |
| 264 | packetIn.data().size()); |
| 265 | } catch (DeserializationException dex) { |
| 266 | throw new PiInterpreterException(dex.getMessage()); |
| 267 | } |
| 268 | |
| 269 | // Returns the ingress port packet metadata. |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 270 | Optional<PiPacketMetadata> packetMetadata = packetIn.metadatas() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 271 | .stream().filter(m -> m.id().equals(FabricConstants.INGRESS_PORT)) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 272 | .findFirst(); |
| 273 | |
| 274 | if (packetMetadata.isPresent()) { |
| 275 | ImmutableByteSequence portByteSequence = packetMetadata.get().value(); |
| 276 | short s = portByteSequence.asReadOnlyBuffer().getShort(); |
| 277 | ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s)); |
| 278 | ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray()); |
| 279 | return new DefaultInboundPacket(receivedFrom, ethPkt, rawData); |
| 280 | } else { |
| 281 | throw new PiInterpreterException(format( |
| 282 | "Missing metadata '%s' in packet-in received from '%s': %s", |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 283 | FabricConstants.INGRESS_PORT, deviceId, packetIn)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
Carmelo Cascone | 6880ba6 | 2018-09-06 00:04:34 -0700 | [diff] [blame] | 286 | |
| 287 | @Override |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 288 | public Optional<PiAction> getOriginalDefaultAction(PiTableId tableId) { |
| 289 | return Optional.ofNullable(DEFAULT_ACTIONS.get(tableId)); |
| 290 | } |
| 291 | |
| 292 | @Override |
Carmelo Cascone | 6880ba6 | 2018-09-06 00:04:34 -0700 | [diff] [blame] | 293 | public Optional<Integer> mapLogicalPortNumber(PortNumber port) { |
| 294 | if (!port.equals(CONTROLLER)) { |
| 295 | return Optional.empty(); |
| 296 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 297 | return capabilities.cpuPort(); |
Carmelo Cascone | 6880ba6 | 2018-09-06 00:04:34 -0700 | [diff] [blame] | 298 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 299 | } |