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.ImmutableBiMap; |
| 20 | import com.google.common.collect.ImmutableList; |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import org.onlab.packet.DeserializationException; |
| 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onlab.util.ImmutableByteSequence; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.Port; |
| 29 | import org.onosproject.net.PortNumber; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 32 | import org.onosproject.net.flow.TrafficTreatment; |
| 33 | import org.onosproject.net.flow.criteria.Criterion; |
| 34 | import org.onosproject.net.flow.instructions.Instructions; |
| 35 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 36 | import org.onosproject.net.packet.InboundPacket; |
| 37 | import org.onosproject.net.packet.OutboundPacket; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 38 | import org.onosproject.net.pi.model.PiMatchFieldId; |
| 39 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 40 | import org.onosproject.net.pi.model.PiTableId; |
| 41 | import org.onosproject.net.pi.runtime.PiAction; |
| 42 | import org.onosproject.net.pi.runtime.PiControlMetadata; |
| 43 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
| 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; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 54 | import static org.onosproject.net.PortNumber.FLOOD; |
| 55 | import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT; |
| 56 | import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT; |
| 57 | |
| 58 | /** |
| 59 | * Interpreter for fabric pipeline. |
| 60 | */ |
| 61 | public class FabricInterpreter extends AbstractHandlerBehaviour |
| 62 | implements PiPipelineInterpreter { |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame^] | 63 | |
| 64 | public static final int PORT_BITWIDTH = 9; |
| 65 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 66 | private static final ImmutableBiMap<Integer, PiTableId> TABLE_ID_MAP = |
| 67 | ImmutableBiMap.<Integer, PiTableId>builder() |
| 68 | // Filtering |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 69 | .put(0, FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN) |
| 70 | .put(1, FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 71 | // Forwarding |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 72 | .put(2, FabricConstants.FABRIC_INGRESS_FORWARDING_MPLS) |
| 73 | .put(3, FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V4) |
| 74 | .put(4, FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V6) |
| 75 | .put(5, FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V4) |
| 76 | .put(6, FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V6) |
| 77 | .put(7, FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING) |
| 78 | .put(8, FabricConstants.FABRIC_INGRESS_FORWARDING_ACL) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 79 | // Next |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 80 | .put(9, FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META) |
| 81 | .put(10, FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE) |
| 82 | .put(11, FabricConstants.FABRIC_INGRESS_NEXT_HASHED) |
| 83 | .put(12, FabricConstants.FABRIC_INGRESS_NEXT_MULTICAST) |
| 84 | .put(13, FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 85 | .build(); |
| 86 | |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 87 | private static final Set<PiTableId> FILTERING_CTRL_TBLS = |
| 88 | ImmutableSet.of(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN, |
| 89 | FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER); |
| 90 | private static final Set<PiTableId> FORWARDING_CTRL_TBLS = |
| 91 | ImmutableSet.of(FabricConstants.FABRIC_INGRESS_FORWARDING_MPLS, |
| 92 | FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V4, |
| 93 | FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V6, |
| 94 | FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V4, |
| 95 | FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V6, |
| 96 | FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING, |
| 97 | FabricConstants.FABRIC_INGRESS_FORWARDING_ACL); |
| 98 | private static final Set<PiTableId> NEXT_CTRL_TBLS = |
Yi Tseng | bf0d437 | 2018-06-21 23:55:58 +0800 | [diff] [blame] | 99 | ImmutableSet.of(FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META, |
| 100 | FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE, |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 101 | FabricConstants.FABRIC_INGRESS_NEXT_HASHED, |
| 102 | FabricConstants.FABRIC_INGRESS_NEXT_MULTICAST); |
| 103 | private static final Set<PiTableId> E_NEXT_CTRL_TBLS = |
| 104 | ImmutableSet.of(FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 105 | |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 106 | private static final ImmutableMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP = |
| 107 | ImmutableMap.<Criterion.Type, PiMatchFieldId>builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 108 | .put(Criterion.Type.IN_PORT, FabricConstants.STANDARD_METADATA_INGRESS_PORT) |
| 109 | .put(Criterion.Type.ETH_DST, FabricConstants.HDR_ETHERNET_DST_ADDR) |
| 110 | .put(Criterion.Type.ETH_SRC, FabricConstants.HDR_ETHERNET_SRC_ADDR) |
| 111 | .put(Criterion.Type.ETH_TYPE, FabricConstants.FABRIC_METADATA_ORIGINAL_ETHER_TYPE) |
| 112 | .put(Criterion.Type.MPLS_LABEL, FabricConstants.HDR_MPLS_LABEL) |
| 113 | .put(Criterion.Type.VLAN_VID, FabricConstants.HDR_VLAN_TAG_VLAN_ID) |
| 114 | .put(Criterion.Type.IPV4_DST, FabricConstants.HDR_IPV4_DST_ADDR) |
| 115 | .put(Criterion.Type.IPV4_SRC, FabricConstants.HDR_IPV4_SRC_ADDR) |
| 116 | .put(Criterion.Type.IPV6_DST, FabricConstants.HDR_IPV6_DST_ADDR) |
| 117 | .put(Criterion.Type.TCP_SRC, FabricConstants.FABRIC_METADATA_L4_SRC_PORT) |
| 118 | .put(Criterion.Type.TCP_DST, FabricConstants.FABRIC_METADATA_L4_DST_PORT) |
| 119 | .put(Criterion.Type.UDP_SRC, FabricConstants.FABRIC_METADATA_L4_SRC_PORT) |
| 120 | .put(Criterion.Type.UDP_DST, FabricConstants.FABRIC_METADATA_L4_DST_PORT) |
| 121 | .put(Criterion.Type.IP_PROTO, FabricConstants.FABRIC_METADATA_IP_PROTO) |
| 122 | .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HDR_ICMP_ICMP_TYPE) |
| 123 | .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HDR_ICMP_ICMP_CODE) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 124 | .build(); |
| 125 | |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 126 | private static final ImmutableMap<PiMatchFieldId, Criterion.Type> INVERSE_CRITERION_MAP = |
| 127 | ImmutableMap.<PiMatchFieldId, Criterion.Type>builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 128 | .put(FabricConstants.STANDARD_METADATA_INGRESS_PORT, Criterion.Type.IN_PORT) |
| 129 | .put(FabricConstants.HDR_ETHERNET_DST_ADDR, Criterion.Type.ETH_DST) |
| 130 | .put(FabricConstants.HDR_ETHERNET_SRC_ADDR, Criterion.Type.ETH_SRC) |
| 131 | .put(FabricConstants.FABRIC_METADATA_ORIGINAL_ETHER_TYPE, Criterion.Type.ETH_TYPE) |
| 132 | .put(FabricConstants.HDR_MPLS_LABEL, Criterion.Type.MPLS_LABEL) |
| 133 | .put(FabricConstants.HDR_VLAN_TAG_VLAN_ID, Criterion.Type.VLAN_VID) |
| 134 | .put(FabricConstants.HDR_IPV4_DST_ADDR, Criterion.Type.IPV4_DST) |
| 135 | .put(FabricConstants.HDR_IPV4_SRC_ADDR, Criterion.Type.IPV4_SRC) |
| 136 | .put(FabricConstants.HDR_IPV6_DST_ADDR, Criterion.Type.IPV6_DST) |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 137 | // FIXME: might be incorrect if we inverse the map.... |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 138 | .put(FabricConstants.FABRIC_METADATA_L4_SRC_PORT, Criterion.Type.UDP_SRC) |
| 139 | .put(FabricConstants.FABRIC_METADATA_L4_DST_PORT, Criterion.Type.UDP_DST) |
| 140 | .put(FabricConstants.FABRIC_METADATA_IP_PROTO, Criterion.Type.IP_PROTO) |
| 141 | .put(FabricConstants.HDR_ICMP_ICMP_TYPE, Criterion.Type.ICMPV6_TYPE) |
| 142 | .put(FabricConstants.HDR_ICMP_ICMP_CODE, Criterion.Type.ICMPV6_CODE) |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 143 | .build(); |
| 144 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 145 | @Override |
| 146 | public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) { |
| 147 | return Optional.ofNullable(CRITERION_MAP.get(type)); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId fieldId) { |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 152 | return Optional.ofNullable(INVERSE_CRITERION_MAP.get(fieldId)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) { |
| 157 | return Optional.ofNullable(TABLE_ID_MAP.get(flowRuleTableId)); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Optional<Integer> mapPiTableId(PiTableId piTableId) { |
| 162 | return Optional.ofNullable(TABLE_ID_MAP.inverse().get(piTableId)); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId) |
| 167 | throws PiInterpreterException { |
| 168 | |
| 169 | if (FILTERING_CTRL_TBLS.contains(piTableId)) { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 170 | return FabricTreatmentInterpreter.mapFilteringTreatment(treatment, piTableId); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 171 | } else if (FORWARDING_CTRL_TBLS.contains(piTableId)) { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 172 | return FabricTreatmentInterpreter.mapForwardingTreatment(treatment, piTableId); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 173 | } else if (NEXT_CTRL_TBLS.contains(piTableId)) { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 174 | return FabricTreatmentInterpreter.mapNextTreatment(treatment, piTableId); |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 175 | } else if (E_NEXT_CTRL_TBLS.contains(piTableId)) { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 176 | return FabricTreatmentInterpreter.mapEgressNextTreatment(treatment, piTableId); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 177 | } else { |
| 178 | throw new PiInterpreterException(String.format("Table %s unsupported", piTableId)); |
| 179 | } |
| 180 | } |
| 181 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 182 | private PiPacketOperation createPiPacketOperation(DeviceId deviceId, ByteBuffer data, long portNumber) |
| 183 | throws PiInterpreterException { |
| 184 | PiControlMetadata metadata = createPacketMetadata(portNumber); |
| 185 | return PiPacketOperation.builder() |
| 186 | .forDevice(deviceId) |
| 187 | .withType(PACKET_OUT) |
| 188 | .withData(copyFrom(data)) |
| 189 | .withMetadatas(ImmutableList.of(metadata)) |
| 190 | .build(); |
| 191 | } |
| 192 | |
| 193 | private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException { |
| 194 | try { |
| 195 | return PiControlMetadata.builder() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 196 | .withId(FabricConstants.EGRESS_PORT) |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame^] | 197 | .withValue(copyFrom(portNumber).fit(PORT_BITWIDTH)) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 198 | .build(); |
| 199 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 200 | throw new PiInterpreterException(format( |
| 201 | "Port number %d too big, %s", portNumber, e.getMessage())); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) |
| 207 | throws PiInterpreterException { |
| 208 | DeviceId deviceId = packet.sendThrough(); |
| 209 | TrafficTreatment treatment = packet.treatment(); |
| 210 | |
| 211 | // fabric.p4 supports only OUTPUT instructions. |
| 212 | List<Instructions.OutputInstruction> outInstructions = treatment |
| 213 | .allInstructions() |
| 214 | .stream() |
| 215 | .filter(i -> i.type().equals(OUTPUT)) |
| 216 | .map(i -> (Instructions.OutputInstruction) i) |
| 217 | .collect(toList()); |
| 218 | |
| 219 | if (treatment.allInstructions().size() != outInstructions.size()) { |
| 220 | // There are other instructions that are not of type OUTPUT. |
| 221 | throw new PiInterpreterException("Treatment not supported: " + treatment); |
| 222 | } |
| 223 | |
| 224 | ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder(); |
| 225 | for (Instructions.OutputInstruction outInst : outInstructions) { |
| 226 | if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) { |
| 227 | throw new PiInterpreterException(format( |
| 228 | "Output on logical port '%s' not supported", outInst.port())); |
| 229 | } else if (outInst.port().equals(FLOOD)) { |
| 230 | // Since fabric.p4 does not support flooding, we create a packet |
| 231 | // operation for each switch port. |
| 232 | final DeviceService deviceService = handler().get(DeviceService.class); |
| 233 | for (Port port : deviceService.getPorts(packet.sendThrough())) { |
| 234 | builder.add(createPiPacketOperation(deviceId, packet.data(), port.number().toLong())); |
| 235 | } |
| 236 | } else { |
| 237 | builder.add(createPiPacketOperation(deviceId, packet.data(), outInst.port().toLong())); |
| 238 | } |
| 239 | } |
| 240 | return builder.build(); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public InboundPacket mapInboundPacket(PiPacketOperation packetIn) throws PiInterpreterException { |
| 245 | // Assuming that the packet is ethernet, which is fine since fabric.p4 |
| 246 | // can deparse only ethernet packets. |
| 247 | DeviceId deviceId = packetIn.deviceId(); |
| 248 | Ethernet ethPkt; |
| 249 | try { |
| 250 | ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0, |
| 251 | packetIn.data().size()); |
| 252 | } catch (DeserializationException dex) { |
| 253 | throw new PiInterpreterException(dex.getMessage()); |
| 254 | } |
| 255 | |
| 256 | // Returns the ingress port packet metadata. |
| 257 | Optional<PiControlMetadata> packetMetadata = packetIn.metadatas() |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 258 | .stream().filter(m -> m.id().equals(FabricConstants.INGRESS_PORT)) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 259 | .findFirst(); |
| 260 | |
| 261 | if (packetMetadata.isPresent()) { |
| 262 | ImmutableByteSequence portByteSequence = packetMetadata.get().value(); |
| 263 | short s = portByteSequence.asReadOnlyBuffer().getShort(); |
| 264 | ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s)); |
| 265 | ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray()); |
| 266 | return new DefaultInboundPacket(receivedFrom, ethPkt, rawData); |
| 267 | } else { |
| 268 | throw new PiInterpreterException(format( |
| 269 | "Missing metadata '%s' in packet-in received from '%s': %s", |
Yi Tseng | 43ee7e8 | 2018-04-12 16:37:34 +0800 | [diff] [blame] | 270 | FabricConstants.INGRESS_PORT, deviceId, packetIn)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |