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