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