blob: 6b057ccce1c5b2356270647651f8b9d150941bb4 [file] [log] [blame]
Carmelo Cascone59f57de2017-07-11 19:55:09 -04001/*
2 * Copyright 2017-present Open Networking Laboratory
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.drivers.bmv2;
18
19import com.google.common.collect.ImmutableBiMap;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020020import com.google.common.collect.ImmutableList;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040021import org.onlab.util.ImmutableByteSequence;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020022import org.onosproject.net.Port;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040023import org.onosproject.net.PortNumber;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020024import org.onosproject.net.device.DeviceService;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040025import org.onosproject.net.driver.AbstractHandlerBehaviour;
26import org.onosproject.net.flow.TrafficTreatment;
27import org.onosproject.net.flow.criteria.Criterion;
28import org.onosproject.net.flow.instructions.Instruction;
29import org.onosproject.net.flow.instructions.Instructions;
Andrea Campanella432f7182017-07-14 18:43:27 +020030import org.onosproject.net.packet.OutboundPacket;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040031import org.onosproject.net.pi.model.PiPipelineInterpreter;
32import org.onosproject.net.pi.runtime.PiAction;
33import org.onosproject.net.pi.runtime.PiActionId;
34import org.onosproject.net.pi.runtime.PiActionParam;
35import org.onosproject.net.pi.runtime.PiActionParamId;
36import org.onosproject.net.pi.runtime.PiHeaderFieldId;
Andrea Campanella432f7182017-07-14 18:43:27 +020037import org.onosproject.net.pi.runtime.PiPacketMetadata;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020038import org.onosproject.net.pi.runtime.PiPacketMetadataId;
39import org.onosproject.net.pi.runtime.PiPacketOperation;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040040import org.onosproject.net.pi.runtime.PiTableId;
41
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020042import java.nio.ByteBuffer;
Andrea Campanella432f7182017-07-14 18:43:27 +020043import java.util.Collection;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020044import java.util.List;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040045import java.util.Optional;
46
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020047import static java.util.stream.Collectors.toList;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040048import static org.onosproject.net.PortNumber.CONTROLLER;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020049import static org.onosproject.net.PortNumber.FLOOD;
50import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
51import static org.onosproject.net.pi.runtime.PiPacketOperation.Type.PACKET_OUT;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040052
53/**
54 * Interpreter implementation for the default pipeconf.
55 */
56public class Bmv2DefaultInterpreter extends AbstractHandlerBehaviour implements PiPipelineInterpreter {
57 private static final String TABLE0 = "table0";
58 private static final String SEND_TO_CPU = "send_to_cpu_0";
59 private static final String PORT = "port";
60 private static final String DROP = "_drop_0";
61 private static final String SET_EGRESS_PORT = "set_egress_port_0";
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020062 private static final String EGRESS_PORT = "egress_port";
63 private static final int PORT_NUMBER_BIT_WIDTH = 9;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040064
65 private static final PiHeaderFieldId IN_PORT_ID = PiHeaderFieldId.of("standard_metadata", "ingress_port");
66 private static final PiHeaderFieldId ETH_DST_ID = PiHeaderFieldId.of("ethernet_t", "dstAddr");
67 private static final PiHeaderFieldId ETH_SRC_ID = PiHeaderFieldId.of("ethernet_t", "srcAddr");
68 private static final PiHeaderFieldId ETH_TYPE_ID = PiHeaderFieldId.of("ethernet_t", "etherType");
69
Frank Wange254d6c2017-07-20 14:46:36 +080070 private static final ImmutableBiMap<Criterion.Type, PiHeaderFieldId> CRITERION_MAP =
71 new ImmutableBiMap.Builder<Criterion.Type, PiHeaderFieldId>()
72 .put(Criterion.Type.IN_PORT, IN_PORT_ID)
73 .put(Criterion.Type.ETH_DST, ETH_DST_ID)
74 .put(Criterion.Type.ETH_SRC, ETH_SRC_ID)
75 .put(Criterion.Type.ETH_TYPE, ETH_TYPE_ID)
76 .build();
Carmelo Cascone59f57de2017-07-11 19:55:09 -040077
78 private static final ImmutableBiMap<Integer, PiTableId> TABLE_MAP = ImmutableBiMap.of(
79 0, PiTableId.of(TABLE0));
80
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020081
Carmelo Cascone59f57de2017-07-11 19:55:09 -040082 @Override
Carmelo Casconef3a1a382017-07-27 12:04:39 -040083 public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId) throws PiInterpreterException {
Carmelo Cascone59f57de2017-07-11 19:55:09 -040084
85 if (treatment.allInstructions().size() == 0) {
86 // No instructions means drop for us.
87 return actionWithName(DROP);
88 } else if (treatment.allInstructions().size() > 1) {
89 // Otherwise, we understand treatments with only 1 instruction.
90 throw new PiPipelineInterpreter.PiInterpreterException("Treatment has multiple instructions");
91 }
92
93 Instruction instruction = treatment.allInstructions().get(0);
94
95 switch (instruction.type()) {
96 case OUTPUT:
97 Instructions.OutputInstruction outInstruction = (Instructions.OutputInstruction) instruction;
98 PortNumber port = outInstruction.port();
99 if (!port.isLogical()) {
100 PiAction.builder()
101 .withId(PiActionId.of(SET_EGRESS_PORT))
102 .withParameter(new PiActionParam(PiActionParamId.of(PORT),
Andrea Campanellafc1d34c2017-07-18 17:01:41 +0200103 ImmutableByteSequence.copyFrom(port.toLong())))
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400104 .build();
105 } else if (port.equals(CONTROLLER)) {
106 return actionWithName(SEND_TO_CPU);
107 } else {
108 throw new PiInterpreterException("Egress on logical port not supported: " + port);
109 }
110 case NOACTION:
111 return actionWithName(DROP);
112 default:
113 throw new PiInterpreterException("Instruction type not supported: " + instruction.type().name());
114 }
115 }
116
Andrea Campanella432f7182017-07-14 18:43:27 +0200117 @Override
Carmelo Casconef3a1a382017-07-27 12:04:39 -0400118 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet)
Andrea Campanella432f7182017-07-14 18:43:27 +0200119 throws PiInterpreterException {
Andrea Campanellafc1d34c2017-07-18 17:01:41 +0200120 TrafficTreatment treatment = packet.treatment();
121
122 // default.p4 supports only OUTPUT instructions.
123 List<Instructions.OutputInstruction> outInstructions = treatment.allInstructions()
124 .stream()
125 .filter(i -> i.type().equals(OUTPUT))
126 .map(i -> (Instructions.OutputInstruction) i)
127 .collect(toList());
128
129 if (treatment.allInstructions().size() != outInstructions.size()) {
130 // There are other instructions that are not of type OUTPUT
131 throw new PiInterpreterException("Treatment not supported: " + treatment);
132 }
133
134 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
135 for (Instructions.OutputInstruction outInst : outInstructions) {
136 if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
137 throw new PiInterpreterException("Logical port not supported: " +
138 outInst.port());
139 } else if (outInst.port().equals(FLOOD)) {
140 //Since default.p4 does not support flood for each port of the device
141 // create a packet operation to send the packet out of that specific port
142 for (Port port : handler().get(DeviceService.class).getPorts(packet.sendThrough())) {
143 builder.add(createPiPacketOperation(packet.data(), port.number().toLong()));
144 }
145 } else {
146 builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong()));
147 }
148 }
149 return builder.build();
150 }
151
152 private PiPacketOperation createPiPacketOperation(ByteBuffer data, long portNumber) throws PiInterpreterException {
153 //create the metadata
154 PiPacketMetadata metadata = createPacketMetadata(portNumber);
155
156 //Create the Packet operation
157 return PiPacketOperation.builder()
158 .withType(PACKET_OUT)
159 .withData(ImmutableByteSequence.copyFrom(data))
160 .withMetadatas(ImmutableList.of(metadata))
161 .build();
162 }
163
164 private PiPacketMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
165 ImmutableByteSequence portValue = ImmutableByteSequence.copyFrom(portNumber);
166 //FIXME remove hardcoded bitWidth and retrieve it from pipelineModel
167 try {
168 portValue = ImmutableByteSequence.fit(portValue, PORT_NUMBER_BIT_WIDTH);
169 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
170 throw new PiInterpreterException("Port number too big: {}" +
171 portNumber + " causes " + e.getMessage());
172 }
173 return PiPacketMetadata.builder()
174 .withId(PiPacketMetadataId.of(EGRESS_PORT))
175 .withValue(portValue)
176 .build();
Andrea Campanella432f7182017-07-14 18:43:27 +0200177 }
178
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400179 /**
180 * Returns an action instance with no runtime parameters.
181 */
182 private PiAction actionWithName(String name) {
183 return PiAction.builder().withId(PiActionId.of(name)).build();
184 }
185
186 @Override
187 public Optional<PiHeaderFieldId> mapCriterionType(Criterion.Type type) {
188 return Optional.ofNullable(CRITERION_MAP.get(type));
189 }
190
191 @Override
192 public Optional<Criterion.Type> mapPiHeaderFieldId(PiHeaderFieldId headerFieldId) {
193 return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId));
194 }
195
196 @Override
197 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
198 return Optional.ofNullable(TABLE_MAP.get(flowRuleTableId));
199 }
200}