blob: 1a7b2ba0a82882be171003e5290cff204e64918f [file] [log] [blame]
Yi Tsengfa4a1c72017-11-03 10:22:38 -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
Carmelo Cascone356ab8b2019-09-25 01:02:53 -070017package org.onosproject.pipelines.fabric.impl.behaviour;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070018
Yi Tsengfa4a1c72017-11-03 10:22:38 -070019import com.google.common.collect.ImmutableList;
Yi Tseng1d842672017-11-28 16:06:52 -080020import com.google.common.collect.ImmutableMap;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070021import com.google.common.collect.ImmutableSet;
22import org.onlab.packet.DeserializationException;
23import 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;
Daniele Morof51d0c12019-07-30 10:43:10 -070030import org.onosproject.net.driver.DriverHandler;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070031import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.net.flow.criteria.Criterion;
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;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070037import org.onosproject.net.pi.model.PiMatchFieldId;
38import org.onosproject.net.pi.model.PiPipelineInterpreter;
39import org.onosproject.net.pi.model.PiTableId;
40import org.onosproject.net.pi.runtime.PiAction;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080041import org.onosproject.net.pi.runtime.PiPacketMetadata;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070042import org.onosproject.net.pi.runtime.PiPacketOperation;
Carmelo Cascone2102bfb2020-12-04 16:54:24 -080043import org.onosproject.pipelines.fabric.FabricConstants;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070044
45import java.nio.ByteBuffer;
46import java.util.Collection;
47import java.util.List;
48import java.util.Optional;
49import java.util.Set;
50
51import static java.lang.String.format;
52import static java.util.stream.Collectors.toList;
53import static org.onlab.util.ImmutableByteSequence.copyFrom;
Carmelo Cascone6880ba62018-09-06 00:04:34 -070054import static org.onosproject.net.PortNumber.CONTROLLER;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070055import static org.onosproject.net.PortNumber.FLOOD;
Carmelo Cascone2388cc12021-05-26 19:30:30 +020056import static org.onosproject.net.PortNumber.TABLE;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070057import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
58import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT;
59
60/**
61 * Interpreter for fabric pipeline.
62 */
Carmelo Casconeb5324e72018-11-25 02:26:32 -080063public class FabricInterpreter extends AbstractFabricHandlerBehavior
Yi Tsengfa4a1c72017-11-03 10:22:38 -070064 implements PiPipelineInterpreter {
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020065
Carmelo Casconeb5324e72018-11-25 02:26:32 -080066 private static final int PORT_BITWIDTH = 9;
Carmelo Cascone2388cc12021-05-26 19:30:30 +020067 public static final byte[] ONE = new byte[]{1};
68 public static final byte[] ZERO = new byte[]{0};
Carmelo Cascone6880ba62018-09-06 00:04:34 -070069
Carmelo Casconeb5324e72018-11-25 02:26:32 -080070 // Group tables by control block.
Carmelo Casconeb5324e72018-11-25 02:26:32 -080071 private static final Set<PiTableId> FORWARDING_CTRL_TBLS = ImmutableSet.of(
72 FabricConstants.FABRIC_INGRESS_FORWARDING_MPLS,
73 FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4,
74 FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V6,
75 FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING);
Wailok Shumfb7e7872021-06-18 17:30:08 +080076 private static final Set<PiTableId> PRE_NEXT_CTRL_TBLS = ImmutableSet.of(
77 FabricConstants.FABRIC_INGRESS_PRE_NEXT_NEXT_MPLS,
78 FabricConstants.FABRIC_INGRESS_PRE_NEXT_NEXT_VLAN);
Carmelo Casconeb5324e72018-11-25 02:26:32 -080079 private static final Set<PiTableId> ACL_CTRL_TBLS = ImmutableSet.of(
80 FabricConstants.FABRIC_INGRESS_ACL_ACL);
81 private static final Set<PiTableId> NEXT_CTRL_TBLS = ImmutableSet.of(
Carmelo Casconeb5324e72018-11-25 02:26:32 -080082 FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE,
Carmelo Cascone45cc0862018-11-26 11:50:41 -080083 FabricConstants.FABRIC_INGRESS_NEXT_HASHED,
84 FabricConstants.FABRIC_INGRESS_NEXT_XCONNECT);
Carmelo Casconeb5324e72018-11-25 02:26:32 -080085 private static final Set<PiTableId> E_NEXT_CTRL_TBLS = ImmutableSet.of(
86 FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN);
Yi Tsengfa4a1c72017-11-03 10:22:38 -070087
Yi Tseng1d842672017-11-28 16:06:52 -080088 private static final ImmutableMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP =
89 ImmutableMap.<Criterion.Type, PiMatchFieldId>builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -080090 .put(Criterion.Type.IN_PORT, FabricConstants.HDR_IG_PORT)
91 .put(Criterion.Type.ETH_DST, FabricConstants.HDR_ETH_DST)
92 .put(Criterion.Type.ETH_SRC, FabricConstants.HDR_ETH_SRC)
93 .put(Criterion.Type.ETH_DST_MASKED, FabricConstants.HDR_ETH_DST)
94 .put(Criterion.Type.ETH_SRC_MASKED, FabricConstants.HDR_ETH_SRC)
95 .put(Criterion.Type.ETH_TYPE, FabricConstants.HDR_ETH_TYPE)
Yi Tseng43ee7e82018-04-12 16:37:34 +080096 .put(Criterion.Type.MPLS_LABEL, FabricConstants.HDR_MPLS_LABEL)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080097 .put(Criterion.Type.VLAN_VID, FabricConstants.HDR_VLAN_ID)
Daniele Moro7c3a0022019-07-12 13:38:34 -070098 .put(Criterion.Type.INNER_VLAN_VID, FabricConstants.HDR_INNER_VLAN_ID)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080099 .put(Criterion.Type.IPV4_DST, FabricConstants.HDR_IPV4_DST)
100 .put(Criterion.Type.IPV4_SRC, FabricConstants.HDR_IPV4_SRC)
101 .put(Criterion.Type.IPV6_DST, FabricConstants.HDR_IPV6_DST)
102 .put(Criterion.Type.IP_PROTO, FabricConstants.HDR_IP_PROTO)
103 .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HDR_ICMP_TYPE)
104 .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HDR_ICMP_CODE)
Daniele Moro59020df2019-08-05 15:37:45 -0700105 .put(Criterion.Type.UDP_DST, FabricConstants.HDR_L4_DPORT)
106 .put(Criterion.Type.UDP_SRC, FabricConstants.HDR_L4_SPORT)
107 .put(Criterion.Type.UDP_DST_MASKED, FabricConstants.HDR_L4_DPORT)
108 .put(Criterion.Type.UDP_SRC_MASKED, FabricConstants.HDR_L4_SPORT)
109 .put(Criterion.Type.TCP_DST, FabricConstants.HDR_L4_DPORT)
110 .put(Criterion.Type.TCP_SRC, FabricConstants.HDR_L4_SPORT)
111 .put(Criterion.Type.TCP_DST_MASKED, FabricConstants.HDR_L4_DPORT)
112 .put(Criterion.Type.TCP_SRC_MASKED, FabricConstants.HDR_L4_SPORT)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700113 .build();
114
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800115 private static final PiAction NOP = PiAction.builder()
116 .withId(FabricConstants.NOP).build();
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700117
118 private static final ImmutableMap<PiTableId, PiAction> DEFAULT_ACTIONS =
119 ImmutableMap.<PiTableId, PiAction>builder()
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800120 .put(FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4, NOP)
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700121 .build();
122
Daniele Morof51d0c12019-07-30 10:43:10 -0700123 private FabricTreatmentInterpreter treatmentInterpreter;
124
125 /**
126 * Creates a new instance of this behavior with the given capabilities.
127 *
128 * @param capabilities capabilities
129 */
130 public FabricInterpreter(FabricCapabilities capabilities) {
131 super(capabilities);
132 instantiateTreatmentInterpreter();
133 }
134
135 /**
136 * Create a new instance of this behaviour. Used by the abstract projectable
137 * model (i.e., {@link org.onosproject.net.Device#as(Class)}.
138 */
139 public FabricInterpreter() {
140 super();
141 }
142
143 private void instantiateTreatmentInterpreter() {
144 this.treatmentInterpreter = new FabricTreatmentInterpreter(this.capabilities);
145 }
146
147 @Override
148 public void setHandler(DriverHandler handler) {
149 super.setHandler(handler);
150 instantiateTreatmentInterpreter();
151 }
152
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700153 @Override
154 public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) {
155 return Optional.ofNullable(CRITERION_MAP.get(type));
156 }
157
158 @Override
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700159 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800160 // The only use case for Index ID->PiTableId is when using the single
161 // table pipeliner. fabric.p4 is never used with such pipeliner.
162 return Optional.empty();
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700163 }
164
165 @Override
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700166 public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId)
167 throws PiInterpreterException {
Wailok Shumfb7e7872021-06-18 17:30:08 +0800168 if (FORWARDING_CTRL_TBLS.contains(piTableId)) {
Daniele Morof51d0c12019-07-30 10:43:10 -0700169 return treatmentInterpreter.mapForwardingTreatment(treatment, piTableId);
Wailok Shumfb7e7872021-06-18 17:30:08 +0800170 } else if (PRE_NEXT_CTRL_TBLS.contains(piTableId)) {
171 return treatmentInterpreter.mapPreNextTreatment(treatment, piTableId);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800172 } else if (ACL_CTRL_TBLS.contains(piTableId)) {
Daniele Morof51d0c12019-07-30 10:43:10 -0700173 return treatmentInterpreter.mapAclTreatment(treatment, piTableId);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700174 } else if (NEXT_CTRL_TBLS.contains(piTableId)) {
Daniele Morof51d0c12019-07-30 10:43:10 -0700175 return treatmentInterpreter.mapNextTreatment(treatment, piTableId);
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800176 } else if (E_NEXT_CTRL_TBLS.contains(piTableId)) {
Daniele Morof51d0c12019-07-30 10:43:10 -0700177 return treatmentInterpreter.mapEgressNextTreatment(treatment, piTableId);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700178 } else {
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800179 throw new PiInterpreterException(format(
180 "Treatment mapping not supported for table '%s'", piTableId));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700181 }
182 }
183
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800184 private PiPacketOperation createPiPacketOperation(
Carmelo Cascone2388cc12021-05-26 19:30:30 +0200185 ByteBuffer data, long portNumber, boolean doForwarding)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700186 throws PiInterpreterException {
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700187 return PiPacketOperation.builder()
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700188 .withType(PACKET_OUT)
189 .withData(copyFrom(data))
Carmelo Cascone2388cc12021-05-26 19:30:30 +0200190 .withMetadatas(createPacketMetadata(portNumber, doForwarding))
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700191 .build();
192 }
193
Carmelo Cascone2388cc12021-05-26 19:30:30 +0200194 private Collection<PiPacketMetadata> createPacketMetadata(
195 long portNumber, boolean doForwarding)
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800196 throws PiInterpreterException {
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700197 try {
Carmelo Cascone2388cc12021-05-26 19:30:30 +0200198 ImmutableList.Builder<PiPacketMetadata> builder = ImmutableList.builder();
Carmelo Cascone2a308ff2021-06-01 18:31:57 -0700199 // We have observed an issue with p4lang/PI and BMv2 where in
200 // presence of multiple metadata fields, the PI implementation for
201 // BMv2 provides an erroneous serialization of the packet-out
202 // header, an hence affects the parsing/forwarding behavior. As a
203 // workaround, since we cannot control the order of fields in the
204 // p4runtime.PacketOut message, we modify the interpreter to only
205 // add one field, egress_port or do_forwarding. Both fields
206 // are treated as mutually exclusive by the P4 pipeline, so the
207 // operation is safe. This is against the P4Runtime spec (all fields
208 // should be provided), but supported by bmv2 (unset fields are
209 // initialized to zero).
210 if (portNumber >= 0) {
211 // 0 is a valid port number.
212 builder.add(PiPacketMetadata.builder()
213 .withId(FabricConstants.EGRESS_PORT)
214 .withValue(copyFrom(portNumber)
215 .fit(PORT_BITWIDTH))
216 .build());
217 }
218 if (doForwarding) {
219 builder.add(PiPacketMetadata.builder()
220 .withId(FabricConstants.DO_FORWARDING)
221 .withValue(copyFrom(ONE))
222 .build());
223 }
Carmelo Cascone2388cc12021-05-26 19:30:30 +0200224 return builder.build();
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700225 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
226 throw new PiInterpreterException(format(
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800227 "Port number '%d' too big, %s", portNumber, e.getMessage()));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700228 }
229 }
230
231 @Override
232 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
233 throws PiInterpreterException {
234 DeviceId deviceId = packet.sendThrough();
235 TrafficTreatment treatment = packet.treatment();
236
237 // fabric.p4 supports only OUTPUT instructions.
238 List<Instructions.OutputInstruction> outInstructions = treatment
239 .allInstructions()
240 .stream()
241 .filter(i -> i.type().equals(OUTPUT))
242 .map(i -> (Instructions.OutputInstruction) i)
243 .collect(toList());
244
245 if (treatment.allInstructions().size() != outInstructions.size()) {
246 // There are other instructions that are not of type OUTPUT.
247 throw new PiInterpreterException("Treatment not supported: " + treatment);
248 }
249
250 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
251 for (Instructions.OutputInstruction outInst : outInstructions) {
Carmelo Cascone2388cc12021-05-26 19:30:30 +0200252 if (outInst.port().equals(TABLE)) {
253 // Logical port. Forward using the switch tables like a regular packet.
Carmelo Cascone2a308ff2021-06-01 18:31:57 -0700254 builder.add(createPiPacketOperation(packet.data(), -1, true));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700255 } else if (outInst.port().equals(FLOOD)) {
Carmelo Cascone2388cc12021-05-26 19:30:30 +0200256 // Logical port. Create a packet operation for each switch port.
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700257 final DeviceService deviceService = handler().get(DeviceService.class);
258 for (Port port : deviceService.getPorts(packet.sendThrough())) {
Carmelo Cascone2388cc12021-05-26 19:30:30 +0200259 builder.add(createPiPacketOperation(packet.data(), port.number().toLong(), false));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700260 }
Carmelo Cascone2388cc12021-05-26 19:30:30 +0200261 } else if (outInst.port().isLogical()) {
262 throw new PiInterpreterException(format(
263 "Output on logical port '%s' not supported", outInst.port()));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700264 } else {
Carmelo Cascone2388cc12021-05-26 19:30:30 +0200265 // Send as-is to given port bypassing all switch tables.
266 builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong(), false));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700267 }
268 }
269 return builder.build();
270 }
271
272 @Override
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800273 public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId) throws PiInterpreterException {
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700274 // Assuming that the packet is ethernet, which is fine since fabric.p4
275 // can deparse only ethernet packets.
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700276 Ethernet ethPkt;
277 try {
278 ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0,
279 packetIn.data().size());
280 } catch (DeserializationException dex) {
281 throw new PiInterpreterException(dex.getMessage());
282 }
283
284 // Returns the ingress port packet metadata.
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800285 Optional<PiPacketMetadata> packetMetadata = packetIn.metadatas()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800286 .stream().filter(m -> m.id().equals(FabricConstants.INGRESS_PORT))
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700287 .findFirst();
288
289 if (packetMetadata.isPresent()) {
290 ImmutableByteSequence portByteSequence = packetMetadata.get().value();
291 short s = portByteSequence.asReadOnlyBuffer().getShort();
292 ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
293 ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
294 return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
295 } else {
296 throw new PiInterpreterException(format(
297 "Missing metadata '%s' in packet-in received from '%s': %s",
Yi Tseng43ee7e82018-04-12 16:37:34 +0800298 FabricConstants.INGRESS_PORT, deviceId, packetIn));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700299 }
300 }
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700301
302 @Override
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700303 public Optional<PiAction> getOriginalDefaultAction(PiTableId tableId) {
304 return Optional.ofNullable(DEFAULT_ACTIONS.get(tableId));
305 }
306
307 @Override
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700308 public Optional<Integer> mapLogicalPortNumber(PortNumber port) {
309 if (!port.equals(CONTROLLER)) {
310 return Optional.empty();
311 }
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800312 return capabilities.cpuPort();
Carmelo Cascone6880ba62018-09-06 00:04:34 -0700313 }
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700314}