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 { |
| 63 | private static final ImmutableBiMap<Integer, PiTableId> TABLE_ID_MAP = |
| 64 | ImmutableBiMap.<Integer, PiTableId>builder() |
| 65 | // Filtering |
| 66 | .put(0, FabricConstants.TBL_INGRESS_PORT_VLAN_ID) |
| 67 | .put(1, FabricConstants.TBL_FWD_CLASSIFIER_ID) |
| 68 | // Forwarding |
| 69 | .put(2, FabricConstants.TBL_MPLS_ID) |
| 70 | .put(3, FabricConstants.TBL_UNICAST_V4_ID) |
| 71 | .put(4, FabricConstants.TBL_UNICAST_V6_ID) |
| 72 | .put(5, FabricConstants.TBL_MULTICAST_V4_ID) |
| 73 | .put(6, FabricConstants.TBL_MULTICAST_V6_ID) |
| 74 | .put(7, FabricConstants.TBL_BRIDGING_ID) |
| 75 | .put(8, FabricConstants.TBL_ACL_ID) |
| 76 | // Next |
Yi Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame] | 77 | .put(9, FabricConstants.TBL_SIMPLE_ID) |
| 78 | .put(10, FabricConstants.TBL_HASHED_ID) |
| 79 | .put(11, FabricConstants.TBL_BROADCAST_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 80 | .build(); |
| 81 | |
| 82 | private static final Set<PiTableId> FILTERING_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_INGRESS_PORT_VLAN_ID, |
| 83 | FabricConstants.TBL_FWD_CLASSIFIER_ID); |
| 84 | private static final Set<PiTableId> FORWARDING_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_MPLS_ID, |
| 85 | FabricConstants.TBL_UNICAST_V4_ID, |
| 86 | FabricConstants.TBL_UNICAST_V6_ID, |
| 87 | FabricConstants.TBL_MULTICAST_V4_ID, |
| 88 | FabricConstants.TBL_MULTICAST_V6_ID, |
| 89 | FabricConstants.TBL_BRIDGING_ID, |
| 90 | FabricConstants.TBL_ACL_ID); |
Yi Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame] | 91 | private static final Set<PiTableId> NEXT_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_SIMPLE_ID, |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 92 | FabricConstants.TBL_HASHED_ID, |
| 93 | FabricConstants.TBL_BROADCAST_ID); |
| 94 | |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 95 | private static final ImmutableMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP = |
| 96 | ImmutableMap.<Criterion.Type, PiMatchFieldId>builder() |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 97 | .put(Criterion.Type.IN_PORT, FabricConstants.HF_STANDARD_METADATA_INGRESS_PORT_ID) |
| 98 | .put(Criterion.Type.ETH_DST, FabricConstants.HF_ETHERNET_DST_ADDR_ID) |
| 99 | .put(Criterion.Type.ETH_SRC, FabricConstants.HF_ETHERNET_SRC_ADDR_ID) |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 100 | .put(Criterion.Type.ETH_TYPE, FabricConstants.HF_FABRIC_METADATA_ORIGINAL_ETHER_TYPE_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 101 | .put(Criterion.Type.MPLS_LABEL, FabricConstants.HF_MPLS_LABEL_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 102 | .put(Criterion.Type.VLAN_VID, FabricConstants.HF_VLAN_TAG_VLAN_ID_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 103 | .put(Criterion.Type.IPV4_DST, FabricConstants.HF_IPV4_DST_ADDR_ID) |
| 104 | .put(Criterion.Type.IPV4_SRC, FabricConstants.HF_IPV4_SRC_ADDR_ID) |
| 105 | .put(Criterion.Type.IPV6_DST, FabricConstants.HF_IPV6_DST_ADDR_ID) |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 106 | .put(Criterion.Type.TCP_SRC, FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID) |
| 107 | .put(Criterion.Type.TCP_DST, FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID) |
| 108 | .put(Criterion.Type.UDP_SRC, FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID) |
| 109 | .put(Criterion.Type.UDP_DST, FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 110 | .put(Criterion.Type.IP_PROTO, FabricConstants.HF_FABRIC_METADATA_IP_PROTO_ID) |
| 111 | .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HF_ICMP_ICMP_TYPE_ID) |
| 112 | .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HF_ICMP_ICMP_CODE_ID) |
| 113 | .build(); |
| 114 | |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 115 | private static final ImmutableMap<PiMatchFieldId, Criterion.Type> INVERSE_CRITERION_MAP = |
| 116 | ImmutableMap.<PiMatchFieldId, Criterion.Type>builder() |
| 117 | .put(FabricConstants.HF_STANDARD_METADATA_INGRESS_PORT_ID, Criterion.Type.IN_PORT) |
| 118 | .put(FabricConstants.HF_ETHERNET_DST_ADDR_ID, Criterion.Type.ETH_DST) |
| 119 | .put(FabricConstants.HF_ETHERNET_SRC_ADDR_ID, Criterion.Type.ETH_SRC) |
| 120 | .put(FabricConstants.HF_FABRIC_METADATA_ORIGINAL_ETHER_TYPE_ID, Criterion.Type.ETH_TYPE) |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 121 | .put(FabricConstants.HF_MPLS_LABEL_ID, Criterion.Type.MPLS_LABEL) |
| 122 | .put(FabricConstants.HF_VLAN_TAG_VLAN_ID_ID, Criterion.Type.VLAN_VID) |
| 123 | .put(FabricConstants.HF_IPV4_DST_ADDR_ID, Criterion.Type.IPV4_DST) |
| 124 | .put(FabricConstants.HF_IPV4_SRC_ADDR_ID, Criterion.Type.IPV4_SRC) |
| 125 | .put(FabricConstants.HF_IPV6_DST_ADDR_ID, Criterion.Type.IPV6_DST) |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 126 | // FIXME: might be incorrect if we inverse the map.... |
| 127 | .put(FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID, Criterion.Type.UDP_SRC) |
| 128 | .put(FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID, Criterion.Type.UDP_DST) |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 129 | .put(FabricConstants.HF_FABRIC_METADATA_IP_PROTO_ID, Criterion.Type.IP_PROTO) |
| 130 | .put(FabricConstants.HF_ICMP_ICMP_TYPE_ID, Criterion.Type.ICMPV6_TYPE) |
| 131 | .put(FabricConstants.HF_ICMP_ICMP_CODE_ID, Criterion.Type.ICMPV6_CODE) |
| 132 | .build(); |
| 133 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 134 | @Override |
| 135 | public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) { |
| 136 | return Optional.ofNullable(CRITERION_MAP.get(type)); |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId fieldId) { |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 141 | return Optional.ofNullable(INVERSE_CRITERION_MAP.get(fieldId)); |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) { |
| 146 | return Optional.ofNullable(TABLE_ID_MAP.get(flowRuleTableId)); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public Optional<Integer> mapPiTableId(PiTableId piTableId) { |
| 151 | return Optional.ofNullable(TABLE_ID_MAP.inverse().get(piTableId)); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId) |
| 156 | throws PiInterpreterException { |
| 157 | |
| 158 | if (FILTERING_CTRL_TBLS.contains(piTableId)) { |
| 159 | return FabricTreatmentInterpreter.mapFilteringTreatment(treatment); |
| 160 | } else if (FORWARDING_CTRL_TBLS.contains(piTableId)) { |
| 161 | return FabricTreatmentInterpreter.mapForwardingTreatment(treatment); |
| 162 | } else if (NEXT_CTRL_TBLS.contains(piTableId)) { |
| 163 | return FabricTreatmentInterpreter.mapNextTreatment(treatment); |
| 164 | } else { |
| 165 | throw new PiInterpreterException(String.format("Table %s unsupported", piTableId)); |
| 166 | } |
| 167 | } |
| 168 | |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 169 | private PiPacketOperation createPiPacketOperation(DeviceId deviceId, ByteBuffer data, long portNumber) |
| 170 | throws PiInterpreterException { |
| 171 | PiControlMetadata metadata = createPacketMetadata(portNumber); |
| 172 | return PiPacketOperation.builder() |
| 173 | .forDevice(deviceId) |
| 174 | .withType(PACKET_OUT) |
| 175 | .withData(copyFrom(data)) |
| 176 | .withMetadatas(ImmutableList.of(metadata)) |
| 177 | .build(); |
| 178 | } |
| 179 | |
| 180 | private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException { |
| 181 | try { |
| 182 | return PiControlMetadata.builder() |
| 183 | .withId(FabricConstants.CTRL_META_EGRESS_PORT_ID) |
Carmelo Cascone | 8a571af | 2018-04-06 23:17:04 -0700 | [diff] [blame] | 184 | .withValue(copyFrom(portNumber).fit(FabricConstants.PORT_BITWIDTH)) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 185 | .build(); |
| 186 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 187 | throw new PiInterpreterException(format( |
| 188 | "Port number %d too big, %s", portNumber, e.getMessage())); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) |
| 194 | throws PiInterpreterException { |
| 195 | DeviceId deviceId = packet.sendThrough(); |
| 196 | TrafficTreatment treatment = packet.treatment(); |
| 197 | |
| 198 | // fabric.p4 supports only OUTPUT instructions. |
| 199 | List<Instructions.OutputInstruction> outInstructions = treatment |
| 200 | .allInstructions() |
| 201 | .stream() |
| 202 | .filter(i -> i.type().equals(OUTPUT)) |
| 203 | .map(i -> (Instructions.OutputInstruction) i) |
| 204 | .collect(toList()); |
| 205 | |
| 206 | if (treatment.allInstructions().size() != outInstructions.size()) { |
| 207 | // There are other instructions that are not of type OUTPUT. |
| 208 | throw new PiInterpreterException("Treatment not supported: " + treatment); |
| 209 | } |
| 210 | |
| 211 | ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder(); |
| 212 | for (Instructions.OutputInstruction outInst : outInstructions) { |
| 213 | if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) { |
| 214 | throw new PiInterpreterException(format( |
| 215 | "Output on logical port '%s' not supported", outInst.port())); |
| 216 | } else if (outInst.port().equals(FLOOD)) { |
| 217 | // Since fabric.p4 does not support flooding, we create a packet |
| 218 | // operation for each switch port. |
| 219 | final DeviceService deviceService = handler().get(DeviceService.class); |
| 220 | for (Port port : deviceService.getPorts(packet.sendThrough())) { |
| 221 | builder.add(createPiPacketOperation(deviceId, packet.data(), port.number().toLong())); |
| 222 | } |
| 223 | } else { |
| 224 | builder.add(createPiPacketOperation(deviceId, packet.data(), outInst.port().toLong())); |
| 225 | } |
| 226 | } |
| 227 | return builder.build(); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public InboundPacket mapInboundPacket(PiPacketOperation packetIn) throws PiInterpreterException { |
| 232 | // Assuming that the packet is ethernet, which is fine since fabric.p4 |
| 233 | // can deparse only ethernet packets. |
| 234 | DeviceId deviceId = packetIn.deviceId(); |
| 235 | Ethernet ethPkt; |
| 236 | try { |
| 237 | ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0, |
| 238 | packetIn.data().size()); |
| 239 | } catch (DeserializationException dex) { |
| 240 | throw new PiInterpreterException(dex.getMessage()); |
| 241 | } |
| 242 | |
| 243 | // Returns the ingress port packet metadata. |
| 244 | Optional<PiControlMetadata> packetMetadata = packetIn.metadatas() |
| 245 | .stream().filter(m -> m.id().equals(FabricConstants.CTRL_META_INGRESS_PORT_ID)) |
| 246 | .findFirst(); |
| 247 | |
| 248 | if (packetMetadata.isPresent()) { |
| 249 | ImmutableByteSequence portByteSequence = packetMetadata.get().value(); |
| 250 | short s = portByteSequence.asReadOnlyBuffer().getShort(); |
| 251 | ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s)); |
| 252 | ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray()); |
| 253 | return new DefaultInboundPacket(receivedFrom, ethPkt, rawData); |
| 254 | } else { |
| 255 | throw new PiInterpreterException(format( |
| 256 | "Missing metadata '%s' in packet-in received from '%s': %s", |
| 257 | FabricConstants.CTRL_META_INGRESS_PORT_ID, deviceId, packetIn)); |
| 258 | } |
| 259 | } |
| 260 | } |