blob: 29e3a485c600111c7528b9b165c5be60284a4a17 [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;
Yi Tseng4fd28432018-02-01 14:48:03 -080032import org.onosproject.net.driver.Driver;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070033import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.flow.criteria.Criterion;
35import org.onosproject.net.flow.instructions.Instructions;
36import org.onosproject.net.packet.DefaultInboundPacket;
37import org.onosproject.net.packet.InboundPacket;
38import org.onosproject.net.packet.OutboundPacket;
39import org.onosproject.net.pi.model.PiCounterId;
40import org.onosproject.net.pi.model.PiMatchFieldId;
41import org.onosproject.net.pi.model.PiPipelineInterpreter;
42import org.onosproject.net.pi.model.PiTableId;
43import org.onosproject.net.pi.runtime.PiAction;
44import org.onosproject.net.pi.runtime.PiControlMetadata;
45import org.onosproject.net.pi.runtime.PiPacketOperation;
46
47import java.nio.ByteBuffer;
48import java.util.Collection;
49import java.util.List;
50import java.util.Optional;
51import java.util.Set;
52
53import static java.lang.String.format;
54import static java.util.stream.Collectors.toList;
55import static org.onlab.util.ImmutableByteSequence.copyFrom;
56import static org.onlab.util.ImmutableByteSequence.fit;
57import static org.onosproject.net.PortNumber.FLOOD;
58import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
59import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT;
60
61/**
62 * Interpreter for fabric pipeline.
63 */
64public class FabricInterpreter extends AbstractHandlerBehaviour
65 implements PiPipelineInterpreter {
66 private static final ImmutableBiMap<Integer, PiTableId> TABLE_ID_MAP =
67 ImmutableBiMap.<Integer, PiTableId>builder()
68 // Filtering
69 .put(0, FabricConstants.TBL_INGRESS_PORT_VLAN_ID)
70 .put(1, FabricConstants.TBL_FWD_CLASSIFIER_ID)
71 // Forwarding
72 .put(2, FabricConstants.TBL_MPLS_ID)
73 .put(3, FabricConstants.TBL_UNICAST_V4_ID)
74 .put(4, FabricConstants.TBL_UNICAST_V6_ID)
75 .put(5, FabricConstants.TBL_MULTICAST_V4_ID)
76 .put(6, FabricConstants.TBL_MULTICAST_V6_ID)
77 .put(7, FabricConstants.TBL_BRIDGING_ID)
78 .put(8, FabricConstants.TBL_ACL_ID)
79 // Next
Yi Tsengf55eaa82017-11-29 15:51:28 -080080 .put(9, FabricConstants.TBL_SIMPLE_ID)
81 .put(10, FabricConstants.TBL_HASHED_ID)
82 .put(11, FabricConstants.TBL_BROADCAST_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070083 .build();
84
85 private static final Set<PiTableId> FILTERING_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_INGRESS_PORT_VLAN_ID,
86 FabricConstants.TBL_FWD_CLASSIFIER_ID);
87 private static final Set<PiTableId> FORWARDING_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_MPLS_ID,
88 FabricConstants.TBL_UNICAST_V4_ID,
89 FabricConstants.TBL_UNICAST_V6_ID,
90 FabricConstants.TBL_MULTICAST_V4_ID,
91 FabricConstants.TBL_MULTICAST_V6_ID,
92 FabricConstants.TBL_BRIDGING_ID,
93 FabricConstants.TBL_ACL_ID);
Yi Tsengf55eaa82017-11-29 15:51:28 -080094 private static final Set<PiTableId> NEXT_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_SIMPLE_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -070095 FabricConstants.TBL_HASHED_ID,
96 FabricConstants.TBL_BROADCAST_ID);
97
Yi Tseng1d842672017-11-28 16:06:52 -080098 private static final ImmutableMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP =
99 ImmutableMap.<Criterion.Type, PiMatchFieldId>builder()
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700100 .put(Criterion.Type.IN_PORT, FabricConstants.HF_STANDARD_METADATA_INGRESS_PORT_ID)
101 .put(Criterion.Type.ETH_DST, FabricConstants.HF_ETHERNET_DST_ADDR_ID)
102 .put(Criterion.Type.ETH_SRC, FabricConstants.HF_ETHERNET_SRC_ADDR_ID)
Yi Tseng1d842672017-11-28 16:06:52 -0800103 .put(Criterion.Type.ETH_TYPE, FabricConstants.HF_FABRIC_METADATA_ORIGINAL_ETHER_TYPE_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700104 .put(Criterion.Type.MPLS_LABEL, FabricConstants.HF_MPLS_LABEL_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700105 .put(Criterion.Type.VLAN_VID, FabricConstants.HF_VLAN_TAG_VLAN_ID_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700106 .put(Criterion.Type.IPV4_DST, FabricConstants.HF_IPV4_DST_ADDR_ID)
107 .put(Criterion.Type.IPV4_SRC, FabricConstants.HF_IPV4_SRC_ADDR_ID)
108 .put(Criterion.Type.IPV6_DST, FabricConstants.HF_IPV6_DST_ADDR_ID)
Yi Tseng1d842672017-11-28 16:06:52 -0800109 .put(Criterion.Type.TCP_SRC, FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID)
110 .put(Criterion.Type.TCP_DST, FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID)
111 .put(Criterion.Type.UDP_SRC, FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID)
112 .put(Criterion.Type.UDP_DST, FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700113 .put(Criterion.Type.IP_PROTO, FabricConstants.HF_FABRIC_METADATA_IP_PROTO_ID)
114 .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HF_ICMP_ICMP_TYPE_ID)
115 .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HF_ICMP_ICMP_CODE_ID)
116 .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()
120 .put(FabricConstants.HF_STANDARD_METADATA_INGRESS_PORT_ID, Criterion.Type.IN_PORT)
121 .put(FabricConstants.HF_ETHERNET_DST_ADDR_ID, Criterion.Type.ETH_DST)
122 .put(FabricConstants.HF_ETHERNET_SRC_ADDR_ID, Criterion.Type.ETH_SRC)
123 .put(FabricConstants.HF_FABRIC_METADATA_ORIGINAL_ETHER_TYPE_ID, Criterion.Type.ETH_TYPE)
Yi Tseng1d842672017-11-28 16:06:52 -0800124 .put(FabricConstants.HF_MPLS_LABEL_ID, Criterion.Type.MPLS_LABEL)
125 .put(FabricConstants.HF_VLAN_TAG_VLAN_ID_ID, Criterion.Type.VLAN_VID)
126 .put(FabricConstants.HF_IPV4_DST_ADDR_ID, Criterion.Type.IPV4_DST)
127 .put(FabricConstants.HF_IPV4_SRC_ADDR_ID, Criterion.Type.IPV4_SRC)
128 .put(FabricConstants.HF_IPV6_DST_ADDR_ID, Criterion.Type.IPV6_DST)
Yi Tseng1d842672017-11-28 16:06:52 -0800129 // FIXME: might be incorrect if we inverse the map....
130 .put(FabricConstants.HF_FABRIC_METADATA_L4_SRC_PORT_ID, Criterion.Type.UDP_SRC)
131 .put(FabricConstants.HF_FABRIC_METADATA_L4_DST_PORT_ID, Criterion.Type.UDP_DST)
Yi Tseng1d842672017-11-28 16:06:52 -0800132 .put(FabricConstants.HF_FABRIC_METADATA_IP_PROTO_ID, Criterion.Type.IP_PROTO)
133 .put(FabricConstants.HF_ICMP_ICMP_TYPE_ID, Criterion.Type.ICMPV6_TYPE)
134 .put(FabricConstants.HF_ICMP_ICMP_CODE_ID, Criterion.Type.ICMPV6_CODE)
135 .build();
136
Yi Tseng4fd28432018-02-01 14:48:03 -0800137 private static final ImmutableBiMap<PiTableId, PiCounterId> TABLE_COUNTER_MAP =
138 ImmutableBiMap.<PiTableId, PiCounterId>builder()
139 .put(FabricConstants.TBL_FWD_CLASSIFIER_ID, FabricConstants.CNT_FWD_CLASSIFIER_COUNTER_ID)
140 .put(FabricConstants.TBL_HASHED_ID, FabricConstants.CNT_HASHED_COUNTER_ID)
141 .put(FabricConstants.TBL_INGRESS_PORT_VLAN_ID, FabricConstants.CNT_INGRESS_PORT_VLAN_COUNTER_ID)
142 .put(FabricConstants.TBL_SIMPLE_ID, FabricConstants.CNT_SIMPLE_COUNTER_ID)
143 .put(FabricConstants.TBL_BRIDGING_ID, FabricConstants.CNT_BRIDGING_COUNTER_ID)
144 .put(FabricConstants.TBL_UNICAST_V4_ID, FabricConstants.CNT_UNICAST_V4_COUNTER_ID)
145 .put(FabricConstants.TBL_MPLS_ID, FabricConstants.CNT_MPLS_COUNTER_ID)
146 .build();
147 private static final String SUPPORT_TABLE_COUNTERS_PROP = "supportTableCounters";
148
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700149 @Override
150 public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) {
151 return Optional.ofNullable(CRITERION_MAP.get(type));
152 }
153
154 @Override
155 public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId fieldId) {
Yi Tseng1d842672017-11-28 16:06:52 -0800156 return Optional.ofNullable(INVERSE_CRITERION_MAP.get(fieldId));
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700157 }
158
159 @Override
160 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
161 return Optional.ofNullable(TABLE_ID_MAP.get(flowRuleTableId));
162 }
163
164 @Override
165 public Optional<Integer> mapPiTableId(PiTableId piTableId) {
166 return Optional.ofNullable(TABLE_ID_MAP.inverse().get(piTableId));
167 }
168
169 @Override
170 public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId)
171 throws PiInterpreterException {
172
173 if (FILTERING_CTRL_TBLS.contains(piTableId)) {
174 return FabricTreatmentInterpreter.mapFilteringTreatment(treatment);
175 } else if (FORWARDING_CTRL_TBLS.contains(piTableId)) {
176 return FabricTreatmentInterpreter.mapForwardingTreatment(treatment);
177 } else if (NEXT_CTRL_TBLS.contains(piTableId)) {
178 return FabricTreatmentInterpreter.mapNextTreatment(treatment);
179 } else {
180 throw new PiInterpreterException(String.format("Table %s unsupported", piTableId));
181 }
182 }
183
184 @Override
185 public Optional<PiCounterId> mapTableCounter(PiTableId piTableId) {
Yi Tseng4fd28432018-02-01 14:48:03 -0800186 Driver driver = handler().driver();
187 boolean supportTableCounters = Boolean.parseBoolean(driver.getProperty(SUPPORT_TABLE_COUNTERS_PROP));
188
189 if (supportTableCounters) {
190 return Optional.ofNullable(TABLE_COUNTER_MAP.get(piTableId));
191 } else {
192 return Optional.empty();
193 }
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700194 }
195
196 private PiPacketOperation createPiPacketOperation(DeviceId deviceId, ByteBuffer data, long portNumber)
197 throws PiInterpreterException {
198 PiControlMetadata metadata = createPacketMetadata(portNumber);
199 return PiPacketOperation.builder()
200 .forDevice(deviceId)
201 .withType(PACKET_OUT)
202 .withData(copyFrom(data))
203 .withMetadatas(ImmutableList.of(metadata))
204 .build();
205 }
206
207 private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
208 try {
209 return PiControlMetadata.builder()
210 .withId(FabricConstants.CTRL_META_EGRESS_PORT_ID)
211 .withValue(fit(copyFrom(portNumber), FabricConstants.PORT_BITWIDTH))
212 .build();
213 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
214 throw new PiInterpreterException(format(
215 "Port number %d too big, %s", portNumber, e.getMessage()));
216 }
217 }
218
219 @Override
220 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
221 throws PiInterpreterException {
222 DeviceId deviceId = packet.sendThrough();
223 TrafficTreatment treatment = packet.treatment();
224
225 // fabric.p4 supports only OUTPUT instructions.
226 List<Instructions.OutputInstruction> outInstructions = treatment
227 .allInstructions()
228 .stream()
229 .filter(i -> i.type().equals(OUTPUT))
230 .map(i -> (Instructions.OutputInstruction) i)
231 .collect(toList());
232
233 if (treatment.allInstructions().size() != outInstructions.size()) {
234 // There are other instructions that are not of type OUTPUT.
235 throw new PiInterpreterException("Treatment not supported: " + treatment);
236 }
237
238 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
239 for (Instructions.OutputInstruction outInst : outInstructions) {
240 if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
241 throw new PiInterpreterException(format(
242 "Output on logical port '%s' not supported", outInst.port()));
243 } else if (outInst.port().equals(FLOOD)) {
244 // Since fabric.p4 does not support flooding, we create a packet
245 // operation for each switch port.
246 final DeviceService deviceService = handler().get(DeviceService.class);
247 for (Port port : deviceService.getPorts(packet.sendThrough())) {
248 builder.add(createPiPacketOperation(deviceId, packet.data(), port.number().toLong()));
249 }
250 } else {
251 builder.add(createPiPacketOperation(deviceId, packet.data(), outInst.port().toLong()));
252 }
253 }
254 return builder.build();
255 }
256
257 @Override
258 public InboundPacket mapInboundPacket(PiPacketOperation packetIn) throws PiInterpreterException {
259 // Assuming that the packet is ethernet, which is fine since fabric.p4
260 // can deparse only ethernet packets.
261 DeviceId deviceId = packetIn.deviceId();
262 Ethernet ethPkt;
263 try {
264 ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0,
265 packetIn.data().size());
266 } catch (DeserializationException dex) {
267 throw new PiInterpreterException(dex.getMessage());
268 }
269
270 // Returns the ingress port packet metadata.
271 Optional<PiControlMetadata> packetMetadata = packetIn.metadatas()
272 .stream().filter(m -> m.id().equals(FabricConstants.CTRL_META_INGRESS_PORT_ID))
273 .findFirst();
274
275 if (packetMetadata.isPresent()) {
276 ImmutableByteSequence portByteSequence = packetMetadata.get().value();
277 short s = portByteSequence.asReadOnlyBuffer().getShort();
278 ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
279 ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
280 return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
281 } else {
282 throw new PiInterpreterException(format(
283 "Missing metadata '%s' in packet-in received from '%s': %s",
284 FabricConstants.CTRL_META_INGRESS_PORT_ID, deviceId, packetIn));
285 }
286 }
287}