blob: a34cada6a272d9ada08d101cf40006d0387f0636 [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;
Carmelo Cascone770507f2017-09-14 20:58:04 +020026import 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.flow.instructions.Instructions;
34import org.onosproject.net.packet.DefaultInboundPacket;
35import org.onosproject.net.packet.InboundPacket;
36import org.onosproject.net.packet.OutboundPacket;
Carmelo Cascone87892e22017-11-13 16:01:29 -080037import org.onosproject.net.pi.model.PiActionId;
38import org.onosproject.net.pi.model.PiActionParamId;
39import org.onosproject.net.pi.model.PiControlMetadataId;
Carmelo Cascone87892e22017-11-13 16:01:29 -080040import org.onosproject.net.pi.model.PiMatchFieldId;
Carmelo Cascone770507f2017-09-14 20:58:04 +020041import org.onosproject.net.pi.model.PiPipelineInterpreter;
Carmelo Cascone87892e22017-11-13 16:01:29 -080042import org.onosproject.net.pi.model.PiTableId;
Carmelo Cascone770507f2017-09-14 20:58:04 +020043import org.onosproject.net.pi.runtime.PiAction;
Carmelo Cascone770507f2017-09-14 20:58:04 +020044import org.onosproject.net.pi.runtime.PiActionParam;
Carmelo Cascone87892e22017-11-13 16:01:29 -080045import org.onosproject.net.pi.runtime.PiControlMetadata;
Carmelo Cascone770507f2017-09-14 20:58:04 +020046import org.onosproject.net.pi.runtime.PiPacketOperation;
Carmelo Cascone770507f2017-09-14 20:58:04 +020047
48import java.nio.ByteBuffer;
49import java.util.Collection;
50import java.util.List;
51import java.util.Optional;
52
53import static java.lang.String.format;
54import static java.util.stream.Collectors.toList;
55import static org.onlab.util.ImmutableByteSequence.copyFrom;
Carmelo Cascone770507f2017-09-14 20:58:04 +020056import static org.onosproject.net.PortNumber.CONTROLLER;
57import static org.onosproject.net.PortNumber.FLOOD;
58import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
Carmelo Cascone87892e22017-11-13 16:01:29 -080059import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT;
Carmelo Cascone770507f2017-09-14 20:58:04 +020060
61/**
62 * Implementation of a PI interpreter for the main.p4 program.
63 */
64public final class PipelineInterpreterImpl extends AbstractHandlerBehaviour implements PiPipelineInterpreter {
65
Kevin Chuangd79bfd02017-11-14 10:34:35 -080066 private static final String DOT = ".";
67 private static final String HDR = "hdr";
Carmelo Cascone770507f2017-09-14 20:58:04 +020068 private static final String TABLE0 = "table0";
69 private static final String IP_PROTO_FILTER_TABLE = "ip_proto_filter_table";
70 private static final String SEND_TO_CPU = "send_to_cpu";
71 private static final String PORT = "port";
72 private static final String DROP = "_drop";
73 private static final String SET_EGRESS_PORT = "set_egress_port";
74 private static final String EGRESS_PORT = "egress_port";
75 private static final String INGRESS_PORT = "ingress_port";
76 private static final String ETHERNET = "ethernet";
77 private static final String STANDARD_METADATA = "standard_metadata";
78 private static final int PORT_FIELD_BITWIDTH = 9;
79
Kevin Chuangd79bfd02017-11-14 10:34:35 -080080 private static final PiMatchFieldId INGRESS_PORT_ID = PiMatchFieldId.of(STANDARD_METADATA + DOT + "ingress_port");
81 private static final PiMatchFieldId ETH_DST_ID = PiMatchFieldId.of(HDR + DOT + ETHERNET + DOT + "dst_addr");
82 private static final PiMatchFieldId ETH_SRC_ID = PiMatchFieldId.of(HDR + DOT + ETHERNET + DOT + "src_addr");
83 private static final PiMatchFieldId ETH_TYPE_ID = PiMatchFieldId.of(HDR + DOT + ETHERNET + DOT + "ether_type");
Carmelo Cascone770507f2017-09-14 20:58:04 +020084 private static final PiTableId TABLE0_ID = PiTableId.of(TABLE0);
85 private static final PiTableId IP_PROTO_FILTER_TABLE_ID = PiTableId.of(IP_PROTO_FILTER_TABLE);
86
87 private static final BiMap<Integer, PiTableId> TABLE_MAP = ImmutableBiMap.of(
88 0, TABLE0_ID,
89 1, IP_PROTO_FILTER_TABLE_ID);
90
Carmelo Cascone87892e22017-11-13 16:01:29 -080091 private static final BiMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP =
92 new ImmutableBiMap.Builder<Criterion.Type, PiMatchFieldId>()
Carmelo Cascone770507f2017-09-14 20:58:04 +020093 .put(Criterion.Type.IN_PORT, INGRESS_PORT_ID)
94 .put(Criterion.Type.ETH_DST, ETH_DST_ID)
95 .put(Criterion.Type.ETH_SRC, ETH_SRC_ID)
96 .put(Criterion.Type.ETH_TYPE, ETH_TYPE_ID)
97 .build();
98
99 @Override
100 public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId)
101 throws PiInterpreterException {
102
103 if (treatment.allInstructions().size() == 0) {
104 // No instructions means drop for us.
105 return PiAction.builder()
106 .withId(PiActionId.of(DROP))
107 .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 // Get the first and only instruction.
114 Instruction instruction = treatment.allInstructions().get(0);
115
116 switch (instruction.type()) {
117 case OUTPUT:
118 // We understand only instructions of type OUTPUT.
119 Instructions.OutputInstruction outInstruction = (Instructions.OutputInstruction) instruction;
120 PortNumber port = outInstruction.port();
121 if (!port.isLogical()) {
122 return PiAction.builder()
123 .withId(PiActionId.of(SET_EGRESS_PORT))
124 .withParameter(new PiActionParam(PiActionParamId.of(PORT), copyFrom(port.toLong())))
125 .build();
126 } else if (port.equals(CONTROLLER)) {
127 return PiAction.builder()
128 .withId(PiActionId.of(SEND_TO_CPU))
129 .build();
130 } else {
131 throw new PiInterpreterException(format("Output on logical port '%s' not supported", port));
132 }
133 default:
134 throw new PiInterpreterException(format("Instruction of type '%s' not supported", instruction.type()));
135 }
136 }
137
138 @Override
Carmelo Cascone770507f2017-09-14 20:58:04 +0200139 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
140 throws PiInterpreterException {
141
142 TrafficTreatment treatment = packet.treatment();
143
144 // We support only packet-out with OUTPUT instructions.
145 List<Instructions.OutputInstruction> outInstructions = treatment.allInstructions().stream()
146 .filter(i -> i.type().equals(OUTPUT))
147 .map(i -> (Instructions.OutputInstruction) i)
148 .collect(toList());
149
150 if (treatment.allInstructions().size() != outInstructions.size()) {
151 // There are other instructions that are not of type OUTPUT.
152 throw new PiInterpreterException("Treatment not supported: " + treatment);
153 }
154
155 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
156
157 for (Instructions.OutputInstruction outInst : outInstructions) {
158 if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
159 throw new PiInterpreterException(format("Output on logical port '%s' not supported", outInst.port()));
160 } else if (outInst.port().equals(FLOOD)) {
161 // Since main.p4 does not support flooding, we create a packet operation for each switch port.
162 DeviceService deviceService = handler().get(DeviceService.class);
163 for (Port port : deviceService.getPorts(packet.sendThrough())) {
164 builder.add(createPiPacketOperation(packet.data(), port.number().toLong()));
165 }
166 } else {
167 builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong()));
168 }
169 }
170 return builder.build();
171 }
172
173 @Override
Carmelo Cascone87892e22017-11-13 16:01:29 -0800174 public InboundPacket mapInboundPacket(PiPacketOperation packetIn)
Carmelo Cascone770507f2017-09-14 20:58:04 +0200175 throws PiInterpreterException {
176 // 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 -0700177 Ethernet ethPkt;
178 try {
179 ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0, packetIn.data().size());
180 } catch (DeserializationException dex) {
181 throw new PiInterpreterException(dex.getMessage());
182 }
Carmelo Cascone770507f2017-09-14 20:58:04 +0200183
184 // Returns the ingress port packet metadata.
Carmelo Cascone87892e22017-11-13 16:01:29 -0800185 Optional<PiControlMetadata> packetMetadata = packetIn.metadatas().stream()
186 .filter(metadata -> metadata.id().toString().equals(INGRESS_PORT))
Carmelo Cascone770507f2017-09-14 20:58:04 +0200187 .findFirst();
188
189 if (packetMetadata.isPresent()) {
190 ImmutableByteSequence portByteSequence = packetMetadata.get().value();
191 short s = portByteSequence.asReadOnlyBuffer().getShort();
Carmelo Cascone87892e22017-11-13 16:01:29 -0800192 ConnectPoint receivedFrom = new ConnectPoint(packetIn.deviceId(), PortNumber.portNumber(s));
Carmelo Cascone770507f2017-09-14 20:58:04 +0200193 return new DefaultInboundPacket(receivedFrom, ethPkt, packetIn.data().asReadOnlyBuffer());
194 } else {
195 throw new PiInterpreterException(format(
Carmelo Cascone87892e22017-11-13 16:01:29 -0800196 "Missing metadata '%s' in packet-in received from '%s': %s",
197 INGRESS_PORT, packetIn.deviceId(), packetIn));
Carmelo Cascone770507f2017-09-14 20:58:04 +0200198 }
199 }
200
201 private PiPacketOperation createPiPacketOperation(ByteBuffer data, long portNumber) throws PiInterpreterException {
Carmelo Cascone87892e22017-11-13 16:01:29 -0800202 PiControlMetadata metadata = createControlMetadata(portNumber);
Carmelo Cascone770507f2017-09-14 20:58:04 +0200203 return PiPacketOperation.builder()
Carmelo Cascone87892e22017-11-13 16:01:29 -0800204 .forDevice(this.data().deviceId())
Carmelo Cascone770507f2017-09-14 20:58:04 +0200205 .withType(PACKET_OUT)
206 .withData(copyFrom(data))
207 .withMetadatas(ImmutableList.of(metadata))
208 .build();
209 }
210
Carmelo Cascone87892e22017-11-13 16:01:29 -0800211 private PiControlMetadata createControlMetadata(long portNumber) throws PiInterpreterException {
Carmelo Cascone770507f2017-09-14 20:58:04 +0200212 try {
Carmelo Cascone87892e22017-11-13 16:01:29 -0800213 return PiControlMetadata.builder()
214 .withId(PiControlMetadataId.of(EGRESS_PORT))
Carmelo Cascone8a571af2018-04-06 23:17:04 -0700215 .withValue(copyFrom(portNumber).fit(PORT_FIELD_BITWIDTH))
Carmelo Cascone770507f2017-09-14 20:58:04 +0200216 .build();
217 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
218 throw new PiInterpreterException(format("Port number %d too big, %s", portNumber, e.getMessage()));
219 }
220 }
221
222 @Override
Carmelo Cascone87892e22017-11-13 16:01:29 -0800223 public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) {
Carmelo Cascone770507f2017-09-14 20:58:04 +0200224 return Optional.ofNullable(CRITERION_MAP.get(type));
225 }
226
227 @Override
Carmelo Cascone87892e22017-11-13 16:01:29 -0800228 public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId headerFieldId) {
Carmelo Cascone770507f2017-09-14 20:58:04 +0200229 return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId));
230 }
231
232 @Override
233 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
234 return Optional.ofNullable(TABLE_MAP.get(flowRuleTableId));
235 }
236
237 @Override
238 public Optional<Integer> mapPiTableId(PiTableId piTableId) {
239 return Optional.ofNullable(TABLE_MAP.inverse().get(piTableId));
240 }
241}