blob: b3f996dcbad5b5acb1029ade79a717e99023ef2d [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)
Charles Chan384aea22018-08-23 22:08:02 -070073 .put(3, FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4)
74 .put(4, FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V6)
75 .put(5, FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING)
76 .put(6, FabricConstants.FABRIC_INGRESS_FORWARDING_ACL)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070077 // Next
Charles Chan384aea22018-08-23 22:08:02 -070078 .put(7, FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META)
79 .put(8, FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE)
80 .put(9, FabricConstants.FABRIC_INGRESS_NEXT_HASHED)
81 .put(10, FabricConstants.FABRIC_INGRESS_NEXT_MULTICAST)
82 .put(11, FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070083 .build();
84
Yi Tseng43ee7e82018-04-12 16:37:34 +080085 private static final Set<PiTableId> FILTERING_CTRL_TBLS =
86 ImmutableSet.of(FabricConstants.FABRIC_INGRESS_FILTERING_INGRESS_PORT_VLAN,
87 FabricConstants.FABRIC_INGRESS_FILTERING_FWD_CLASSIFIER);
88 private static final Set<PiTableId> FORWARDING_CTRL_TBLS =
89 ImmutableSet.of(FabricConstants.FABRIC_INGRESS_FORWARDING_MPLS,
Charles Chan384aea22018-08-23 22:08:02 -070090 FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V4,
91 FabricConstants.FABRIC_INGRESS_FORWARDING_ROUTING_V6,
Yi Tseng43ee7e82018-04-12 16:37:34 +080092 FabricConstants.FABRIC_INGRESS_FORWARDING_BRIDGING,
93 FabricConstants.FABRIC_INGRESS_FORWARDING_ACL);
94 private static final Set<PiTableId> NEXT_CTRL_TBLS =
Yi Tsengbf0d4372018-06-21 23:55:58 +080095 ImmutableSet.of(FabricConstants.FABRIC_INGRESS_NEXT_VLAN_META,
96 FabricConstants.FABRIC_INGRESS_NEXT_SIMPLE,
Yi Tseng43ee7e82018-04-12 16:37:34 +080097 FabricConstants.FABRIC_INGRESS_NEXT_HASHED,
98 FabricConstants.FABRIC_INGRESS_NEXT_MULTICAST);
99 private static final Set<PiTableId> E_NEXT_CTRL_TBLS =
100 ImmutableSet.of(FabricConstants.FABRIC_EGRESS_EGRESS_NEXT_EGRESS_VLAN);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700101
Yi Tseng1d842672017-11-28 16:06:52 -0800102 private static final ImmutableMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP =
103 ImmutableMap.<Criterion.Type, PiMatchFieldId>builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800104 .put(Criterion.Type.IN_PORT, FabricConstants.STANDARD_METADATA_INGRESS_PORT)
Charles Chan384aea22018-08-23 22:08:02 -0700105 .put(Criterion.Type.ETH_DST_MASKED, FabricConstants.HDR_ETHERNET_DST_ADDR)
106 .put(Criterion.Type.ETH_SRC_MASKED, FabricConstants.HDR_ETHERNET_SRC_ADDR)
Yi Tseng8235a1a2018-07-24 20:57:28 +0800107 .put(Criterion.Type.ETH_TYPE, FabricConstants.HDR_VLAN_TAG_ETHER_TYPE)
Yi Tseng43ee7e82018-04-12 16:37:34 +0800108 .put(Criterion.Type.MPLS_LABEL, FabricConstants.HDR_MPLS_LABEL)
109 .put(Criterion.Type.VLAN_VID, FabricConstants.HDR_VLAN_TAG_VLAN_ID)
110 .put(Criterion.Type.IPV4_DST, FabricConstants.HDR_IPV4_DST_ADDR)
111 .put(Criterion.Type.IPV4_SRC, FabricConstants.HDR_IPV4_SRC_ADDR)
112 .put(Criterion.Type.IPV6_DST, FabricConstants.HDR_IPV6_DST_ADDR)
Yi Tseng43ee7e82018-04-12 16:37:34 +0800113 .put(Criterion.Type.IP_PROTO, FabricConstants.FABRIC_METADATA_IP_PROTO)
114 .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HDR_ICMP_ICMP_TYPE)
115 .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HDR_ICMP_ICMP_CODE)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700116 .build();
117
Yi Tseng1d842672017-11-28 16:06:52 -0800118 private static final ImmutableMap<PiMatchFieldId, Criterion.Type> INVERSE_CRITERION_MAP =
119 ImmutableMap.<PiMatchFieldId, Criterion.Type>builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800120 .put(FabricConstants.STANDARD_METADATA_INGRESS_PORT, Criterion.Type.IN_PORT)
Charles Chan384aea22018-08-23 22:08:02 -0700121 .put(FabricConstants.HDR_ETHERNET_DST_ADDR, Criterion.Type.ETH_DST_MASKED)
122 .put(FabricConstants.HDR_ETHERNET_SRC_ADDR, Criterion.Type.ETH_SRC_MASKED)
Yi Tseng8235a1a2018-07-24 20:57:28 +0800123 .put(FabricConstants.HDR_VLAN_TAG_ETHER_TYPE, Criterion.Type.ETH_TYPE)
Yi Tseng43ee7e82018-04-12 16:37:34 +0800124 .put(FabricConstants.HDR_MPLS_LABEL, Criterion.Type.MPLS_LABEL)
125 .put(FabricConstants.HDR_VLAN_TAG_VLAN_ID, Criterion.Type.VLAN_VID)
126 .put(FabricConstants.HDR_IPV4_DST_ADDR, Criterion.Type.IPV4_DST)
127 .put(FabricConstants.HDR_IPV4_SRC_ADDR, Criterion.Type.IPV4_SRC)
128 .put(FabricConstants.HDR_IPV6_DST_ADDR, Criterion.Type.IPV6_DST)
Yi Tseng1d842672017-11-28 16:06:52 -0800129 // FIXME: might be incorrect if we inverse the map....
Yi Tseng43ee7e82018-04-12 16:37:34 +0800130 .put(FabricConstants.FABRIC_METADATA_L4_SRC_PORT, Criterion.Type.UDP_SRC)
131 .put(FabricConstants.FABRIC_METADATA_L4_DST_PORT, Criterion.Type.UDP_DST)
132 .put(FabricConstants.FABRIC_METADATA_IP_PROTO, Criterion.Type.IP_PROTO)
133 .put(FabricConstants.HDR_ICMP_ICMP_TYPE, Criterion.Type.ICMPV6_TYPE)
134 .put(FabricConstants.HDR_ICMP_ICMP_CODE, Criterion.Type.ICMPV6_CODE)
Yi Tseng1d842672017-11-28 16:06:52 -0800135 .build();
136
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700137 @Override
138 public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) {
139 return Optional.ofNullable(CRITERION_MAP.get(type));
140 }
141
142 @Override
143 public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId fieldId) {
Yi Tseng1d842672017-11-28 16:06:52 -0800144 return Optional.ofNullable(INVERSE_CRITERION_MAP.get(fieldId));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700145 }
146
147 @Override
148 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
149 return Optional.ofNullable(TABLE_ID_MAP.get(flowRuleTableId));
150 }
151
152 @Override
153 public Optional<Integer> mapPiTableId(PiTableId piTableId) {
154 return Optional.ofNullable(TABLE_ID_MAP.inverse().get(piTableId));
155 }
156
157 @Override
158 public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId)
159 throws PiInterpreterException {
160
161 if (FILTERING_CTRL_TBLS.contains(piTableId)) {
Yi Tseng47eac892018-07-11 02:17:04 +0800162 return FabricTreatmentInterpreter.mapFilteringTreatment(treatment, piTableId);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700163 } else if (FORWARDING_CTRL_TBLS.contains(piTableId)) {
Yi Tseng47eac892018-07-11 02:17:04 +0800164 return FabricTreatmentInterpreter.mapForwardingTreatment(treatment, piTableId);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700165 } else if (NEXT_CTRL_TBLS.contains(piTableId)) {
Yi Tseng47eac892018-07-11 02:17:04 +0800166 return FabricTreatmentInterpreter.mapNextTreatment(treatment, piTableId);
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800167 } else if (E_NEXT_CTRL_TBLS.contains(piTableId)) {
Yi Tseng47eac892018-07-11 02:17:04 +0800168 return FabricTreatmentInterpreter.mapEgressNextTreatment(treatment, piTableId);
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700169 } else {
170 throw new PiInterpreterException(String.format("Table %s unsupported", piTableId));
171 }
172 }
173
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700174 private PiPacketOperation createPiPacketOperation(DeviceId deviceId, ByteBuffer data, long portNumber)
175 throws PiInterpreterException {
176 PiControlMetadata metadata = createPacketMetadata(portNumber);
177 return PiPacketOperation.builder()
178 .forDevice(deviceId)
179 .withType(PACKET_OUT)
180 .withData(copyFrom(data))
181 .withMetadatas(ImmutableList.of(metadata))
182 .build();
183 }
184
185 private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
186 try {
187 return PiControlMetadata.builder()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800188 .withId(FabricConstants.EGRESS_PORT)
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200189 .withValue(copyFrom(portNumber).fit(PORT_BITWIDTH))
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700190 .build();
191 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
192 throw new PiInterpreterException(format(
193 "Port number %d too big, %s", portNumber, e.getMessage()));
194 }
195 }
196
197 @Override
198 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
199 throws PiInterpreterException {
200 DeviceId deviceId = packet.sendThrough();
201 TrafficTreatment treatment = packet.treatment();
202
203 // fabric.p4 supports only OUTPUT instructions.
204 List<Instructions.OutputInstruction> outInstructions = treatment
205 .allInstructions()
206 .stream()
207 .filter(i -> i.type().equals(OUTPUT))
208 .map(i -> (Instructions.OutputInstruction) i)
209 .collect(toList());
210
211 if (treatment.allInstructions().size() != outInstructions.size()) {
212 // There are other instructions that are not of type OUTPUT.
213 throw new PiInterpreterException("Treatment not supported: " + treatment);
214 }
215
216 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
217 for (Instructions.OutputInstruction outInst : outInstructions) {
218 if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
219 throw new PiInterpreterException(format(
220 "Output on logical port '%s' not supported", outInst.port()));
221 } else if (outInst.port().equals(FLOOD)) {
222 // Since fabric.p4 does not support flooding, we create a packet
223 // operation for each switch port.
224 final DeviceService deviceService = handler().get(DeviceService.class);
225 for (Port port : deviceService.getPorts(packet.sendThrough())) {
226 builder.add(createPiPacketOperation(deviceId, packet.data(), port.number().toLong()));
227 }
228 } else {
229 builder.add(createPiPacketOperation(deviceId, packet.data(), outInst.port().toLong()));
230 }
231 }
232 return builder.build();
233 }
234
235 @Override
236 public InboundPacket mapInboundPacket(PiPacketOperation packetIn) throws PiInterpreterException {
237 // Assuming that the packet is ethernet, which is fine since fabric.p4
238 // can deparse only ethernet packets.
239 DeviceId deviceId = packetIn.deviceId();
240 Ethernet ethPkt;
241 try {
242 ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0,
243 packetIn.data().size());
244 } catch (DeserializationException dex) {
245 throw new PiInterpreterException(dex.getMessage());
246 }
247
248 // Returns the ingress port packet metadata.
249 Optional<PiControlMetadata> packetMetadata = packetIn.metadatas()
Yi Tseng43ee7e82018-04-12 16:37:34 +0800250 .stream().filter(m -> m.id().equals(FabricConstants.INGRESS_PORT))
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700251 .findFirst();
252
253 if (packetMetadata.isPresent()) {
254 ImmutableByteSequence portByteSequence = packetMetadata.get().value();
255 short s = portByteSequence.asReadOnlyBuffer().getShort();
256 ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
257 ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
258 return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
259 } else {
260 throw new PiInterpreterException(format(
261 "Missing metadata '%s' in packet-in received from '%s': %s",
Yi Tseng43ee7e82018-04-12 16:37:34 +0800262 FabricConstants.INGRESS_PORT, deviceId, packetIn));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700263 }
264 }
265}