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. |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 71 | private static final Set<PiTableId> FORWARDING_CTRL_TBLS = ImmutableSet.of( |
| 72 | FabricConstants.FABRIC_INGRESS_FORWARDING_MPLS, |
| 73 | FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4, |
| 74 | FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V6, |
| 75 | FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING); |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 76 | private static final Set<PiTableId> PRE_NEXT_CTRL_TBLS = ImmutableSet.of( |
| 77 | FabricConstants.FABRIC_INGRESS_PRE_NEXT_NEXT_MPLS, |
| 78 | FabricConstants.FABRIC_INGRESS_PRE_NEXT_NEXT_VLAN); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 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( |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 82 | FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE, |
Carmelo Cascone | 45cc086 | 2018-11-26 11:50:41 -0800 | [diff] [blame] | 83 | FabricConstants.FABRIC_INGRESS_NEXT_HASHED, |
| 84 | FabricConstants.FABRIC_INGRESS_NEXT_XCONNECT); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 85 | private static final Set<PiTableId> E_NEXT_CTRL_TBLS = ImmutableSet.of( |
| 86 | FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 87 | |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 88 | private static final ImmutableMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP = |
| 89 | ImmutableMap.<Criterion.Type, PiMatchFieldId>builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 90 | .put(Criterion.Type.IN_PORT, FabricConstants.HDR_IG_PORT) |
| 91 | .put(Criterion.Type.ETH_DST, FabricConstants.HDR_ETH_DST) |
| 92 | .put(Criterion.Type.ETH_SRC, FabricConstants.HDR_ETH_SRC) |
| 93 | .put(Criterion.Type.ETH_DST_MASKED, FabricConstants.HDR_ETH_DST) |
| 94 | .put(Criterion.Type.ETH_SRC_MASKED, FabricConstants.HDR_ETH_SRC) |
| 95 | .put(Criterion.Type.ETH_TYPE, FabricConstants.HDR_ETH_TYPE) |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 96 | .put(Criterion.Type.MPLS_LABEL, FabricConstants.HDR_MPLS_LABEL) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 97 | .put(Criterion.Type.VLAN_VID, FabricConstants.HDR_VLAN_ID) |
Daniele Moro | 7c3a002 | 2019-07-12 13:38:34 -0700 | [diff] [blame] | 98 | .put(Criterion.Type.INNER_VLAN_VID, FabricConstants.HDR_INNER_VLAN_ID) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 99 | .put(Criterion.Type.IPV4_DST, FabricConstants.HDR_IPV4_DST) |
| 100 | .put(Criterion.Type.IPV4_SRC, FabricConstants.HDR_IPV4_SRC) |
| 101 | .put(Criterion.Type.IPV6_DST, FabricConstants.HDR_IPV6_DST) |
| 102 | .put(Criterion.Type.IP_PROTO, FabricConstants.HDR_IP_PROTO) |
| 103 | .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HDR_ICMP_TYPE) |
| 104 | .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HDR_ICMP_CODE) |
Daniele Moro | 59020df | 2019-08-05 15:37:45 -0700 | [diff] [blame] | 105 | .put(Criterion.Type.UDP_DST, FabricConstants.HDR_L4_DPORT) |
| 106 | .put(Criterion.Type.UDP_SRC, FabricConstants.HDR_L4_SPORT) |
| 107 | .put(Criterion.Type.UDP_DST_MASKED, FabricConstants.HDR_L4_DPORT) |
| 108 | .put(Criterion.Type.UDP_SRC_MASKED, FabricConstants.HDR_L4_SPORT) |
| 109 | .put(Criterion.Type.TCP_DST, FabricConstants.HDR_L4_DPORT) |
| 110 | .put(Criterion.Type.TCP_SRC, FabricConstants.HDR_L4_SPORT) |
| 111 | .put(Criterion.Type.TCP_DST_MASKED, FabricConstants.HDR_L4_DPORT) |
| 112 | .put(Criterion.Type.TCP_SRC_MASKED, FabricConstants.HDR_L4_SPORT) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 113 | .build(); |
| 114 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 115 | private static final PiAction NOP = PiAction.builder() |
| 116 | .withId(FabricConstants.NOP).build(); |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 117 | |
| 118 | private static final ImmutableMap<PiTableId, PiAction> DEFAULT_ACTIONS = |
| 119 | ImmutableMap.<PiTableId, PiAction>builder() |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 120 | .put(FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4, NOP) |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 121 | .build(); |
| 122 | |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 123 | private FabricTreatmentInterpreter treatmentInterpreter; |
| 124 | |
| 125 | /** |
| 126 | * Creates a new instance of this behavior with the given capabilities. |
| 127 | * |
| 128 | * @param capabilities capabilities |
| 129 | */ |
| 130 | public FabricInterpreter(FabricCapabilities capabilities) { |
| 131 | super(capabilities); |
| 132 | instantiateTreatmentInterpreter(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Create a new instance of this behaviour. Used by the abstract projectable |
| 137 | * model (i.e., {@link org.onosproject.net.Device#as(Class)}. |
| 138 | */ |
| 139 | public FabricInterpreter() { |
| 140 | super(); |
| 141 | } |
| 142 | |
| 143 | private void instantiateTreatmentInterpreter() { |
| 144 | this.treatmentInterpreter = new FabricTreatmentInterpreter(this.capabilities); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void setHandler(DriverHandler handler) { |
| 149 | super.setHandler(handler); |
| 150 | instantiateTreatmentInterpreter(); |
| 151 | } |
| 152 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 153 | @Override |
| 154 | public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) { |
| 155 | return Optional.ofNullable(CRITERION_MAP.get(type)); |
| 156 | } |
| 157 | |
| 158 | @Override |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 159 | public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 160 | // The only use case for Index ID->PiTableId is when using the single |
| 161 | // table pipeliner. fabric.p4 is never used with such pipeliner. |
| 162 | return Optional.empty(); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | @Override |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 166 | public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId) |
| 167 | throws PiInterpreterException { |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 168 | if (FORWARDING_CTRL_TBLS.contains(piTableId)) { |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 169 | return treatmentInterpreter.mapForwardingTreatment(treatment, piTableId); |
Wailok Shum | fb7e787 | 2021-06-18 17:30:08 +0800 | [diff] [blame] | 170 | } else if (PRE_NEXT_CTRL_TBLS.contains(piTableId)) { |
| 171 | return treatmentInterpreter.mapPreNextTreatment(treatment, piTableId); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 172 | } else if (ACL_CTRL_TBLS.contains(piTableId)) { |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 173 | return treatmentInterpreter.mapAclTreatment(treatment, piTableId); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 174 | } else if (NEXT_CTRL_TBLS.contains(piTableId)) { |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 175 | return treatmentInterpreter.mapNextTreatment(treatment, piTableId); |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 176 | } else if (E_NEXT_CTRL_TBLS.contains(piTableId)) { |
Daniele Moro | f51d0c1 | 2019-07-30 10:43:10 -0700 | [diff] [blame] | 177 | return treatmentInterpreter.mapEgressNextTreatment(treatment, piTableId); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 178 | } else { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 179 | throw new PiInterpreterException(format( |
| 180 | "Treatment mapping not supported for table '%s'", piTableId)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 184 | private PiPacketOperation createPiPacketOperation( |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 185 | ByteBuffer data, long portNumber, boolean doForwarding) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 186 | throws PiInterpreterException { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 187 | return PiPacketOperation.builder() |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 188 | .withType(PACKET_OUT) |
| 189 | .withData(copyFrom(data)) |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 190 | .withMetadatas(createPacketMetadata(portNumber, doForwarding)) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 191 | .build(); |
| 192 | } |
| 193 | |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 194 | private Collection<PiPacketMetadata> createPacketMetadata( |
| 195 | long portNumber, boolean doForwarding) |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 196 | throws PiInterpreterException { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 197 | try { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 198 | ImmutableList.Builder<PiPacketMetadata> builder = ImmutableList.builder(); |
Carmelo Cascone | 2a308ff | 2021-06-01 18:31:57 -0700 | [diff] [blame] | 199 | // We have observed an issue with p4lang/PI and BMv2 where in |
| 200 | // presence of multiple metadata fields, the PI implementation for |
| 201 | // BMv2 provides an erroneous serialization of the packet-out |
| 202 | // header, an hence affects the parsing/forwarding behavior. As a |
| 203 | // workaround, since we cannot control the order of fields in the |
| 204 | // p4runtime.PacketOut message, we modify the interpreter to only |
| 205 | // add one field, egress_port or do_forwarding. Both fields |
| 206 | // are treated as mutually exclusive by the P4 pipeline, so the |
| 207 | // operation is safe. This is against the P4Runtime spec (all fields |
| 208 | // should be provided), but supported by bmv2 (unset fields are |
| 209 | // initialized to zero). |
| 210 | if (portNumber >= 0) { |
| 211 | // 0 is a valid port number. |
| 212 | builder.add(PiPacketMetadata.builder() |
| 213 | .withId(FabricConstants.EGRESS_PORT) |
| 214 | .withValue(copyFrom(portNumber) |
| 215 | .fit(PORT_BITWIDTH)) |
| 216 | .build()); |
| 217 | } |
| 218 | if (doForwarding) { |
| 219 | builder.add(PiPacketMetadata.builder() |
| 220 | .withId(FabricConstants.DO_FORWARDING) |
| 221 | .withValue(copyFrom(ONE)) |
| 222 | .build()); |
| 223 | } |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 224 | return builder.build(); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 225 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 226 | throw new PiInterpreterException(format( |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 227 | "Port number '%d' too big, %s", portNumber, e.getMessage())); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) |
| 233 | throws PiInterpreterException { |
| 234 | DeviceId deviceId = packet.sendThrough(); |
| 235 | TrafficTreatment treatment = packet.treatment(); |
| 236 | |
| 237 | // fabric.p4 supports only OUTPUT instructions. |
| 238 | List<Instructions.OutputInstruction> outInstructions = treatment |
| 239 | .allInstructions() |
| 240 | .stream() |
| 241 | .filter(i -> i.type().equals(OUTPUT)) |
| 242 | .map(i -> (Instructions.OutputInstruction) i) |
| 243 | .collect(toList()); |
| 244 | |
| 245 | if (treatment.allInstructions().size() != outInstructions.size()) { |
| 246 | // There are other instructions that are not of type OUTPUT. |
| 247 | throw new PiInterpreterException("Treatment not supported: " + treatment); |
| 248 | } |
| 249 | |
| 250 | ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder(); |
| 251 | for (Instructions.OutputInstruction outInst : outInstructions) { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 252 | if (outInst.port().equals(TABLE)) { |
| 253 | // Logical port. Forward using the switch tables like a regular packet. |
Carmelo Cascone | 2a308ff | 2021-06-01 18:31:57 -0700 | [diff] [blame] | 254 | builder.add(createPiPacketOperation(packet.data(), -1, true)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 255 | } else if (outInst.port().equals(FLOOD)) { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 256 | // Logical port. Create a packet operation for each switch port. |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 257 | final DeviceService deviceService = handler().get(DeviceService.class); |
| 258 | for (Port port : deviceService.getPorts(packet.sendThrough())) { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 259 | builder.add(createPiPacketOperation(packet.data(), port.number().toLong(), false)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 260 | } |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 261 | } else if (outInst.port().isLogical()) { |
| 262 | throw new PiInterpreterException(format( |
| 263 | "Output on logical port '%s' not supported", outInst.port())); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 264 | } else { |
Carmelo Cascone | 2388cc1 | 2021-05-26 19:30:30 +0200 | [diff] [blame] | 265 | // Send as-is to given port bypassing all switch tables. |
| 266 | builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong(), false)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | return builder.build(); |
| 270 | } |
| 271 | |
| 272 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 273 | public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId) throws PiInterpreterException { |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 274 | // Assuming that the packet is ethernet, which is fine since fabric.p4 |
| 275 | // can deparse only ethernet packets. |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 276 | Ethernet ethPkt; |
| 277 | try { |
| 278 | ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0, |
| 279 | packetIn.data().size()); |
| 280 | } catch (DeserializationException dex) { |
| 281 | throw new PiInterpreterException(dex.getMessage()); |
| 282 | } |
| 283 | |
| 284 | // Returns the ingress port packet metadata. |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 285 | Optional<PiPacketMetadata> packetMetadata = packetIn.metadatas() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 286 | .stream().filter(m -> m.id().equals(FabricConstants.INGRESS_PORT)) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 287 | .findFirst(); |
| 288 | |
| 289 | if (packetMetadata.isPresent()) { |
| 290 | ImmutableByteSequence portByteSequence = packetMetadata.get().value(); |
| 291 | short s = portByteSequence.asReadOnlyBuffer().getShort(); |
| 292 | ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s)); |
| 293 | ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray()); |
| 294 | return new DefaultInboundPacket(receivedFrom, ethPkt, rawData); |
| 295 | } else { |
| 296 | throw new PiInterpreterException(format( |
| 297 | "Missing metadata '%s' in packet-in received from '%s': %s", |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 298 | FabricConstants.INGRESS_PORT, deviceId, packetIn)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 299 | } |
| 300 | } |
Carmelo Cascone | 6880ba6 | 2018-09-06 00:04:34 -0700 | [diff] [blame] | 301 | |
| 302 | @Override |
Carmelo Cascone | 50d195f | 2018-09-11 13:26:38 -0700 | [diff] [blame] | 303 | public Optional<PiAction> getOriginalDefaultAction(PiTableId tableId) { |
| 304 | return Optional.ofNullable(DEFAULT_ACTIONS.get(tableId)); |
| 305 | } |
| 306 | |
| 307 | @Override |
Carmelo Cascone | 6880ba6 | 2018-09-06 00:04:34 -0700 | [diff] [blame] | 308 | public Optional<Integer> mapLogicalPortNumber(PortNumber port) { |
| 309 | if (!port.equals(CONTROLLER)) { |
| 310 | return Optional.empty(); |
| 311 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 312 | return capabilities.cpuPort(); |
Carmelo Cascone | 6880ba6 | 2018-09-06 00:04:34 -0700 | [diff] [blame] | 313 | } |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 314 | } |