blob: 3b0a2f84c099945fb4c4f6cb2090ac69102ba4da [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
17package org.onosproject.pipelines.fabric;
18
19import com.google.common.collect.ImmutableBiMap;
20import com.google.common.collect.ImmutableList;
Yi Tseng1d842672017-11-28 16:06:52 -080021import com.google.common.collect.ImmutableMap;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070022import com.google.common.collect.ImmutableSet;
23import org.onlab.packet.DeserializationException;
24import org.onlab.packet.Ethernet;
25import org.onlab.util.ImmutableByteSequence;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import org.onosproject.net.Port;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.device.DeviceService;
31import org.onosproject.net.driver.AbstractHandlerBehaviour;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.flow.criteria.Criterion;
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;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070038import org.onosproject.net.pi.model.PiMatchFieldId;
39import org.onosproject.net.pi.model.PiPipelineInterpreter;
40import org.onosproject.net.pi.model.PiTableId;
41import org.onosproject.net.pi.runtime.PiAction;
42import org.onosproject.net.pi.runtime.PiControlMetadata;
43import org.onosproject.net.pi.runtime.PiPacketOperation;
44
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;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070054import static org.onosproject.net.PortNumber.FLOOD;
55import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
56import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT;
57
58/**
59 * Interpreter for fabric pipeline.
60 */
61public class FabricInterpreter extends AbstractHandlerBehaviour
62 implements PiPipelineInterpreter {
63 private static final ImmutableBiMap<Integer, PiTableId> TABLE_ID_MAP =
64 ImmutableBiMap.<Integer, PiTableId>builder()
65 // Filtering
66 .put(0, FabricConstants.TBL_INGRESS_PORT_VLAN_ID)
67 .put(1, FabricConstants.TBL_FWD_CLASSIFIER_ID)
68 // Forwarding
69 .put(2, FabricConstants.TBL_MPLS_ID)
70 .put(3, FabricConstants.TBL_UNICAST_V4_ID)
71 .put(4, FabricConstants.TBL_UNICAST_V6_ID)
72 .put(5, FabricConstants.TBL_MULTICAST_V4_ID)
73 .put(6, FabricConstants.TBL_MULTICAST_V6_ID)
74 .put(7, FabricConstants.TBL_BRIDGING_ID)
75 .put(8, FabricConstants.TBL_ACL_ID)
76 // Next
Yi Tsengf55eaa82017-11-29 15:51:28 -080077 .put(9, FabricConstants.TBL_SIMPLE_ID)
78 .put(10, FabricConstants.TBL_HASHED_ID)
79 .put(11, FabricConstants.TBL_BROADCAST_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070080 .build();
81
82 private static final Set<PiTableId> FILTERING_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_INGRESS_PORT_VLAN_ID,
83 FabricConstants.TBL_FWD_CLASSIFIER_ID);
84 private static final Set<PiTableId> FORWARDING_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_MPLS_ID,
85 FabricConstants.TBL_UNICAST_V4_ID,
86 FabricConstants.TBL_UNICAST_V6_ID,
87 FabricConstants.TBL_MULTICAST_V4_ID,
88 FabricConstants.TBL_MULTICAST_V6_ID,
89 FabricConstants.TBL_BRIDGING_ID,
90 FabricConstants.TBL_ACL_ID);
Yi Tsengf55eaa82017-11-29 15:51:28 -080091 private static final Set<PiTableId> NEXT_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_SIMPLE_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -070092 FabricConstants.TBL_HASHED_ID,
93 FabricConstants.TBL_BROADCAST_ID);
94
Yi Tseng1d842672017-11-28 16:06:52 -080095 private static final ImmutableMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP =
96 ImmutableMap.<Criterion.Type, PiMatchFieldId>builder()
Yi Tsengfa4a1c72017-11-03 10:22:38 -070097 .put(Criterion.Type.IN_PORT, FabricConstants.HF_STANDARD_METADATA_INGRESS_PORT_ID)
98 .put(Criterion.Type.ETH_DST, FabricConstants.HF_ETHERNET_DST_ADDR_ID)
99 .put(Criterion.Type.ETH_SRC, FabricConstants.HF_ETHERNET_SRC_ADDR_ID)
Yi Tseng1d842672017-11-28 16:06:52 -0800100 .put(Criterion.Type.ETH_TYPE, FabricConstants.HF_FABRIC_METADATA_ORIGINAL_ETHER_TYPE_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700101 .put(Criterion.Type.MPLS_LABEL, FabricConstants.HF_MPLS_LABEL_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700102 .put(Criterion.Type.VLAN_VID, FabricConstants.HF_VLAN_TAG_VLAN_ID_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700103 .put(Criterion.Type.IPV4_DST, FabricConstants.HF_IPV4_DST_ADDR_ID)
104 .put(Criterion.Type.IPV4_SRC, FabricConstants.HF_IPV4_SRC_ADDR_ID)
105 .put(Criterion.Type.IPV6_DST, FabricConstants.HF_IPV6_DST_ADDR_ID)
Yi Tseng1d842672017-11-28 16:06:52 -0800106 .put(Criterion.Type.TCP_SRC, FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID)
107 .put(Criterion.Type.TCP_DST, FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID)
108 .put(Criterion.Type.UDP_SRC, FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID)
109 .put(Criterion.Type.UDP_DST, FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700110 .put(Criterion.Type.IP_PROTO, FabricConstants.HF_FABRIC_METADATA_IP_PROTO_ID)
111 .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HF_ICMP_ICMP_TYPE_ID)
112 .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HF_ICMP_ICMP_CODE_ID)
113 .build();
114
Yi Tseng1d842672017-11-28 16:06:52 -0800115 private static final ImmutableMap<PiMatchFieldId, Criterion.Type> INVERSE_CRITERION_MAP =
116 ImmutableMap.<PiMatchFieldId, Criterion.Type>builder()
117 .put(FabricConstants.HF_STANDARD_METADATA_INGRESS_PORT_ID, Criterion.Type.IN_PORT)
118 .put(FabricConstants.HF_ETHERNET_DST_ADDR_ID, Criterion.Type.ETH_DST)
119 .put(FabricConstants.HF_ETHERNET_SRC_ADDR_ID, Criterion.Type.ETH_SRC)
120 .put(FabricConstants.HF_FABRIC_METADATA_ORIGINAL_ETHER_TYPE_ID, Criterion.Type.ETH_TYPE)
Yi Tseng1d842672017-11-28 16:06:52 -0800121 .put(FabricConstants.HF_MPLS_LABEL_ID, Criterion.Type.MPLS_LABEL)
122 .put(FabricConstants.HF_VLAN_TAG_VLAN_ID_ID, Criterion.Type.VLAN_VID)
123 .put(FabricConstants.HF_IPV4_DST_ADDR_ID, Criterion.Type.IPV4_DST)
124 .put(FabricConstants.HF_IPV4_SRC_ADDR_ID, Criterion.Type.IPV4_SRC)
125 .put(FabricConstants.HF_IPV6_DST_ADDR_ID, Criterion.Type.IPV6_DST)
Yi Tseng1d842672017-11-28 16:06:52 -0800126 // FIXME: might be incorrect if we inverse the map....
127 .put(FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID, Criterion.Type.UDP_SRC)
128 .put(FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID, Criterion.Type.UDP_DST)
Yi Tseng1d842672017-11-28 16:06:52 -0800129 .put(FabricConstants.HF_FABRIC_METADATA_IP_PROTO_ID, Criterion.Type.IP_PROTO)
130 .put(FabricConstants.HF_ICMP_ICMP_TYPE_ID, Criterion.Type.ICMPV6_TYPE)
131 .put(FabricConstants.HF_ICMP_ICMP_CODE_ID, Criterion.Type.ICMPV6_CODE)
132 .build();
133
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700134 @Override
135 public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) {
136 return Optional.ofNullable(CRITERION_MAP.get(type));
137 }
138
139 @Override
140 public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId fieldId) {
Yi Tseng1d842672017-11-28 16:06:52 -0800141 return Optional.ofNullable(INVERSE_CRITERION_MAP.get(fieldId));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700142 }
143
144 @Override
145 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
146 return Optional.ofNullable(TABLE_ID_MAP.get(flowRuleTableId));
147 }
148
149 @Override
150 public Optional<Integer> mapPiTableId(PiTableId piTableId) {
151 return Optional.ofNullable(TABLE_ID_MAP.inverse().get(piTableId));
152 }
153
154 @Override
155 public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId)
156 throws PiInterpreterException {
157
158 if (FILTERING_CTRL_TBLS.contains(piTableId)) {
159 return FabricTreatmentInterpreter.mapFilteringTreatment(treatment);
160 } else if (FORWARDING_CTRL_TBLS.contains(piTableId)) {
161 return FabricTreatmentInterpreter.mapForwardingTreatment(treatment);
162 } else if (NEXT_CTRL_TBLS.contains(piTableId)) {
163 return FabricTreatmentInterpreter.mapNextTreatment(treatment);
164 } else {
165 throw new PiInterpreterException(String.format("Table %s unsupported", piTableId));
166 }
167 }
168
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700169 private PiPacketOperation createPiPacketOperation(DeviceId deviceId, ByteBuffer data, long portNumber)
170 throws PiInterpreterException {
171 PiControlMetadata metadata = createPacketMetadata(portNumber);
172 return PiPacketOperation.builder()
173 .forDevice(deviceId)
174 .withType(PACKET_OUT)
175 .withData(copyFrom(data))
176 .withMetadatas(ImmutableList.of(metadata))
177 .build();
178 }
179
180 private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
181 try {
182 return PiControlMetadata.builder()
183 .withId(FabricConstants.CTRL_META_EGRESS_PORT_ID)
Carmelo Cascone8a571af2018-04-06 23:17:04 -0700184 .withValue(copyFrom(portNumber).fit(FabricConstants.PORT_BITWIDTH))
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700185 .build();
186 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
187 throw new PiInterpreterException(format(
188 "Port number %d too big, %s", portNumber, e.getMessage()));
189 }
190 }
191
192 @Override
193 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
194 throws PiInterpreterException {
195 DeviceId deviceId = packet.sendThrough();
196 TrafficTreatment treatment = packet.treatment();
197
198 // fabric.p4 supports only OUTPUT instructions.
199 List<Instructions.OutputInstruction> outInstructions = treatment
200 .allInstructions()
201 .stream()
202 .filter(i -> i.type().equals(OUTPUT))
203 .map(i -> (Instructions.OutputInstruction) i)
204 .collect(toList());
205
206 if (treatment.allInstructions().size() != outInstructions.size()) {
207 // There are other instructions that are not of type OUTPUT.
208 throw new PiInterpreterException("Treatment not supported: " + treatment);
209 }
210
211 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
212 for (Instructions.OutputInstruction outInst : outInstructions) {
213 if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
214 throw new PiInterpreterException(format(
215 "Output on logical port '%s' not supported", outInst.port()));
216 } else if (outInst.port().equals(FLOOD)) {
217 // Since fabric.p4 does not support flooding, we create a packet
218 // operation for each switch port.
219 final DeviceService deviceService = handler().get(DeviceService.class);
220 for (Port port : deviceService.getPorts(packet.sendThrough())) {
221 builder.add(createPiPacketOperation(deviceId, packet.data(), port.number().toLong()));
222 }
223 } else {
224 builder.add(createPiPacketOperation(deviceId, packet.data(), outInst.port().toLong()));
225 }
226 }
227 return builder.build();
228 }
229
230 @Override
231 public InboundPacket mapInboundPacket(PiPacketOperation packetIn) throws PiInterpreterException {
232 // Assuming that the packet is ethernet, which is fine since fabric.p4
233 // can deparse only ethernet packets.
234 DeviceId deviceId = packetIn.deviceId();
235 Ethernet ethPkt;
236 try {
237 ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0,
238 packetIn.data().size());
239 } catch (DeserializationException dex) {
240 throw new PiInterpreterException(dex.getMessage());
241 }
242
243 // Returns the ingress port packet metadata.
244 Optional<PiControlMetadata> packetMetadata = packetIn.metadatas()
245 .stream().filter(m -> m.id().equals(FabricConstants.CTRL_META_INGRESS_PORT_ID))
246 .findFirst();
247
248 if (packetMetadata.isPresent()) {
249 ImmutableByteSequence portByteSequence = packetMetadata.get().value();
250 short s = portByteSequence.asReadOnlyBuffer().getShort();
251 ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
252 ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
253 return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
254 } else {
255 throw new PiInterpreterException(format(
256 "Missing metadata '%s' in packet-in received from '%s': %s",
257 FabricConstants.CTRL_META_INGRESS_PORT_ID, deviceId, packetIn));
258 }
259 }
260}