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