Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 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.net.pi.impl; |
| 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 | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 21 | import org.onlab.util.ImmutableByteSequence; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 23 | import org.onosproject.net.PortNumber; |
| 24 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 25 | import org.onosproject.net.flow.TrafficTreatment; |
| 26 | import org.onosproject.net.flow.criteria.Criterion; |
| 27 | import org.onosproject.net.flow.instructions.Instruction; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 28 | import org.onosproject.net.packet.InboundPacket; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 29 | import org.onosproject.net.packet.OutboundPacket; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 30 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 31 | import org.onosproject.net.pi.runtime.PiAction; |
| 32 | import org.onosproject.net.pi.runtime.PiActionId; |
| 33 | import org.onosproject.net.pi.runtime.PiActionParam; |
| 34 | import org.onosproject.net.pi.runtime.PiActionParamId; |
| 35 | import org.onosproject.net.pi.runtime.PiHeaderFieldId; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 36 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 37 | import org.onosproject.net.pi.runtime.PiTableId; |
| 38 | |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 39 | import java.util.Collection; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 40 | import java.util.Optional; |
| 41 | |
| 42 | import static org.onosproject.net.PortNumber.CONTROLLER; |
| 43 | import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 44 | |
| 45 | /** |
| 46 | * Mock interpreter implementation. |
| 47 | */ |
| 48 | public class MockInterpreter extends AbstractHandlerBehaviour implements PiPipelineInterpreter { |
| 49 | |
| 50 | static final String TABLE0 = "table0"; |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 51 | static final String SEND_TO_CPU = "send_to_cpu"; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 52 | static final String PORT = "port"; |
Carmelo Cascone | eb01812 | 2017-09-06 13:16:03 +0200 | [diff] [blame^] | 53 | static final String DROP = "_drop"; |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 54 | static final String SET_EGRESS_PORT = "set_egress_port"; |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 55 | |
| 56 | static final PiHeaderFieldId IN_PORT_ID = PiHeaderFieldId.of("standard_metadata", "ingress_port"); |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 57 | static final PiHeaderFieldId ETH_DST_ID = PiHeaderFieldId.of("ethernet", "dstAddr"); |
| 58 | static final PiHeaderFieldId ETH_SRC_ID = PiHeaderFieldId.of("ethernet", "srcAddr"); |
| 59 | static final PiHeaderFieldId ETH_TYPE_ID = PiHeaderFieldId.of("ethernet", "etherType"); |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 60 | |
| 61 | private static final ImmutableBiMap<Criterion.Type, PiHeaderFieldId> CRITERION_MAP = ImmutableBiMap.of( |
| 62 | Criterion.Type.IN_PORT, IN_PORT_ID, |
| 63 | Criterion.Type.ETH_DST, ETH_DST_ID, |
| 64 | Criterion.Type.ETH_SRC, ETH_SRC_ID, |
| 65 | Criterion.Type.ETH_TYPE, ETH_TYPE_ID); |
| 66 | |
| 67 | private static final ImmutableBiMap<Integer, PiTableId> TABLE_MAP = ImmutableBiMap.of( |
| 68 | 0, PiTableId.of(TABLE0)); |
| 69 | |
| 70 | @Override |
Carmelo Cascone | f3a1a38 | 2017-07-27 12:04:39 -0400 | [diff] [blame] | 71 | public PiAction mapTreatment(TrafficTreatment treatment, PiTableId piTableId) throws PiInterpreterException { |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 72 | |
| 73 | if (treatment.allInstructions().size() == 0) { |
| 74 | // No instructions means drop for us. |
| 75 | return actionWithName(DROP); |
| 76 | } else if (treatment.allInstructions().size() > 1) { |
| 77 | // Otherwise, we understand treatments with only 1 instruction. |
| 78 | throw new PiPipelineInterpreter.PiInterpreterException("Treatment has multiple instructions"); |
| 79 | } |
| 80 | |
| 81 | Instruction instruction = treatment.allInstructions().get(0); |
| 82 | |
| 83 | switch (instruction.type()) { |
| 84 | case OUTPUT: |
| 85 | OutputInstruction outInstruction = (OutputInstruction) instruction; |
| 86 | PortNumber port = outInstruction.port(); |
| 87 | if (!port.isLogical()) { |
| 88 | PiAction.builder() |
| 89 | .withId(PiActionId.of(SET_EGRESS_PORT)) |
| 90 | .withParameter(new PiActionParam(PiActionParamId.of(PORT), |
| 91 | ImmutableByteSequence.copyFrom(port.toLong()))) |
| 92 | .build(); |
| 93 | } else if (port.equals(CONTROLLER)) { |
| 94 | return actionWithName(SEND_TO_CPU); |
| 95 | } else { |
| 96 | throw new PiInterpreterException("Egress on logical port not supported: " + port); |
| 97 | } |
| 98 | case NOACTION: |
| 99 | return actionWithName(DROP); |
| 100 | default: |
| 101 | throw new PiInterpreterException("Instruction type not supported: " + instruction.type().name()); |
| 102 | } |
| 103 | } |
| 104 | |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 105 | @Override |
Carmelo Cascone | f3a1a38 | 2017-07-27 12:04:39 -0400 | [diff] [blame] | 106 | public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet) |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 107 | throws PiInterpreterException { |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 108 | return ImmutableList.of(); |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 111 | @Override |
| 112 | public InboundPacket mapInboundPacket(DeviceId deviceId, PiPacketOperation packetInOperation) |
| 113 | throws PiInterpreterException { |
| 114 | return null; |
| 115 | } |
| 116 | |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 117 | /** |
| 118 | * Returns an action instance with no runtime parameters. |
| 119 | */ |
| 120 | private PiAction actionWithName(String name) { |
| 121 | return PiAction.builder().withId(PiActionId.of(name)).build(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Optional<PiHeaderFieldId> mapCriterionType(Criterion.Type type) { |
| 126 | return Optional.ofNullable(CRITERION_MAP.get(type)); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public Optional<Criterion.Type> mapPiHeaderFieldId(PiHeaderFieldId headerFieldId) { |
| 131 | return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId)); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) { |
| 136 | return Optional.ofNullable(TABLE_MAP.get(flowRuleTableId)); |
| 137 | } |
| 138 | |
Carmelo Cascone | 0b22d8f | 2017-07-31 07:22:27 +0200 | [diff] [blame] | 139 | @Override |
| 140 | public Optional<Integer> mapPiTableId(PiTableId piTableId) { |
| 141 | return Optional.ofNullable(TABLE_MAP.inverse().get(piTableId)); |
| 142 | } |
| 143 | |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 144 | } |