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