Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -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.basic; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableBiMap; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import org.onlab.packet.DeserializationException; |
| 22 | import org.onlab.packet.Ethernet; |
| 23 | import org.onlab.util.ImmutableByteSequence; |
| 24 | import org.onosproject.net.ConnectPoint; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Port; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
| 30 | import org.onosproject.net.flow.criteria.Criterion; |
| 31 | import org.onosproject.net.flow.instructions.Instruction; |
| 32 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 33 | import org.onosproject.net.packet.InboundPacket; |
| 34 | import org.onosproject.net.packet.OutboundPacket; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 35 | import org.onosproject.net.pi.model.PiCounterId; |
| 36 | import org.onosproject.net.pi.model.PiMatchFieldId; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 37 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 38 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 39 | import org.onosproject.net.pi.runtime.PiAction; |
| 40 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 41 | import org.onosproject.net.pi.runtime.PiControlMetadata; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 42 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 43 | |
| 44 | import java.nio.ByteBuffer; |
| 45 | import java.util.Collection; |
| 46 | import java.util.List; |
| 47 | import java.util.Optional; |
| 48 | |
| 49 | import static java.lang.String.format; |
| 50 | import static java.util.stream.Collectors.toList; |
| 51 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
| 52 | import static org.onlab.util.ImmutableByteSequence.fit; |
| 53 | import static org.onosproject.net.PortNumber.CONTROLLER; |
| 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.flow.instructions.Instructions.OutputInstruction; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 57 | import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 58 | import static org.onosproject.pipelines.basic.BasicConstants.ACT_DROP_ID; |
| 59 | import static org.onosproject.pipelines.basic.BasicConstants.ACT_NOACTION_ID; |
| 60 | import static org.onosproject.pipelines.basic.BasicConstants.ACT_PRM_PORT_ID; |
| 61 | import static org.onosproject.pipelines.basic.BasicConstants.ACT_SEND_TO_CPU_ID; |
| 62 | import static org.onosproject.pipelines.basic.BasicConstants.ACT_SET_EGRESS_PORT_ID; |
| 63 | import static org.onosproject.pipelines.basic.BasicConstants.CNT_TABLE0_ID; |
| 64 | import static org.onosproject.pipelines.basic.BasicConstants.CNT_WCMP_TABLE_ID; |
| 65 | import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_DST_ID; |
| 66 | import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_SRC_ID; |
| 67 | import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_TYPE_ID; |
| 68 | import static org.onosproject.pipelines.basic.BasicConstants.HDR_IN_PORT_ID; |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame^] | 69 | import static org.onosproject.pipelines.basic.BasicConstants.HDR_IPV4_DST_ID; |
| 70 | import static org.onosproject.pipelines.basic.BasicConstants.HDR_IPV4_SRC_ID; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 71 | import static org.onosproject.pipelines.basic.BasicConstants.PKT_META_EGRESS_PORT_ID; |
| 72 | import static org.onosproject.pipelines.basic.BasicConstants.PKT_META_INGRESS_PORT_ID; |
| 73 | import static org.onosproject.pipelines.basic.BasicConstants.PORT_BITWIDTH; |
| 74 | import static org.onosproject.pipelines.basic.BasicConstants.TBL_TABLE0_ID; |
| 75 | import static org.onosproject.pipelines.basic.BasicConstants.TBL_WCMP_TABLE_ID; |
| 76 | |
| 77 | /** |
| 78 | * Interpreter implementation for basic.p4. |
| 79 | */ |
| 80 | public class BasicInterpreterImpl extends AbstractHandlerBehaviour |
| 81 | implements PiPipelineInterpreter { |
| 82 | |
| 83 | private static final ImmutableBiMap<Integer, PiTableId> TABLE_MAP = |
| 84 | new ImmutableBiMap.Builder<Integer, PiTableId>() |
| 85 | .put(0, TBL_TABLE0_ID) |
| 86 | .build(); |
| 87 | private static final ImmutableBiMap<PiTableId, PiCounterId> TABLE_COUNTER_MAP = |
| 88 | new ImmutableBiMap.Builder<PiTableId, PiCounterId>() |
| 89 | .put(TBL_TABLE0_ID, CNT_TABLE0_ID) |
| 90 | .put(TBL_WCMP_TABLE_ID, CNT_WCMP_TABLE_ID) |
| 91 | .build(); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 92 | private static final ImmutableBiMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP = |
| 93 | new ImmutableBiMap.Builder<Criterion.Type, PiMatchFieldId>() |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 94 | .put(Criterion.Type.IN_PORT, HDR_IN_PORT_ID) |
| 95 | .put(Criterion.Type.ETH_DST, HDR_ETH_DST_ID) |
| 96 | .put(Criterion.Type.ETH_SRC, HDR_ETH_SRC_ID) |
| 97 | .put(Criterion.Type.ETH_TYPE, HDR_ETH_TYPE_ID) |
Carmelo Cascone | 5167f32 | 2017-11-21 21:58:50 -0800 | [diff] [blame^] | 98 | .put(Criterion.Type.IPV4_SRC, HDR_IPV4_SRC_ID) |
| 99 | .put(Criterion.Type.IPV4_DST, HDR_IPV4_DST_ID) |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 100 | .build(); |
| 101 | |
| 102 | @Override |
| 103 | public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId) |
| 104 | throws PiInterpreterException { |
| 105 | if (treatment.allInstructions().size() == 0) { |
| 106 | // No actions means drop. |
| 107 | return PiAction.builder().withId(ACT_DROP_ID).build(); |
| 108 | } else if (treatment.allInstructions().size() > 1) { |
| 109 | // We understand treatments with only 1 instruction. |
| 110 | throw new PiInterpreterException("Treatment has multiple instructions"); |
| 111 | } |
| 112 | |
| 113 | Instruction instruction = treatment.allInstructions().get(0); |
| 114 | switch (instruction.type()) { |
| 115 | case OUTPUT: |
| 116 | return outputPiAction((OutputInstruction) instruction); |
| 117 | case NOACTION: |
| 118 | return PiAction.builder().withId(ACT_NOACTION_ID).build(); |
| 119 | default: |
| 120 | throw new PiInterpreterException(format( |
| 121 | "Instruction type '%s' not supported", instruction.type())); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private PiAction outputPiAction(OutputInstruction outInstruction) |
| 126 | throws PiInterpreterException { |
| 127 | PortNumber port = outInstruction.port(); |
| 128 | if (!port.isLogical()) { |
| 129 | try { |
| 130 | return PiAction.builder() |
| 131 | .withId(ACT_SET_EGRESS_PORT_ID) |
| 132 | .withParameter(new PiActionParam(ACT_PRM_PORT_ID, |
| 133 | fit(copyFrom(port.toLong()), PORT_BITWIDTH))) |
| 134 | .build(); |
| 135 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 136 | throw new PiInterpreterException(e.getMessage()); |
| 137 | } |
| 138 | } else if (port.equals(CONTROLLER)) { |
| 139 | return PiAction.builder().withId(ACT_SEND_TO_CPU_ID).build(); |
| 140 | } else { |
| 141 | throw new PiInterpreterException(format( |
| 142 | "Egress on logical port '%s' not supported", port)); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Optional<PiCounterId> mapTableCounter(PiTableId piTableId) { |
| 148 | return Optional.ofNullable(TABLE_COUNTER_MAP.get(piTableId)); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) |
| 153 | throws PiInterpreterException { |
| 154 | TrafficTreatment treatment = packet.treatment(); |
| 155 | |
| 156 | // basic.p4 supports only OUTPUT instructions. |
| 157 | List<OutputInstruction> outInstructions = treatment |
| 158 | .allInstructions() |
| 159 | .stream() |
| 160 | .filter(i -> i.type().equals(OUTPUT)) |
| 161 | .map(i -> (OutputInstruction) i) |
| 162 | .collect(toList()); |
| 163 | |
| 164 | if (treatment.allInstructions().size() != outInstructions.size()) { |
| 165 | // There are other instructions that are not of type OUTPUT. |
| 166 | throw new PiInterpreterException("Treatment not supported: " + treatment); |
| 167 | } |
| 168 | |
| 169 | ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder(); |
| 170 | for (OutputInstruction outInst : outInstructions) { |
| 171 | if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) { |
| 172 | throw new PiInterpreterException(format( |
| 173 | "Output on logical port '%s' not supported", outInst.port())); |
| 174 | } else if (outInst.port().equals(FLOOD)) { |
| 175 | // Since basic.p4 does not support flooding, we create a packet |
| 176 | // operation for each switch port. |
| 177 | final DeviceService deviceService = handler().get(DeviceService.class); |
| 178 | for (Port port : deviceService.getPorts(packet.sendThrough())) { |
| 179 | builder.add(createPiPacketOperation(packet.data(), port.number().toLong())); |
| 180 | } |
| 181 | } else { |
| 182 | builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong())); |
| 183 | } |
| 184 | } |
| 185 | return builder.build(); |
| 186 | } |
| 187 | |
| 188 | @Override |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 189 | public InboundPacket mapInboundPacket(PiPacketOperation packetIn) |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 190 | throws PiInterpreterException { |
| 191 | // Assuming that the packet is ethernet, which is fine since basic.p4 |
| 192 | // can deparse only ethernet packets. |
| 193 | Ethernet ethPkt; |
| 194 | try { |
| 195 | ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0, |
| 196 | packetIn.data().size()); |
| 197 | } catch (DeserializationException dex) { |
| 198 | throw new PiInterpreterException(dex.getMessage()); |
| 199 | } |
| 200 | |
| 201 | // Returns the ingress port packet metadata. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 202 | Optional<PiControlMetadata> packetMetadata = packetIn.metadatas() |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 203 | .stream().filter(m -> m.id().equals(PKT_META_INGRESS_PORT_ID)) |
| 204 | .findFirst(); |
| 205 | |
| 206 | if (packetMetadata.isPresent()) { |
| 207 | ImmutableByteSequence portByteSequence = packetMetadata.get().value(); |
| 208 | short s = portByteSequence.asReadOnlyBuffer().getShort(); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 209 | ConnectPoint receivedFrom = new ConnectPoint(packetIn.deviceId(), PortNumber.portNumber(s)); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 210 | ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray()); |
| 211 | return new DefaultInboundPacket(receivedFrom, ethPkt, rawData); |
| 212 | } else { |
| 213 | throw new PiInterpreterException(format( |
| 214 | "Missing metadata '%s' in packet-in received from '%s': %s", |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 215 | PKT_META_INGRESS_PORT_ID, packetIn.deviceId(), packetIn)); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | private PiPacketOperation createPiPacketOperation(ByteBuffer data, long portNumber) |
| 220 | throws PiInterpreterException { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 221 | PiControlMetadata metadata = createPacketMetadata(portNumber); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 222 | return PiPacketOperation.builder() |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 223 | .forDevice(this.data().deviceId()) |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 224 | .withType(PACKET_OUT) |
| 225 | .withData(copyFrom(data)) |
| 226 | .withMetadatas(ImmutableList.of(metadata)) |
| 227 | .build(); |
| 228 | } |
| 229 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 230 | private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException { |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 231 | try { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 232 | return PiControlMetadata.builder() |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 233 | .withId(PKT_META_EGRESS_PORT_ID) |
| 234 | .withValue(fit(copyFrom(portNumber), PORT_BITWIDTH)) |
| 235 | .build(); |
| 236 | } catch (ImmutableByteSequence.ByteSequenceTrimException e) { |
| 237 | throw new PiInterpreterException(format( |
| 238 | "Port number %d too big, %s", portNumber, e.getMessage())); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | @Override |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 243 | public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) { |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 244 | return Optional.ofNullable(CRITERION_MAP.get(type)); |
| 245 | } |
| 246 | |
| 247 | @Override |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 248 | public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId headerFieldId) { |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 249 | return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId)); |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) { |
| 254 | return Optional.ofNullable(TABLE_MAP.get(flowRuleTableId)); |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public Optional<Integer> mapPiTableId(PiTableId piTableId) { |
| 259 | return Optional.ofNullable(TABLE_MAP.inverse().get(piTableId)); |
| 260 | } |
| 261 | } |