Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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.translators; |
| 18 | |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 19 | import com.google.common.annotations.Beta; |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 20 | import com.google.common.collect.ImmutableMap; |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 21 | import org.onlab.util.ImmutableByteSequence; |
| 22 | import org.onosproject.bmv2.api.model.Bmv2Model; |
| 23 | import org.onosproject.bmv2.api.runtime.Bmv2Action; |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 24 | import org.onosproject.net.PortNumber; |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 25 | import org.onosproject.net.flow.TrafficTreatment; |
| 26 | import org.onosproject.net.flow.criteria.Criterion; |
| 27 | import org.onosproject.net.flow.instructions.Instruction; |
| 28 | import org.onosproject.net.flow.instructions.Instructions; |
| 29 | |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 30 | import java.util.Map; |
| 31 | |
| 32 | /** |
| 33 | * Implementation of a Bmv2 flow rule translator configuration for the |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 34 | * simple.p4 model. |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 35 | */ |
| 36 | @Beta |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 37 | public class Bmv2SimpleTranslatorConfig extends Bmv2DefaultTranslatorConfig { |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 38 | |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 39 | // Lazily populate field map. |
| 40 | private static final Map<String, Criterion.Type> FIELD_MAP = ImmutableMap.of( |
| 41 | "standard_metadata.ingress_port", Criterion.Type.IN_PORT, |
| 42 | "ethernet.dstAddr", Criterion.Type.ETH_DST, |
| 43 | "ethernet.srcAddr", Criterion.Type.ETH_SRC, |
| 44 | "ethernet.etherType", Criterion.Type.ETH_TYPE); |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Creates a new simple pipeline translator configuration. |
| 48 | */ |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 49 | public Bmv2SimpleTranslatorConfig(Bmv2Model model) { |
| 50 | // Populate fieldMap. |
| 51 | super(model, FIELD_MAP); |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | private static Bmv2Action buildDropAction() { |
| 55 | return Bmv2Action.builder() |
| 56 | .withName("_drop") |
| 57 | .build(); |
| 58 | } |
| 59 | |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 60 | private static Bmv2Action buildPushToCpAction() { |
| 61 | return Bmv2Action.builder() |
| 62 | .withName("send_to_cpu") |
| 63 | .build(); |
| 64 | } |
| 65 | |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 66 | private static Bmv2Action buildFwdAction(Instructions.OutputInstruction inst) |
| 67 | throws Bmv2FlowRuleTranslatorException { |
| 68 | |
| 69 | Bmv2Action.Builder actionBuilder = Bmv2Action.builder(); |
| 70 | |
| 71 | actionBuilder.withName("fwd"); |
| 72 | |
| 73 | if (inst.port().isLogical()) { |
Carmelo Cascone | e912164 | 2016-04-27 17:02:38 -0700 | [diff] [blame] | 74 | if (inst.port() == PortNumber.CONTROLLER) { |
| 75 | return buildPushToCpAction(); |
| 76 | } else { |
| 77 | throw new Bmv2FlowRuleTranslatorException( |
| 78 | "Output logic port number not supported: " + inst); |
| 79 | } |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | actionBuilder.addParameter( |
| 83 | ImmutableByteSequence.copyFrom((short) inst.port().toLong())); |
| 84 | |
| 85 | return actionBuilder.build(); |
| 86 | } |
| 87 | |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 88 | @Override |
| 89 | public Bmv2Action buildAction(TrafficTreatment treatment) |
| 90 | throws Bmv2FlowRuleTranslatorException { |
| 91 | |
| 92 | |
| 93 | if (treatment.allInstructions().size() == 0) { |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 94 | // No instructions means drop. |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 95 | return buildDropAction(); |
| 96 | } else if (treatment.allInstructions().size() > 1) { |
Carmelo Cascone | d4e7a77 | 2016-05-03 11:21:29 -0700 | [diff] [blame^] | 97 | // Otherwise, we understand treatments with only 1 instruction. |
Carmelo Cascone | b7388bd | 2016-04-14 10:20:13 -0700 | [diff] [blame] | 98 | throw new Bmv2FlowRuleTranslatorException( |
| 99 | "Treatment not supported, more than 1 instructions found: " |
| 100 | + treatment.toString()); |
| 101 | } |
| 102 | |
| 103 | Instruction instruction = treatment.allInstructions().get(0); |
| 104 | |
| 105 | switch (instruction.type()) { |
| 106 | case OUTPUT: |
| 107 | return buildFwdAction((Instructions.OutputInstruction) instruction); |
| 108 | case NOACTION: |
| 109 | return buildDropAction(); |
| 110 | default: |
| 111 | throw new Bmv2FlowRuleTranslatorException( |
| 112 | "Instruction type not supported: " |
| 113 | + instruction.type().name()); |
| 114 | } |
| 115 | } |
| 116 | } |