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