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