blob: a2e78124cf39be459a4a61e2efebc900ac8ccfda [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.PiPipeconf;
32import org.onosproject.net.pi.model.PiPipelineInterpreter;
33import org.onosproject.net.pi.runtime.PiAction;
34import org.onosproject.net.pi.runtime.PiActionId;
35import org.onosproject.net.pi.runtime.PiActionParam;
36import org.onosproject.net.pi.runtime.PiActionParamId;
37import org.onosproject.net.pi.runtime.PiHeaderFieldId;
Andrea Campanella432f7182017-07-14 18:43:27 +020038import org.onosproject.net.pi.runtime.PiPacketMetadata;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020039import org.onosproject.net.pi.runtime.PiPacketMetadataId;
40import org.onosproject.net.pi.runtime.PiPacketOperation;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040041import org.onosproject.net.pi.runtime.PiTableAction;
42import org.onosproject.net.pi.runtime.PiTableId;
43
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020044import java.nio.ByteBuffer;
Andrea Campanella432f7182017-07-14 18:43:27 +020045import java.util.Collection;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020046import java.util.List;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040047import java.util.Optional;
48
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020049import static java.util.stream.Collectors.toList;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040050import static org.onosproject.net.PortNumber.CONTROLLER;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020051import static org.onosproject.net.PortNumber.FLOOD;
52import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT;
53import static org.onosproject.net.pi.runtime.PiPacketOperation.Type.PACKET_OUT;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040054
55/**
56 * Interpreter implementation for the default pipeconf.
57 */
58public class Bmv2DefaultInterpreter extends AbstractHandlerBehaviour implements PiPipelineInterpreter {
59 private static final String TABLE0 = "table0";
60 private static final String SEND_TO_CPU = "send_to_cpu_0";
61 private static final String PORT = "port";
62 private static final String DROP = "_drop_0";
63 private static final String SET_EGRESS_PORT = "set_egress_port_0";
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020064 private static final String EGRESS_PORT = "egress_port";
65 private static final int PORT_NUMBER_BIT_WIDTH = 9;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040066
67 private static final PiHeaderFieldId IN_PORT_ID = PiHeaderFieldId.of("standard_metadata", "ingress_port");
68 private static final PiHeaderFieldId ETH_DST_ID = PiHeaderFieldId.of("ethernet_t", "dstAddr");
69 private static final PiHeaderFieldId ETH_SRC_ID = PiHeaderFieldId.of("ethernet_t", "srcAddr");
70 private static final PiHeaderFieldId ETH_TYPE_ID = PiHeaderFieldId.of("ethernet_t", "etherType");
71
Frank Wange254d6c2017-07-20 14:46:36 +080072 private static final ImmutableBiMap<Criterion.Type, PiHeaderFieldId> CRITERION_MAP =
73 new ImmutableBiMap.Builder<Criterion.Type, PiHeaderFieldId>()
74 .put(Criterion.Type.IN_PORT, IN_PORT_ID)
75 .put(Criterion.Type.ETH_DST, ETH_DST_ID)
76 .put(Criterion.Type.ETH_SRC, ETH_SRC_ID)
77 .put(Criterion.Type.ETH_TYPE, ETH_TYPE_ID)
78 .build();
Carmelo Cascone59f57de2017-07-11 19:55:09 -040079
80 private static final ImmutableBiMap<Integer, PiTableId> TABLE_MAP = ImmutableBiMap.of(
81 0, PiTableId.of(TABLE0));
82
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020083
Carmelo Cascone59f57de2017-07-11 19:55:09 -040084 @Override
85 public PiTableAction mapTreatment(TrafficTreatment treatment, PiPipeconf pipeconf) throws PiInterpreterException {
86
87 if (treatment.allInstructions().size() == 0) {
88 // No instructions means drop for us.
89 return actionWithName(DROP);
90 } else if (treatment.allInstructions().size() > 1) {
91 // Otherwise, we understand treatments with only 1 instruction.
92 throw new PiPipelineInterpreter.PiInterpreterException("Treatment has multiple instructions");
93 }
94
95 Instruction instruction = treatment.allInstructions().get(0);
96
97 switch (instruction.type()) {
98 case OUTPUT:
99 Instructions.OutputInstruction outInstruction = (Instructions.OutputInstruction) instruction;
100 PortNumber port = outInstruction.port();
101 if (!port.isLogical()) {
102 PiAction.builder()
103 .withId(PiActionId.of(SET_EGRESS_PORT))
104 .withParameter(new PiActionParam(PiActionParamId.of(PORT),
Andrea Campanellafc1d34c2017-07-18 17:01:41 +0200105 ImmutableByteSequence.copyFrom(port.toLong())))
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400106 .build();
107 } else if (port.equals(CONTROLLER)) {
108 return actionWithName(SEND_TO_CPU);
109 } else {
110 throw new PiInterpreterException("Egress on logical port not supported: " + port);
111 }
112 case NOACTION:
113 return actionWithName(DROP);
114 default:
115 throw new PiInterpreterException("Instruction type not supported: " + instruction.type().name());
116 }
117 }
118
Andrea Campanella432f7182017-07-14 18:43:27 +0200119 @Override
Andrea Campanellafc1d34c2017-07-18 17:01:41 +0200120 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet, PiPipeconf pipeconf)
Andrea Campanella432f7182017-07-14 18:43:27 +0200121 throws PiInterpreterException {
Andrea Campanellafc1d34c2017-07-18 17:01:41 +0200122 TrafficTreatment treatment = packet.treatment();
123
124 // default.p4 supports only OUTPUT instructions.
125 List<Instructions.OutputInstruction> outInstructions = treatment.allInstructions()
126 .stream()
127 .filter(i -> i.type().equals(OUTPUT))
128 .map(i -> (Instructions.OutputInstruction) i)
129 .collect(toList());
130
131 if (treatment.allInstructions().size() != outInstructions.size()) {
132 // There are other instructions that are not of type OUTPUT
133 throw new PiInterpreterException("Treatment not supported: " + treatment);
134 }
135
136 ImmutableList.Builder<PiPacketOperation> builder = ImmutableList.builder();
137 for (Instructions.OutputInstruction outInst : outInstructions) {
138 if (outInst.port().isLogical() && !outInst.port().equals(FLOOD)) {
139 throw new PiInterpreterException("Logical port not supported: " +
140 outInst.port());
141 } else if (outInst.port().equals(FLOOD)) {
142 //Since default.p4 does not support flood for each port of the device
143 // create a packet operation to send the packet out of that specific port
144 for (Port port : handler().get(DeviceService.class).getPorts(packet.sendThrough())) {
145 builder.add(createPiPacketOperation(packet.data(), port.number().toLong()));
146 }
147 } else {
148 builder.add(createPiPacketOperation(packet.data(), outInst.port().toLong()));
149 }
150 }
151 return builder.build();
152 }
153
154 private PiPacketOperation createPiPacketOperation(ByteBuffer data, long portNumber) throws PiInterpreterException {
155 //create the metadata
156 PiPacketMetadata metadata = createPacketMetadata(portNumber);
157
158 //Create the Packet operation
159 return PiPacketOperation.builder()
160 .withType(PACKET_OUT)
161 .withData(ImmutableByteSequence.copyFrom(data))
162 .withMetadatas(ImmutableList.of(metadata))
163 .build();
164 }
165
166 private PiPacketMetadata createPacketMetadata(long portNumber) throws PiInterpreterException {
167 ImmutableByteSequence portValue = ImmutableByteSequence.copyFrom(portNumber);
168 //FIXME remove hardcoded bitWidth and retrieve it from pipelineModel
169 try {
170 portValue = ImmutableByteSequence.fit(portValue, PORT_NUMBER_BIT_WIDTH);
171 } catch (ImmutableByteSequence.ByteSequenceTrimException e) {
172 throw new PiInterpreterException("Port number too big: {}" +
173 portNumber + " causes " + e.getMessage());
174 }
175 return PiPacketMetadata.builder()
176 .withId(PiPacketMetadataId.of(EGRESS_PORT))
177 .withValue(portValue)
178 .build();
Andrea Campanella432f7182017-07-14 18:43:27 +0200179 }
180
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400181 /**
182 * Returns an action instance with no runtime parameters.
183 */
184 private PiAction actionWithName(String name) {
185 return PiAction.builder().withId(PiActionId.of(name)).build();
186 }
187
188 @Override
189 public Optional<PiHeaderFieldId> mapCriterionType(Criterion.Type type) {
190 return Optional.ofNullable(CRITERION_MAP.get(type));
191 }
192
193 @Override
194 public Optional<Criterion.Type> mapPiHeaderFieldId(PiHeaderFieldId headerFieldId) {
195 return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId));
196 }
197
198 @Override
199 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
200 return Optional.ofNullable(TABLE_MAP.get(flowRuleTableId));
201 }
202}