blob: aa3a99a737bf7e7b06fc7a9a39c41dd7a503dcbb [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;
38import org.onosproject.net.pi.model.PiCounterId;
39import org.onosproject.net.pi.model.PiMatchFieldId;
40import org.onosproject.net.pi.model.PiPipelineInterpreter;
41import org.onosproject.net.pi.model.PiTableId;
42import org.onosproject.net.pi.runtime.PiAction;
43import org.onosproject.net.pi.runtime.PiControlMetadata;
44import org.onosproject.net.pi.runtime.PiPacketOperation;
45
46import java.nio.ByteBuffer;
47import java.util.Collection;
48import java.util.List;
49import java.util.Optional;
50import java.util.Set;
51
52import static java.lang.String.format;
53import static java.util.stream.Collectors.toList;
54import static org.onlab.util.ImmutableByteSequence.copyFrom;
55import static org.onlab.util.ImmutableByteSequence.fit;
56import static org.onosproject.net.PortNumber.FLOOD;
57import 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 */
63public class FabricInterpreter extends AbstractHandlerBehaviour
64 implements PiPipelineInterpreter {
65 private static final ImmutableBiMap<Integer, PiTableId> TABLE_ID_MAP =
66 ImmutableBiMap.<Integer, PiTableId>builder()
67 // Filtering
68 .put(0, FabricConstants.TBL_INGRESS_PORT_VLAN_ID)
69 .put(1, FabricConstants.TBL_FWD_CLASSIFIER_ID)
70 // Forwarding
71 .put(2, FabricConstants.TBL_MPLS_ID)
72 .put(3, FabricConstants.TBL_UNICAST_V4_ID)
73 .put(4, FabricConstants.TBL_UNICAST_V6_ID)
74 .put(5, FabricConstants.TBL_MULTICAST_V4_ID)
75 .put(6, FabricConstants.TBL_MULTICAST_V6_ID)
76 .put(7, FabricConstants.TBL_BRIDGING_ID)
77 .put(8, FabricConstants.TBL_ACL_ID)
78 // Next
Yi Tsengf55eaa82017-11-29 15:51:28 -080079 .put(9, FabricConstants.TBL_SIMPLE_ID)
80 .put(10, FabricConstants.TBL_HASHED_ID)
81 .put(11, FabricConstants.TBL_BROADCAST_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070082 .build();
83
84 private static final Set<PiTableId> FILTERING_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_INGRESS_PORT_VLAN_ID,
85 FabricConstants.TBL_FWD_CLASSIFIER_ID);
86 private static final Set<PiTableId> FORWARDING_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_MPLS_ID,
87 FabricConstants.TBL_UNICAST_V4_ID,
88 FabricConstants.TBL_UNICAST_V6_ID,
89 FabricConstants.TBL_MULTICAST_V4_ID,
90 FabricConstants.TBL_MULTICAST_V6_ID,
91 FabricConstants.TBL_BRIDGING_ID,
92 FabricConstants.TBL_ACL_ID);
Yi Tsengf55eaa82017-11-29 15:51:28 -080093 private static final Set<PiTableId> NEXT_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_SIMPLE_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -070094 FabricConstants.TBL_HASHED_ID,
95 FabricConstants.TBL_BROADCAST_ID);
96
97 private static final ImmutableBiMap<PiTableId, PiCounterId> TABLE_COUNTER_MAP =
98 ImmutableBiMap.<PiTableId, PiCounterId>builder()
Yi Tsengfa4a1c72017-11-03 10:22:38 -070099 .build();
Yi Tseng1d842672017-11-28 16:06:52 -0800100 private static final ImmutableMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP =
101 ImmutableMap.<Criterion.Type, PiMatchFieldId>builder()
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700102 .put(Criterion.Type.IN_PORT, FabricConstants.HF_STANDARD_METADATA_INGRESS_PORT_ID)
103 .put(Criterion.Type.ETH_DST, FabricConstants.HF_ETHERNET_DST_ADDR_ID)
104 .put(Criterion.Type.ETH_SRC, FabricConstants.HF_ETHERNET_SRC_ADDR_ID)
Yi Tseng1d842672017-11-28 16:06:52 -0800105 .put(Criterion.Type.ETH_TYPE, FabricConstants.HF_FABRIC_METADATA_ORIGINAL_ETHER_TYPE_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700106 .put(Criterion.Type.MPLS_BOS, FabricConstants.HF_MPLS_BOS_ID)
107 .put(Criterion.Type.MPLS_LABEL, FabricConstants.HF_MPLS_LABEL_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700108 .put(Criterion.Type.VLAN_VID, FabricConstants.HF_VLAN_TAG_VLAN_ID_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700109 .put(Criterion.Type.IPV4_DST, FabricConstants.HF_IPV4_DST_ADDR_ID)
110 .put(Criterion.Type.IPV4_SRC, FabricConstants.HF_IPV4_SRC_ADDR_ID)
111 .put(Criterion.Type.IPV6_DST, FabricConstants.HF_IPV6_DST_ADDR_ID)
112 .put(Criterion.Type.IPV6_SRC, FabricConstants.HF_IPV6_SRC_ADDR_ID)
Yi Tseng1d842672017-11-28 16:06:52 -0800113 .put(Criterion.Type.TCP_SRC, FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID)
114 .put(Criterion.Type.TCP_DST, FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID)
115 .put(Criterion.Type.UDP_SRC, FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID)
116 .put(Criterion.Type.UDP_DST, FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700117 .put(Criterion.Type.IP_PROTO, FabricConstants.HF_FABRIC_METADATA_IP_PROTO_ID)
118 .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HF_ICMP_ICMP_TYPE_ID)
119 .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HF_ICMP_ICMP_CODE_ID)
120 .build();
121
Yi Tseng1d842672017-11-28 16:06:52 -0800122 private static final ImmutableMap<PiMatchFieldId, Criterion.Type> INVERSE_CRITERION_MAP =
123 ImmutableMap.<PiMatchFieldId, Criterion.Type>builder()
124 .put(FabricConstants.HF_STANDARD_METADATA_INGRESS_PORT_ID, Criterion.Type.IN_PORT)
125 .put(FabricConstants.HF_ETHERNET_DST_ADDR_ID, Criterion.Type.ETH_DST)
126 .put(FabricConstants.HF_ETHERNET_SRC_ADDR_ID, Criterion.Type.ETH_SRC)
127 .put(FabricConstants.HF_FABRIC_METADATA_ORIGINAL_ETHER_TYPE_ID, Criterion.Type.ETH_TYPE)
128 .put(FabricConstants.HF_MPLS_BOS_ID, Criterion.Type.MPLS_BOS)
129 .put(FabricConstants.HF_MPLS_LABEL_ID, Criterion.Type.MPLS_LABEL)
130 .put(FabricConstants.HF_VLAN_TAG_VLAN_ID_ID, Criterion.Type.VLAN_VID)
131 .put(FabricConstants.HF_IPV4_DST_ADDR_ID, Criterion.Type.IPV4_DST)
132 .put(FabricConstants.HF_IPV4_SRC_ADDR_ID, Criterion.Type.IPV4_SRC)
133 .put(FabricConstants.HF_IPV6_DST_ADDR_ID, Criterion.Type.IPV6_DST)
134 .put(FabricConstants.HF_IPV6_SRC_ADDR_ID, Criterion.Type.IPV6_SRC)
135 // FIXME: might be incorrect if we inverse the map....
136 .put(FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID, Criterion.Type.UDP_SRC)
137 .put(FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID, Criterion.Type.UDP_DST)
138// .put(FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID, Criterion.Type.TCP_SRC)
139// .put(FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID, Criterion.Type.TCP_DST)
140 .put(FabricConstants.HF_FABRIC_METADATA_IP_PROTO_ID, Criterion.Type.IP_PROTO)
141 .put(FabricConstants.HF_ICMP_ICMP_TYPE_ID, Criterion.Type.ICMPV6_TYPE)
142 .put(FabricConstants.HF_ICMP_ICMP_CODE_ID, Criterion.Type.ICMPV6_CODE)
143 .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)) {
170 return FabricTreatmentInterpreter.mapFilteringTreatment(treatment);
171 } else if (FORWARDING_CTRL_TBLS.contains(piTableId)) {
172 return FabricTreatmentInterpreter.mapForwardingTreatment(treatment);
173 } else if (NEXT_CTRL_TBLS.contains(piTableId)) {
174 return FabricTreatmentInterpreter.mapNextTreatment(treatment);
175 } else {
176 throw new PiInterpreterException(String.format("Table %s unsupported", piTableId));
177 }
178 }
179
180 @Override
181 public Optional<PiCounterId> mapTableCounter(PiTableId piTableId) {
182 return Optional.ofNullable(TABLE_COUNTER_MAP.get(piTableId));
183 }
184
185 private PiPacketOperation createPiPacketOperation(DeviceId deviceId, ByteBuffer data, long portNumber)
186 throws PiInterpreterException {
187 PiControlMetadata metadata = createPacketMetadata(portNumber);
188 return PiPacketOperation.builder()
189 .forDevice(deviceId)
190 .withType(PACKET_OUT)
191 .withData(copyFrom(data))
192 .withMetadatas(ImmutableList.of(metadata))
193 .build();
194 }
195
196 private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
197 try {
198 return PiControlMetadata.builder()
199 .withId(FabricConstants.CTRL_META_EGRESS_PORT_ID)
200 .withValue(fit(copyFrom(portNumber), FabricConstants.PORT_BITWIDTH))
201 .build();
202 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
203 throw new PiInterpreterException(format(
204 "Port number %d too big, %s", portNumber, e.getMessage()));
205 }
206 }
207
208 @Override
209 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
210 throws PiInterpreterException {
211 DeviceId deviceId = packet.sendThrough();
212 TrafficTreatment treatment = packet.treatment();
213
214 // fabric.p4 supports only OUTPUT instructions.
215 List<Instructions.OutputInstruction> outInstructions = treatment
216 .allInstructions()
217 .stream()
218 .filter(i -> i.type().equals(OUTPUT))
219 .map(i -> (Instructions.OutputInstruction) i)
220 .collect(toList());
221
222 if (treatment.allInstructions().size() != outInstructions.size()) {
223 // There are other instructions that are not of type OUTPUT.
224 throw new PiInterpreterException("Treatment not supported: " + treatment);
225 }
226
227 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
228 for (Instructions.OutputInstruction outInst : outInstructions) {
229 if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
230 throw new PiInterpreterException(format(
231 "Output on logical port '%s' not supported", outInst.port()));
232 } else if (outInst.port().equals(FLOOD)) {
233 // Since fabric.p4 does not support flooding, we create a packet
234 // operation for each switch port.
235 final DeviceService deviceService = handler().get(DeviceService.class);
236 for (Port port : deviceService.getPorts(packet.sendThrough())) {
237 builder.add(createPiPacketOperation(deviceId, packet.data(), port.number().toLong()));
238 }
239 } else {
240 builder.add(createPiPacketOperation(deviceId, packet.data(), outInst.port().toLong()));
241 }
242 }
243 return builder.build();
244 }
245
246 @Override
247 public InboundPacket mapInboundPacket(PiPacketOperation packetIn) throws PiInterpreterException {
248 // Assuming that the packet is ethernet, which is fine since fabric.p4
249 // can deparse only ethernet packets.
250 DeviceId deviceId = packetIn.deviceId();
251 Ethernet ethPkt;
252 try {
253 ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0,
254 packetIn.data().size());
255 } catch (DeserializationException dex) {
256 throw new PiInterpreterException(dex.getMessage());
257 }
258
259 // Returns the ingress port packet metadata.
260 Optional<PiControlMetadata> packetMetadata = packetIn.metadatas()
261 .stream().filter(m -> m.id().equals(FabricConstants.CTRL_META_INGRESS_PORT_ID))
262 .findFirst();
263
264 if (packetMetadata.isPresent()) {
265 ImmutableByteSequence portByteSequence = packetMetadata.get().value();
266 short s = portByteSequence.asReadOnlyBuffer().getShort();
267 ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
268 ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
269 return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
270 } else {
271 throw new PiInterpreterException(format(
272 "Missing metadata '%s' in packet-in received from '%s': %s",
273 FabricConstants.CTRL_META_INGRESS_PORT_ID, deviceId, packetIn));
274 }
275 }
276}