blob: d58ff1697a78667e701451b4d362657c69a627c1 [file] [log] [blame]
wu5f6c5b82017-08-04 16:45:19 +08001/*
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.pi.demo.app.ecmp;
18
19import com.google.common.collect.ImmutableBiMap;
20import com.google.common.collect.ImmutableList;
21import org.onlab.packet.Ethernet;
22import org.onlab.util.ImmutableByteSequence;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Port;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.driver.AbstractHandlerBehaviour;
29import org.onosproject.net.flow.TrafficTreatment;
30import org.onosproject.net.flow.criteria.Criterion;
31import org.onosproject.net.flow.instructions.Instruction;
32import org.onosproject.net.flow.instructions.Instructions;
33import org.onosproject.net.packet.DefaultInboundPacket;
34import org.onosproject.net.packet.InboundPacket;
35import org.onosproject.net.packet.OutboundPacket;
36import org.onosproject.net.pi.model.PiPipelineInterpreter;
37import org.onosproject.net.pi.runtime.PiAction;
38import org.onosproject.net.pi.runtime.PiActionId;
39import org.onosproject.net.pi.runtime.PiActionParam;
40import org.onosproject.net.pi.runtime.PiActionParamId;
41import org.onosproject.net.pi.runtime.PiHeaderFieldId;
42import org.onosproject.net.pi.runtime.PiPacketMetadata;
43import org.onosproject.net.pi.runtime.PiPacketMetadataId;
44import org.onosproject.net.pi.runtime.PiPacketOperation;
45import org.onosproject.net.pi.runtime.PiTableId;
46
47import java.nio.ByteBuffer;
48import java.util.Collection;
49import java.util.List;
50import java.util.Optional;
51
52import static java.util.stream.Collectors.toList;
53import static org.onosproject.net.PortNumber.CONTROLLER;
54import static org.onosproject.net.PortNumber.FLOOD;
55import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
56import static org.onosproject.net.pi.runtime.PiPacketOperation.Type.PACKET_OUT;
57
58/**
59 * Implementation of a PiPipeline interpreter for the ecmp.json configuration.
60 */
61public class EcmpInterpreter extends AbstractHandlerBehaviour implements PiPipelineInterpreter {
62
63 protected static final String ECMP_METADATA_HEADER_NAME = "ecmp_metadata_t";
64 protected static final String ECMP_GROUP_ACTION_NAME = "ecmp_group";
65 protected static final String GROUP_ID = "group_id";
66 protected static final String SELECTOR = "selector";
67 protected static final String GROUP_SIZE = "groupSize";
68 protected static final String ECMP_GROUP_TABLE = "ecmp_group_table";
69 protected static final String TABLE0 = "table0";
70 private static final String SEND_TO_CPU = "send_to_cpu";
71 private static final String PORT = "port";
72 private static final String DROP = "drop";
73 private static final String SET_EGRESS_PORT = "set_egress_port";
74 private static final String EGRESS_PORT = "egress_port";
75 private static final int PORT_NUMBER_BIT_WIDTH = 9;
76
77 private static final PiHeaderFieldId IN_PORT_ID = PiHeaderFieldId.of("standard_metadata", "ingress_port");
78 private static final PiHeaderFieldId ETH_DST_ID = PiHeaderFieldId.of("ethernet", "dstAddr");
79 private static final PiHeaderFieldId ETH_SRC_ID = PiHeaderFieldId.of("ethernet", "srcAddr");
80 private static final PiHeaderFieldId ETH_TYPE_ID = PiHeaderFieldId.of("ethernet", "etherType");
81
82 private static final ImmutableBiMap<Criterion.Type, PiHeaderFieldId> CRITERION_MAP =
83 new ImmutableBiMap.Builder<Criterion.Type, PiHeaderFieldId>()
84 .put(Criterion.Type.IN_PORT, IN_PORT_ID)
85 .put(Criterion.Type.ETH_DST, ETH_DST_ID)
86 .put(Criterion.Type.ETH_SRC, ETH_SRC_ID)
87 .put(Criterion.Type.ETH_TYPE, ETH_TYPE_ID)
88 .build();
89
90 private static final ImmutableBiMap<Integer, PiTableId> TABLE_MAP = ImmutableBiMap.of(
91 0, PiTableId.of(TABLE0),
92 1, PiTableId.of(ECMP_GROUP_TABLE));
93
94 public static final String INGRESS_PORT = "ingress_port";
95
96 @Override
97 public Optional<Integer> mapPiTableId(PiTableId piTableId) {
98 return Optional.ofNullable(TABLE_MAP.inverse().get(piTableId));
99 }
100
101 @Override
102 public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId)
103 throws PiInterpreterException {
104
105 if (treatment.allInstructions().size() == 0) {
106 // No instructions means drop for us.
107 return actionWithName(DROP);
108 } else if (treatment.allInstructions().size() > 1) {
109 // Otherwise, we understand treatments with only 1 instruction.
110 throw new PiPipelineInterpreter.PiInterpreterException("Treatment has multiple instructions");
111 }
112
113 Instruction instruction = treatment.allInstructions().get(0);
114
115 switch (instruction.type()) {
116 case OUTPUT:
117 Instructions.OutputInstruction outInstruction = (Instructions.OutputInstruction) instruction;
118 PortNumber port = outInstruction.port();
119 if (!port.isLogical()) {
120 PiAction.builder()
121 .withId(PiActionId.of(SET_EGRESS_PORT))
122 .withParameter(new PiActionParam(PiActionParamId.of(PORT),
123 ImmutableByteSequence.copyFrom(port.toLong())))
124 .build();
125 } else if (port.equals(CONTROLLER)) {
126 return actionWithName(SEND_TO_CPU);
127 } else {
128 throw new PiInterpreterException("Egress on logical port not supported: " + port);
129 }
130 case NOACTION:
131 return actionWithName(DROP);
132 default:
133 throw new PiInterpreterException("Instruction type not supported: " + instruction.type().name());
134 }
135 }
136
137 private static PiAction actionWithName(String name) {
138 return PiAction.builder().withId(PiActionId.of(name)).build();
139 }
140
141 @Override
142 public Optional<PiHeaderFieldId> mapCriterionType(Criterion.Type type) {
143 return Optional.ofNullable(CRITERION_MAP.get(type));
144 }
145
146 @Override
147 public Optional<Criterion.Type> mapPiHeaderFieldId(PiHeaderFieldId headerFieldId) {
148 return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId));
149 }
150
151 @Override
152 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
153 return Optional.ofNullable(TABLE_MAP.get(flowRuleTableId));
154 }
155
156 @Override
157 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
158 throws PiInterpreterException {
159 TrafficTreatment treatment = packet.treatment();
160
161 // ecmp.p4 supports only OUTPUT instructions.
162 List<Instructions.OutputInstruction> outInstructions = treatment.allInstructions()
163 .stream()
164 .filter(i -> i.type().equals(OUTPUT))
165 .map(i -> (Instructions.OutputInstruction) i)
166 .collect(toList());
167
168 if (treatment.allInstructions().size() != outInstructions.size()) {
169 // There are other instructions that are not of type OUTPUT
170 throw new PiInterpreterException("Treatment not supported: " + treatment);
171 }
172
173 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
174 for (Instructions.OutputInstruction outInst : outInstructions) {
175 if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
176 throw new PiInterpreterException("Logical port not supported: " +
177 outInst.port());
178 } else if (outInst.port().equals(FLOOD)) {
179 //Since ecmp.p4 does not support flood for each port of the device
180 // create a packet operation to send the packet out of that specific port
181 for (Port port : handler().get(DeviceService.class).getPorts(packet.sendThrough())) {
182 builder.add(createPiPacketOperation(packet.data(), port.number().toLong()));
183 }
184 } else {
185 builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong()));
186 }
187 }
188 return builder.build();
189 }
190
191 private PiPacketOperation createPiPacketOperation(ByteBuffer data, long portNumber) throws PiInterpreterException {
192 //create the metadata
193 PiPacketMetadata metadata = createPacketMetadata(portNumber);
194
195 //Create the Packet operation
196 return PiPacketOperation.builder()
197 .withType(PACKET_OUT)
198 .withData(ImmutableByteSequence.copyFrom(data))
199 .withMetadatas(ImmutableList.of(metadata))
200 .build();
201 }
202
203 private PiPacketMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
204 ImmutableByteSequence portValue = ImmutableByteSequence.copyFrom(portNumber);
205 //FIXME remove hardcoded bitWidth and retrieve it from pipelineModel
206 try {
207 portValue = ImmutableByteSequence.fit(portValue, PORT_NUMBER_BIT_WIDTH);
208 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
209 throw new PiInterpreterException("Port number too big: {}" +
210 portNumber + " causes " + e.getMessage());
211 }
212 return PiPacketMetadata.builder()
213 .withId(PiPacketMetadataId.of(EGRESS_PORT))
214 .withValue(portValue)
215 .build();
216 }
217
218 @Override
219 public InboundPacket mapInboundPacket(DeviceId deviceId, PiPacketOperation packetInOperation)
220 throws PiInterpreterException {
221 //We are assuming that the packet is ethernet type
222 Ethernet ethPkt = new Ethernet();
223
224 ethPkt.deserialize(packetInOperation.data().asArray(), 0, packetInOperation.data().size());
225
226 //Returns the ingress port packet metadata
227 Optional<PiPacketMetadata> packetMetadata = packetInOperation.metadatas()
228 .stream().filter(metadata -> metadata.id().name().equals(INGRESS_PORT))
229 .findFirst();
230
231 if (packetMetadata.isPresent()) {
232
233 //Obtaining the ingress port as an immutable byte sequence
234 ImmutableByteSequence portByteSequence = packetMetadata.get().value();
235
236 //Converting immutableByteSequence to short
237 short s = portByteSequence.asReadOnlyBuffer().getShort();
238
239 ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
240
241 //FIXME should be optimizable with .asReadOnlyBytebuffer
242 ByteBuffer rawData = ByteBuffer.wrap(packetInOperation.data().asArray());
243 return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
244
245 } else {
246 throw new PiInterpreterException("Can't get packet metadata for" + INGRESS_PORT);
247 }
248 }
249}