Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.bmv2; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableBiMap; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 21 | import org.onlab.util.ImmutableByteSequence; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 22 | import org.onosproject.net.Port; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 23 | import org.onosproject.net.PortNumber; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 24 | import org.onosproject.net.device.DeviceService; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 25 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
| 27 | import org.onosproject.net.flow.criteria.Criterion; |
| 28 | import org.onosproject.net.flow.instructions.Instruction; |
| 29 | import org.onosproject.net.flow.instructions.Instructions; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 30 | import org.onosproject.net.packet.OutboundPacket; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 31 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 32 | import org.onosproject.net.pi.runtime.PiAction; |
| 33 | import org.onosproject.net.pi.runtime.PiActionId; |
| 34 | import org.onosproject.net.pi.runtime.PiActionParam; |
| 35 | import org.onosproject.net.pi.runtime.PiActionParamId; |
| 36 | import org.onosproject.net.pi.runtime.PiHeaderFieldId; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 37 | import org.onosproject.net.pi.runtime.PiPacketMetadata; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 38 | import org.onosproject.net.pi.runtime.PiPacketMetadataId; |
| 39 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 40 | import org.onosproject.net.pi.runtime.PiTableId; |
| 41 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 42 | import java.nio.ByteBuffer; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 43 | import java.util.Collection; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 44 | import java.util.List; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 45 | import java.util.Optional; |
| 46 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 47 | import static java.util.stream.Collectors.toList; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 48 | import static org.onosproject.net.PortNumber.CONTROLLER; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 49 | import static org.onosproject.net.PortNumber.FLOOD; |
| 50 | import static org.onosproject.net.flow.instructions.Instruction.Type.OUTPUT; |
| 51 | import static org.onosproject.net.pi.runtime.PiPacketOperation.Type.PACKET_OUT; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Interpreter implementation for the default pipeconf. |
| 55 | */ |
| 56 | public 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 Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 62 | private static final String EGRESS_PORT = "egress_port"; |
| 63 | private static final int PORT_NUMBER_BIT_WIDTH = 9; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 64 | |
| 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 Wang | e254d6c | 2017-07-20 14:46:36 +0800 | [diff] [blame] | 70 | 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 Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 77 | |
| 78 | private static final ImmutableBiMap<Integer, PiTableId> TABLE_MAP = ImmutableBiMap.of( |
| 79 | 0, PiTableId.of(TABLE0)); |
| 80 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 81 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 82 | @Override |
Carmelo Cascone | f3a1a38 | 2017-07-27 12:04:39 -0400 | [diff] [blame^] | 83 | public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId) throws PiInterpreterException { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 84 | |
| 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 Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 103 | ImmutableByteSequence.copyFrom(port.toLong()))) |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 104 | .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 Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 117 | @Override |
Carmelo Cascone | f3a1a38 | 2017-07-27 12:04:39 -0400 | [diff] [blame^] | 118 | public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 119 | throws PiInterpreterException { |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 120 | 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 Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 179 | /** |
| 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 | } |