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