Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -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.bmv2.demo.app.ecmp; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableBiMap; |
| 20 | import org.onlab.util.ImmutableByteSequence; |
| 21 | import org.onosproject.bmv2.api.context.Bmv2Configuration; |
| 22 | import org.onosproject.bmv2.api.context.Bmv2Interpreter; |
| 23 | import org.onosproject.bmv2.api.context.Bmv2InterpreterException; |
| 24 | import org.onosproject.bmv2.api.runtime.Bmv2Action; |
| 25 | import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.flow.TrafficTreatment; |
| 28 | import org.onosproject.net.flow.criteria.Criterion; |
| 29 | import org.onosproject.net.flow.instructions.Instruction; |
| 30 | |
| 31 | import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence; |
| 32 | import static org.onosproject.net.PortNumber.CONTROLLER; |
| 33 | import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 34 | |
| 35 | /** |
| 36 | * Implementation of a BMv2 interpreter for the ecmp.json configuration. |
| 37 | */ |
| 38 | public class EcmpInterpreter implements Bmv2Interpreter { |
| 39 | |
| 40 | protected static final String ECMP_METADATA_T = "ecmp_metadata_t"; |
| 41 | protected static final String ECMP_METADATA = "ecmp_metadata"; |
| 42 | protected static final String SELECTOR = "selector"; |
| 43 | protected static final String GROUP_ID = "groupId"; |
| 44 | protected static final String GROUP_SIZE = "groupSize"; |
| 45 | protected static final String ECMP_GROUP = "ecmp_group"; |
| 46 | protected static final String ECMP_GROUP_TABLE = "ecmp_group_table"; |
| 47 | protected static final String TABLE0 = "table0"; |
| 48 | protected static final String SEND_TO_CPU = "send_to_cpu"; |
| 49 | protected static final String DROP = "_drop"; |
| 50 | protected static final String SET_EGRESS_PORT = "set_egress_port"; |
| 51 | protected static final String PORT = "port"; |
| 52 | |
| 53 | private static final ImmutableBiMap<Criterion.Type, String> CRITERION_TYPE_MAP = ImmutableBiMap.of( |
| 54 | Criterion.Type.IN_PORT, "standard_metadata.ingress_port", |
| 55 | Criterion.Type.ETH_DST, "ethernet.dstAddr", |
| 56 | Criterion.Type.ETH_SRC, "ethernet.srcAddr", |
| 57 | Criterion.Type.ETH_TYPE, "ethernet.etherType"); |
| 58 | |
| 59 | private static final ImmutableBiMap<Integer, String> TABLE_ID_MAP = ImmutableBiMap.of( |
| 60 | 0, TABLE0, |
| 61 | 1, ECMP_GROUP_TABLE); |
| 62 | |
| 63 | @Override |
| 64 | public ImmutableBiMap<Integer, String> tableIdMap() { |
| 65 | return TABLE_ID_MAP; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public ImmutableBiMap<Criterion.Type, String> criterionTypeMap() { |
| 70 | return CRITERION_TYPE_MAP; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Bmv2Action mapTreatment(TrafficTreatment treatment, Bmv2Configuration configuration) |
| 75 | throws Bmv2InterpreterException { |
| 76 | |
| 77 | if (treatment.allInstructions().size() == 0) { |
| 78 | // No instructions means drop for us. |
| 79 | return actionWithName(DROP); |
| 80 | } else if (treatment.allInstructions().size() > 1) { |
| 81 | // Otherwise, we understand treatments with only 1 instruction. |
| 82 | throw new Bmv2InterpreterException("Treatment has multiple instructions"); |
| 83 | } |
| 84 | |
| 85 | Instruction instruction = treatment.allInstructions().get(0); |
| 86 | |
| 87 | switch (instruction.type()) { |
| 88 | case OUTPUT: |
| 89 | OutputInstruction outInstruction = (OutputInstruction) instruction; |
| 90 | PortNumber port = outInstruction.port(); |
| 91 | if (!port.isLogical()) { |
| 92 | return buildEgressAction(port, configuration); |
| 93 | } else if (port.equals(CONTROLLER)) { |
| 94 | return actionWithName(SEND_TO_CPU); |
| 95 | } else { |
| 96 | throw new Bmv2InterpreterException("Egress on logical port not supported: " + port); |
| 97 | } |
| 98 | case NOACTION: |
| 99 | return actionWithName(DROP); |
| 100 | default: |
| 101 | throw new Bmv2InterpreterException("Instruction type not supported: " + instruction.type().name()); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private static Bmv2Action buildEgressAction(PortNumber port, Bmv2Configuration configuration) |
| 106 | throws Bmv2InterpreterException { |
| 107 | |
| 108 | int portBitWidth = configuration.action(SET_EGRESS_PORT).runtimeData(PORT).bitWidth(); |
| 109 | |
| 110 | try { |
| 111 | ImmutableByteSequence portBs = fitByteSequence(ImmutableByteSequence.copyFrom(port.toLong()), portBitWidth); |
| 112 | return Bmv2Action.builder() |
| 113 | .withName(SET_EGRESS_PORT) |
| 114 | .addParameter(portBs) |
| 115 | .build(); |
| 116 | } catch (Bmv2TranslatorUtils.ByteSequenceFitException e) { |
| 117 | throw new Bmv2InterpreterException(e.getMessage()); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private static Bmv2Action actionWithName(String name) { |
| 122 | return Bmv2Action.builder().withName(name).build(); |
| 123 | } |
| 124 | } |