blob: 69a686ee1e7e2212e1fe22a130fe76a03d619433 [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;
21import com.google.common.collect.ImmutableSet;
22import org.onlab.packet.DeserializationException;
23import org.onlab.packet.Ethernet;
24import org.onlab.util.ImmutableByteSequence;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Port;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.device.DeviceService;
30import org.onosproject.net.driver.AbstractHandlerBehaviour;
31import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.net.flow.criteria.Criterion;
33import org.onosproject.net.flow.instructions.Instructions;
34import org.onosproject.net.packet.DefaultInboundPacket;
35import org.onosproject.net.packet.InboundPacket;
36import org.onosproject.net.packet.OutboundPacket;
37import org.onosproject.net.pi.model.PiCounterId;
38import 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;
54import static org.onlab.util.ImmutableByteSequence.fit;
55import static org.onosproject.net.PortNumber.FLOOD;
56import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
57import static org.onosproject.net.pi.model.PiPacketOperationType.PACKET_OUT;
58
59/**
60 * Interpreter for fabric pipeline.
61 */
62public class FabricInterpreter extends AbstractHandlerBehaviour
63 implements PiPipelineInterpreter {
64 private static final ImmutableBiMap<Integer, PiTableId> TABLE_ID_MAP =
65 ImmutableBiMap.<Integer, PiTableId>builder()
66 // Filtering
67 .put(0, FabricConstants.TBL_INGRESS_PORT_VLAN_ID)
68 .put(1, FabricConstants.TBL_FWD_CLASSIFIER_ID)
69 // Forwarding
70 .put(2, FabricConstants.TBL_MPLS_ID)
71 .put(3, FabricConstants.TBL_UNICAST_V4_ID)
72 .put(4, FabricConstants.TBL_UNICAST_V6_ID)
73 .put(5, FabricConstants.TBL_MULTICAST_V4_ID)
74 .put(6, FabricConstants.TBL_MULTICAST_V6_ID)
75 .put(7, FabricConstants.TBL_BRIDGING_ID)
76 .put(8, FabricConstants.TBL_ACL_ID)
77 // Next
Yi Tsengf55eaa82017-11-29 15:51:28 -080078 .put(9, FabricConstants.TBL_SIMPLE_ID)
79 .put(10, FabricConstants.TBL_HASHED_ID)
80 .put(11, FabricConstants.TBL_BROADCAST_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070081 .build();
82
83 private static final Set<PiTableId> FILTERING_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_INGRESS_PORT_VLAN_ID,
84 FabricConstants.TBL_FWD_CLASSIFIER_ID);
85 private static final Set<PiTableId> FORWARDING_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_MPLS_ID,
86 FabricConstants.TBL_UNICAST_V4_ID,
87 FabricConstants.TBL_UNICAST_V6_ID,
88 FabricConstants.TBL_MULTICAST_V4_ID,
89 FabricConstants.TBL_MULTICAST_V6_ID,
90 FabricConstants.TBL_BRIDGING_ID,
91 FabricConstants.TBL_ACL_ID);
Yi Tsengf55eaa82017-11-29 15:51:28 -080092 private static final Set<PiTableId> NEXT_CTRL_TBLS = ImmutableSet.of(FabricConstants.TBL_SIMPLE_ID,
Yi Tsengfa4a1c72017-11-03 10:22:38 -070093 FabricConstants.TBL_HASHED_ID,
94 FabricConstants.TBL_BROADCAST_ID);
95
96 private static final ImmutableBiMap<PiTableId, PiCounterId> TABLE_COUNTER_MAP =
97 ImmutableBiMap.<PiTableId, PiCounterId>builder()
98 .put(FabricConstants.TBL_MULTICAST_V4_ID, FabricConstants.CNT_MULTICAST_V4_COUNTER_ID)
99 .put(FabricConstants.TBL_MULTICAST_V6_ID, FabricConstants.CNT_MULTICAST_V6_COUNTER_ID)
100 .put(FabricConstants.TBL_FWD_CLASSIFIER_ID, FabricConstants.CNT_FWD_CLASSIFIER_COUNTER_ID)
101 .put(FabricConstants.TBL_ACL_ID, FabricConstants.CNT_ACL_COUNTER_ID)
102 .put(FabricConstants.TBL_BROADCAST_ID, FabricConstants.CNT_BROADCAST_COUNTER_ID)
103 .put(FabricConstants.TBL_HASHED_ID, FabricConstants.CNT_HASHED_COUNTER_ID)
104 .put(FabricConstants.TBL_INGRESS_PORT_VLAN_ID, FabricConstants.CNT_INGRESS_PORT_VLAN_COUNTER_ID)
Yi Tsengfa4a1c72017-11-03 10:22:38 -0700105 .put(FabricConstants.TBL_UNICAST_V6_ID, FabricConstants.CNT_UNICAST_V6_COUNTER_ID)
106 .put(FabricConstants.TBL_SIMPLE_ID, FabricConstants.CNT_SIMPLE_COUNTER_ID)
107 .put(FabricConstants.TBL_BRIDGING_ID, FabricConstants.CNT_BRIDGING_COUNTER_ID)
108 .put(FabricConstants.TBL_UNICAST_V4_ID, FabricConstants.CNT_UNICAST_V4_COUNTER_ID)
109 .put(FabricConstants.TBL_MPLS_ID, FabricConstants.CNT_MPLS_COUNTER_ID)
110 .build();
111 private static final ImmutableBiMap<Criterion.Type, PiMatchFieldId> CRITERION_MAP =
112 ImmutableBiMap.<Criterion.Type, PiMatchFieldId>builder()
113 .put(Criterion.Type.IN_PORT, FabricConstants.HF_STANDARD_METADATA_INGRESS_PORT_ID)
114 .put(Criterion.Type.ETH_DST, FabricConstants.HF_ETHERNET_DST_ADDR_ID)
115 .put(Criterion.Type.ETH_SRC, FabricConstants.HF_ETHERNET_SRC_ADDR_ID)
116 .put(Criterion.Type.ETH_TYPE, FabricConstants.HF_ETHERNET_ETHER_TYPE_ID)
117 .put(Criterion.Type.MPLS_BOS, FabricConstants.HF_MPLS_BOS_ID)
118 .put(Criterion.Type.MPLS_LABEL, FabricConstants.HF_MPLS_LABEL_ID)
119 .put(Criterion.Type.MPLS_TC, FabricConstants.HF_MPLS_TC_ID)
120 .put(Criterion.Type.VLAN_VID, FabricConstants.HF_VLAN_TAG_VLAN_ID_ID)
121 .put(Criterion.Type.VLAN_PCP, FabricConstants.HF_VLAN_TAG_PRI_ID)
122 .put(Criterion.Type.IPV4_DST, FabricConstants.HF_IPV4_DST_ADDR_ID)
123 .put(Criterion.Type.IPV4_SRC, FabricConstants.HF_IPV4_SRC_ADDR_ID)
124 .put(Criterion.Type.IPV6_DST, FabricConstants.HF_IPV6_DST_ADDR_ID)
125 .put(Criterion.Type.IPV6_SRC, FabricConstants.HF_IPV6_SRC_ADDR_ID)
126 .put(Criterion.Type.TCP_SRC, FabricConstants.HF_TCP_SRC_PORT_ID)
127 .put(Criterion.Type.TCP_DST, FabricConstants.HF_TCP_DST_PORT_ID)
128 .put(Criterion.Type.UDP_SRC, FabricConstants.HF_UDP_SRC_PORT_ID)
129 .put(Criterion.Type.UDP_DST, FabricConstants.HF_UDP_DST_PORT_ID)
130 .put(Criterion.Type.IP_PROTO, FabricConstants.HF_FABRIC_METADATA_IP_PROTO_ID)
131 .put(Criterion.Type.ICMPV6_TYPE, FabricConstants.HF_ICMP_ICMP_TYPE_ID)
132 .put(Criterion.Type.ICMPV6_CODE, FabricConstants.HF_ICMP_ICMP_CODE_ID)
133 .build();
134
135 @Override
136 public Optional<PiMatchFieldId> mapCriterionType(Criterion.Type type) {
137 return Optional.ofNullable(CRITERION_MAP.get(type));
138 }
139
140 @Override
141 public Optional<Criterion.Type> mapPiMatchFieldId(PiMatchFieldId fieldId) {
142 return Optional.ofNullable(CRITERION_MAP.inverse().get(fieldId));
143 }
144
145 @Override
146 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
147 return Optional.ofNullable(TABLE_ID_MAP.get(flowRuleTableId));
148 }
149
150 @Override
151 public Optional<Integer> mapPiTableId(PiTableId piTableId) {
152 return Optional.ofNullable(TABLE_ID_MAP.inverse().get(piTableId));
153 }
154
155 @Override
156 public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId)
157 throws PiInterpreterException {
158
159 if (FILTERING_CTRL_TBLS.contains(piTableId)) {
160 return FabricTreatmentInterpreter.mapFilteringTreatment(treatment);
161 } else if (FORWARDING_CTRL_TBLS.contains(piTableId)) {
162 return FabricTreatmentInterpreter.mapForwardingTreatment(treatment);
163 } else if (NEXT_CTRL_TBLS.contains(piTableId)) {
164 return FabricTreatmentInterpreter.mapNextTreatment(treatment);
165 } else {
166 throw new PiInterpreterException(String.format("Table %s unsupported", piTableId));
167 }
168 }
169
170 @Override
171 public Optional<PiCounterId> mapTableCounter(PiTableId piTableId) {
172 return Optional.ofNullable(TABLE_COUNTER_MAP.get(piTableId));
173 }
174
175 private PiPacketOperation createPiPacketOperation(DeviceId deviceId, ByteBuffer data, long portNumber)
176 throws PiInterpreterException {
177 PiControlMetadata metadata = createPacketMetadata(portNumber);
178 return PiPacketOperation.builder()
179 .forDevice(deviceId)
180 .withType(PACKET_OUT)
181 .withData(copyFrom(data))
182 .withMetadatas(ImmutableList.of(metadata))
183 .build();
184 }
185
186 private PiControlMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
187 try {
188 return PiControlMetadata.builder()
189 .withId(FabricConstants.CTRL_META_EGRESS_PORT_ID)
190 .withValue(fit(copyFrom(portNumber), FabricConstants.PORT_BITWIDTH))
191 .build();
192 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
193 throw new PiInterpreterException(format(
194 "Port number %d too big, %s", portNumber, e.getMessage()));
195 }
196 }
197
198 @Override
199 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
200 throws PiInterpreterException {
201 DeviceId deviceId = packet.sendThrough();
202 TrafficTreatment treatment = packet.treatment();
203
204 // fabric.p4 supports only OUTPUT instructions.
205 List<Instructions.OutputInstruction> outInstructions = treatment
206 .allInstructions()
207 .stream()
208 .filter(i -> i.type().equals(OUTPUT))
209 .map(i -> (Instructions.OutputInstruction) i)
210 .collect(toList());
211
212 if (treatment.allInstructions().size() != outInstructions.size()) {
213 // There are other instructions that are not of type OUTPUT.
214 throw new PiInterpreterException("Treatment not supported: " + treatment);
215 }
216
217 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
218 for (Instructions.OutputInstruction outInst : outInstructions) {
219 if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
220 throw new PiInterpreterException(format(
221 "Output on logical port '%s' not supported", outInst.port()));
222 } else if (outInst.port().equals(FLOOD)) {
223 // Since fabric.p4 does not support flooding, we create a packet
224 // operation for each switch port.
225 final DeviceService deviceService = handler().get(DeviceService.class);
226 for (Port port : deviceService.getPorts(packet.sendThrough())) {
227 builder.add(createPiPacketOperation(deviceId, packet.data(), port.number().toLong()));
228 }
229 } else {
230 builder.add(createPiPacketOperation(deviceId, packet.data(), outInst.port().toLong()));
231 }
232 }
233 return builder.build();
234 }
235
236 @Override
237 public InboundPacket mapInboundPacket(PiPacketOperation packetIn) throws PiInterpreterException {
238 // Assuming that the packet is ethernet, which is fine since fabric.p4
239 // can deparse only ethernet packets.
240 DeviceId deviceId = packetIn.deviceId();
241 Ethernet ethPkt;
242 try {
243 ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0,
244 packetIn.data().size());
245 } catch (DeserializationException dex) {
246 throw new PiInterpreterException(dex.getMessage());
247 }
248
249 // Returns the ingress port packet metadata.
250 Optional<PiControlMetadata> packetMetadata = packetIn.metadatas()
251 .stream().filter(m -> m.id().equals(FabricConstants.CTRL_META_INGRESS_PORT_ID))
252 .findFirst();
253
254 if (packetMetadata.isPresent()) {
255 ImmutableByteSequence portByteSequence = packetMetadata.get().value();
256 short s = portByteSequence.asReadOnlyBuffer().getShort();
257 ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
258 ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
259 return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
260 } else {
261 throw new PiInterpreterException(format(
262 "Missing metadata '%s' in packet-in received from '%s': %s",
263 FabricConstants.CTRL_META_INGRESS_PORT_ID, deviceId, packetIn));
264 }
265 }
266}