ONOS-7058 Refactored default pipeconfs in new pipelines directory
- Minimal refactoring of P4 programs
- Removed symlinks to BMv2 JSON/P4Info
- Bumped p4c commit (which fixes known parser bug)
- Renamed "default" pipeconf to "basic" (ONOS-6818)
Change-Id: I319f8b142ab22dba9b15457e28cd62d17f78a423
diff --git a/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/BasicInterpreterImpl.java b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/BasicInterpreterImpl.java
new file mode 100644
index 0000000..f9ff527
--- /dev/null
+++ b/pipelines/basic/src/main/java/org/onosproject/pipelines/basic/BasicInterpreterImpl.java
@@ -0,0 +1,257 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.pipelines.basic;
+
+import com.google.common.collect.ImmutableBiMap;
+import com.google.common.collect.ImmutableList;
+import org.onlab.packet.DeserializationException;
+import org.onlab.packet.Ethernet;
+import org.onlab.util.ImmutableByteSequence;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Port;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flow.instructions.Instruction;
+import org.onosproject.net.packet.DefaultInboundPacket;
+import org.onosproject.net.packet.InboundPacket;
+import org.onosproject.net.packet.OutboundPacket;
+import org.onosproject.net.pi.model.PiPipelineInterpreter;
+import org.onosproject.net.pi.runtime.PiAction;
+import org.onosproject.net.pi.runtime.PiActionParam;
+import org.onosproject.net.pi.runtime.PiCounterId;
+import org.onosproject.net.pi.runtime.PiHeaderFieldId;
+import org.onosproject.net.pi.runtime.PiPacketMetadata;
+import org.onosproject.net.pi.runtime.PiPacketOperation;
+import org.onosproject.net.pi.runtime.PiTableId;
+
+import java.nio.ByteBuffer;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+
+import static java.lang.String.format;
+import static java.util.stream.Collectors.toList;
+import static org.onlab.util.ImmutableByteSequence.copyFrom;
+import static org.onlab.util.ImmutableByteSequence.fit;
+import static org.onosproject.net.PortNumber.CONTROLLER;
+import static org.onosproject.net.PortNumber.FLOOD;
+import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
+import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
+import static org.onosproject.net.pi.runtime.PiPacketOperation.Type.PACKET_OUT;
+import static org.onosproject.pipelines.basic.BasicConstants.ACT_DROP_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.ACT_NOACTION_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.ACT_PRM_PORT_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.ACT_SEND_TO_CPU_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.ACT_SET_EGRESS_PORT_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.CNT_TABLE0_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.CNT_WCMP_TABLE_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_DST_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_SRC_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_TYPE_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.HDR_IN_PORT_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.PKT_META_EGRESS_PORT_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.PKT_META_INGRESS_PORT_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.PORT_BITWIDTH;
+import static org.onosproject.pipelines.basic.BasicConstants.TBL_TABLE0_ID;
+import static org.onosproject.pipelines.basic.BasicConstants.TBL_WCMP_TABLE_ID;
+
+/**
+ * Interpreter implementation for basic.p4.
+ */
+public class BasicInterpreterImpl extends AbstractHandlerBehaviour
+ implements PiPipelineInterpreter {
+
+ private static final ImmutableBiMap<Integer, PiTableId> TABLE_MAP =
+ new ImmutableBiMap.Builder<Integer, PiTableId>()
+ .put(0, TBL_TABLE0_ID)
+ .build();
+ private static final ImmutableBiMap<PiTableId, PiCounterId> TABLE_COUNTER_MAP =
+ new ImmutableBiMap.Builder<PiTableId, PiCounterId>()
+ .put(TBL_TABLE0_ID, CNT_TABLE0_ID)
+ .put(TBL_WCMP_TABLE_ID, CNT_WCMP_TABLE_ID)
+ .build();
+ private static final ImmutableBiMap<Criterion.Type, PiHeaderFieldId> CRITERION_MAP =
+ new ImmutableBiMap.Builder<Criterion.Type, PiHeaderFieldId>()
+ .put(Criterion.Type.IN_PORT, HDR_IN_PORT_ID)
+ .put(Criterion.Type.ETH_DST, HDR_ETH_DST_ID)
+ .put(Criterion.Type.ETH_SRC, HDR_ETH_SRC_ID)
+ .put(Criterion.Type.ETH_TYPE, HDR_ETH_TYPE_ID)
+ .build();
+
+ @Override
+ public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId)
+ throws PiInterpreterException {
+ if (treatment.allInstructions().size() == 0) {
+ // No actions means drop.
+ return PiAction.builder().withId(ACT_DROP_ID).build();
+ } else if (treatment.allInstructions().size() > 1) {
+ // We understand treatments with only 1 instruction.
+ throw new PiInterpreterException("Treatment has multiple instructions");
+ }
+
+ Instruction instruction = treatment.allInstructions().get(0);
+ switch (instruction.type()) {
+ case OUTPUT:
+ return outputPiAction((OutputInstruction) instruction);
+ case NOACTION:
+ return PiAction.builder().withId(ACT_NOACTION_ID).build();
+ default:
+ throw new PiInterpreterException(format(
+ "Instruction type '%s' not supported", instruction.type()));
+ }
+ }
+
+ private PiAction outputPiAction(OutputInstruction outInstruction)
+ throws PiInterpreterException {
+ PortNumber port = outInstruction.port();
+ if (!port.isLogical()) {
+ try {
+ return PiAction.builder()
+ .withId(ACT_SET_EGRESS_PORT_ID)
+ .withParameter(new PiActionParam(ACT_PRM_PORT_ID,
+ fit(copyFrom(port.toLong()), PORT_BITWIDTH)))
+ .build();
+ } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
+ throw new PiInterpreterException(e.getMessage());
+ }
+ } else if (port.equals(CONTROLLER)) {
+ return PiAction.builder().withId(ACT_SEND_TO_CPU_ID).build();
+ } else {
+ throw new PiInterpreterException(format(
+ "Egress on logical port '%s' not supported", port));
+ }
+ }
+
+ @Override
+ public Optional<PiCounterId> mapTableCounter(PiTableId piTableId) {
+ return Optional.ofNullable(TABLE_COUNTER_MAP.get(piTableId));
+ }
+
+ @Override
+ public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
+ throws PiInterpreterException {
+ TrafficTreatment treatment = packet.treatment();
+
+ // basic.p4 supports only OUTPUT instructions.
+ List<OutputInstruction> outInstructions = treatment
+ .allInstructions()
+ .stream()
+ .filter(i -> i.type().equals(OUTPUT))
+ .map(i -> (OutputInstruction) i)
+ .collect(toList());
+
+ if (treatment.allInstructions().size() != outInstructions.size()) {
+ // There are other instructions that are not of type OUTPUT.
+ throw new PiInterpreterException("Treatment not supported: " + treatment);
+ }
+
+ ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
+ for (OutputInstruction outInst : outInstructions) {
+ if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
+ throw new PiInterpreterException(format(
+ "Output on logical port '%s' not supported", outInst.port()));
+ } else if (outInst.port().equals(FLOOD)) {
+ // Since basic.p4 does not support flooding, we create a packet
+ // operation for each switch port.
+ final DeviceService deviceService = handler().get(DeviceService.class);
+ for (Port port : deviceService.getPorts(packet.sendThrough())) {
+ builder.add(createPiPacketOperation(packet.data(), port.number().toLong()));
+ }
+ } else {
+ builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong()));
+ }
+ }
+ return builder.build();
+ }
+
+ @Override
+ public InboundPacket mapInboundPacket(DeviceId deviceId, PiPacketOperation packetIn)
+ throws PiInterpreterException {
+ // Assuming that the packet is ethernet, which is fine since basic.p4
+ // can deparse only ethernet packets.
+ Ethernet ethPkt;
+ try {
+ ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0,
+ packetIn.data().size());
+ } catch (DeserializationException dex) {
+ throw new PiInterpreterException(dex.getMessage());
+ }
+
+ // Returns the ingress port packet metadata.
+ Optional<PiPacketMetadata> packetMetadata = packetIn.metadatas()
+ .stream().filter(m -> m.id().equals(PKT_META_INGRESS_PORT_ID))
+ .findFirst();
+
+ if (packetMetadata.isPresent()) {
+ ImmutableByteSequence portByteSequence = packetMetadata.get().value();
+ short s = portByteSequence.asReadOnlyBuffer().getShort();
+ ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
+ ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
+ return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
+ } else {
+ throw new PiInterpreterException(format(
+ "Missing metadata '%s' in packet-in received from '%s': %s",
+ PKT_META_INGRESS_PORT_ID, deviceId, packetIn));
+ }
+ }
+
+ private PiPacketOperation createPiPacketOperation(ByteBuffer data, long portNumber)
+ throws PiInterpreterException {
+ PiPacketMetadata metadata = createPacketMetadata(portNumber);
+ return PiPacketOperation.builder()
+ .withType(PACKET_OUT)
+ .withData(copyFrom(data))
+ .withMetadatas(ImmutableList.of(metadata))
+ .build();
+ }
+
+ private PiPacketMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
+ try {
+ return PiPacketMetadata.builder()
+ .withId(PKT_META_EGRESS_PORT_ID)
+ .withValue(fit(copyFrom(portNumber), PORT_BITWIDTH))
+ .build();
+ } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
+ throw new PiInterpreterException(format(
+ "Port number %d too big, %s", portNumber, e.getMessage()));
+ }
+ }
+
+ @Override
+ public Optional<PiHeaderFieldId> mapCriterionType(Criterion.Type type) {
+ return Optional.ofNullable(CRITERION_MAP.get(type));
+ }
+
+ @Override
+ public Optional<Criterion.Type> mapPiHeaderFieldId(PiHeaderFieldId headerFieldId) {
+ return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId));
+ }
+
+ @Override
+ public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
+ return Optional.ofNullable(TABLE_MAP.get(flowRuleTableId));
+ }
+
+ @Override
+ public Optional<Integer> mapPiTableId(PiTableId piTableId) {
+ return Optional.ofNullable(TABLE_MAP.inverse().get(piTableId));
+ }
+}