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