blob: 5381e361976cea508e02a50b8ba33c49cc62f046 [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 {
Carmelo Cascone1e8843f2018-07-19 19:01:12 +020063
64 public static final int PORT_BITWIDTH = 9;
65
Yi Tsengfa4a1c72017-11-03 10:22:38 -070066 private static final ImmutableBiMap<Integer, PiTableId> TABLE_ID_MAP =
67 ImmutableBiMap.<Integer, PiTableId>builder()
68 // Filtering
Yi Tseng43ee7e82018-04-12 16:37:34 +080069 .put(0, FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN)
70 .put(1, FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070071 // Forwarding
Yi Tseng43ee7e82018-04-12 16:37:34 +080072 .put(2, FabricConstants.FABRIC_INGRESS_FORWARDING_MPLS)
73 .put(3, FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V4)
74 .put(4, FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V6)
75 .put(5, FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V4)
76 .put(6, FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V6)
77 .put(7, FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING)
78 .put(8, FabricConstants.FABRIC_INGRESS_FORWARDING_ACL)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070079 // Next
Yi Tseng43ee7e82018-04-12 16:37:34 +080080 .put(9, FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META)
81 .put(10, FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE)
82 .put(11, FabricConstants.FABRIC_INGRESS_NEXT_HASHED)
83 .put(12, FabricConstants.FABRIC_INGRESS_NEXT_MULTICAST)
84 .put(13, FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070085 .build();
86
Yi Tseng43ee7e82018-04-12 16:37:34 +080087 private static final Set<PiTableId> FILTERING_CTRL_TBLS =
88 ImmutableSet.of(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN,
89 FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER);
90 private static final Set<PiTableId> FORWARDING_CTRL_TBLS =
91 ImmutableSet.of(FabricConstants.FABRIC_INGRESS_FORWARDING_MPLS,
92 FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V4,
93 FabricConstants.FABRIC_INGRESS_FORWARDING_UNICAST_V6,
94 FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V4,
95 FabricConstants.FABRIC_INGRESS_FORWARDING_MULTICAST_V6,
96 FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING,
97 FabricConstants.FABRIC_INGRESS_FORWARDING_ACL);
98 private static final Set<PiTableId> NEXT_CTRL_TBLS =
Yi Tsengbf0d4372018-06-21 23:55:58 +080099 ImmutableSet.of(FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META,
100 FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE,
Yi Tseng43ee7e82018-04-12 16:37:34 +0800101 FabricConstants.FABRIC_INGRESS_NEXT_HASHED,
102 FabricConstants.FABRIC_INGRESS_NEXT_MULTICAST);
103 private static final Set<PiTableId> E_NEXT_CTRL_TBLS =
104 ImmutableSet.of(FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700105
Yi Tseng1d842672017-11-28 16:06:52 -0800106 private static final ImmutableMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP =
107 ImmutableMap.<Criterion.Type, PiMatchFieldId>builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800108 .put(Criterion.Type.IN_PORT, FabricConstants.STANDARD_METADATA_INGRESS_PORT)
109 .put(Criterion.Type.ETH_DST, FabricConstants.HDR_ETHERNET_DST_ADDR)
110 .put(Criterion.Type.ETH_SRC, FabricConstants.HDR_ETHERNET_SRC_ADDR)
111 .put(Criterion.Type.ETH_TYPE, FabricConstants.FABRIC_METADATA_ORIGINAL_ETHER_TYPE)
112 .put(Criterion.Type.MPLS_LABEL, FabricConstants.HDR_MPLS_LABEL)
113 .put(Criterion.Type.VLAN_VID, FabricConstants.HDR_VLAN_TAG_VLAN_ID)
114 .put(Criterion.Type.IPV4_DST, FabricConstants.HDR_IPV4_DST_ADDR)
115 .put(Criterion.Type.IPV4_SRC, FabricConstants.HDR_IPV4_SRC_ADDR)
116 .put(Criterion.Type.IPV6_DST, FabricConstants.HDR_IPV6_DST_ADDR)
117 .put(Criterion.Type.TCP_SRC, FabricConstants.FABRIC_METADATA_L4_SRC_PORT)
118 .put(Criterion.Type.TCP_DST, FabricConstants.FABRIC_METADATA_L4_DST_PORT)
119 .put(Criterion.Type.UDP_SRC, FabricConstants.FABRIC_METADATA_L4_SRC_PORT)
120 .put(Criterion.Type.UDP_DST, FabricConstants.FABRIC_METADATA_L4_DST_PORT)
121 .put(Criterion.Type.IP_PROTO, FabricConstants.FABRIC_METADATA_IP_PROTO)
122 .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HDR_ICMP_ICMP_TYPE)
123 .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HDR_ICMP_ICMP_CODE)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700124 .build();
125
Yi Tseng1d842672017-11-28 16:06:52 -0800126 private static final ImmutableMap<PiMatchFieldId, Criterion.Type> INVERSE_CRITERION_MAP =
127 ImmutableMap.<PiMatchFieldId, Criterion.Type>builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800128 .put(FabricConstants.STANDARD_METADATA_INGRESS_PORT, Criterion.Type.IN_PORT)
129 .put(FabricConstants.HDR_ETHERNET_DST_ADDR, Criterion.Type.ETH_DST)
130 .put(FabricConstants.HDR_ETHERNET_SRC_ADDR, Criterion.Type.ETH_SRC)
131 .put(FabricConstants.FABRIC_METADATA_ORIGINAL_ETHER_TYPE, Criterion.Type.ETH_TYPE)
132 .put(FabricConstants.HDR_MPLS_LABEL, Criterion.Type.MPLS_LABEL)
133 .put(FabricConstants.HDR_VLAN_TAG_VLAN_ID, Criterion.Type.VLAN_VID)
134 .put(FabricConstants.HDR_IPV4_DST_ADDR, Criterion.Type.IPV4_DST)
135 .put(FabricConstants.HDR_IPV4_SRC_ADDR, Criterion.Type.IPV4_SRC)
136 .put(FabricConstants.HDR_IPV6_DST_ADDR, Criterion.Type.IPV6_DST)
Yi Tseng1d842672017-11-28 16:06:52 -0800137 // FIXME: might be incorrect if we inverse the map....
Yi Tseng43ee7e82018-04-12 16:37:34 +0800138 .put(FabricConstants.FABRIC_METADATA_L4_SRC_PORT, Criterion.Type.UDP_SRC)
139 .put(FabricConstants.FABRIC_METADATA_L4_DST_PORT, Criterion.Type.UDP_DST)
140 .put(FabricConstants.FABRIC_METADATA_IP_PROTO, Criterion.Type.IP_PROTO)
141 .put(FabricConstants.HDR_ICMP_ICMP_TYPE, Criterion.Type.ICMPV6_TYPE)
142 .put(FabricConstants.HDR_ICMP_ICMP_CODE, Criterion.Type.ICMPV6_CODE)
Yi Tseng1d842672017-11-28 16:06:52 -0800143 .build();
144
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700145 @Override
146 public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) {
147 return Optional.ofNullable(CRITERION_MAP.get(type));
148 }
149
150 @Override
151 public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId fieldId) {
Yi Tseng1d842672017-11-28 16:06:52 -0800152 return Optional.ofNullable(INVERSE_CRITERION_MAP.get(fieldId));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700153 }
154
155 @Override
156 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
157 return Optional.ofNullable(TABLE_ID_MAP.get(flowRuleTableId));
158 }
159
160 @Override
161 public Optional<Integer> mapPiTableId(PiTableId piTableId) {
162 return Optional.ofNullable(TABLE_ID_MAP.inverse().get(piTableId));
163 }
164
165 @Override
166 public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId)
167 throws PiInterpreterException {
168
169 if (FILTERING_CTRL_TBLS.contains(piTableId)) {
Yi Tseng47eac892018-07-11 02:17:04 +0800170 return FabricTreatmentInterpreter.mapFilteringTreatment(treatment, piTableId);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700171 } else if (FORWARDING_CTRL_TBLS.contains(piTableId)) {
Yi Tseng47eac892018-07-11 02:17:04 +0800172 return FabricTreatmentInterpreter.mapForwardingTreatment(treatment, piTableId);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700173 } else if (NEXT_CTRL_TBLS.contains(piTableId)) {
Yi Tseng47eac892018-07-11 02:17:04 +0800174 return FabricTreatmentInterpreter.mapNextTreatment(treatment, piTableId);
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800175 } else if (E_NEXT_CTRL_TBLS.contains(piTableId)) {
Yi Tseng47eac892018-07-11 02:17:04 +0800176 return FabricTreatmentInterpreter.mapEgressNextTreatment(treatment, piTableId);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700177 } else {
178 throw new PiInterpreterException(String.format("Table %s unsupported", piTableId));
179 }
180 }
181
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700182 private PiPacketOperation createPiPacketOperation(DeviceId deviceId, ByteBuffer data, long portNumber)
183 throws PiInterpreterException {
184 PiControlMetadata metadata = createPacketMetadata(portNumber);
185 return PiPacketOperation.builder()
186 .forDevice(deviceId)
187 .withType(PACKET_OUT)
188 .withData(copyFrom(data))
189 .withMetadatas(ImmutableList.of(metadata))
190 .build();
191 }
192
193 private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
194 try {
195 return PiControlMetadata.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800196 .withId(FabricConstants.EGRESS_PORT)
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200197 .withValue(copyFrom(portNumber).fit(PORT_BITWIDTH))
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700198 .build();
199 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
200 throw new PiInterpreterException(format(
201 "Port number %d too big, %s", portNumber, e.getMessage()));
202 }
203 }
204
205 @Override
206 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
207 throws PiInterpreterException {
208 DeviceId deviceId = packet.sendThrough();
209 TrafficTreatment treatment = packet.treatment();
210
211 // fabric.p4 supports only OUTPUT instructions.
212 List<Instructions.OutputInstruction> outInstructions = treatment
213 .allInstructions()
214 .stream()
215 .filter(i -> i.type().equals(OUTPUT))
216 .map(i -> (Instructions.OutputInstruction) i)
217 .collect(toList());
218
219 if (treatment.allInstructions().size() != outInstructions.size()) {
220 // There are other instructions that are not of type OUTPUT.
221 throw new PiInterpreterException("Treatment not supported: " + treatment);
222 }
223
224 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
225 for (Instructions.OutputInstruction outInst : outInstructions) {
226 if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
227 throw new PiInterpreterException(format(
228 "Output on logical port '%s' not supported", outInst.port()));
229 } else if (outInst.port().equals(FLOOD)) {
230 // Since fabric.p4 does not support flooding, we create a packet
231 // operation for each switch port.
232 final DeviceService deviceService = handler().get(DeviceService.class);
233 for (Port port : deviceService.getPorts(packet.sendThrough())) {
234 builder.add(createPiPacketOperation(deviceId, packet.data(), port.number().toLong()));
235 }
236 } else {
237 builder.add(createPiPacketOperation(deviceId, packet.data(), outInst.port().toLong()));
238 }
239 }
240 return builder.build();
241 }
242
243 @Override
244 public InboundPacket mapInboundPacket(PiPacketOperation packetIn) throws PiInterpreterException {
245 // Assuming that the packet is ethernet, which is fine since fabric.p4
246 // can deparse only ethernet packets.
247 DeviceId deviceId = packetIn.deviceId();
248 Ethernet ethPkt;
249 try {
250 ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0,
251 packetIn.data().size());
252 } catch (DeserializationException dex) {
253 throw new PiInterpreterException(dex.getMessage());
254 }
255
256 // Returns the ingress port packet metadata.
257 Optional<PiControlMetadata> packetMetadata = packetIn.metadatas()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800258 .stream().filter(m -> m.id().equals(FabricConstants.INGRESS_PORT))
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700259 .findFirst();
260
261 if (packetMetadata.isPresent()) {
262 ImmutableByteSequence portByteSequence = packetMetadata.get().value();
263 short s = portByteSequence.asReadOnlyBuffer().getShort();
264 ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
265 ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
266 return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
267 } else {
268 throw new PiInterpreterException(format(
269 "Missing metadata '%s' in packet-in received from '%s': %s",
Yi Tseng43ee7e82018-04-12 16:37:34 +0800270 FabricConstants.INGRESS_PORT, deviceId, packetIn));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700271 }
272 }
273}