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