blob: 11f551e1af95b243d1e7f195c69944641f9aa534 [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
Yi Tseng43ee7e82018-04-12 16:37:34 +080066 .put(0, FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)
67 .put(1, FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070068 // Forwarding
Yi Tseng43ee7e82018-04-12 16:37:34 +080069 .put(2, FabricConstants.FABRIC_INGRESS_FORWARDING_MPLS)
70 .put(3, FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V4)
71 .put(4, FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V6)
72 .put(5, FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V4)
73 .put(6, FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V6)
74 .put(7, FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING)
75 .put(8, FabricConstants.FABRIC_INGRESS_FORWARDING_ACL)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070076 // Next
Yi Tseng43ee7e82018-04-12 16:37:34 +080077 .put(9, FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META)
78 .put(10, FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE)
79 .put(11, FabricConstants.FABRIC_INGRESS_NEXT_HASHED)
80 .put(12, FabricConstants.FABRIC_INGRESS_NEXT_MULTICAST)
81 .put(13, FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070082 .build();
83
Yi Tseng43ee7e82018-04-12 16:37:34 +080084 private static final Set<PiTableId> FILTERING_CTRL_TBLS =
85 ImmutableSet.of(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN,
86 FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER);
87 private static final Set<PiTableId> FORWARDING_CTRL_TBLS =
88 ImmutableSet.of(FabricConstants.FABRIC_INGRESS_FORWARDING_MPLS,
89 FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V4,
90 FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V6,
91 FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V4,
92 FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V6,
93 FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING,
94 FabricConstants.FABRIC_INGRESS_FORWARDING_ACL);
95 private static final Set<PiTableId> NEXT_CTRL_TBLS =
Yi Tsengbf0d4372018-06-21 23:55:58 +080096 ImmutableSet.of(FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META,
97 FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE,
Yi Tseng43ee7e82018-04-12 16:37:34 +080098 FabricConstants.FABRIC_INGRESS_NEXT_HASHED,
99 FabricConstants.FABRIC_INGRESS_NEXT_MULTICAST);
100 private static final Set<PiTableId> E_NEXT_CTRL_TBLS =
101 ImmutableSet.of(FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700102
Yi Tseng1d842672017-11-28 16:06:52 -0800103 private static final ImmutableMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP =
104 ImmutableMap.<Criterion.Type, PiMatchFieldId>builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800105 .put(Criterion.Type.IN_PORT, FabricConstants.STANDARD_METADATA_INGRESS_PORT)
106 .put(Criterion.Type.ETH_DST, FabricConstants.HDR_ETHERNET_DST_ADDR)
107 .put(Criterion.Type.ETH_SRC, FabricConstants.HDR_ETHERNET_SRC_ADDR)
108 .put(Criterion.Type.ETH_TYPE, FabricConstants.FABRIC_METADATA_ORIGINAL_ETHER_TYPE)
109 .put(Criterion.Type.MPLS_LABEL, FabricConstants.HDR_MPLS_LABEL)
110 .put(Criterion.Type.VLAN_VID, FabricConstants.HDR_VLAN_TAG_VLAN_ID)
111 .put(Criterion.Type.IPV4_DST, FabricConstants.HDR_IPV4_DST_ADDR)
112 .put(Criterion.Type.IPV4_SRC, FabricConstants.HDR_IPV4_SRC_ADDR)
113 .put(Criterion.Type.IPV6_DST, FabricConstants.HDR_IPV6_DST_ADDR)
114 .put(Criterion.Type.TCP_SRC, FabricConstants.FABRIC_METADATA_L4_SRC_PORT)
115 .put(Criterion.Type.TCP_DST, FabricConstants.FABRIC_METADATA_L4_DST_PORT)
116 .put(Criterion.Type.UDP_SRC, FabricConstants.FABRIC_METADATA_L4_SRC_PORT)
117 .put(Criterion.Type.UDP_DST, FabricConstants.FABRIC_METADATA_L4_DST_PORT)
118 .put(Criterion.Type.IP_PROTO, FabricConstants.FABRIC_METADATA_IP_PROTO)
119 .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HDR_ICMP_ICMP_TYPE)
120 .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HDR_ICMP_ICMP_CODE)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700121 .build();
122
Yi Tseng1d842672017-11-28 16:06:52 -0800123 private static final ImmutableMap<PiMatchFieldId, Criterion.Type> INVERSE_CRITERION_MAP =
124 ImmutableMap.<PiMatchFieldId, Criterion.Type>builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800125 .put(FabricConstants.STANDARD_METADATA_INGRESS_PORT, Criterion.Type.IN_PORT)
126 .put(FabricConstants.HDR_ETHERNET_DST_ADDR, Criterion.Type.ETH_DST)
127 .put(FabricConstants.HDR_ETHERNET_SRC_ADDR, Criterion.Type.ETH_SRC)
128 .put(FabricConstants.FABRIC_METADATA_ORIGINAL_ETHER_TYPE, Criterion.Type.ETH_TYPE)
129 .put(FabricConstants.HDR_MPLS_LABEL, Criterion.Type.MPLS_LABEL)
130 .put(FabricConstants.HDR_VLAN_TAG_VLAN_ID, Criterion.Type.VLAN_VID)
131 .put(FabricConstants.HDR_IPV4_DST_ADDR, Criterion.Type.IPV4_DST)
132 .put(FabricConstants.HDR_IPV4_SRC_ADDR, Criterion.Type.IPV4_SRC)
133 .put(FabricConstants.HDR_IPV6_DST_ADDR, Criterion.Type.IPV6_DST)
Yi Tseng1d842672017-11-28 16:06:52 -0800134 // FIXME: might be incorrect if we inverse the map....
Yi Tseng43ee7e82018-04-12 16:37:34 +0800135 .put(FabricConstants.FABRIC_METADATA_L4_SRC_PORT, Criterion.Type.UDP_SRC)
136 .put(FabricConstants.FABRIC_METADATA_L4_DST_PORT, Criterion.Type.UDP_DST)
137 .put(FabricConstants.FABRIC_METADATA_IP_PROTO, Criterion.Type.IP_PROTO)
138 .put(FabricConstants.HDR_ICMP_ICMP_TYPE, Criterion.Type.ICMPV6_TYPE)
139 .put(FabricConstants.HDR_ICMP_ICMP_CODE, Criterion.Type.ICMPV6_CODE)
Yi Tseng1d842672017-11-28 16:06:52 -0800140 .build();
141
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700142 @Override
143 public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) {
144 return Optional.ofNullable(CRITERION_MAP.get(type));
145 }
146
147 @Override
148 public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId fieldId) {
Yi Tseng1d842672017-11-28 16:06:52 -0800149 return Optional.ofNullable(INVERSE_CRITERION_MAP.get(fieldId));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700150 }
151
152 @Override
153 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
154 return Optional.ofNullable(TABLE_ID_MAP.get(flowRuleTableId));
155 }
156
157 @Override
158 public Optional<Integer> mapPiTableId(PiTableId piTableId) {
159 return Optional.ofNullable(TABLE_ID_MAP.inverse().get(piTableId));
160 }
161
162 @Override
163 public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId)
164 throws PiInterpreterException {
165
166 if (FILTERING_CTRL_TBLS.contains(piTableId)) {
167 return FabricTreatmentInterpreter.mapFilteringTreatment(treatment);
168 } else if (FORWARDING_CTRL_TBLS.contains(piTableId)) {
169 return FabricTreatmentInterpreter.mapForwardingTreatment(treatment);
170 } else if (NEXT_CTRL_TBLS.contains(piTableId)) {
171 return FabricTreatmentInterpreter.mapNextTreatment(treatment);
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800172 } else if (E_NEXT_CTRL_TBLS.contains(piTableId)) {
173 return FabricTreatmentInterpreter.mapEgressNextTreatment(treatment);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700174 } else {
175 throw new PiInterpreterException(String.format("Table %s unsupported", piTableId));
176 }
177 }
178
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700179 private PiPacketOperation createPiPacketOperation(DeviceId deviceId, ByteBuffer data, long portNumber)
180 throws PiInterpreterException {
181 PiControlMetadata metadata = createPacketMetadata(portNumber);
182 return PiPacketOperation.builder()
183 .forDevice(deviceId)
184 .withType(PACKET_OUT)
185 .withData(copyFrom(data))
186 .withMetadatas(ImmutableList.of(metadata))
187 .build();
188 }
189
190 private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
191 try {
192 return PiControlMetadata.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800193 .withId(FabricConstants.EGRESS_PORT)
Carmelo Cascone8a571af2018-04-06 23:17:04 -0700194 .withValue(copyFrom(portNumber).fit(FabricConstants.PORT_BITWIDTH))
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700195 .build();
196 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
197 throw new PiInterpreterException(format(
198 "Port number %d too big, %s", portNumber, e.getMessage()));
199 }
200 }
201
202 @Override
203 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
204 throws PiInterpreterException {
205 DeviceId deviceId = packet.sendThrough();
206 TrafficTreatment treatment = packet.treatment();
207
208 // fabric.p4 supports only OUTPUT instructions.
209 List<Instructions.OutputInstruction> outInstructions = treatment
210 .allInstructions()
211 .stream()
212 .filter(i -> i.type().equals(OUTPUT))
213 .map(i -> (Instructions.OutputInstruction) i)
214 .collect(toList());
215
216 if (treatment.allInstructions().size() != outInstructions.size()) {
217 // There are other instructions that are not of type OUTPUT.
218 throw new PiInterpreterException("Treatment not supported: " + treatment);
219 }
220
221 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
222 for (Instructions.OutputInstruction outInst : outInstructions) {
223 if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
224 throw new PiInterpreterException(format(
225 "Output on logical port '%s' not supported", outInst.port()));
226 } else if (outInst.port().equals(FLOOD)) {
227 // Since fabric.p4 does not support flooding, we create a packet
228 // operation for each switch port.
229 final DeviceService deviceService = handler().get(DeviceService.class);
230 for (Port port : deviceService.getPorts(packet.sendThrough())) {
231 builder.add(createPiPacketOperation(deviceId, packet.data(), port.number().toLong()));
232 }
233 } else {
234 builder.add(createPiPacketOperation(deviceId, packet.data(), outInst.port().toLong()));
235 }
236 }
237 return builder.build();
238 }
239
240 @Override
241 public InboundPacket mapInboundPacket(PiPacketOperation packetIn) throws PiInterpreterException {
242 // Assuming that the packet is ethernet, which is fine since fabric.p4
243 // can deparse only ethernet packets.
244 DeviceId deviceId = packetIn.deviceId();
245 Ethernet ethPkt;
246 try {
247 ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0,
248 packetIn.data().size());
249 } catch (DeserializationException dex) {
250 throw new PiInterpreterException(dex.getMessage());
251 }
252
253 // Returns the ingress port packet metadata.
254 Optional<PiControlMetadata> packetMetadata = packetIn.metadatas()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800255 .stream().filter(m -> m.id().equals(FabricConstants.INGRESS_PORT))
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700256 .findFirst();
257
258 if (packetMetadata.isPresent()) {
259 ImmutableByteSequence portByteSequence = packetMetadata.get().value();
260 short s = portByteSequence.asReadOnlyBuffer().getShort();
261 ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
262 ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
263 return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
264 } else {
265 throw new PiInterpreterException(format(
266 "Missing metadata '%s' in packet-in received from '%s': %s",
Yi Tseng43ee7e82018-04-12 16:37:34 +0800267 FabricConstants.INGRESS_PORT, deviceId, packetIn));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700268 }
269 }
270}