blob: f5e95df499474ba595cebf83843486b62e63f7ed [file] [log] [blame]
Carmelo Cascone17fc9e42016-05-31 11:29:21 -07001/*
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
17package org.onosproject.bmv2.ctl;
18
19import com.google.common.collect.ImmutableBiMap;
20import org.onlab.util.ImmutableByteSequence;
21import org.onosproject.bmv2.api.context.Bmv2Configuration;
22import org.onosproject.bmv2.api.context.Bmv2Interpreter;
23import org.onosproject.bmv2.api.context.Bmv2InterpreterException;
24import org.onosproject.bmv2.api.runtime.Bmv2Action;
25import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils;
26import org.onosproject.net.flow.TrafficTreatment;
27import org.onosproject.net.flow.criteria.Criterion;
28import org.onosproject.net.flow.instructions.Instruction;
29
30import static org.onosproject.net.PortNumber.CONTROLLER;
31import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
32
33/**
34 * Implementation of a BMv2 interpreter for the BMv2 default.json configuration.
35 */
36public final class Bmv2DefaultInterpreterImpl implements Bmv2Interpreter {
37
38 public static final String TABLE0 = "table0";
39 public static final String SEND_TO_CPU = "send_to_cpu";
40 public static final String DROP = "_drop";
41 public static final String SET_EGRESS_PORT = "set_egress_port";
42
43 // Lazily populate field map.
44 private static final ImmutableBiMap<Criterion.Type, String> CRITERION_MAP = ImmutableBiMap.of(
45 Criterion.Type.IN_PORT, "standard_metadata.ingress_port",
46 Criterion.Type.ETH_DST, "ethernet.dstAddr",
47 Criterion.Type.ETH_SRC, "ethernet.srcAddr",
48 Criterion.Type.ETH_TYPE, "ethernet.etherType");
49
50 private static final ImmutableBiMap<Integer, String> TABLE_MAP = ImmutableBiMap.of(
51 0, TABLE0);
52
53 @Override
54 public ImmutableBiMap<Criterion.Type, String> criterionTypeMap() {
55 return CRITERION_MAP;
56 }
57
58 @Override
59 public ImmutableBiMap<Integer, String> tableIdMap() {
60 return TABLE_MAP;
61 }
62
63 @Override
64 public Bmv2Action mapTreatment(TrafficTreatment treatment, Bmv2Configuration configuration)
65 throws Bmv2InterpreterException {
66
67
68 if (treatment.allInstructions().size() == 0) {
69 // No instructions means drop for us.
70 return Bmv2Action.builder().withName(DROP).build();
71 } else if (treatment.allInstructions().size() > 1) {
72 // Otherwise, we understand treatments with only 1 instruction.
73 throw new Bmv2InterpreterException("Treatment has multiple instructions");
74 }
75
76 Instruction instruction = treatment.allInstructions().get(0);
77
78 switch (instruction.type()) {
79 case OUTPUT:
80 OutputInstruction outInstruction = (OutputInstruction) instruction;
81 if (outInstruction.port() == CONTROLLER) {
82 return Bmv2Action.builder().withName(SEND_TO_CPU).build();
83 } else {
84 return buildEgressAction(outInstruction, configuration);
85 }
86 case NOACTION:
87 return Bmv2Action.builder().withName(DROP).build();
88 default:
89 throw new Bmv2InterpreterException("Instruction type not supported: " + instruction.type().name());
90 }
91 }
92
93
94 /**
95 * Builds an egress action equivalent to the given output instruction for the given configuration.
96 *
97 * @param instruction an output instruction
98 * @param configuration a configuration
99 * @return a BMv2 action
100 * @throws Bmv2InterpreterException if the instruction cannot be translated to a BMv2 action
101 */
102 private Bmv2Action buildEgressAction(OutputInstruction instruction, Bmv2Configuration configuration)
103 throws Bmv2InterpreterException {
104
105 Bmv2Action.Builder actionBuilder = Bmv2Action.builder();
106
107 actionBuilder.withName(SET_EGRESS_PORT);
108
109 if (instruction.port().isLogical()) {
110 throw new Bmv2InterpreterException("Output on logic port not supported: " + instruction);
111 }
112
113 // Get the byte sequence for the out port with the right length
114 long portNumber = instruction.port().toLong();
115 int bitWidth = configuration.action(SET_EGRESS_PORT).runtimeData("port").bitWidth();
116 try {
117 ImmutableByteSequence outPort = Bmv2TranslatorUtils.fitByteSequence(
118 ImmutableByteSequence.copyFrom(portNumber), bitWidth);
119 return Bmv2Action.builder().withName(SET_EGRESS_PORT).addParameter(outPort).build();
120 } catch (Bmv2TranslatorUtils.ByteSequenceFitException e) {
121 throw new Bmv2InterpreterException(e.getMessage());
122 }
123 }
124}