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