blob: 8e7b39952270ea700b3aa533033f26ff7bacc1e0 [file] [log] [blame]
Carmelo Casconeca94bcf2017-10-27 14:16:59 -07001/*
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
17package org.onosproject.pipelines.basic;
18
19import com.google.common.collect.ImmutableBiMap;
20import com.google.common.collect.ImmutableList;
21import org.onlab.packet.DeserializationException;
22import org.onlab.packet.Ethernet;
23import org.onlab.util.ImmutableByteSequence;
24import org.onosproject.net.ConnectPoint;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070025import org.onosproject.net.Port;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.driver.AbstractHandlerBehaviour;
29import org.onosproject.net.flow.TrafficTreatment;
30import org.onosproject.net.flow.criteria.Criterion;
31import org.onosproject.net.flow.instructions.Instruction;
32import org.onosproject.net.packet.DefaultInboundPacket;
33import org.onosproject.net.packet.InboundPacket;
34import org.onosproject.net.packet.OutboundPacket;
Carmelo Cascone87892e22017-11-13 16:01:29 -080035import org.onosproject.net.pi.model.PiCounterId;
36import org.onosproject.net.pi.model.PiMatchFieldId;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070037import org.onosproject.net.pi.model.PiPipelineInterpreter;
Carmelo Cascone87892e22017-11-13 16:01:29 -080038import org.onosproject.net.pi.model.PiTableId;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070039import org.onosproject.net.pi.runtime.PiAction;
40import org.onosproject.net.pi.runtime.PiActionParam;
Carmelo Cascone87892e22017-11-13 16:01:29 -080041import org.onosproject.net.pi.runtime.PiControlMetadata;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070042import org.onosproject.net.pi.runtime.PiPacketOperation;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070043
44import java.nio.ByteBuffer;
45import java.util.Collection;
46import java.util.List;
47import java.util.Optional;
48
49import static java.lang.String.format;
50import static java.util.stream.Collectors.toList;
51import static org.onlab.util.ImmutableByteSequence.copyFrom;
52import static org.onlab.util.ImmutableByteSequence.fit;
53import static org.onosproject.net.PortNumber.CONTROLLER;
54import static org.onosproject.net.PortNumber.FLOOD;
55import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
56import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Carmelo Cascone87892e22017-11-13 16:01:29 -080057import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070058import static org.onosproject.pipelines.basic.BasicConstants.ACT_DROP_ID;
59import static org.onosproject.pipelines.basic.BasicConstants.ACT_NOACTION_ID;
60import static org.onosproject.pipelines.basic.BasicConstants.ACT_PRM_PORT_ID;
61import static org.onosproject.pipelines.basic.BasicConstants.ACT_SEND_TO_CPU_ID;
62import static org.onosproject.pipelines.basic.BasicConstants.ACT_SET_EGRESS_PORT_ID;
63import static org.onosproject.pipelines.basic.BasicConstants.CNT_TABLE0_ID;
64import static org.onosproject.pipelines.basic.BasicConstants.CNT_WCMP_TABLE_ID;
65import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_DST_ID;
66import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_SRC_ID;
67import static org.onosproject.pipelines.basic.BasicConstants.HDR_ETH_TYPE_ID;
68import static org.onosproject.pipelines.basic.BasicConstants.HDR_IN_PORT_ID;
Carmelo Cascone5167f322017-11-21 21:58:50 -080069import static org.onosproject.pipelines.basic.BasicConstants.HDR_IPV4_DST_ID;
70import static org.onosproject.pipelines.basic.BasicConstants.HDR_IPV4_SRC_ID;
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070071import static org.onosproject.pipelines.basic.BasicConstants.PKT_META_EGRESS_PORT_ID;
72import static org.onosproject.pipelines.basic.BasicConstants.PKT_META_INGRESS_PORT_ID;
73import static org.onosproject.pipelines.basic.BasicConstants.PORT_BITWIDTH;
74import static org.onosproject.pipelines.basic.BasicConstants.TBL_TABLE0_ID;
75import static org.onosproject.pipelines.basic.BasicConstants.TBL_WCMP_TABLE_ID;
76
77/**
78 * Interpreter implementation for basic.p4.
79 */
80public 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 Cascone87892e22017-11-13 16:01:29 -080092 private static final ImmutableBiMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP =
93 new ImmutableBiMap.Builder<Criterion.Type, PiMatchFieldId>()
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070094 .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 Cascone5167f322017-11-21 21:58:50 -080098 .put(Criterion.Type.IPV4_SRC, HDR_IPV4_SRC_ID)
99 .put(Criterion.Type.IPV4_DST, HDR_IPV4_DST_ID)
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700100 .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 Cascone87892e22017-11-13 16:01:29 -0800189 public InboundPacket mapInboundPacket(PiPacketOperation packetIn)
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700190 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 Cascone87892e22017-11-13 16:01:29 -0800202 Optional<PiControlMetadata> packetMetadata = packetIn.metadatas()
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700203 .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 Cascone87892e22017-11-13 16:01:29 -0800209 ConnectPoint receivedFrom = new ConnectPoint(packetIn.deviceId(), PortNumber.portNumber(s));
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700210 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 Cascone87892e22017-11-13 16:01:29 -0800215 PKT_META_INGRESS_PORT_ID, packetIn.deviceId(), packetIn));
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700216 }
217 }
218
219 private PiPacketOperation createPiPacketOperation(ByteBuffer data, long portNumber)
220 throws PiInterpreterException {
Carmelo Cascone87892e22017-11-13 16:01:29 -0800221 PiControlMetadata metadata = createPacketMetadata(portNumber);
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700222 return PiPacketOperation.builder()
Carmelo Cascone87892e22017-11-13 16:01:29 -0800223 .forDevice(this.data().deviceId())
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700224 .withType(PACKET_OUT)
225 .withData(copyFrom(data))
226 .withMetadatas(ImmutableList.of(metadata))
227 .build();
228 }
229
Carmelo Cascone87892e22017-11-13 16:01:29 -0800230 private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700231 try {
Carmelo Cascone87892e22017-11-13 16:01:29 -0800232 return PiControlMetadata.builder()
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700233 .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 Cascone87892e22017-11-13 16:01:29 -0800243 public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) {
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700244 return Optional.ofNullable(CRITERION_MAP.get(type));
245 }
246
247 @Override
Carmelo Cascone87892e22017-11-13 16:01:29 -0800248 public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId headerFieldId) {
Carmelo Casconeca94bcf2017-10-27 14:16:59 -0700249 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}